Question

I'm currently using AntLR to parse some files with a proprietary language. I have a need of highlighting sections of it on an editor (think of highlighting a method in a Java class, for instance).

Does anyone has a hint on how to get them? Say I have this code:

function test(param1, param2) {
}

as function is a keyword, the first position I get in the parser is the one of the identifier "test". How can I get the positions from there up to the ending curly brace? The parameters list is dynamic, as one would expect, so you don't know in advance its length.

Thank you!

Was it helpful?

Solution

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

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

which refers to the character position within the line counting from zero.

OTHER TIPS

Not quite following why the first position you are getting is the position of test. You should easily be able to get the character offset of the "function" token if you designed the pattern specification correctly. Can you list the relevant parts of the specification?

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