1   // Copyright 2012- Bill Campbell, Swami Iyer and Bahar Akbal-Delibas
2   
3   package jminusminus;
4   
5   /**
6    * An enum of token kinds. Each entry in this enum represents the kind of a token along with its
7    * image (string representation).
8    */
9   enum TokenKind {
10      // End of file.
11      EOF(""),
12  
13      // Reserved words.
14      ABSTRACT("abstract"), BOOLEAN("boolean"), CHAR("char"), CLASS("class"), ELSE("else"),
15      EXTENDS("extends"), IF("if"), IMPORT("import"), INSTANCEOF("instanceof"), INT("int"),
16      NEW("new"), PACKAGE("package"), PRIVATE("private"), PROTECTED("protected"),
17      PUBLIC("public"), RETURN("return"), STATIC("static"), SUPER("super"), THIS("this"),
18      VOID("void"), WHILE("while"),
19  
20      // Operators.
21      ASSIGN("="), DEC("--"), EQUAL("=="), GT(">"), INC("++"), LAND("&&"), LE("<="), LNOT("!"),
22      MINUS("-"), PLUS("+"), PLUS_ASSIGN("+="), STAR("*"),
23  
24      // Separators.
25      COMMA(","), DOT("."), LBRACK("["), LCURLY("{"), LPAREN("("), RBRACK("]"), RCURLY("}"),
26      RPAREN(")"), SEMI(";"),
27  
28      // Identifiers.
29      IDENTIFIER("<IDENTIFIER>"),
30  
31      // Literals.
32      CHAR_LITERAL("<CHAR_LITERAL>"), FALSE("false"), INT_LITERAL("<INT_LITERAL>"), NULL("null"),
33      STRING_LITERAL("<STRING_LITERAL>"), TRUE("true");
34  
35      // The token kind's string representation.
36      private String image;
37  
38      /**
39       * Constructs an instance of TokenKind given its string representation.
40       *
41       * @param image string representation of the token kind.
42       */
43      private TokenKind(String image) {
44          this.image = image;
45      }
46  
47      /**
48       * Returns the token kind's string representation.
49       *
50       * @return the token kind's string representation.
51       */
52      public String tokenRep() {
53          if (this == EOF) {
54              return "<EOF>";
55          }
56          if (image.startsWith("<") && image.endsWith(">")) {
57              return image;
58          }
59          return "\"" + image + "\"";
60      }
61  
62      /**
63       * Returns the token kind's image.
64       *
65       * @return the token kind's image.
66       */
67      public String image() {
68          return image;
69      }
70  }
71  
72  /**
73   * A representation of tokens returned by the Scanner method getNextToken(). A token has a kind
74   * identifying what kind of token it is, an image for providing any semantic text, and the line in
75   * which it occurred in the source file.
76   */
77  public class TokenInfo {
78      // Token kind.
79      private TokenKind kind;
80  
81      // Semantic text (if any). For example, the identifier name when the token kind is IDENTIFIER
82      // . For tokens without a semantic text, it is simply its string representation. For example,
83      // "+=" when the token kind is PLUS_ASSIGN.
84      private String image;
85  
86      // Line in which the token occurs in the source file.
87      private int line;
88  
89      /**
90       * Constructs a TokenInfo object given its kind, the semantic text forming the token, and its
91       * line number.
92       *
93       * @param kind  the token's kind.
94       * @param image the semantic text forming the token.
95       * @param line  the line in which the token occurs in the source file.
96       */
97      public TokenInfo(TokenKind kind, String image, int line) {
98          this.kind = kind;
99          this.image = image;
100         this.line = line;
101     }
102 
103     /**
104      * Constructs a TokenInfo object given its kind and its line number. Its image is simply the
105      * token kind's string representation.
106      *
107      * @param kind the token's identifying number.
108      * @param line the line in which the token occurs in the source file.
109      */
110     public TokenInfo(TokenKind kind, int line) {
111         this(kind, kind.image(), line);
112     }
113 
114     /**
115      * Returns the token's kind.
116      *
117      * @return the token's kind.
118      */
119     public TokenKind kind() {
120         return kind;
121     }
122 
123     /**
124      * Returns the line number associated with the token.
125      *
126      * @return the line number associated with the token.
127      */
128     public int line() {
129         return line;
130     }
131 
132     /**
133      * Returns the token's string representation.
134      *
135      * @return the token's string representation.
136      */
137     public String tokenRep() {
138         return kind.tokenRep();
139     }
140 
141     /**
142      * Returns the token's image.
143      *
144      * @return the token's image.
145      */
146     public String image() {
147         return image;
148     }
149 }
150