tags:

views:

128

answers:

2

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!

A: 

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?

Josh
You are correct, I'll rephrase the question.
pgb
function...name( enters parameter state, ) exits it (at same paren depth), so that { starts the lexical scope state, so that } ends it (not inside string/comment and at same depth). So you should be able to walk from start to finish in the state transitions. Are you checking for "}" ?
Josh
A: 

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.

antispam