Antlr newbie question: Ignoring tokens, or a better way to do this:

D

Daniel Pitts

Basically, my input is something like:



something{

value: >>>

This is the text

<<<

}



I want a rule that can give me as a value "This is the text".

I've tried:



LQS: LQSS *options {greedy=false;} : .)* LQSE;

LQSS : '>>>\n';

LQSE: '\n<<<';



and I've tried using channel=HIDDEN on LQSS and LQSE, but then I get
errors while parsing other parts of my source.



I have a work around, but its ugly. I have a rule



lstring returns [String value]: LQS {value=
$LQS.getText().replaceFirst(">>>\n","").replaceFirst("\n<<<$", "");};



but that seems like an awful big hack. Any suggestions?

Thanks,
Daniel.
P.S. I've also sent this message to the antlr-interest mailing list.
 
D

Daniel Pitts

If you can't crack this, try JavaCC. There are a ton of examples out
there plus a textbook.

seehttp://mindprod.com/jgloss/javacc.html

I have found a "better" way, but I hope its still not the "right" way.

LQS: LQSS {tokenStartCharIndex=getCharIndex();}(options
{greedy=false;} : . )*{text = getText();}LQSE;
LQSS: ('>>>' SPACE* CRLF);
LQSE: CRLF SPACE* '<<<';

That this does: Sets the token start index to the "current" index
after it matches LQSS, and sets the text permanently to what it is
before matching the LQSE.

Not exactly what I call elegant.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top