[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getting line numbers



On Wed, Nov 13, 2002 at 05:50:18PM +0000, ivan moore wrote:
> How do I access the line numbers/character positions
> that a node, e.g. an instance of
> org.sablecc.java.node.AMethodDeclaration
> represents?

Good question.  Currently, SableCC only records line/column numbers
for tokens (e.g. Txxx instances). So, you woould need to search for
the first token within the "AMethodDeclaration", or the first token of
that follows in the tree, if there's no token in the subtree rooted at
the AMethodDeclaration node.

You can possibly do this using the following psedocode:

class Positions extends ReverseDepthFirstAdapter
{
  // line/column -1 means "end of file"
  int line = -1;
  int column = -1;  

  HashMap lines = new HashMap();
  HashMap columns = new HashMap();

  public void defaultIn(Node node)
  {
    lines.put(node, new Integer(line));
    columnss.put(node, new Integer(column));
  }

  public void defaultCase(Node node)
  {
    if (node instanceof Token)
    {
      Token token = (Token) node;
      line = token.getLine();
      column = token.getPos();
    }
  }
}

Later, when you need the line of a node, you write:
  positions.line.get(node).toInteger();
or better, you write the appropriate methods to do:
  int line = positions.getLine(node);

I hope this helps.

Have fun!

Etienne
-- 
Etienne M. Gagnon                    http://www.info.uqam.ca/~egagnon/
SableVM:                                       http://www.sablevm.org/
SableCC:                                       http://www.sablecc.org/