Question

I have a grammar that works with v3, and I am now trying to migrate to v4. I have taken some hints from questions 18431158 and 14778570, but I have one more question:

In my v3 code, I was counting line numbers like this (newLine() just keeps a count of lines parsed and saves it, so that later, when I try to report some semantic errors, I could also report the corresponding line number):

COMMENT : '/*' ( options {greedy=false;} : 
    (
       '\n' {newLine();}
     | ~ '\n'
    ) )* '*/' {$channel=HIDDEN;}
;
LINE_COMMENT : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN; newLine();} ;
WS: (
  ' '
 |'\r'
 |'\t'
 |'\n' {newLine();}
)+     {$channel=HIDDEN;};

In v4, the lexer actions are not permitted inside. How do I migrate this code to v4?

Was it helpful?

Solution

ANTLR 4 always tracks the line and column numbers in the Token objects. You can simply remove the actions which call the newLine() method and use Token.getLine().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top