0

I'm workng on a code translator from TSX7 code to S7 SCL. There I like to have the original TSX7 code as a comment in the generated SCL code. This works so far fine, but I could not figure out how can I output the original input line in an antrl rule like:

With the TSX7 input line:

!       INC W1469;0->W1465[3]

I like to have the SCL output like:

// OLD> !       INC W1469;0->W1465[3] <OLD
W1469 := W1469+1;
W1465 := 0;

For this I wrote an action on the codelines rule:

codelines returns [string code] 
    : codeline     
      { 
        _localctx.code = "// OLD> " + _localctx.codeline().GetText() + " <OLD" + Environment.NewLine;
        _localctx.code += _localctx.codeline().code;
      }
    | commentline  
      { 
        _localctx.code = _localctx.commentline().code; 
      }
    ;

(the actual TSX7 to SCL translation is done in the "codeline" rule)

What I get is almost ok, but I'm missing all the spaces in the string I get form the call to "_localctx.codeline().GetText()":

// OLD> !INCW1469;0->W1465[3] <OLD
W1469 := W1469+1;
W1465 := 0;

(the space between INC and W1465 is missing)

Is there a way to get the original text with all the spaces, which was used as input to the rule "codelines"?

TINU
  • 1

1 Answers1

0

You have to access the text directly from an ANTLR-stream. That could be the CommonTokenStream with the methodd getHiddenTokensToLeft() or getHiddenTokensToRight() but that is quite impractical.

However a quick google search brought this question into the light that should anser your question.

Community
  • 1
  • 1
Raven
  • 2,951
  • 2
  • 26
  • 42