-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.java
More file actions
126 lines (111 loc) · 3.86 KB
/
Copy pathToken.java
File metadata and controls
126 lines (111 loc) · 3.86 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
/*
Class: CS 4308 Section 03
Term: Fall 2021
Name: Faith Swetnam
Instructor: Sharon Perry
Project: Deliverable 1 Scanner
Updated: 10/28/2021
*/
//Token object holds a tokens TokenType type, lexeme (value), and the line its on
public class Token {
private TokenType type;
private String lexeme;
private int line;
//Token constructor
Token(TokenType type, String lexeme, int line){
this.type = type;
this.lexeme = lexeme;
this.line = line;
}
Token(TokenType type, int line){
this.type = type;
this.lexeme = "ERROR";
this.line = line;
}
//Added
Token(){
this.type = TokenType.NULL;
this.lexeme = "NULL";
this.line = 0;
}
//Prints out token values
void printToken() {
System.out.printf("%-10s\t%-20s\t%-10d\t%-10d\n", lexeme, type.label, type.opcode, line);
}
//Returns lexeme
String getLexeme(){ return lexeme; }
//Returns line
int getLine(){ return line; }
//Returns TokenType
TokenType getType(){
return type;
}
//Returns if the tokens TokenType is equal to the passed TokenType
boolean checkType(TokenType tokenType){
if(type == tokenType){
return true;
}
return false;
}
//Enumerated type to hold the type of character in nextChar
//Each CharacterClass has a description for printing
enum CharacterClass {
LETTER ("identifier"),
DIGIT ("number"),
UNKNOWN ("unknown symbol"),
EOF("end of program");
final String description;
private CharacterClass(String description) {
this.description = description;
}
}
//Enumerated type to hold legal keywords and symbols (essentially the legal token types allowed by language)
//Each TokenType has an associated label and opcode. It should contain the String value that holds the lexeme
enum TokenType {
//ADDITIONS: ERROR, NULL, END, REPEAT
NULL("null", "", 0),
LETTER("identifier", "", 1),
DIGIT("number", "", 2),
EOF("end of file", "end", 99),
END("keyword_end", "end", 1000),
FUNCT("keyword_function", "function", 1001),
WHILE("keyword_while", "while", 1002),
DO("keyword_do", "do", 1003),
PRINT("keyword_print", "print", 1004),
IF("keyword_if", "if", 1005),
THEN("keyword_then", "then", 1006),
ELSE("keyword_else", "else", 1007),
REPEAT("keyword_repeat", "repeat", 1008),
UNTIL("keyword_until", "until", 1009),
ASSIGN_OP("assignment_operator", "=", 2000),
LE_OP("less_equal", "<=", 2001),
LT_OP("less", "<", 2002),
GE_OP("greater_equal", ">=", 2003),
GT_OP("greater", ">", 2004),
EQ_OP("equal", "==", 2005),
NE_OP("not_equal", "!=", 2006),
AE_OP("addition_assignment", "+=", 2007),
ADD_OP("addition_operator", "+", 2008),
SUB_OP("subtraction_operator", "-", 2009),
MUL_OP("multiplication_operator", "*", 2010),
DIV_OP("division_operator", "/", 2011),
L_PAREN("left_parenthesis", "(", 2012),
R_PAREN("right_parenthesis", ")", 2013),
ERROR("error", new Error("error occurred"), 9999);
final String label;
final int opcode;
Error e = new Error("error occurred");
String value;
//TokenType constructor
private TokenType(String label, String value, int opcode) {
this.label = label;
this.value = value;
this.opcode = opcode;
}
private TokenType(String label, Error e, int opcode){
this.label = label;
this.e = e;
this.opcode = opcode;
}
}
}