-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.cpp
379 lines (303 loc) · 7.83 KB
/
tokenizer.cpp
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
// #define DEBUG
#include <utility>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <map>
#include <memory>
#include "tokenizer.hpp"
// PROCESS STREAM
charGroup processStream::getCurrent() const {
if(eof_) return charGroup::eof;
switch(curr_) {
case ';':
return charGroup::semicolon;
case '\n':
case '\r':
return charGroup::newline;
case ' ':
case '\t':
case '\v':
case '\f':
return charGroup::whitespace;
case '/':
return charGroup::slash;
case '\\':
return charGroup::backslash;
case '"':
return charGroup::quote;
case ',':
return charGroup::comma;
case '(':
return charGroup::open;
case ')':
return charGroup::close;
case '#':
return charGroup::hash;
case '*':
return charGroup::star;
}
if(('a'<=curr_ && curr_<='z') ||
('A'<=curr_ && curr_<='Z') ||
('0'<=curr_ && curr_<='9') ||
('_' == curr_))
return charGroup::alphanum;
for(char c: ops_) {
if(c==curr_)
return charGroup::op;
}
#ifdef DEBUG
std::cout<<"INVALID CHARACTER "<<curr_<<" "<<(int)curr_<<std::endl;
#endif
return charGroup::invalid;
};
std::string processStream::flush() {
return std::move(buffer_);
};
void processStream::move() {
// in this order, so curr_ is never in the buffer
buffer_.push_back(curr_);
in_>>curr_;
// this is true when there are no more chars
// -> previous op returns nonsense
if(in_.eof())
eof_=true;
#ifdef DEBUG
std::cout<<"DEBUG: PROCESS STREAM: EOF? "<<in_.eof()<<" "<<curr_<<std::endl;
#endif
};
// needed for comments
void processStream::moveBack() {
if(buffer_.size()!=0)
buffer_.pop_back();
if(in_.tellg()>0)
in_.unget();
};
// TOKENIZER
token parseDummy(processStream& in) {
return token::invalidCharacter;
}
template<charGroup group, token tok>
token parseOneChar(processStream& in) {
if(in.getCurrent()==group) {
in.move();
return tok;
}
return token::invalidCharacter;
}
token parseSymbol(processStream& in) {
charGroup group=in.getCurrent();
// star alone is a symbol
if(group==charGroup::star) {
in.move();
return token::symbol;
}
while(group==charGroup::alphanum ||
group==charGroup::quote ||
group==charGroup::op) {
in.move();
group=in.getCurrent();
}
return token::symbol;
}
// leaves current at the last character of the comment (\n or slash)
token parseComment(processStream& in) {
charGroup group=in.getCurrent();
bool inlineComment=false;
if(group==charGroup::slash) {
in.move();
group=in.getCurrent();
// multiline comment
if(group==charGroup::star) {
while(true) {
in.move();
if(in.getCurrent()==charGroup::star) {
in.move();
if(in.getCurrent()==charGroup::slash)
return token::skip;
// because of **/
in.moveBack();
}
}
}
else if(group==charGroup::slash)
inlineComment=true;
else {
in.moveBack();
return token::invalidCharacter;
}
}
if(group==charGroup::hash || inlineComment) {
while(group!=charGroup::newline &&
group!=charGroup::eof) {
in.move();
group=in.getCurrent();
}
return token::skip;
}
return token::invalidCharacter;
}
// Whitespace (' ', \f, \t, \v), [Newline (\r, \n)] + comments
// satisfies empty string
template<bool newLines>
token parseWC(processStream& in) {
charGroup group=in.getCurrent();
while(group==charGroup::whitespace ||
(newLines && group==charGroup::newline) ||
parseComment(in)==token::skip) {
in.move();
group=in.getCurrent();
}
return token::skip;
}
token parseTerminator(processStream& in) {
charGroup group=in.getCurrent();
if(group==charGroup::semicolon ||
group==charGroup::newline) {
in.move();
return token::terminator;
}
return token::invalidCharacter;
}
// checks for ) , or EOF
token parseBlockEnd(processStream& in) {
charGroup group=in.getCurrent();
if(group==charGroup::eof ||
group==charGroup::comma ||
group==charGroup::close)
// don't move here - comma or close will be used by the upper layer
return token::endOfBlock;
return token::invalidCharacter;
}
// this cannot be run in constructor to avoid indefinite recursion
void tokenizer::prepare() {
if(ready_) return;
ready_=true;
// prepare boxes
boxes_.insert({"Dummy", std::make_unique<tokenBox>(in_, parseDummy)});
boxes_.insert({"WC1", std::make_unique<tokenBox>(in_, parseWC<false>)});
boxes_.insert({"BlockEnd",
std::make_unique<tokenBox>(in_, parseBlockEnd)});
boxes_.insert({"Terminator",
std::make_unique<tokenBox>(in_, parseTerminator)});
boxes_.insert({"Symbol",
std::make_unique<tokenBox>(in_, parseSymbol)});
boxes_.insert({"WC2",
std::make_unique<tokenBox>(in_, parseWC<false>)});
boxes_.insert({"Open",
std::make_unique<tokenBox>(in_,
parseOneChar<charGroup::open, token::openArguments>)});
boxes_.insert({"NWC1",
std::make_unique<tokenBox>(in_, parseWC<true>)});
boxes_.insert({"Argument", std::make_unique<argumentBox>(in_)});
boxes_.insert({"NWC2",
std::make_unique<tokenBox>(in_, parseWC<true>)});
boxes_.insert({"Comma",
std::make_unique<tokenBox>(in_,
parseOneChar<charGroup::comma, token::nextArgument>)});
boxes_.insert({"Close",
std::make_unique<tokenBox>(in_,
parseOneChar<charGroup::close, token::closeArguments>)});
// transition
std::vector<std::pair<std::string, std::string> > transitions={
{"Dummy", "WC1"},
{"WC1", "Terminator"},
{"Terminator", "WC1"},
{"WC1", "BlockEnd"},
// lower priority than Terminator and BlokEnd
{"WC1", "Symbol"},
{"Symbol", "WC2"},
{"WC2", "Open"},
{"Open", "NWC1"},
{"NWC1", "Argument"},
{"Argument", "NWC2"},
{"NWC2", "Close"},
{"Close", "WC1"},
{"NWC2", "Comma"},
{"Comma", "NWC1"},
{"WC2", "WC1"}}; // this has lower priority
for(auto && tr: transitions) {
box* to=boxes_.find(tr.second)
->second.get();
boxes_.find(tr.first)
->second
->addNextBox(to);
}
auto dummyBox=boxes_.find("Dummy")->second.get();
for(auto && box: boxes_) {
box.second->addNextBox(dummyBox);
}
curr_=dummyBox;
}
token tokenizer::nextToken() {
token currToken;
// automatically skips tokens skip
do {
in_.flush();
box* newCurr=curr_->getNextBox();
currToken=newCurr->parseToken();
while(currToken==token::invalidCharacter) {
newCurr=curr_->getNextBox();
currToken=newCurr->parseToken();
}
curr_=newCurr;
} while(currToken==token::skip);
// std::cout<<"TOKEN "<<(int)currToken<<std::endl;
return currToken;
}
box* box::getNextBox() {
if(indexNextBox_>nextBoxes_.size()-1)
indexNextBox_=nextBoxes_.size()-1;
return nextBoxes_[indexNextBox_++];
};
box* argumentBox::getNextBox() {
if(endOfArgument_) {
argument_.reset(); // prepare for the next argument
return box::getNextBox();
}
return this;
};
token argumentBox::parseToken() {
if(!argument_.ready())
argument_.prepare();
indexNextBox_=0; // offer nextBoxes from start again
endOfArgument_=false;
token nextToken=argument_.nextToken();
if(nextToken==token::endOfBlock) {
endOfArgument_=true;
// EndOfArgument is for this layer to know it should continue
return token::skip;
}
return nextToken;
};
// TOKENIZERBUILDER CLASS DEFINITION
tokenizerBuilder::tokenizerBuilder(tokenlist& ts) {
addTokens(ts);
it=tokens.begin();
}
tokenizerBuilder::tokenizerBuilder() {
it=tokens.begin();
}
token tokenizerBuilder::nextToken() {
currString=it->second;
return (it++)->first;
}
std::string tokenizerBuilder::flush() {
return currString;
}
bool tokenizerBuilder::eof() {
return it==tokens.end();
}
void tokenizerBuilder::addToken(token tok, const std::string& str) {
tokens.emplace_back(tok, str);
}
void tokenizerBuilder::addToken(token tok, std::string&& str) {
tokens.emplace_back(tok, std::move(str));
}
void tokenizerBuilder::addTokens(const tokenlist& q) {
for(auto&& tok:q) {
tokens.push_back(tok);
}
}