blob: 6dadd0e592917fee927c80601859816e0112b5d8 (
plain) (
blame)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/*
* File: tokenscanner.cpp
* ----------------------
* Implementation for the TokenScanner class.
*/
#include <cctype>
#include <iostream>
#include "error.h"
#include "tokenscanner.h"
#include "strlib.h"
#include "stack.h"
using namespace std;
TokenScanner::TokenScanner() {
initScanner();
setInput("");
}
TokenScanner::TokenScanner(string str) {
initScanner();
setInput(str);
}
TokenScanner::TokenScanner(istream & infile) {
initScanner();
setInput(infile);
}
TokenScanner::~TokenScanner() {
if (stringInputFlag) delete isp;
}
void TokenScanner::setInput(string str) {
stringInputFlag = true;
buffer = str;
isp = new istringstream(buffer);
savedTokens = NULL;
}
void TokenScanner::setInput(istream & infile) {
stringInputFlag = false;
isp = &infile;
savedTokens = NULL;
}
bool TokenScanner::hasMoreTokens() {
string token = nextToken();
saveToken(token);
return (token != "");
}
string TokenScanner::nextToken() {
if (savedTokens != NULL) {
StringCell *cp = savedTokens;
string token = cp->str;
savedTokens = cp->link;
delete cp;
return token;
}
while (true) {
if (ignoreWhitespaceFlag) skipSpaces();
int ch = isp->get();
if (ch == '/' && ignoreCommentsFlag) {
ch = isp->get();
if (ch == '/') {
while (true) {
ch = isp->get();
if (ch == '\n' || ch == '\r' || ch == EOF) break;
}
continue;
} else if (ch == '*') {
int prev = EOF;
while (true) {
ch = isp->get();
if (ch == EOF || (prev == '*' && ch == '/')) break;
prev = ch;
}
continue;
}
if (ch != EOF) isp->unget();
ch = '/';
}
if (ch == EOF) return "";
if ((ch == '"' || ch == '\'') && scanStringsFlag) {
isp->unget();
return scanString();
}
if (isdigit(ch) && scanNumbersFlag) {
isp->unget();
return scanNumber();
}
if (isWordCharacter(ch)) {
isp->unget();
return scanWord();
}
string op = string(1, ch);
while (isOperatorPrefix(op)) {
ch = isp->get();
if (ch == EOF) break;
op += ch;
}
while (op.length() > 1 && !isOperator(op)) {
isp->unget();
op.erase(op.length() - 1, 1);
}
return op;
}
}
void TokenScanner::saveToken(string token) {
StringCell *cp = new StringCell;
cp->str = token;
cp->link = savedTokens;
savedTokens = cp;
}
void TokenScanner::ignoreWhitespace() {
ignoreWhitespaceFlag = true;
}
void TokenScanner::ignoreComments() {
ignoreCommentsFlag = true;
}
void TokenScanner::scanNumbers() {
scanNumbersFlag = true;
}
void TokenScanner::scanStrings() {
scanStringsFlag = true;
}
void TokenScanner::addWordCharacters(string str) {
wordChars += str;
}
void TokenScanner::addOperator(string op) {
StringCell *cp = new StringCell;
cp->str = op;
cp->link = operators;
operators = cp;
}
int TokenScanner::getPosition() const {
if (savedTokens == NULL) {
return int(isp->tellg());
} else {
return int(isp->tellg()) - savedTokens->str.length();
}
return -1;
}
bool TokenScanner::isWordCharacter(char ch) const {
return isalnum(ch) || wordChars.find(ch) != string::npos;
};
void TokenScanner::verifyToken(string expected) {
string token = nextToken();
if (token != expected) {
string msg = "Found \"" + token + "\"" +
" when expecting \"" + expected + "\"";
error(msg);
}
};
TokenType TokenScanner::getTokenType(string token) const {
if (token == "") return TokenType(EOF);
char ch = token[0];
if (isspace(ch)) return SEPARATOR;
if (ch == '"' || (ch == '\'' && token.length() > 1)) return STRING;
if (isdigit(ch)) return NUMBER;
if (isWordCharacter(ch)) return WORD;
return OPERATOR;
};
string TokenScanner::getStringValue(string token) const {
string str = "";
int start = 0;
int finish = token.length();
if (finish > 1 && (token[0] == '"' || token[0] == '\'')) {
start = 1;
finish--;
}
for (int i = start; i < finish; i++) {
char ch = token[i];
if (ch == '\\') {
ch = token[++i];
if (isdigit(ch) || ch == 'x') {
int base = 8;
if (ch == 'x') {
base = 16;
i++;
}
int result = 0;
int digit = 0;
while (i < finish) {
ch = token[i];
if (isdigit(ch)) {
digit = ch - '0';
} else if (isalpha(ch)) {
digit = toupper(ch) - 'A' + 10;
} else {
digit = base;
}
if (digit >= base) break;
result = base * result + digit;
i++;
}
ch = char(result);
i--;
} else {
switch (ch) {
case 'a': ch = '\a'; break;
case 'b': ch = '\b'; break;
case 'f': ch = '\f'; break;
case 'n': ch = '\n'; break;
case 'r': ch = '\r'; break;
case 't': ch = '\t'; break;
case 'v': ch = '\v'; break;
case '"': ch = '"'; break;
case '\'': ch = '\''; break;
case '\\': ch = '\\'; break;
}
}
}
str += ch;
}
return str;
}
int TokenScanner::getChar() {
return isp->get();
}
void TokenScanner::ungetChar(int) {
isp->unget();
}
/* Private methods */
void TokenScanner::initScanner() {
ignoreWhitespaceFlag = false;
ignoreCommentsFlag = false;
scanNumbersFlag = false;
scanStringsFlag = false;
operators = NULL;
}
/*
* Implementation notes: skipSpaces
* --------------------------------
* Advances the position of the scanner until the current character is
* not a whitespace character.
*/
void TokenScanner::skipSpaces() {
while (true) {
int ch = isp->get();
if (ch == EOF) return;
if (!isspace(ch)) {
isp->unget();
return;
}
}
}
/*
* Implementation notes: scanWord
* ------------------------------
* Reads characters until the scanner reaches the end of a sequence
* of word characters.
*/
string TokenScanner::scanWord() {
string token = "";
while (true) {
int ch = isp->get();
if (ch == EOF) break;
if (!isWordCharacter(ch)) {
isp->unget();
break;
}
token += char(ch);
}
return token;
}
/*
* Implementation notes: scanNumber
* --------------------------------
* Reads characters until the scanner reaches the end of a legal number.
* The function operates by simulating what computer scientists
* call a finite-state machine. The program uses the variable
* <code>state</code> to record the history of the process and
* determine what characters would be legal at this point in time.
*/
string TokenScanner::scanNumber() {
string token = "";
NumberScannerState state = INITIAL_STATE;
while (state != FINAL_STATE) {
int ch = isp->get();
switch (state) {
case INITIAL_STATE:
if (!isdigit(ch)) {
error("Internal error: illegal call to scanNumber");
}
state = BEFORE_DECIMAL_POINT;
break;
case BEFORE_DECIMAL_POINT:
if (ch == '.') {
state = AFTER_DECIMAL_POINT;
} else if (ch == 'E' || ch == 'e') {
state = STARTING_EXPONENT;
} else if (!isdigit(ch)) {
if (ch != EOF) isp->unget();
state = FINAL_STATE;
}
break;
case AFTER_DECIMAL_POINT:
if (ch == 'E' || ch == 'e') {
state = STARTING_EXPONENT;
} else if (!isdigit(ch)) {
if (ch != EOF) isp->unget();
state = FINAL_STATE;
}
break;
case STARTING_EXPONENT:
if (ch == '+' || ch == '-') {
state = FOUND_EXPONENT_SIGN;
} else if (isdigit(ch)) {
state = SCANNING_EXPONENT;
} else {
if (ch != EOF) isp->unget();
isp->unget();
state = FINAL_STATE;
}
break;
case FOUND_EXPONENT_SIGN:
if (isdigit(ch)) {
state = SCANNING_EXPONENT;
} else {
if (ch != EOF) isp->unget();
isp->unget();
isp->unget();
state = FINAL_STATE;
}
break;
case SCANNING_EXPONENT:
if (!isdigit(ch)) {
if (ch != EOF) isp->unget();
state = FINAL_STATE;
}
break;
default:
state = FINAL_STATE;
break;
}
if (state != FINAL_STATE) {
token += char(ch);
}
}
return token;
}
/*
* Implementation notes: scanString
* --------------------------------
* Reads and returns a quoted string from the scanner, continuing until
* it scans the matching delimiter. The scanner generates an error if
* there is no closing quotation mark before the end of the input.
*/
string TokenScanner::scanString() {
string token = "";
char delim = isp->get();
token += delim;
bool escape = false;
while (true) {
int ch = isp->get();
if (ch == EOF) error("TokenScanner found unterminated string");
if (ch == delim && !escape) break;
escape = (ch == '\\') && !escape;
token += ch;
}
return token + delim;
}
/*
* Implementation notes: isOperator, isOperatorPrefix
* --------------------------------------------------
* These methods search the list of operators and return true if the
* specified operator is either in the list or a prefix of an operator
* in the list, respectively. This code could be made considerably more
* efficient by implementing operators as a trie.
*/
bool TokenScanner::isOperator(string op) {
for (StringCell *cp = operators; cp != NULL; cp = cp->link) {
if (op == cp->str) return true;
}
return false;
}
bool TokenScanner::isOperatorPrefix(string op) {
for (StringCell *cp = operators; cp != NULL; cp = cp->link) {
if (startsWith(cp->str, op)) return true;
}
return false;
}
|