[antlr-interest] newline() -> parser

FranklinChen at cmu.edu FranklinChen at cmu.edu
Fri Jun 25 16:01:11 PDT 2004


Kaleb Pederson writes:
> I'm successfully calling newline in my lexer actions, and my lines and columns 
> are getting correctly set.
> 
> However, I want all the token information available to me in the parser.  When 
> I call getLine() or getColumn(), I get 0 and 0 returned.

Since I heavily use ASTs, and delay a lot of semantic checks to the
AST level (simplifying parsing), I use the following class.  

[To the ANTLR developers:  I suggest that something like this be made
a standard part of the ANTLR distribution, since it is to useful.]


import antlr.*;
import antlr.collections.AST;


/**
 * AST with location information.
 */
public class LocationAST extends CommonAST {
    private int line = 0;
    private int column = 0;

    /** Override. */
    public int getLine() {
        return this.line;
    }

    /** Override. */
    public int getColumn() {
        return this.column;
    }

    public void setLine(int line) {
        this.line = line;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    /** Override. */
    public void initialize(Token tok) {
        super.initialize(tok);
        setLine(tok.getLine());
        setColumn(tok.getColumn());
    }

    /** Override. */
    public void initialize(AST t) {
        super.initialize(t);
        if (t instanceof LocationAST) {
            LocationAST actual = (LocationAST) t;
            setLine(actual.getLine());
            setColumn(actual.getColumn());
        }
    }
}

-- 
Franklin


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list