[antlr-interest] grammar for a template library like underscore.js

Rémi Maréchal remi.marechal at gmail.com
Tue Jun 12 13:10:35 PDT 2012


Hi

i know StringTemplate is a powerful template library but i want write my own.

i want a grammar for a template library like underscore.js style :


some text
<% some.code() %>
some other text <%= some.obj.property %> and other and other....


inspirated by xml parser tutorial we found at
http://www.antlr.org/wiki/display/ANTLR3/Parsing+XML

the difference with xml is that

my tags start with two character and not one

and my problem come when i want to define CONTENT in lexer (
equivalent to CDATA in tutorial )


in xml tutorial ~'<' work but in my case ~'<%' don't work .

a solution exists ?

here, piece of my lexer and parser


lexer grammar TemplateLexer;

@members {
boolean tagMode = false;
}

TAG_START : '<%' { tagMode = true; } ;
TAG_END :  { tagMode }?=> '%>'  { tagMode = true; } ;
CONTENT : { !tagMode }?=> ( ~'<%' )* ;


parser grammar TemplateParser;

options { output=AST; }

template : ( content | tag )* ;

tag : TAG_START script  TAG_END ;

script : ........ ;


More information about the antlr-interest mailing list