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

Acorn loose output does not conforms to ECMA specs #975

Closed
KFlash opened this issue Aug 12, 2020 · 4 comments
Closed

Acorn loose output does not conforms to ECMA specs #975

KFlash opened this issue Aug 12, 2020 · 4 comments

Comments

@KFlash
Copy link

KFlash commented Aug 12, 2020

Hi. I was playing around with Acorn loose, and got confused

= a and also = are parsed as an AssignmentExpression.

= is not an start of an statement or expression and should be consumed.

if was parsed as an empty IfStatement with an EmptyStatement as part of the Consequent. Where did this semicolon come from? Or why is there an EmptyStatement when the block body should not be parsed?

It would make sense if there actually was an ; because it's an valid start of an statement.

try parsed as an BlockStatement. Where is the TryStatement ? Because switch parses out an empty SwitchStatement so that is correct, so same rule should apply for try.

catch parsed as an EmptyStatement. It's not an valid start of an statement, so should only be consumed.

This case - switch try - is also weird. Try statement is missing and instead a BlockStatement become part part of the SwitchCase.

For correct error recovery the SwitchCase should not be parsed out. A Try statement should be parsed out
instead because ``Try` is an valid statement start and should result in an empty Try statement.

You can see it here in my REPL how it should have been parsed.

+++. Here the parser consumes the last +. In fact it should be parsed as an UpdateExpression and UnaryExpression.
I mention it because ++! are parsed correct, so I'm not sure why +++ isn't parsed in the same way. Both + and !are
part of the UnaryExpression production

This is only a few weird cases I found, but there are tons of them.

@marijnh
Copy link
Member

marijnh commented Aug 13, 2020

There is no spec on loose parsing, so I'm not sure where you're getting all your "should"s. Are you clean on what the loose parser does? It parses everything, never raising an error, and emitting a syntax tree for as much of the syntax as it could recognize (including incomplete constructs).

@KFlash
Copy link
Author

KFlash commented Aug 13, 2020

My "should"s come from the ECMAScript® 2021 Language Specification. I agree there is no specs about it, but I thought a JS parser should follow the ECMA specs, that why the title on this issue. Example punctuators are never valid start of an expression except for a few, but still - as I mentioned - got an AssignmentExpression while parsing =.

And it doesn't conform to the specs while parsing out an try statement or get an BlockStatement or nothing at all while switch works different.

That was the things I found weird.

I also noticed that it doesn't throw any errors, and you did a great job there :)

@marijnh
Copy link
Member

marijnh commented Aug 13, 2020

I thought a JS parser should follow the ECMA specs

It parses valid JS code. It does its own thing, creating a tree as close to the presented code as possible, for invalid code.

@marijnh marijnh closed this as completed Aug 13, 2020
@KFlash
Copy link
Author

KFlash commented Aug 13, 2020

I agree it almost passes valid JS code even thought some weird output etc. But I respect that you want fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants