Start and end position with AntLR

I am currently using AntLR to parse some proprietary language files. I need to highlight it in an editor (think for example highlighting a method in a Java class).

Does anyone have a hint on how to get them? Let's say I have this code:

function test(param1, param2) {
}

      

since function is a keyword, the first position I get in the parser is the identifier "test". How can I get the positions from there to the finishing curly brace? The parameter list is dynamic, as you would expect, so you don't know its length beforehand.

Thank!

0


source to share


2 answers


If I understand your question, I think you can use the 'pos' attribute for each token

func: FUNCTION ID '(' ID (',' ID)* ')' {
        System.out.println("Position = " + $FUNCTION.pos);
}

      



which refers to the position of the character in the string, counted from zero.

+1


source


It's not entirely clear why the first position you get is the test position. You should easily get the character offset of the function marker if you have worked out the template specification correctly. Can you list the relevant parts of the specification?



0


source







All Articles