Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AS combinator runs code regardless of success #4

Open
erjiang opened this issue Jan 23, 2021 · 0 comments
Open

AS combinator runs code regardless of success #4

erjiang opened this issue Jan 23, 2021 · 0 comments
Labels
bug Something isn't working

Comments

@erjiang
Copy link

erjiang commented Jan 23, 2021

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
   }
@gosukiwi gosukiwi added the bug Something isn't working label Jan 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants