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
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
getToken(1).kind != SEMICOLON && getToken(0).endLine < getToken(1).beginLine
)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()
}