antlraux package
v0.2.1

antlraux.util
Class LexInfoAST

java.lang.Object
  |
  +--antlr.BaseAST
        |
        +--antlr.CommonAST
              |
              +--antlraux.util.LexInfoAST
All Implemented Interfaces:
antlr.collections.AST, LexInfo, java.io.Serializable
Direct Known Subclasses:
ScopeAST, TypeAST

public class LexInfoAST
extends antlr.CommonAST
implements LexInfo

This subclass of CommonAST implements LexInfo, so it can return the filename, line and column of its information. It also provides additional navigation methods.

In order to use LexInfoAST in your parser or tree parser, you'll have to use the method Parser.setASTNodeClass(String):

 import antlraux.util.LexInfoAST;
 ...

 MyLexer lexer = new MyLexer(new FileInputStream(filename));
 lexer.setFilename(filename);
 MyParser parser = new MyParser(lexer);
 parser.setFilename(filename);
 parser.setASTNodeClass("antlraux.util.LexInfoAST");
 
In order to obtain the right filenames, you'll also have to specify a Token type different from the default (CommonToken), for example LexInfoToken (you should read that link if you're using antlr ver. 2.7.2 at least; there's a little problem with filenames).

When working with ASTs that are not of the default type (CommonAST) in tree parsers, it is usually usefull using the ASTLabelType option:

 header {
 import antlraux.util.LexInfoAST;	
 }

 class MyTreeParser extends TreeParser;
 options{
    ASTLabelType = antlraux.util.LexInfoAST;
 }
 
This makes a lot of authomatic castings to LexInfoAST, saving a lot of writing to the programmer. But it only works for grammars that parse trees of type LexInfoAST (or subclasses of it).

Author:
Enrique José García Cota
See Also:
LexInfoToken, LexInfo, Serialized Form

Field Summary
protected static boolean verboseStringConversion
           
 
Fields inherited from class antlr.BaseAST
down, right
 
Fields inherited from interface antlraux.util.LexInfo
DEFAULT_COLUMN, DEFAULT_FILENAME, DEFAULT_LINE
 
Constructor Summary
LexInfoAST()
          Default constructor.
LexInfoAST(antlr.collections.AST ast)
          Calls initialize(antlr.collections.AST)
LexInfoAST(int type, java.lang.String text)
          Calls CommonAST.initialize(int,String)
LexInfoAST(antlr.Token tok)
          Calls initialize(antlr.Token)
 
Method Summary
 void addChildLeft(antlr.collections.AST newFirstChild)
          Similar to AST.addChild(AST), but this one inserts the child at the beginning of the children list, instead of at the end.
 void addChildLeftWithSiblings(antlr.collections.AST newFirstChild)
          Similar to addChildLeft(AST), but conservating newFirstChild's sibling list.
 void addSibling(antlr.collections.AST newLastSibling)
          Adds a sibling to the sibling list, without erasing
 void copyLexInfo(LexInfo from)
          Copies filename, line and column from "from"
 int getColumn()
          Get the file name for this node
 java.lang.String getFilename()
          Get the file name for this node
 antlr.collections.AST getLastChild()
          Returns the last child of the children, or null if there are no children
 antlr.collections.AST getLastSibling()
          It returns the last sibling of the sibling list.
 java.lang.String getLexInfoString()
          Returns a string representing the lexInfo of the AST (filename:line:column).
 int getLine()
          Get the file name for this node
 antlr.collections.AST getPrevChild(antlr.collections.AST child)
          It returns the sibling that precedes "child" in the children list.
 void initialize(antlr.collections.AST ast)
          Firstly this method calls super.initialize(ast) Then it checks if ast is instanceof LexInfoAST.
 void initialize(antlr.Token t)
          Firstly this method calls super.initialize(token) Then it copies filename, line and column information from the Token.
 boolean replaceChild(antlr.collections.AST prevAST, antlr.collections.AST newAST)
          Replaces a child node (prevAST) with another one (newAST) This method erases any siblings of newAST and adds the current tree's children as siblings
 boolean replaceChildWithSiblings(antlr.collections.AST prevAST, antlr.collections.AST newAST)
          Similar to replaceChild(AST,AST), but conservating newAST's sibling list (current tree's children are added after that list).
 void setColumn(int column)
          Set the file name for this node
 void setFilename(java.lang.String fn)
          Set the file name for this node.
 void setLine(int line)
          Set the file name for this node
static void setVerboseStringConversion(boolean b)
          When set to true, adds information to toString()
protected static void tabulate(int t, java.lang.StringBuffer sb)
           
 java.lang.String toString()
          Returns a String representing the information in the root of the node.
 java.lang.String toStringList()
           
 java.lang.String toStringList(int tabLevel)
          Print out a child-sibling tree in LISP notation
 java.lang.String toStringTree()
           
 
Methods inherited from class antlr.CommonAST
getText, getType, initialize, setText, setType
 
Methods inherited from class antlr.BaseAST
addChild, decode, encode, equals, equalsList, equalsListPartial, equalsTree, equalsTreePartial, findAll, findAllPartial, getFirstChild, getNextSibling, getNumberOfChildren, removeChildren, setFirstChild, setNextSibling, setVerboseStringConversion, xmlSerialize, xmlSerializeNode, xmlSerializeRootClose, xmlSerializeRootOpen
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

verboseStringConversion

protected static boolean verboseStringConversion
Constructor Detail

LexInfoAST

public LexInfoAST()
Default constructor. Every AST implementation must provide a default constructor in order to be created in a parser or treeparser


LexInfoAST

public LexInfoAST(antlr.Token tok)
Calls initialize(antlr.Token)


LexInfoAST

public LexInfoAST(int type,
                  java.lang.String text)
Calls CommonAST.initialize(int,String)


LexInfoAST

public LexInfoAST(antlr.collections.AST ast)
Calls initialize(antlr.collections.AST)

Method Detail

initialize

public void initialize(antlr.collections.AST ast)
Firstly this method calls super.initialize(ast) Then it checks if ast is instanceof LexInfoAST. If yes, it copies its lex info using copyLexInfo(antlraux.util.LexInfo)

Specified by:
initialize in interface antlr.collections.AST
Overrides:
initialize in class antlr.CommonAST
Parameters:
ast - The AST whose information is to be copied

initialize

public void initialize(antlr.Token t)
Firstly this method calls super.initialize(token) Then it copies filename, line and column information from the Token.

Specified by:
initialize in interface antlr.collections.AST
Overrides:
initialize in class antlr.CommonAST
Parameters:
t - The token whose information is to be copied

getFilename

public java.lang.String getFilename()
Get the file name for this node

Specified by:
getFilename in interface LexInfo

getColumn

public int getColumn()
Get the file name for this node

Specified by:
getColumn in interface LexInfo

getLine

public int getLine()
Get the file name for this node

Specified by:
getLine in interface LexInfo

setFilename

public void setFilename(java.lang.String fn)
Set the file name for this node.

Specified by:
setFilename in interface LexInfo

setColumn

public void setColumn(int column)
Set the file name for this node

Specified by:
setColumn in interface LexInfo

setLine

public void setLine(int line)
Set the file name for this node

Specified by:
setLine in interface LexInfo

copyLexInfo

public void copyLexInfo(LexInfo from)
Copies filename, line and column from "from"

Specified by:
copyLexInfo in interface LexInfo
Parameters:
from - The lex info provider

getLexInfoString

public java.lang.String getLexInfoString()
Returns a string representing the lexInfo of the AST (filename:line:column). If any information is ommitted (like when filename==null or line==-1) then it is not included in the resulting String.

Specified by:
getLexInfoString in interface LexInfo

getPrevChild

public antlr.collections.AST getPrevChild(antlr.collections.AST child)
It returns the sibling that precedes "child" in the children list.

Parameters:
child - the child whose left sibling is being searched
Returns:
null if child is the first child of the list or if it is not a child of "this".

getLastChild

public antlr.collections.AST getLastChild()
Returns the last child of the children, or null if there are no children

Returns:
the last child of the children list, or null if no children are found

getLastSibling

public antlr.collections.AST getLastSibling()
It returns the last sibling of the sibling list. If no siblings, returs this.

Returns:
the last sibling, or "this" if no siblings are found

addSibling

public void addSibling(antlr.collections.AST newLastSibling)
Adds a sibling to the sibling list, without erasing

Parameters:
newLastSibling - The new last sibling. If it is null then the method does nothing

addChildLeft

public void addChildLeft(antlr.collections.AST newFirstChild)
Similar to AST.addChild(AST), but this one inserts the child at the beginning of the children list, instead of at the end. If null==newFirstChild, then the children list will be erased. This method erases any siblings of newFirstChild and adds the current tree's children as siblings

Parameters:
newFirstChild - the new first child

addChildLeftWithSiblings

public void addChildLeftWithSiblings(antlr.collections.AST newFirstChild)
Similar to addChildLeft(AST), but conservating newFirstChild's sibling list. If null==newFirstChild, then the children list will be erased.

Parameters:
newFirstChild - the new first child

replaceChild

public boolean replaceChild(antlr.collections.AST prevAST,
                            antlr.collections.AST newAST)
Replaces a child node (prevAST) with another one (newAST) This method erases any siblings of newAST and adds the current tree's children as siblings

Parameters:
prevAST - the AST that is going to be substituted
newAST - the new AST
Returns:
true if the replacement was successful, false otherwise (prevAST was not a child of this).

replaceChildWithSiblings

public boolean replaceChildWithSiblings(antlr.collections.AST prevAST,
                                        antlr.collections.AST newAST)
Similar to replaceChild(AST,AST), but conservating newAST's sibling list (current tree's children are added after that list).

Parameters:
prevAST - the AST that is going to be substituted
newAST - the new AST
Returns:
true if the replacement was successful, false otherwise (prevAST was not a child of this).

setVerboseStringConversion

public static void setVerboseStringConversion(boolean b)
When set to true, adds information to toString()


toString

public java.lang.String toString()
Returns a String representing the information in the root of the node. If setVerboseStringConversion(boolean) has been set to true, it adds the lex information of the AST (using getLexInfoString())

Specified by:
toString in interface antlr.collections.AST
Overrides:
toString in class antlr.BaseAST

tabulate

protected static void tabulate(int t,
                               java.lang.StringBuffer sb)

toStringList

public java.lang.String toStringList()
Specified by:
toStringList in interface antlr.collections.AST
Overrides:
toStringList in class antlr.BaseAST

toStringList

public java.lang.String toStringList(int tabLevel)
Print out a child-sibling tree in LISP notation


toStringTree

public java.lang.String toStringTree()
Specified by:
toStringTree in interface antlr.collections.AST
Overrides:
toStringTree in class antlr.BaseAST

antlraux package
v0.2.1

Created by Enrique José García Cota