How to implement JavaScript automatic semicolon insertion in JavaCC?

佐手、 提交于 2019-12-02 08:28:25
Theodore Norvell

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() 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!