gulyan
gulyan

Reputation: 662

Antlr, get last line from token

I have a token definition that can contain multiple lines (something like multi line comments).

I can use the .line attribute to get the line where the token starts, but I need to know where the token end.

Is it possible to get the last line of the token?

Upvotes: 0

Views: 949

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170227

You can change the line number of a token by placing the (Java) code-block {$line=getLine();} at the end of the rule.

So, for multi-line comments, that would look like this:

COMMENT
  :  '/*' .* '*/' {$line=getLine();}
  ;

causing the method getLine() of the token COMMENT to return the line number the sub-string "*/" occurred on.

Upvotes: 1

Related Questions