Package EDU.purdue.jtb.parser
Class Token
- java.lang.Object
-
- EDU.purdue.jtb.parser.Token
-
- All Implemented Interfaces:
INode,java.io.Serializable
public class Token extends java.lang.Object implements INode, java.io.Serializable
Represents a JavaCC single token in the grammar and a JTB corresponding node.
The class holds all the fields and methods generated normally by JavaCC, plus the ones required by JTB.
If the "-tk" JTB option is used, it also contains an ArrayList of preceding special tokens.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description intbeginColumnThe column number of the first character of this token.intbeginLineThe line number of the first character of this token.intendColumnThe column number of the last character of this token.intendLineThe line number of the last character of this token.java.lang.StringimageThe string image of the token.intkindAn integer that describes the kind of this token.
This numbering system is determined by JavaCCParser,
and a table of these numbers is stored in the class &l;ParserName>Constants.java.TokennextFor a regular token, a reference to the next regular token from the input stream,
or null if this is the last token from the input stream, or if the token manager
has not (yet) read a regular token beyond this one.TokenspecialTokenFor a regular token, a reference to the special token just before to this token,
(without an intervening regular token), or null if there is no such special token.java.util.List<Token>specialTokensThe list of special tokens.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intaccept(IIntVisitor vis)Accepts aIIntVisitorvisitor with user return data.voidaccept(IVoidVisitor vis)Accepts aIVoidVisitorvisitor} visitor with user return data.voidaddSpecial(Token s)Adds a special token to the special tokens list.TokengetSpecialAt(int i)Gets the special token in the special tokens list at a given position.java.lang.StringgetSpecials(java.lang.String spc)Returns the string of the special tokens of the currentToken, taking in account a given indentation.java.lang.ObjectgetValue()An optional attribute value of the Token.
Tokens which are not used as syntactic sugar will often contain meaningful values
that will be used later on by the compiler or interpreter.
This attribute value is often different from the image.
Any subclass of Token that actually wants to return a non-null value
can override this method as appropriate.
Not used in JTB.static TokennewToken(int ofKind)Factory method callingnewToken(int, String)with a null image.static TokennewToken(int ofKind, java.lang.String image)Factory method used by JavaCC to create a newToken
(which is also a JTB node).intnumSpecials()java.lang.StringtoString()voidtrimSpecials()Trims the special tokens list.java.lang.StringwithSpecials(java.lang.String spc)Returns the string of the special tokens and the normal token of the currentToken, taking in account a given indentation.java.lang.StringwithSpecials(java.lang.String spc, java.lang.String var)Returns the string of the special tokens and the normal token of the currentToken, taking in account a given indentation and a given assignment.
-
-
-
Field Detail
-
kind
public int kind
An integer that describes the kind of this token.
This numbering system is determined by JavaCCParser,
and a table of these numbers is stored in the class &l;ParserName>Constants.java.
-
beginLine
public int beginLine
The line number of the first character of this token.
-
beginColumn
public int beginColumn
The column number of the first character of this token.
-
endLine
public int endLine
The line number of the last character of this token.
-
endColumn
public int endColumn
The column number of the last character of this token.
-
image
public java.lang.String image
The string image of the token.
-
next
public Token next
For a regular token, a reference to the next regular token from the input stream,
or null if this is the last token from the input stream, or if the token manager
has not (yet) read a regular token beyond this one.For a special token, a reference to the special token that just after it
(without an intervening regular token) if it exists, or null otherwise.
-
specialToken
public Token specialToken
For a regular token, a reference to the special token just before to this token,
(without an intervening regular token), or null if there is no such special token.For a special token, a reference to the special token just after it
(without an intervening regular token) if it exists, or null otherwise.
-
specialTokens
public java.util.List<Token> specialTokens
The list of special tokens. TODO add explanation
-
-
Constructor Detail
-
Token
public Token()
No-argument constructor.
-
Token
public Token(int ki)
Constructs a newTokenfor the specified kind, with a null image.
Not used in JTB nor JavaCC.- Parameters:
ki- - the token kind
-
Token
public Token(int ki, java.lang.String im)Constructs aTokenwith a given kind and image.- Parameters:
ki- - the token kindim- - the token image
-
-
Method Detail
-
getValue
public java.lang.Object getValue()
An optional attribute value of the Token.
Tokens which are not used as syntactic sugar will often contain meaningful values
that will be used later on by the compiler or interpreter.
This attribute value is often different from the image.
Any subclass of Token that actually wants to return a non-null value
can override this method as appropriate.
Not used in JTB.- Returns:
- a value
-
newToken
public static Token newToken(int ofKind, java.lang.String image)
Factory method used by JavaCC to create a newToken
(which is also a JTB node). By default returns a newTokenobject. You can override it to create and return subclass objects
based on the value of ofKind.
Simply add the cases to the switch for all those special cases.
For example, if you have a subclass of Token called IDToken
that you want to create if ofKind is ID, simply add something like:
case MyParserConstants.ID : return new IDToken(ofKind, image);
to the following switch statement.
Then you can cast matchedToken variable to the appropriate type
and use it in your lexical actions.- Parameters:
ofKind- - the token kindimage- - the token image- Returns:
- a new Token
-
newToken
public static Token newToken(int ofKind)
Factory method callingnewToken(int, String)with a null image.- Parameters:
ofKind- - the token kind- Returns:
- a new Token
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- Returns:
- the token image
-
getSpecialAt
public Token getSpecialAt(int i)
Gets the special token in the special tokens list at a given position.- Parameters:
i- - the special token's position- Returns:
- the special token
-
numSpecials
public int numSpecials()
- Returns:
- the number of special tokens
-
addSpecial
public void addSpecial(Token s)
Adds a special token to the special tokens list.- Parameters:
s- - the special token to add
-
trimSpecials
public void trimSpecials()
Trims the special tokens list.
-
getSpecials
public java.lang.String getSpecials(java.lang.String spc)
Returns the string of the special tokens of the currentToken, taking in account a given indentation.- Parameters:
spc- - the indentation- Returns:
- the string representing the special tokens list
-
withSpecials
public java.lang.String withSpecials(java.lang.String spc)
Returns the string of the special tokens and the normal token of the currentToken, taking in account a given indentation.- Parameters:
spc- - the indentation- Returns:
- the string representing the special tokens list and the token
-
withSpecials
public java.lang.String withSpecials(java.lang.String spc, java.lang.String var)Returns the string of the special tokens and the normal token of the currentToken, taking in account a given indentation and a given assignment.- Parameters:
spc- - the indentationvar- - the variable assignment to be inserted- Returns:
- the string representing the special tokens list and the token
-
accept
public int accept(IIntVisitor vis)
Accepts aIIntVisitorvisitor with user return data.
-
accept
public void accept(IVoidVisitor vis)
Accepts aIVoidVisitorvisitor} visitor with user return data.
-
-