-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.java
More file actions
151 lines (133 loc) · 2.87 KB
/
Token.java
File metadata and controls
151 lines (133 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package icsi311;
public class Token {
// 2. Has String numbers ?
// 3. Has line number - Done
// 4. Has start position - Done
// 5. Has both token constructors - Done
// 6. toString() Exists and outputs type and value members clearly - Done
// 7. All four (variables) are correct and private - Done
// Enum initiated with WORD, NUMBER, and SEPARATOR
public enum TokenType {
WORD,
NUMBER,
SEPARATOR,
WHILE,
IF,
DO,
FOR,
BREAK,
CONTINUE,
ELSE,
RETURN,
BEGIN,
END,
PRINT,
PRINTF,
NEXT,
IN,
DELETE,
GETLINE,
EXIT,
NEXTFILE,
FUNCTION,
STRINGLITERAL,
// Single character types
OPBRAC,
CLBRAC,
OPBRACE,
CLBRACE,
OPAREN,
CPAREN,
DOLLAR,
TILDE,
ASSIGN,
LETHAN,
GRTHAN,
EXLPT,
PLUS,
CARROT,
MINUS,
QMARK,
COLON,
STAR,
SLASH,
PERCNT,
VERTBAR,
COMMA,
BACKTIC,
// Two character symbols
GREQ,
ADD,
SUBT,
LEEQ,
EQUALS,
NEQ,
CAREQ,
PEREQ,
TIEQ,
DIVEQ,
PLEQ,
MINEQ,
REGEXP,
AND,
LEADS,
OR
}
// Type used to specify enum type
private TokenType type;
// Holds string value
private String str;
// Holds numerical value
private double value;
// Holds line #
private int ln;
// Holds string position in text file
private int startPos;
// Constructor used for string type cases
public Token(String s, int p, int l) {
if (s.charAt(0) == 10)
type = TokenType.SEPARATOR;
else
type = TokenType.WORD;
str = s;
ln = l;
startPos = p;
}
public Token(TokenType t, String s, int p, int l) {
type = t;
ln = l;
startPos = p;
str = s;
if (t == TokenType.NUMBER) {
value = Double.valueOf(s);
}
}
// Shows what the file would look like as a toString
public String toString() {
if (type == TokenType.SEPARATOR)
return "SEPARATOR";
if (type == TokenType.WORD)
return str;
else if (type == TokenType.NUMBER)
return getValue();
else if (type == TokenType.STRINGLITERAL)
return "\"" + str + "\"";
else
return String.valueOf(type);
}
public int getLine() {
return ln;
}
public int getStartPos() {
return startPos;
}
public TokenType getType() {
return type;
}
public String getValue() {
return str;
}
public double getVal(){
return value;
}
}