gosukiwi/Pasukon

AS combinator runs code regardless of success

Opened this issue · 0 comments

If you write a rule that contains code that references an AS parse expression, it will throw an error if the rule is not matched:

foo
| :FOO as :value
|> 'return $.value.length'
| bar
;

it will throw cannot get .length of null, even if foo is not matched and the rule falls through to bar.

Seems like the AS combinator is missing a check:

--- a/lib/parsers/combinators/as.js
+++ b/lib/parsers/combinators/as.js
@@ -13,7 +13,7 @@ module.exports = class As {
   parse (tokens) {
     const result = this.lhs.parse(tokens).clone()
     result.matched = { name: this.name, value: result.matched }
-    if (this.code) result.matched = Evaluator.eval(this.code, result.matched)
+    if (result.success && this.code) result.matched = Evaluator.eval(this.code, result.matched)
     return result
   }