ANTLR, optional ';' in JavaScript
- by vava
I'm just playing with ANTLR and decided to try parsing JavaScript with it. But I hit the wall in dealing with optional ';' in it, where statement end is marked by newline instead. Can it be done in some straightforward way?
Just a simple grammar example that doesn't work
grammar optional_newline;
def : statements ;
statements : statement (statement)* ;
statement : expression (';' | '\n') ;
expression : ID | INT | 'var' ID '=' INT ;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
INT : '0'..'9'+ ;
WS : ( ' ' | '\t' | '\r' | '\n') {$channel=HIDDEN;} ;
and I want to be able to parse this (which can be parsed by JavaScript parsers)
var i =
10
10;
PS: I don't want to put WS in parser rules, I would be much happier if lexer just get rid of those.