ANTLR, optional ';' in JavaScript

Posted by vava on Stack Overflow See other posts from Stack Overflow or by vava
Published on 2010-03-20T15:10:26Z Indexed on 2010/03/20 15:31 UTC
Read the original article Hit count: 349

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about antlr3

Related posts about JavaScript