[antlr-interest] How to set imaginary token text?

Vaclav Barta vbar at comp.cz
Mon Jul 16 12:04:35 PDT 2007


On Sunday 15 July 2007 20:00, Vaclav Barta wrote:
> On Sunday 15 July 2007 19:07, Randall R Schulz wrote:
> > On Sunday 15 July 2007 09:59, Vaclav Barta wrote:
> > > I'd like to have a lexer rule for string characters:
> > >
> > > StringChar
> > >
> > > 	: .
> > >
> > > 	;
> > You might want to consider consolidating these characters, if that would
> > work for your purposes:
Experimenting some more, maybe I'd like to parse (some of) these characters 
individually but consolidate them into one AST node - something like

quotedString returns [ String value ]
@init { StringBuffer sb = new StringBuffer(); }
	: DQUOTE (
		EscapeSequence { sb.append($EscapeSequence.getText()); }
		| BareString { sb.append($BareString.getText()); }
	)* DQUOTE { $value = sb.toString(); }
	;

string
	: s = quotedString -> LITERAL
	| BareString -> LITERAL
	;

where LITERAL is an imaginary token - but as written, it obviously loses the 
string value. How can I set LITERAL token text to the value returned from 
quotedString, or $BareString.getText() ?

 	Bye
 		Vasek


More information about the antlr-interest mailing list