diff --git a/parse.js b/parse.js index 873ee63..f1a45d5 100644 --- a/parse.js +++ b/parse.js @@ -84,6 +84,7 @@ If:11, // if statement with condition, trueCase, and optional falseCase For:12, // a for statement with loop variable, range, and loop body Map:13, // like a for statement, but is actually an expression where you can 'return' in the body and generate an array + On:14, // handle a named event on a given target with given params, returning given values }); const StatementsWithSemicolon = Object.freeze([ @@ -212,9 +213,22 @@ } } + let eventValues; + ({ args:eventValues, error } = this.funcParams(false, false)); + if (error) + { + eventValues = []; + if (error.expectingMatch) + { + return this.getError(error); + } + } + let body; ({ ast:body, error } = this.attemptSubtree(this.block)); if (error) return this.getError(error, true); + + return { ast: { node: ASTNode.On, target: target, eventName: eventName, eventParams: eventParams, eventValues: eventValues }, error: null }; } forStmt() @@ -292,6 +306,7 @@ { parse: this.expression, handle: ast => ({ node: ASTNode.ExprStatement, expr: ast }) }, { parse: this.ifStmt, handle: ast => ast }, { parse: this.forStmt, handle: ast => ast }, + { parse: this.onStmt, handle: ast => ast }, ], "Expecting statement"); let error, bestError, numMatched, bestNumMatched = 0; @@ -471,7 +486,7 @@ * so don't actually consume the token yet, just peek it (just in case) */ let { type:opType, token:opToken, pos:opPos, error } = this.tokenStream.peek(lex.TokenType.Op); - if (error) return this.getError(error); + if (error) return { ast: lhs, error: null }; let largestOp = this.matchLargestOp(PostfixOpTree, opToken); if (largestOp)