Monday 15 April 2013

antlr - How to preserve whitespace when we use text attribute in Antlr4 -



antlr - How to preserve whitespace when we use text attribute in Antlr4 -

i want maintain white space when phone call text attribute of token, there way it? here situation: have next code

if l > 40 then; else if = 20 set "hello";

in case, want transform into:

if (!(l>40){ if (a=20) set "hello"; }

the rule in antlr that:

stmt_if_block: if expr x=stmt (else y=stmt)? { if ($x.text.equalsignorecase(";")) { writeln("if(!(" + $expr.text +")){"); writeln($stmt.text); writeln("}"); } }

but result looks like:

if(!(l>40)) { ifa=20put"hello"; }

the reason white space in $stmt removed. wondering if there anyway maintain these white space give thanks much

update: if add together

space: [ ] -> channel(hidden);

the space preserved, , result below, many spaces between tokens:

if substr(wname3,m-1,1) = ')' m = l; else m = l - 1;

this c# extension method utilize purpose:

class="lang-csharp prettyprint-override">public static string getfulltext(this parserrulecontext context) { if (context.start == null || context.stop == null || context.start.startindex < 0 || context.stop.stopindex < 0) homecoming context.gettext(); // fallback homecoming context.start.inputstream.gettext(interval.of(context.start.startindex, context.stop.stopindex)); }

since you're using java, you'll have translate it, should straightforward - api same.

explanation: first token, lastly token, , text input stream between first char of first token , lastly char of lastly token.

antlr antlr4

No comments:

Post a Comment