Search code examples
javascriptecmascript-5javacc

How to implement JavaScript/ECMAScript "no LineTerminator here" rule in JavaCC?


I continue working on my JavaCC grammar for ECMAScript 5.1. It actually goes quite well, I think I've covered most of the expressions now.

I have now two questions, both of them are related to the automatic semicolon insertion (§7.9.1). This is one of them.

The specification defines the following production:

PostfixExpression :
    LeftHandSideExpression
    LeftHandSideExpression [no LineTerminator here] ++
    LeftHandSideExpression [no LineTerminator here] --

How can I implement a reliable "no LineTerminator here" check?

For the record my LINE_TERMINATOR is at the moment something like:

SPECIAL_TOKEN :
{
    <LINE_TERMINATOR: <LF> | <CR> | <LS> | <PS> >
|   < #LF: "\n" > /* Line Feed */
|   < #CR: "\r" > /* Carriage Return */
|   < #LS: "\u2028" > /* Line separator */
|   < #PS: "\u2029" > /* Paragraph separator */
}

I have read about lexical states, but I am not sure if this is a right direction. I've checked a few other JavaScript grammars I have found, but did not find any similar rules there. (I actually feel myself a total cargo culter when I try to overtake something from these grammars.)

I'd be grateful for a pointer, a hint or just a keyword for the right search direction.


Solution

  • I think for the "restricted productions" you can do this

    void PostfixExpression() : 
    {} {
         LeftHandSideExpression() 
         (
             LOOKAHEAD( "++", {getToken(0).beginLine == getToken(1).beginLine})
             "++"
         |
             LOOKAHEAD( "--", {getToken(0).beginLine == getToken(1).beginLine})
             "--"
         |
             {}
         )
    }