antlraux package
v0.2.1

antlraux.util
Class LexInfoToken

java.lang.Object
  |
  +--antlr.Token
        |
        +--antlr.CommonToken
              |
              +--antlraux.util.LexInfoToken
All Implemented Interfaces:
java.lang.Cloneable, LexInfo

public class LexInfoToken
extends antlr.CommonToken
implements LexInfo

This subclass of CommonToken implements LexInfo, really allowing the use of a filename (which is ommited in CommonToken) To make your lexer use this class of tokens, you'll have to use the method setTokenObjectClass:

 import antlraux.util.LexInfoToken;
 ...
 MyLexer lexer = new MyLexer(new FileInputStream(filename));
 lexer.setFilename(filename);
 lexer.setTokenObjectClass("antlraux.util.LexInfoToken");
 
Warning! By default, the antlr v2.7.2 implementation of lexer does NOT add filename information to the tokens it creates. Tokens are created in method CharScanner.makeToken(int) of class antlr.CharScanner, which is the superClass of every Lexer we can create. So in order to set the filename in our lexer we'll have to override it in the grammar definition file of the lexer:
 public class MyLexer extends Lexer;
 options {...}
 tokens {...}
 // Code section
 {
    public Token makeToken(int type)
    {
        // Obtain the token with no filename, and then add it
        Token result = super.makeToken(type);
        result.setFilename(getFilename());
        return result;
    }
 }
 
Once you include this on your lexer LexInfoToken will work perfectly (Keep in mind that this modification might not be necessary in future versions of antlr).

Author:
Enrique José García Cota
See Also:
LexInfoAST, LexInfo

Field Summary
 
Fields inherited from class antlr.CommonToken
col, line, text
 
Fields inherited from class antlr.Token
badToken, EOF_TYPE, INVALID_TYPE, MIN_USER_TYPE, NULL_TREE_LOOKAHEAD, SKIP
 
Fields inherited from interface antlraux.util.LexInfo
DEFAULT_COLUMN, DEFAULT_FILENAME, DEFAULT_LINE
 
Constructor Summary
LexInfoToken()
          Default constructor.
LexInfoToken(int type, java.lang.String text)
          Only initializes type and text.
LexInfoToken(antlr.Token t)
          Copy constructor
 
Method Summary
 void copyLexInfo(LexInfo from)
          Sets the Lex information
 java.lang.String getFilename()
          Get the file name for this token
 java.lang.String getLexInfoString()
          Returns a String in the stype "filename:line:column".
 void setFilename(java.lang.String fn)
          Set the file name for this token.
 
Methods inherited from class antlr.CommonToken
getColumn, getLine, getText, setColumn, setLine, setText, toString
 
Methods inherited from class antlr.Token
getType, setType
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface antlraux.util.LexInfo
getColumn, getLine, setColumn, setLine
 

Constructor Detail

LexInfoToken

public LexInfoToken()
Default constructor. This constructor is necessary for every subclass of Token in order to being able to create them in the CharScanner


LexInfoToken

public LexInfoToken(int type,
                    java.lang.String text)
Only initializes type and text. Lexical information is set to default values


LexInfoToken

public LexInfoToken(antlr.Token t)
Copy constructor

Method Detail

getFilename

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

Specified by:
getFilename in interface LexInfo
Overrides:
getFilename in class antlr.Token

setFilename

public void setFilename(java.lang.String fn)
Set the file name for this token. If null is guiven, then filename=defaultFilename.

Specified by:
setFilename in interface LexInfo
Overrides:
setFilename in class antlr.Token

copyLexInfo

public void copyLexInfo(LexInfo from)
Sets the Lex information

Specified by:
copyLexInfo in interface LexInfo

getLexInfoString

public java.lang.String getLexInfoString()
Returns a String in the stype "filename:line:column". This is done with the help of class FileLineFormatter, so problems like filename==null are taken into account.

Specified by:
getLexInfoString in interface LexInfo

antlraux package
v0.2.1

Created by Enrique José García Cota