json_scanner.yy
8.3 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
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
/* This file is part of QJson
*
* Copyright (C) 2013 Silvio Moioli <silvio@moioli.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110yy::json_parser::token::INVALID301, USA.
*/
/* Flex output settings */
%option 8bit c++ full warn
%option noyywrap nounistd
%option noinput nounput noyy_push_state noyy_pop_state noyy_top_state noyy_scan_buffer noyy_scan_bytes noyy_scan_string noyyget_extra noyyset_extra noyyget_leng noyyget_text noyyget_lineno noyyset_lineno noyyget_in noyyset_in noyyget_out noyyset_out noyyget_lval noyyset_lval noyyget_lloc noyyset_lloc noyyget_debug noyyset_debug
%option yyclass="JSonScanner"
%option outfile="json_scanner.cc"
%{
#include "json_scanner.h"
#include "json_parser.hh"
#if defined(_WIN32) && !defined(__MINGW32__)
#define strtoll _strtoi64
#define strtoull _strtoui64
#endif
#define YY_USER_INIT if(m_allowSpecialNumbers) { \
BEGIN(ALLOW_SPECIAL_NUMBERS); \
}
%}
/* Exclusive subscanners for strings and escaped hex sequences */
%x QUOTMARK_OPEN HEX_OPEN
/* Extra-JSON rules active iff m_allowSpecialNumbers is true */
%s ALLOW_SPECIAL_NUMBERS
%%
/* Whitespace */
[\v\f\t ]+ {
m_yylloc->columns(yyleng);
}
[\r\n]+ {
m_yylloc->lines(yyleng);
}
/* Special values */
true {
m_yylloc->columns(yyleng);
*m_yylval = QVariant(true);
return yy::json_parser::token::TRUE_VAL;
}
false {
m_yylloc->columns(yyleng);
*m_yylval = QVariant(false);
return yy::json_parser::token::FALSE_VAL;
}
null {
m_yylloc->columns(yyleng);
*m_yylval = QVariant();
return yy::json_parser::token::NULL_VAL;
}
/* Numbers */
[0-9] |
[1-9][0-9]+ {
m_yylloc->columns(yyleng);
unsigned long long val = strtoull(yytext, NULL, 10);
*m_yylval = QVariant(val);
if (val == ULLONG_MAX && errno == ERANGE) {
qCritical() << "Number is out of range: " << yytext;
return yy::json_parser::token::INVALID;
}
return yy::json_parser::token::NUMBER;
}
-[0-9] |
-[1-9][0-9]+ {
m_yylloc->columns(yyleng);
long long val = strtoll(yytext, NULL, 10);
*m_yylval = QVariant(val);
if ((val == LLONG_MAX || val == LLONG_MIN) && errno == ERANGE) {
qCritical() << "Number is out of range: " << yytext;
return yy::json_parser::token::INVALID;
}
return yy::json_parser::token::NUMBER;
}
-?(([0-9])|([1-9][0-9]+))(\.[0-9]+)?([Ee][+\-]?[0-9]+)? {
m_yylloc->columns(yyleng);
bool ok;
*m_yylval = QVariant(m_C_locale.toDouble(QLatin1String(yytext),&ok));
if (!ok) {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
// See QTBUG-71256
*m_yylval = QVariant(0.);
#endif
qCritical() << "Number is out of range: " << yytext;
return yy::json_parser::token::INVALID;
}
return yy::json_parser::token::NUMBER;
}
/* Strings */
\" {
m_yylloc->columns(yyleng);
BEGIN(QUOTMARK_OPEN);
}
<QUOTMARK_OPEN>{
\\\" {
m_currentString.append(QLatin1String("\""));
}
\\\\ {
m_currentString.append(QLatin1String("\\"));
}
\\\/ {
m_currentString.append(QLatin1String("/"));
}
\\b {
m_currentString.append(QLatin1String("\b"));
}
\\f {
m_currentString.append(QLatin1String("\f"));
}
\\n {
m_currentString.append(QLatin1String("\n"));
}
\\r {
m_currentString.append(QLatin1String("\r"));
}
\\t {
m_currentString.append(QLatin1String("\t"));
}
\\u {
BEGIN(HEX_OPEN);
}
[^\"\\]+ {
m_currentString.append(QString::fromUtf8(yytext));
}
\\ {
// ignore
}
\" {
m_yylloc->columns(yyleng);
*m_yylval = QVariant(m_currentString);
m_currentString.clear();
BEGIN(INITIAL);
return yy::json_parser::token::STRING;
}
<<EOF>> {
qCritical() << "Unterminated string";
m_yylloc->columns(yyleng);
return yy::json_parser::token::INVALID;
}
}
<HEX_OPEN>{
[0-9A-Fa-f]{4} {
QString hexDigits = QString::fromUtf8(yytext, yyleng);
bool ok;
ushort hexDigit1 = hexDigits.left(2).toShort(&ok, 16);
ushort hexDigit2 = hexDigits.right(2).toShort(&ok, 16);
m_currentString.append(QChar(hexDigit2, hexDigit1));
BEGIN(QUOTMARK_OPEN);
}
.|\n {
qCritical() << "Invalid hex string";
m_yylloc->columns(yyleng);
*m_yylval = QVariant(QLatin1String(""));
BEGIN(QUOTMARK_OPEN);
return yy::json_parser::token::INVALID;
}
}
/* "Compound type" related tokens */
: {
m_yylloc->columns(yyleng);
return yy::json_parser::token::COLON;
}
, {
m_yylloc->columns(yyleng);
return yy::json_parser::token::COMMA;
}
\[ {
m_yylloc->columns(yyleng);
return yy::json_parser::token::SQUARE_BRACKET_OPEN;
}
\] {
m_yylloc->columns(yyleng);
return yy::json_parser::token::SQUARE_BRACKET_CLOSE;
}
\{ {
m_yylloc->columns(yyleng);
return yy::json_parser::token::CURLY_BRACKET_OPEN;
}
\} {
m_yylloc->columns(yyleng);
return yy::json_parser::token::CURLY_BRACKET_CLOSE;
}
/* Extra-JSON numbers */
<ALLOW_SPECIAL_NUMBERS>{
(?i:nan) {
m_yylloc->columns(yyleng);
*m_yylval = QVariant(std::numeric_limits<double>::quiet_NaN());
return yy::json_parser::token::NUMBER;
}
[Ii]nfinity {
m_yylloc->columns(yyleng);
*m_yylval = QVariant(std::numeric_limits<double>::infinity());
return yy::json_parser::token::NUMBER;
}
-[Ii]nfinity {
m_yylloc->columns(yyleng);
*m_yylval = QVariant(-std::numeric_limits<double>::infinity());
return yy::json_parser::token::NUMBER;
}
}
/* If all else fails */
. {
m_yylloc->columns(yyleng);
return yy::json_parser::token::INVALID;
}
<<EOF>> return yy::json_parser::token::END;