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

new Class(argument) is not parsed correctly for JavaScript #2178

Closed
mitar opened this issue May 3, 2021 · 4 comments
Closed

new Class(argument) is not parsed correctly for JavaScript #2178

mitar opened this issue May 3, 2021 · 4 comments

Comments

@mitar
Copy link
Contributor

mitar commented May 3, 2021

So current grammar parses new Class(argument) not as NewExpressionwith singleExpression and arguments, but as NewExpression with only singleExpression, where then in turn singleExpression is parsed as singleExpression arguments.

The relevant bits of grammar are here:

arguments
    : '('(argument (',' argument)* ','?)?')'
    ;

argument
    : Ellipsis? (singleExpression | identifier)
    ;

singleExpression
    : anoymousFunction                                                      # FunctionExpression
    | Class identifier? classTail                                           # ClassExpression
    | singleExpression '[' expressionSequence ']'                           # MemberIndexExpression
    | singleExpression '?'? '.' '#'? identifierName                         # MemberDotExpression
    | singleExpression arguments                                            # ArgumentsExpression
    | New singleExpression arguments?                                       # NewExpression
    | New '.' identifier                                                    # MetaExpression // new.target
    ...

So the issue is that NewExpression has ? for arguments in New singleExpression arguments?, so a different parsing path is chosen, where New singleExpression is followed, and then singleExpression expands back into singleExpression arguments.

Is there a way to fix grammar to prefer NewExpression case? If I change things to New singleExpression arguments then parsing is done correctly, but of course new with any arguments does not succeed anymore.

@mitar
Copy link
Contributor Author

mitar commented May 3, 2021

I did try:

singleExpression
    : anoymousFunction                                                      # FunctionExpression
    | Class identifier? classTail                                           # ClassExpression
    | New singleExpression arguments?                                       # NewExpression
    | New '.' identifier                                                    # MetaExpression // new.target
    | singleExpression '[' expressionSequence ']'                           # MemberIndexExpression
    | singleExpression '?'? '.' '#'? identifierName                         # MemberDotExpression
    | singleExpression arguments                                            # ArgumentsExpression
    ...

But there was no difference. :-(

@mitar
Copy link
Contributor Author

mitar commented May 4, 2021

I also tried:

    | {!p.prev("new")}? singleExpression arguments                                            # ArgumentsExpression
    | New singleExpression arguments?                                       # NewExpression
    | New '.' identifier                                                    # MetaExpression // new.target

But I get:

The following sets of rules are mutually left-recursive [singleExpression]

@mitar
Copy link
Contributor Author

mitar commented May 4, 2021

@studentmain Any chance you could help here a bit?

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

No branches or pull requests

3 participants
@mitar @KvanTTT and others