Package EDU.purdue.jtb.parser
Class Token
java.lang.Object
EDU.purdue.jtb.parser.Token
- All Implemented Interfaces:
INode
,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.
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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionint
The column number of the first character of this token.int
The line number of the first character of this token.int
The column number of the last character of this token.int
The line number of the last character of this token.The string image of the token.int
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.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 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.The list of special tokens. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
accept
(IIntVisitor vis) Accepts aIIntVisitor
visitor with user return data.void
accept
(IVoidVisitor vis) Accepts aIVoidVisitor
visitor} visitor with user return data.void
addSpecial
(Token s) Adds a special token to the special tokens list.getSpecialAt
(int i) Gets the special token in the special tokens list at a given position.getSpecials
(String spc) Returns the string of the special tokens of the currentToken
, taking in account a given indentation.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.static Token
newToken
(int ofKind) Factory method callingnewToken(int, String)
with a null image.static Token
Factory method used by JavaCC to create a newToken
(which is also a JTB node).int
toString()
void
Trims the special tokens list.withSpecials
(String spc) Returns the string of the special tokens and the normal token of the currentToken
, taking in account a given indentation.withSpecials
(String spc, 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 Details
-
kind
public int kindAn 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 beginLineThe line number of the first character of this token. -
beginColumn
public int beginColumnThe column number of the first character of this token. -
endLine
public int endLineThe line number of the last character of this token. -
endColumn
public int endColumnThe column number of the last character of this token. -
image
The string image of the 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
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
The list of special tokens. TODO add explanation
-
-
Constructor Details
-
Token
public Token()No-argument constructor. -
Token
public Token(int ki) Constructs a newToken
for the specified kind, with a null image.
Not used in JTB nor JavaCC.- Parameters:
ki
- - the token kind
-
Token
Constructs aToken
with a given kind and image.- Parameters:
ki
- - the token kindim
- - the token image
-
-
Method Details
-
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
Factory method used by JavaCC to create a newToken
(which is also a JTB node). By default returns a newToken
object. 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
Factory method callingnewToken(int, String)
with a null image.- Parameters:
ofKind
- - the token kind- Returns:
- a new Token
-
toString
-
getSpecialAt
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
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
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
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
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
Accepts aIIntVisitor
visitor with user return data. -
accept
Accepts aIVoidVisitor
visitor} visitor with user return data.
-