Skip to content

new Class().method() has incorrect precedence #273

Closed

Description

@jcdickinson

The precedence in new Class().method() isn't correct. This expression is parsed as new (Class().method()), which is incorrect, the correct parse tree would be equivalent to (new Class()).method().

A real-world example is new Date().getTime().

Activity

  1. jasonwilliams commented on Mar 9, 2020

    @jasonwilliams
    Member

    Tokens for new Date().getTime()

    [boa\src\lib.rs:26] &tokens = [
        Token {
            data: Keyword(
                New,
            ),
            pos: Position {        
                column_number: 1,  
                line_number: 1,    
            },
        },
        Token {
            data: Identifier(      
                "Date",
            ),
            pos: Position {        
                column_number: 5,  
                line_number: 1,    
            },
        },
        Token {
            data: Punctuator(      
                OpenParen,
            ),
            pos: Position {        
                column_number: 9,  
                line_number: 1,    
            },
        },
        Token {
            data: Punctuator(      
                CloseParen,        
            ),
            pos: Position {       
                column_number: 10,
                line_number: 1,   
            },
        },
        Token {
            data: Punctuator(     
                Dot,
            ),
            pos: Position {       
                column_number: 11,
                line_number: 1,   
            },
        },
        Token {
            data: Identifier(     
                "getTime",        
            ),
            pos: Position {       
                column_number: 12,
                line_number: 1,   
            },
        },
        Token {
            data: Punctuator(     
                OpenParen,        
            ),
            pos: Position {       
                column_number: 19,
                line_number: 1,
            },
        },
        Token {
            data: Punctuator(
                CloseParen,
            ),
            pos: Position {
                column_number: 20,
                line_number: 1,
            },
        },
        Token {
            data: Punctuator(
                Semicolon,
            ),
            pos: Position {
                column_number: 21,
                line_number: 1,
            },
        },
    ]
    

    Tokens looks fine

    Parser output

    [boa\src\lib.rs:27] &expr = Expr {
        def: Block(
            [
                Expr {
                    def: Construct(
                        Expr {
                            def: GetConstField(        
                                Expr {
                                    def: Call(
                                        Expr {
                                            def: Local(
                                                "Date",
                                            ),
                                        },
                                        [],
                                    ),
                                },
                                "getTime",
                            ),
                        },    
                        [],   
                    ),        
                },
            ],
        ),
    }
    

    Yeah looks like there'll need to be some work here:
    https://github.com/jasonwilliams/boa/blob/master/boa/src/syntax/parser/mod.rs#L232-L240

  2. jasonwilliams commented on Mar 9, 2020

    @jasonwilliams
    Member

    Upon deeper inspection the issue is around here: https://github.com/jasonwilliams/boa/blob/master/boa/src/syntax/parser/mod.rs#L688-L721

    Normally it just carries on, which is why it continues parsing until the method.
    There needs to be some signal to stop after the first call.

  3. jasonwilliams commented on Mar 9, 2020

    @jasonwilliams
    Member
  4. jasonwilliams commented on Mar 9, 2020

    @jasonwilliams
    Member
  5. jasonwilliams commented on Mar 10, 2020

    @jasonwilliams
    Member
  6. added
    C-Parser
    on Mar 31, 2020
  7. added a commit that references this issue on Mar 31, 2020
  8. jasonwilliams commented on Apr 13, 2020

    @jasonwilliams
    Member

    new output since 0.7

    StatementList(
        [
            Call(
                GetConstField(
                    New(
                        Call(
                            Local(     
                                "Date",
                            ),
                            [],        
                        ),
                    ),
                    "getTime",
                ),
                [],
            ),
        ],
    )
    

    The precedence looks fixed here
    @HalidOdat @Razican ?

  9. Razican commented on Apr 13, 2020

    @Razican
    Member

    The precedence looks fixed here
    @HalidOdat @Razican ?

    It looks good, yes! Might be worth adding a test for this?

  10. HalidOdat commented on Apr 13, 2020

    @HalidOdat
    Member

    It looks good, yes! Might be worth adding a test for this?

    Are you adding the test, or should I?

  11. added
    A-Bug
    on Apr 13, 2020
  12. jasonwilliams commented on Apr 13, 2020

    @jasonwilliams
    Member

    im doing it now

  13. added a commit that references this issue on Apr 13, 2020
  14. added a commit that references this issue on Apr 13, 2020
  15. added this to the v0.8.0 milestone on Jun 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-BugC-Parser

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions