How to implement JavaScript automatic semicolon insertion in JavaCC?

前端 未结 1 1037
野趣味
野趣味 2021-01-29 03:07

I am finishing my ECMAScript 5.1/JavaScript grammar for JavaCC. I\'ve done all the tokens and productions according to the specification.

Now I\'m facing a big question

相关标签:
1条回答
  • 2021-01-29 03:55

    The 3 rules for semicolon insertion can be found in section 7.9.1 of the ECMAScript 5.1 standard

    I think rules 1 and 2 from the standard can be handled with semantic lookahead.

    void PossiblyInsertedSemicolon() 
    {}
    {
        LOOKAHEAD( {semicolonNeedsInserting()} ) {}
    |
        ";"
    }
    

    So when does a semicolon need inserting? When one of these is true

    • When the next token is not a semicolon and is on another line (getToken(1).kind != SEMICOLON && getToken(0).endLine < getToken(1).beginLine)
    • When the next token is a right brace.
    • When the next token is EOF

    So we need

    boolean semicolonNeedsInserting() {
        return (`getToken(1).kind != SEMICOLON && getToken(0).endLine < getToken(1).beginLine`) 
        || getToken(1).kind == RBRACE
        || getToken(1).kind == EOF ;
    }
    

    That takes care of rules 1 and 2 of the standard.

    For rule 3 (restricted productions) , as mentioned in my answer to this question, you could do the following

    void returnStatement()
    {}
    {
        "return"
        [   // Parse an expression unless either the next token is a ";", "}" or EOF, or the next token is on another line.
            LOOKAHEAD( {   getToken(1).kind != SEMICOLON
                        && getToken(1).kind != RBRACE
                        && getToken(1).kind != EOF
                        && getToken(0).endLine == getToken(1).beginLine} )
            Expression()
        ]
        PossiblyInsertedSemicolon() 
    }
    
    0 讨论(0)
提交回复
热议问题