Class Token

java.lang.Object
EDU.purdue.jtb.parser.Token
All Implemented Interfaces:
INode, Serializable

public class Token extends Object implements 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.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
    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.

    Fields inherited from interface EDU.purdue.jtb.parser.syntaxtree.INode

    LS
  • Constructor Summary

    Constructors
    Constructor
    Description
    No-argument constructor.
    Token(int ki)
    Constructs a new Token for the specified kind, with a null image.
    Not used in JTB nor JavaCC.
    Token(int ki, String im)
    Constructs a Token with a given kind and image.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Accepts a IIntVisitor visitor with user return data.
    void
    Accepts a IVoidVisitor visitor} visitor with user return data.
    void
    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.
    Returns the string of the special tokens of the current Token, taking in account a given indentation.
    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 calling newToken(int, String) with a null image.
    static Token
    newToken(int ofKind, String image)
    Factory method used by JavaCC to create a new Token
    (which is also a JTB node).
    int
     
     
    void
    Trims the special tokens list.
    Returns the string of the special tokens and the normal token of the current Token, taking in account a given indentation.
    Returns the string of the special tokens and the normal token of the current Token, taking in account a given indentation and a given assignment.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • 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 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 List<Token> specialTokens
      The list of special tokens. TODO add explanation
  • Constructor Details

    • Token

      public Token()
      No-argument constructor.
    • Token

      public Token(int ki)
      Constructs a new Token for the specified kind, with a null image.
      Not used in JTB nor JavaCC.
      Parameters:
      ki - - the token kind
    • Token

      public Token(int ki, String im)
      Constructs a Token with a given kind and image.
      Parameters:
      ki - - the token kind
      im - - the token image
  • Method Details

    • getValue

      public 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, String image)
      Factory method used by JavaCC to create a new Token
      (which is also a JTB node). By default returns a new Token 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 kind
      image - - the token image
      Returns:
      a new Token
    • newToken

      public static Token newToken(int ofKind)
      Factory method calling newToken(int, String) with a null image.
      Parameters:
      ofKind - - the token kind
      Returns:
      a new Token
    • toString

      public String toString()
      Overrides:
      toString in class 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 String getSpecials(String spc)
      Returns the string of the special tokens of the current Token, taking in account a given indentation.
      Parameters:
      spc - - the indentation
      Returns:
      the string representing the special tokens list
    • withSpecials

      public String withSpecials(String spc)
      Returns the string of the special tokens and the normal token of the current Token, taking in account a given indentation.
      Parameters:
      spc - - the indentation
      Returns:
      the string representing the special tokens list and the token
    • withSpecials

      public String withSpecials(String spc, String var)
      Returns the string of the special tokens and the normal token of the current Token, taking in account a given indentation and a given assignment.
      Parameters:
      spc - - the indentation
      var - - the variable assignment to be inserted
      Returns:
      the string representing the special tokens list and the token
    • accept

      public int accept(IIntVisitor vis)
      Accepts a IIntVisitor visitor with user return data.
      Specified by:
      accept in interface INode
      Parameters:
      vis - - the visitor
      Returns:
      the user Return data
    • accept

      public void accept(IVoidVisitor vis)
      Accepts a IVoidVisitor visitor} visitor with user return data.
      Specified by:
      accept in interface INode
      Parameters:
      vis - - the visitor