From 478885dfe4e4745a6c06229c9187cee20eab0e6f Mon Sep 17 00:00:00 2001 From: Richard Ore Date: Thu, 25 May 2023 01:34:56 +0100 Subject: [PATCH] Added support for new anonymous syntax for ast module. --- libs/ast/parser.b | 28 +++++++++++++++++++++++++--- libs/ast/scanner.b | 8 +++++++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/libs/ast/parser.b b/libs/ast/parser.b index f80984b0..f58b71ca 100644 --- a/libs/ast/parser.b +++ b/libs/ast/parser.b @@ -252,7 +252,8 @@ class Parser { if self._match(LPAREN) return self._grouping() if self._match(LBRACE) return self._dict() if self._match(LBRACKET) return self._list() - if self._match(BAR) return self._anonymous() + if self._match(BAR) return self._anonymous_compat() + if self._match(AT) return self._anonymous() return nil } @@ -896,9 +897,9 @@ class Parser { } /** - * anonymous functions + * anonymous compartibility functions */ - _anonymous() { + _anonymous_compat() { var params = [] while !self._check(BAR) { @@ -915,6 +916,27 @@ class Parser { return FunctionDecl('', params, body) } + /** + * anonymous functions + */ + _anonymous() { + var params = [] + self._consume(LPAREN, "expected '(' at start of anonymous function") + + while !self._check(RPAREN) { + params.append(self._consume_any('parameter name expected', IDENTIFIER, TRI_DOT).literal) + + if !self._check(RPAREN) + self._consume(COMMA, "',' expected between function params") + } + + self._consume(RPAREN, "expected ')' after anonymous function parameters") + self._consume(LBRACE, "'{' expected after function declaration") + var body = self._block() + + return FunctionDecl('', params, body) + } + /** * function definitions */ diff --git a/libs/ast/scanner.b b/libs/ast/scanner.b index 6cf4654b..884396e9 100644 --- a/libs/ast/scanner.b +++ b/libs/ast/scanner.b @@ -365,7 +365,13 @@ class Scanner { } when ',' self._add_token(COMMA) when ';' self._add_token(SEMICOLON) - when '@' self._decorator() + when '@' { + if !self._is_alpha(self._peek()) { + self._add_token(AT) + } else { + self._decorator() + } + } when '.' { if self._match('.') { self._add_token(self._match('.') ? TRI_DOT : RANGE)