This repository has been archived by the owner on Feb 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.h
288 lines (282 loc) · 7.26 KB
/
scanner.h
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
#pragma once
enum struct Tok
{
none,
eof,
id,
colon,
semi,
alt,
lbrace,
rbrace,
lbrack,
rbrack,
lparen,
rparen,
literal,
epsilon
};
struct Bad_token_error
{
};
struct Scanner
{
FILE* fp_; // input source.
const char* filename_; // Input file name.
std::string line_; // current line
Tok la_; // look ahead
size_t la_pos_; // position of the look ahead token.
size_t pos_; // read pos in line
size_t lineno_; // Line number
std::string alfa_; // value of identifier for la_ == Tok::id
std::string literal_; // value of literal string
public:
Scanner(FILE* fp, const char* filename)
: fp_{ fp }
, filename_{ filename }
, la_{ Tok::none }
, la_pos_{ 0 }
, pos_{ 0 }
, lineno_{ 0 }
{
assert(fp != nullptr && !feof(fp));
get();
}
Tok token() const { return la_; }
const char* filename() const { return filename_; }
size_t lineno() const { return lineno_; }
std::string curr_token_str() const
{
using namespace std::string_literals;
switch (la_)
{
default:
assert(false && "unreached");
return "Tok::none";
case Tok::alt:
return "|";
case Tok::colon:
return ":";
case Tok::lbrace:
return "{";
case Tok::rbrace:
return "}";
case Tok::lbrack:
return "[";
case Tok::rbrack:
return "]";
case Tok::lparen:
return "(";
case Tok::rparen:
return ")";
case Tok::eof:
return "END-OF-FILE";
case Tok::epsilon:
return "EPSILON";
case Tok::id:
return "ID("s + alfa_ + ")"s;
case Tok::literal:
return "LITERAL("s + literal_ + ")"s;
case Tok::semi:
return ";";
}
}
static const char* string_for_token(Tok t)
{
switch (t)
{
default:
assert(false && "unreached");
return "Tok::none";
case Tok::alt:
return "|";
case Tok::colon:
return ":";
case Tok::lbrace:
return "{";
case Tok::rbrace:
return "}";
case Tok::lbrack:
return "[";
case Tok::rbrack:
return "]";
case Tok::lparen:
return "(";
case Tok::rparen:
return ")";
case Tok::eof:
return "end-of-file";
case Tok::epsilon:
return "EPSILON";
case Tok::id:
return "identifier";
case Tok::literal:
return "terminal-literal";
case Tok::semi:
return ";";
}
}
std::string identifier() const
{
assert(la_ == Tok::id);
return alfa_;
}
std::string literal() const
{
assert(la_ == Tok::literal);
return literal_;
}
void getline()
{
int c;
line_.clear();
for (;;)
{
c = fgetc(fp_);
if (c == EOF || feof(fp_))
break;
line_ += static_cast<char>(c);
if (c == '\n')
{
++lineno_;
break;
}
}
pos_ = 0;
}
void skip_white_space()
{
while (pos_ < line_.size() && isspace(line_[pos_]))
++pos_;
}
void show_line()
{
const unsigned spaces_per_tab = 8;
unsigned charpos = 0;
unsigned col = 0;
for (auto c : line_)
{
if (c == '\t')
{
unsigned n = spaces_per_tab - (col % spaces_per_tab);
col += (charpos < la_pos_ ? n : 0);
while (n-- > 0)
putchar(' ');
}
else
{
putchar(c);
col += (charpos < la_pos_ ? 1 : 0);
}
++charpos;
}
for (auto i = 0U; i < col; ++i)
putchar(' ');
putchar('^');
putchar('\n');
for (auto i = 0U; i < col; ++i)
putchar(' ');
putchar('|');
putchar('\n');
}
void bad_token()
{
show_line();
printf("%s(%zu): error: bad token\n", filename_, lineno_);
throw Bad_token_error{};
}
void scan_id()
{
alfa_.clear();
assert(isalpha(line_[pos_]));
alfa_ += line_[pos_++];
while (pos_ < line_.size() && (isalnum(line_[pos_]) || line_[pos_] == '-' || line_[pos_] == '_'))
alfa_ += line_[pos_++];
if (alfa_ == "EPSILON")
la_ = Tok::epsilon;
else
la_ = Tok::id;
}
void scan_literal()
{
assert(line_[pos_] == '"');
literal_.clear();
literal_ += line_[pos_++];
for (;;)
{
if (pos_ >= line_.size())
{
bad_token();
}
literal_ += line_[pos_++];
if (line_[pos_ - 1] == '"')
break;
}
la_ = Tok::literal;
}
void get()
{
if (la_ == Tok::eof)
return;
skip_white_space();
la_pos_ = pos_;
if (pos_ >= line_.size())
{
getline();
if (line_.size() == 0)
la_ = Tok::eof;
return get();
}
char c = line_[pos_];
switch (c)
{
case '|':
la_ = Tok::alt;
++pos_;
break;
case ':':
la_ = Tok::colon;
++pos_;
break;
case ';':
la_ = Tok::semi;
++pos_;
break;
case '{':
la_ = Tok::lbrace;
++pos_;
break;
case '}':
la_ = Tok::rbrace;
++pos_;
break;
case '[':
la_ = Tok::lbrack;
++pos_;
break;
case ']':
la_ = Tok::rbrack;
++pos_;
break;
case '(':
la_ = Tok::lparen;
++pos_;
break;
case ')':
la_ = Tok::rparen;
++pos_;
break;
case '"':
return scan_literal();
default:
if (isalpha(c))
{
return scan_id();
}
else
{
bad_token();
}
}
}
};