-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexal.cpp
248 lines (208 loc) · 6.17 KB
/
lexal.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
#include "lexal.h"
#include "convert.h"
Lexeme::Lexeme(Type t, const std::wstring &cont, int line, int pos)
{
type = t;
content = cont;
this->line = line;
this->pos = pos;
}
std::wstring Lexeme::getPosStr() const
{
return Lexal::posToStr(line, pos);
}
Lexal::Lexal(UtfStreamReader &rdr): reader(rdr)
{
line = 1;
pos = 0;
}
static bool isIdentStart(wchar_t ch) {
return ((L'a' <= ch) && (L'z' >= ch)) || ((L'A' <= ch) && (L'Z' >= ch))
|| (L'_' == ch);
}
static bool isIdentCont(wchar_t ch) {
return ((L'a' <= ch) && (L'z' >= ch)) || ((L'A' <= ch) && (L'Z' >= ch))
|| (L'_' == ch) || (L'.' == ch) || ((L'0' <= ch) && (L'9' >= ch));
}
static bool isWhiteSpace(wchar_t ch) {
return (L' ' == ch) || (L'\t' == ch) || (L'\n' == ch) || (L'\r' == ch);
}
static bool isDigit(wchar_t ch) {
return (L'0' <= ch) && (L'9' >= ch);
}
static bool isSymbol(wchar_t ch) {
return (L'{' == ch) || (L'}' == ch) || (L',' == ch) || (L'=' == ch)
|| (L';' == ch);
}
static bool isQuote(wchar_t ch) {
return (L'\'' == ch) || (L'"' == ch);
}
std::wstring Lexal::posToStr(int line, int pos)
{
return L"(" + toString(line) + L":" + toString(pos) + L")";
}
Lexeme Lexal::getNext()
{
skipSpaces();
if (reader.isEof())
return Lexeme(Lexeme::Eof, L"", line, pos);
int startLine = line;
int startPos = pos;
wchar_t ch = reader.getNextChar();
pos++;
if (isIdentStart(ch))
return readIdent(startLine, startPos, ch);
else if (isDigit(ch))
return readNumber(startLine, startPos, ch);
else if (isQuote(ch))
return readString(startLine, startPos, ch);
else if (isSymbol(ch))
return Lexeme(Lexeme::Symbol, toString(ch), startLine, startPos);
throw Exception(L"Invalid character at "+ posToStr(startLine, startPos));
}
Lexeme Lexal::readString(int startLine, int startPos, wchar_t quote)
{
std::wstring str;
bool closed = false;
while (! reader.isEof()) {
wchar_t ch = reader.getNextChar();
pos++;
if ('\n' == ch) {
line++;
pos = 0;
} else if ('\\' == ch) {
if (! reader.isEof()) {
wchar_t nextCh = reader.getNextChar();
if (isWhiteSpace(nextCh))
throw Exception(L"Invalid escape sequence at " +
posToStr(line, pos));
pos++;
switch (nextCh) {
case L'\t': str += L'\t'; break;
case L'\n': str += L'\n'; break;
case L'\r': str += L'\r'; break;
default:
str += nextCh;
}
}
} else if (quote == ch) {
closed = true;
break;
} else
str += ch;
}
if (! closed)
throw Exception(L"String at " + posToStr(startLine, startPos)
+ L" doesn't closed");
return Lexeme(Lexeme::String, str, startLine, startPos);
}
Lexeme Lexal::readNumber(int startLine, int startPos, wchar_t first)
{
std::wstring number;
number += first;
Lexeme::Type type = Lexeme::Integer;
while (! reader.isEof()) {
wchar_t ch = reader.getNextChar();
pos++;
if (isDigit(ch))
number += ch;
else if (L'.' == ch) {
if (Lexeme::Integer == type) {
type = Lexeme::Float;
number += ch;
} else
throw Exception(L"To many dots in number at " +
posToStr(line, pos));
} else if ((! isSymbol(ch)) && (! isWhiteSpace(ch)))
throw Exception(L"invalid number at " + posToStr(line, pos));
else {
pos--;
reader.ungetChar(ch);
break;
}
}
if (L'.' == number[number.length() - 1])
throw Exception(L"Missing digit after dot at " + posToStr(line, pos));
return Lexeme(type, number, startLine, startPos);
}
Lexeme Lexal::readIdent(int startLine, int startPos, wchar_t first)
{
std::wstring ident;
ident += first;
while (! reader.isEof()) {
wchar_t ch = reader.getNextChar();
if (! isIdentCont(ch)) {
reader.ungetChar(ch);
break;
}
ident += ch;
pos++;
}
return Lexeme(Lexeme::Ident, ident, startLine, startPos);
}
void Lexal::skipToLineEnd()
{
while (! reader.isEof()) {
wchar_t ch = reader.getNextChar();
pos++;
if ('\n' == ch) {
pos = 0;
line++;
return;
}
}
}
void Lexal::skipMultilineComment(int startLine, int startPos)
{
while (! reader.isEof()) {
wchar_t ch = reader.getNextChar();
pos++;
if ('\n' == ch) {
pos = 0;
line++;
} else if (('*' == ch) && (! reader.isEof())) {
wchar_t nextCh = reader.getNextChar();
if ('/' != nextCh)
reader.ungetChar(nextCh);
}
}
throw Exception(L"Remark started at " + posToStr(startLine, startPos)
+ L" is not closed");
}
void Lexal::skipSpaces()
{
while (! reader.isEof()) {
wchar_t ch = reader.getNextChar();
pos++;
if (! isWhiteSpace(ch)) {
if ('#' == ch)
skipToLineEnd();
else {
bool finish = false;
if (('/' == ch) && (! reader.isEof())) {
wchar_t nextCh = reader.getNextChar();
pos++;
if ('/' == nextCh)
skipToLineEnd();
else if ('*' == nextCh)
skipMultilineComment(line, pos);
else {
pos--;
reader.ungetChar(nextCh);
finish = true;
}
} else
finish = true;
if (finish) {
pos--;
reader.ungetChar(ch);
return;
}
}
} else
if ('\n' == ch) {
pos = 0;
line++;
}
}
}