-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImpLexicalParser.cpp
208 lines (186 loc) · 3.5 KB
/
ImpLexicalParser.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
/*
* ImpLexicalParser.cpp
*
* Created on: 17.04.2013
* Author: urandon
*/
#include "ImpLexicalParser.h"
#include "ImpSyntaxParser.h"
//#include <cstdlib>
#include <ctype.h>
#include <cstdio>
//#include <string.h>
ImpLexicalParser::ImpLexicalParser():state(H),line(1) {
for(int i = 0; i < ImpSyntaxParser::PREDEFINED_ATOMS; ++i){
map.push(ImpSyntaxParser::atom_essence[i]);
}
}
ImpLexicalParser::~ImpLexicalParser() {
}
bool ImpLexicalParser::isseparator(char ch){
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%'||
ch == '&' || ch == '|' || ch == '!' ||
ch == '>' || ch == '<' || ch == '=' ||
ch == '[' || ch == ']' || ch == '(' || ch == ')' ||
ch == '{' || ch == '}' ||
ch == ',' || ch == ';' || ch == ':';
}
lexem * ImpLexicalParser::ret(enum state_t mstate){
lexem * lex;
buffer.push('\0');
//lex = new lexem(buffer.data, buffer.size, current_type, line);
lex = new lexem(map.push(buffer.data, buffer.size), current_type, line);
state = mstate;
buffer.flush();
return lex;
}
lexem * ImpLexicalParser::push(int ch){
lexem * lex;
if(ch == '\n'){
++line;
}
if(ch == EOF){
if(state != H){
throw EOFinsideLexemError(line);
} else {
return NULL;
}
}
switch(state){
case H :
if(isdigit(ch)){
state = N;
current_type = NUMERIC;
buffer.push(ch);
} else
if(isalpha(ch)){
state = K;
current_type = KEYWORD;
buffer.push(ch);
} else
if(isseparator(ch) && ch != ':'){
state = SEP;
current_type = SEPARATOR;
buffer.push(ch);
} else
if(isspace(ch)){
// do nothing
} else
switch(ch){
case '?' : case '@' : case '$' :
state = I;
if(ch == '?'){
current_type = FUNCTION;
} else
if(ch == '@'){
current_type = LABEL;
} else {
current_type = VARIABLE;
}
buffer.push(ch);
break;
case ':' :
state = A;
current_type = EQUALITY;
buffer.push(ch);
break;
case '"' :
state = S;
current_type = STRING;
str_slash = false;
buffer.push(ch);
break;
default:
throw BadSymbolError(line, ch);
};
break;
case N :
if(isdigit(ch)){
buffer.push(ch);
} else
if(isspace(ch)){
return ret(H);
} else
if(isseparator(ch)){
lex = ret(SEP);
buffer.push(ch);
return lex;
} else {
throw BadSymbolError(line, ch);
};
break;
case I :
if(isdigit(ch) || isalpha(ch) || ch == '_'){
buffer.push(ch);
} else
if(isspace(ch)){
return ret(H);
} else
if(isseparator(ch)){
lex = ret(SEP);
buffer.push(ch);
return lex;
} /* else
if(ch == ':' && current_type == LABEL){
buffer.push(ch);
current_type = LABELREF;
} */
else{
throw BadSymbolError(line, ch);
};
break;
case K :
if(isalpha(ch)){
buffer.push(ch);
} else
if(isspace(ch)){
return ret(H);
} else
if(isseparator(ch)){
lex = ret(SEP);
buffer.push(ch);
return lex;
} else {
throw BadSymbolError(line, ch);
};
break;
case A :
if(ch == '='){
buffer.push(ch);
return ret(H);
} else {
throw BadSymbolError(line, ch);
}
break;
case S :
buffer.push(ch);
if(str_slash){
str_slash = false;
} else
if(ch == '"'){
return ret(H);
} else
if(ch == '\\'){
str_slash = true;
}
break;
case SEP :
current_type = SEPARATOR;
if(isseparator(ch)){
lex = ret(SEP);
buffer.push(ch);
} else {
lex = ret(H);
push(ch);
}
return lex;
break;
};
return NULL;
}
bool ImpLexicalParser::isEmpty() {
return buffer.size == 0;
}
const char * ImpLexicalParser::getString(int id) {
return map.getString(id);
}