[antlr-interest] Capturing Line numbers

Martin Probst mail at martin-probst.com
Mon Jan 30 03:16:28 PST 2006



> declarationStmt!
>     : v:declaration SEMI!
>       {#declarationStmt = #([DECLSTMT, "DeclStmt"], #v);
> 
> In this scenario, the initialize method that gets called is:
> public void initialize(int t, String txt) {
> }
> and not:
> public void initialize(Token tok) {
> }
> 
> Any ideas how I can handle this scenario?

Well, either you set the lines manually, or you implement getLine() like
this:
public int getLine() {
  if (line == -1 && getFirstChild() != null)
    return getFirstChild().getLine();
  else
    return line;
}

E.g. if you don't have a line number set (and use -1 as the initial
value, like in this example) it will query any existing children. This
works pretty good as the most common usecase is to insert artificial
parent nodes to trees. If you reorder the subtrees you will have to do
some magic though (e.g. set manually).

Martin



More information about the antlr-interest mailing list