-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.java
145 lines (134 loc) · 3.07 KB
/
Lexer.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
//
// Compiler for CS 480
// class Lexer
//
// Written by Tim Budd, Winter term 2006
//
// modified by:
//
import java.io.*;
//
//--------Lexer----------------
//
public class Lexer {
private PushbackReader input;
private String token;
private int tokenType;
public Lexer(Reader in) {
input = new PushbackReader(in);
}
private void skipWhiteSpace() throws ParseException, IOException {
int c = input.read();
while ((c != -1) && Character.isWhitespace((char) c))
c = input.read();
if (c == -1)
return;
if (c == '{') {
c = input.read();
while (c != '}') {
if (c == '{')
throw new ParseException(1);
if (c == -1)
throw new ParseException(1);
c = input.read();
}
skipWhiteSpace();
}
else if (c != -1)
input.unread(c);
}
public void nextLex() throws ParseException {
token = "";
try{
skipWhiteSpace();
int c = input.read();
if (c == -1) {
token = "<eof>";
tokenType = endOfInput;
return;
}
if (Character.isDigit((char) c)) {
tokenType = intToken;
while (Character.isDigit((char) c)) {
token = token + (char) c;
c = input.read();
}
if (c == '.') {
tokenType = realToken;
token = token + (char) c;
c = input.read();
while (Character.isDigit((char) c)) {
token = token + (char) c;
c = input.read();
}
}
if (c != -1)
input.unread(c);
}
else if (Character.isLetter((char) c)) {
tokenType = identifierToken;
while (Character.isLetterOrDigit((char) c)) {
token = token + (char) c;
c = input.read();
}
if (c != -1)
input.unread(c);
String [] keywords = {"begin", "class", "const",
"and", "or",
"else", "end", "function", "if", "not",
"return", "type", "while", "var", };
for (int i = 0; i < keywords.length; i++)
if (token.equals(keywords[i]))
tokenType = keywordToken;
}
else if (c == '"') {
tokenType = stringToken;
c = input.read();
while (c != '"') {
token = token + (char) c;
c = input.read();
if (c == -1)
throw new ParseException(2);
}
}
else {
tokenType = otherToken;
token = token + (char) c;
int d = input.read();
if ((c == '<') && (d == '='))
token = token + (char) d;
else if ((c == '<') && (d == '<'))
token = token + (char) d;
else if ((c == '>') && (d == '='))
token = token + (char) d;
else if ((c == '=') && (d == '='))
token = token + (char) d;
else if ((c == '!') && (d == '='))
token = token + (char) d;
else if (d != -1)
input.unread(d);
}
}
catch (IOException e)
{ throw new ParseException(0); }
}
static final int identifierToken = 1;
static final int keywordToken = 2;
static final int intToken = 3;
static final int realToken = 4;
static final int stringToken = 5;
static final int otherToken = 6;
static final int endOfInput = 7;
public String tokenText() {
return token;
}
public int tokenCategory() {
return tokenType;
}
public boolean isIdentifier() {
return tokenType == identifierToken;
}
public boolean match (String test) {
return test.equals(token);
}
}