-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSchLexicalParser.cpp
More file actions
186 lines (166 loc) · 3.07 KB
/
SchLexicalParser.cpp
File metadata and controls
186 lines (166 loc) · 3.07 KB
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
/*
* SchLexicalParser.cpp
*
* Created on: 17.04.2013
* Author: urandon
*/
#include "SchLexicalParser.h"
#include <ctype.h>
#include <stdio.h>
SchLexicalParser::SchLexicalParser():state(H), line(1), last_line(1), predefined_length(0) { }
SchLexicalParser::SchLexicalParser(const char * const * wordlist, int length):
state(H), line(1), predefined_length(length)
{
for(int i = 0; i < length; ++i){
map.push(wordlist[i]);
}
}
SchLexicalParser::~SchLexicalParser() { }
lexem * SchLexicalParser::ret(enum state_t _state){
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, last_line);
if(lex->type == NAME && lex->id < predefined_length){
lex->type = SPECIAL;
}
state = _state;
buffer.flush();
return lex;
}
bool SchLexicalParser::isEmpty() {
return buffer.size == 0;
}
const char * SchLexicalParser::getString(int id) {
return map.getString(id);
}
lexem * SchLexicalParser::push(int ch){
lexem * lex;
last_line = line;
if(ch == '\n'){
++line;
}
if(ch == EOF){
if(state != H){
throw EOFinsideLexemError(line);
} else {
return NULL;
}
}
switch(state){
case H :
if(isdigit(ch)){
state = I;
buffer.push(ch);
} else
if(isspace(ch)){
// do nothing
} else
switch(ch){
case '"' :
state = S;
// buffer.push(ch);
break;
case '.' :
state = F;
buffer.push(ch);
break;
case '(' : case ')' :
current_type = (ch == '(') ? LBRACE : RBRACE;
state = SEP;
buffer.push(ch);
break;
default :
state = N;
buffer.push(ch);
break;
}
break;
case I :
current_type = INTEGER;
if(!isdigit(ch)){
if(ch == '.'){
buffer.push(ch);
state = F;
} else
if(isspace(ch)){
return ret(H);
} else
if(ch == ')' || ch == '('){
lex = ret(SEP);
buffer.push(ch);
current_type = (ch == '(') ? LBRACE : RBRACE;
return lex;
} else {
buffer.push(ch);
state = N;
}
} else {
buffer.push(ch);
}
break;
case F :
current_type = FLOAT;
if(!isdigit(ch)){
if(isspace(ch)){
return ret(H);
} else
if(ch == ')' || ch == '('){
lex = ret(SEP);
buffer.push(ch);
current_type = (ch == '(') ? LBRACE : RBRACE;
return lex;
} else {
buffer.push(ch);
state = N;
}
} else {
buffer.push(ch);
}
break;
case N :
current_type = NAME;
if(isspace(ch)){
return ret(H);
} else
if(ch == ')' || ch == '('){
lex = ret(SEP);
buffer.push(ch);
current_type = (ch == '(') ? LBRACE : RBRACE;
return lex;
} else {
buffer.push(ch);
}
break;
case S :
current_type = STRING;
if(str_slash){
buffer.push(ch);
str_slash = false;
} else
if(ch == '"'){
return ret(H);
} else
if(ch == '\\'){
str_slash = true;
} else {
buffer.push(ch);
}
break;
case SEP :
if(ch == ')' || ch == '('){
lex = ret(SEP);
current_type = (ch == '(') ? LBRACE : RBRACE;
buffer.push(ch);
} else {
lex = ret(H);
if(ch == '\n'){
--line;
}
push(ch);
}
return lex;
break;
};
return NULL;
}