-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Lexer: Inline else
binds to wrong if
/switch
#2342
Comments
This is a common problem in generating AST for languages, even JavaScript exhibits this issue. Here are two ways of interpreting the example: if (a) if (b) return c; else return d;
Outer ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Inner ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (a) if (b) return c; else return d;
Outer ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Inner ~~~~~~~~~~~~~~~~ This is normally solved is picking a convention and sticking to it, based on the token's greediness. This gist demonstrates how both CoffeeScript and JavaScript deals with the ambiguity; it appears that CoffeeScript is sticking to JavaScript semantics. Seeing as though CoffeeScript can only portray meaning (or rather scoping, I don't mean variable scoping however) through whitespace, may I suggest being explicit: # Ambiguous
if a then if b then c else d
# Explicit: A
if a
if b then c else d
# Explicit: B
if a
c if b
else d |
Parentheses can also be used to make it explicit: # Explicit: A'
if a then (if b then c else d)
# Explicit: B'
if a then (if b then c) else d That being said, i agree with @satyr: the current behaviour in the ambiguous case looks quite confusing to me; especially the |
Entirely forgot about parentheses :< they work perfectly, how foolish of me.
|
Doesn't seem to be fixed here |
I retract my previous statement that CoffeeScript was sticking to JavaScript semantics, I was wrong. The gist has been updated to properlly illustrate the bug using @epidemian's parentheses suggestion. The rewriter, when resolving some LARL issues, appears to be replacing |
see satyr/coco#127 |
else
binds to wrong if
/switch
else
binds to wrong if
/switch
(Migrating from satyr/coco#131)
Expected compilation:
The text was updated successfully, but these errors were encountered: