-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexicalParser.cpp
More file actions
executable file
·226 lines (180 loc) · 7.64 KB
/
Copy pathLexicalParser.cpp
File metadata and controls
executable file
·226 lines (180 loc) · 7.64 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
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
#include "LexicalParser.hpp"
#include <optional>
#include <algorithm>
using WT = JSONWord::Type;
// Auxiliary function to check constants, save current word, and RAZ word current word data
static void save_current_word(std::vector<JSONWord> & result, std::optional<WT> & current_word_type, std::string & current_word)
{
// Checking constant values in case they are incomplete
if ( (current_word_type == WT::TRUE && current_word != "true")
|| (current_word_type == WT::FALSE && current_word != "false")
|| (current_word_type == WT::NULL_VALUE && current_word != "null") )
throw LexicalParser::lexical_exception();
if (current_word_type)
{
result.push_back(JSONWord(*current_word_type, current_word));
current_word_type = std::nullopt;
}
current_word = "";
}
// Parsing auxiliary functions
static void parse_simple_symbol(std::vector<JSONWord> & result, std::optional<WT> & current_word_type, std::string & current_word, WT actual_type);
static void parse_string(std::vector<JSONWord> & result, std::optional<WT> & current_word_type, std::string & current_word);
static void parse_whitespace(std::vector<JSONWord> & result, char c, std::optional<WT> & current_word_type, std::string & current_word);
static void parse_other(std::vector<JSONWord> & result, char c, std::optional<WT> & current_word_type, std::string & current_word);
static void parse_new_number_or_constant(std::vector<JSONWord> & result, char c, std::optional<WT> & current_word_type, std::string & current_word);
// Mais function of the parser
std::vector<WT> LexicalParser::computeLexicalAnalysis(std::string json_input)
{
std::vector<JSONWord> aux_result;
std::optional<WT> current_word_type;
std::string current_word;
for (char c : json_input)
{
if (current_word_type == WT::STRING && c != '"')
{
current_word += c;
}
else
{
switch (c)
{
case '{':
parse_simple_symbol(aux_result, current_word_type, current_word, WT::OBJECT_BEGIN);
break;
case '}':
parse_simple_symbol(aux_result, current_word_type, current_word, WT::OBJECT_END);
break;
case '[':
parse_simple_symbol(aux_result, current_word_type, current_word, WT::ARRAY_BEGIN);
break;
case ']':
parse_simple_symbol(aux_result, current_word_type, current_word, WT::ARRAY_END);
break;
case ',':
parse_simple_symbol(aux_result, current_word_type, current_word, WT::COMMA);
break;
case ':':
parse_simple_symbol(aux_result, current_word_type, current_word, WT::COLON);
break;
case '"':
parse_string(aux_result, current_word_type, current_word);
break;
// WHITESPACE
case ' ':
case '\t':
case '\n':
case '\r':
parse_whitespace(aux_result, c, current_word_type, current_word);
break;
// Other characters (for NUMBERs and constants)
default:
parse_other(aux_result, c, current_word_type, current_word);
}
}
}
// TODO Reparsing NUMBERs in order to detect inconsistencies
std::vector<WT> result(aux_result.size());
std::transform(begin(aux_result), end(aux_result), begin(result), [](const JSONWord & word){ return word.type; });
return result;
}
// Parses a simple symbol that contains no data
void parse_simple_symbol(std::vector<JSONWord> & result, std::optional<WT> & current_word_type, std::string & current_word, WT actual_type)
{
save_current_word(result, current_word_type, current_word);
result.push_back(JSONWord(actual_type));
}
// Parses the beginning of the end of a STRING
void parse_string(std::vector<JSONWord> & result, std::optional<WT> & current_word_type, std::string & current_word)
{
if (current_word_type == WT::STRING)
{
save_current_word(result, current_word_type, current_word);
}
else
{
save_current_word(result, current_word_type, current_word);
current_word_type = WT::STRING;
}
}
// Parse a set of whitespaces
void parse_whitespace(std::vector<JSONWord> & result, char c, std::optional<WT> & current_word_type, std::string & current_word)
{
if (current_word_type != WT::WHITESPACE)
{
save_current_word(result, current_word_type, current_word);
current_word_type = WT::WHITESPACE;
}
current_word += c;
}
// Parses any other value (i.e. numbers or constant such as true, false and null)
void parse_other(std::vector<JSONWord> & result, char c, std::optional<WT> & current_word_type, std::string & current_word)
{
if (current_word_type)
{
switch (*current_word_type)
{
// Middle of a NUMBER
case WT::NUMBER:
if ( (c < '0' || c > '9') && c != '.' && c != 'e' && c != 'E' && c != '-' && c != '+' )
throw LexicalParser::lexical_exception();
break;
// Middle of TRUE
case WT::TRUE:
if ( !(c == 'r' && current_word == "t")
&& !(c == 'u' && current_word == "tr")
&& !(c == 'e' && current_word == "tru") )
throw LexicalParser::lexical_exception();
current_word += c;
if (current_word == "true")
save_current_word(result, current_word_type, current_word);
break;
// Middle of FALSE
case WT::FALSE:
if ( !(c == 'a' && current_word == "f")
&& !(c == 'l' && current_word == "fa")
&& !(c == 's' && current_word == "fal")
&& !(c == 'e' && current_word == "fals") )
throw LexicalParser::lexical_exception();
current_word += c;
if (current_word == "false")
save_current_word(result, current_word_type, current_word);
break;
// Middle of NULL
case WT::NULL_VALUE:
if ( !(c == 'u' && current_word == "n") && !(c == 'l' && ( current_word == "nu" || current_word == "nul") ) )
throw LexicalParser::lexical_exception();
current_word += c;
if (current_word == "null")
save_current_word(result, current_word_type, current_word);
break;
default:
// Forcing new word
save_current_word(result, current_word_type, current_word);
current_word += c;
parse_new_number_or_constant(result, c, current_word_type, current_word);
}
}
else
{
parse_new_number_or_constant(result, c, current_word_type, current_word);
}
}
// Parse a new number or constant
void parse_new_number_or_constant(std::vector<JSONWord> & result, char c, std::optional<WT> & current_word_type, std::string & current_word)
{
// Beginning of a number
if ( (c >= '0' && c <= '9') || c == '-' )
current_word_type = WT::NUMBER;
// Beginning of a true
else if (c == 't')
current_word_type = WT::TRUE;
// Beginning of a false
else if (c == 'f')
current_word_type = WT::FALSE;
// Beginning of a null
else if (c == 'n')
current_word_type = WT::NULL_VALUE;
else
throw LexicalParser::lexical_exception();
}