[antlr-interest] virtual semicolons again: JavaScript, ECMAScript, ActionScript

Gavin Lambert antlr at mirality.co.nz
Wed Dec 27 12:07:04 PST 2006


At 08:15 28/12/2006, Gyula László wrote:
>Operator after a newline, like:
>
>something.getHerProperty()
>.YetAnotherMethodAccessorAfterTheNewLine()
>
>or:
>
>if ( (this.width > 0) && (this.height > 200)
>&& (this.doesMrBunnyHaveAHat ) )
>{
>beatTheBunny()
>}
>
>I really can't put the newline into the 
>expressions themselves (FYI: the operator with 
>the lowest precedence swallows the newline 
>tokens insted of the (unknown to the parser!) 
>real statement end), because there are statement 
>lists like with the following:
>
>var showStopper:Object =
>{
>foreground:0xff0000,
>background:0x550000
>}
>
>var theParserNeverMakesItToHere:int = 0
>
>I could exclude the newline, and try with 
>virtual semicolons, however I found, that none 
>of the examples for the virtual semicolons 
>(ASDT's AS2 & 3 grammar, ECMAScript grammar) 
>could handle these cases AND the flex sdk AND 
>not being an ANTLR-freezer, so I feel like there's something here.

Can't you just mark the semicolon as optional at 
the end of a statement (ie. 'SEMI?')?

With a sufficiently high k (or ANTLR v3 and 
backtracking) it ought to be able to disambiguate 
where statements begin and end depending on 
whether the following line is a valid statement 
or merely a statement fragment.  (Or at least I 
think so.  I'm mostly an ANTLR newbie myself.)

For this sort of language you really have to hide 
the newlines, since whitespace is treated as 
insignificant most of the time.

There ought to be formal EBNF grammars for those 
languages, which should be fairly easy to convert to ANTLR syntax.



More information about the antlr-interest mailing list