-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tokenizer.java
167 lines (129 loc) · 4.55 KB
/
Tokenizer.java
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package compilerone;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
/**
*
* @author Jared
*/
public class Tokenizer {
static String keywords[] = {"class", "constructor", "function",
"method", "field", "static", "var", "int", "char", "boolean", "void", "true",
"false", "null", "this", "let", "do", "if", "else", "while", "return"};
private final static String symbols = "{}()[].,;+-*/&|<>=~";
static char[] symbolslist = {'{', '}', '(', ')', '[', ']', '.', ',', ';', '+', '-', '*', '/', '&', '|', '<', '>', '=', '~'};
static String token = "";
static Scanner s;
static ArrayList<String> tokens = new ArrayList<String>();
static Iterator iter = tokens.iterator();
Tokenizer(String path) throws FileNotFoundException {
boolean multiComment = false;
boolean comment = false;
s = new Scanner(new File(path));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()) {
// Strips comments
do {
comment = false;
token = s.next();
if (token.startsWith("//")) {
comment = true;
token = s.nextLine();
}
if (token.startsWith("/*") && multiComment == false) {
multiComment = true;
} else if (token.endsWith("*/") && multiComment == true) {
multiComment = false;
token = s.next();
}
} while (multiComment || comment); // End comment stripping
boolean containsSymbol = false;
for (int i = 0; i < symbolslist.length; i++) {
// if token is alone break
if (token.equals(symbolslist[i] + "")) {
break;
} else if (token.contains(symbolslist[i] + "")) {
containsSymbol = true;
int prev = 0;
for (int j = 0; j < token.length(); j++) {
for (int k = 0; k < symbolslist.length; k++) {
if (token.charAt(j) == symbolslist[k]) {
if (prev != j) {
list.add(token.substring(prev, j));
}
list.add(symbolslist[k] + "");
prev = j + 1;
}
}
if (prev < token.length() && j == token.length() - 1) {
list.add(token.substring(prev, token.length()));
}
}
break;
}
}
if (!containsSymbol) {
list.add(token);
}
} // All tokens are added.
s.close();
tokens = list;
iter = tokens.iterator();
}
boolean hasMoreTokens() {
return iter.hasNext();
}
void advance() {
if (iter.hasNext()) {
token = (String) iter.next();
}
}
String tokenType() {
for (String keyword : keywords) {
if (token.equals(keyword)) {
return "KEYWORD";
}
}
if (symbols.contains(token) && token != "") {
return "SYMBOL";
}
// "regex. \\d+" is any digit from 0-9, + adds support for > 1 digit"
if (token.matches("\\d+")) {
return "INT_CONST";
}
// double quotes, \ is escape char
if (token.startsWith("\"")) {
return "STRING_CONST";
} else {
return "IDENTIFIER";
}
}
String keyWord() {
for (String keyword : keywords) {
if (token.equals(keyword)) {
return keyword;
}
}
return "";
}
char symbol() {
return token.charAt(0);
}
String identifier() {
return token;
}
int intVal() {
return Integer.parseInt(token);
}
String stringVal() {
token = token.replaceAll("\"", "");
return token;
}
}