4 * Copyright IBM, Corp. 2009
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
18 #include "qemu-common.h"
19 #include "json-lexer.h"
22 * \"([^\\\"]|(\\\"\\'\\\\\\/\\b\\f\\n\\r\\t\\u[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]))*\"
23 * '([^\\']|(\\\"\\'\\\\\\/\\b\\f\\n\\r\\t\\u[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]))*'
24 * 0|([1-9][0-9]*(.[0-9]+)?([eE]([-+])?[0-9]+))
30 enum json_lexer_state
{
52 IN_NEG_NONZERO_NUMBER
,
66 #define TERMINAL(state) [0 ... 0x7F] = (state)
68 static const uint8_t json_lexer
[][256] = {
70 TERMINAL(JSON_STRING
),
73 /* double quote string */
75 ['0' ... '9'] = IN_DQ_STRING
,
76 ['a' ... 'f'] = IN_DQ_STRING
,
77 ['A' ... 'F'] = IN_DQ_STRING
,
80 ['0' ... '9'] = IN_DQ_UCODE3
,
81 ['a' ... 'f'] = IN_DQ_UCODE3
,
82 ['A' ... 'F'] = IN_DQ_UCODE3
,
85 ['0' ... '9'] = IN_DQ_UCODE2
,
86 ['a' ... 'f'] = IN_DQ_UCODE2
,
87 ['A' ... 'F'] = IN_DQ_UCODE2
,
90 ['0' ... '9'] = IN_DQ_UCODE1
,
91 ['a' ... 'f'] = IN_DQ_UCODE1
,
92 ['A' ... 'F'] = IN_DQ_UCODE1
,
94 [IN_DQ_STRING_ESCAPE
] = {
100 ['\''] = IN_DQ_STRING
,
101 ['\"'] = IN_DQ_STRING
,
102 ['u'] = IN_DQ_UCODE0
,
105 [1 ... 0xFF] = IN_DQ_STRING
,
106 ['\\'] = IN_DQ_STRING_ESCAPE
,
107 ['"'] = IN_DONE_STRING
,
110 /* single quote string */
112 ['0' ... '9'] = IN_SQ_STRING
,
113 ['a' ... 'f'] = IN_SQ_STRING
,
114 ['A' ... 'F'] = IN_SQ_STRING
,
117 ['0' ... '9'] = IN_SQ_UCODE3
,
118 ['a' ... 'f'] = IN_SQ_UCODE3
,
119 ['A' ... 'F'] = IN_SQ_UCODE3
,
122 ['0' ... '9'] = IN_SQ_UCODE2
,
123 ['a' ... 'f'] = IN_SQ_UCODE2
,
124 ['A' ... 'F'] = IN_SQ_UCODE2
,
127 ['0' ... '9'] = IN_SQ_UCODE1
,
128 ['a' ... 'f'] = IN_SQ_UCODE1
,
129 ['A' ... 'F'] = IN_SQ_UCODE1
,
131 [IN_SQ_STRING_ESCAPE
] = {
132 ['b'] = IN_SQ_STRING
,
133 ['f'] = IN_SQ_STRING
,
134 ['n'] = IN_SQ_STRING
,
135 ['r'] = IN_SQ_STRING
,
136 ['t'] = IN_SQ_STRING
,
137 ['\''] = IN_SQ_STRING
,
138 ['\"'] = IN_SQ_STRING
,
139 ['u'] = IN_SQ_UCODE0
,
142 [1 ... 0xFF] = IN_SQ_STRING
,
143 ['\\'] = IN_SQ_STRING_ESCAPE
,
144 ['\''] = IN_DONE_STRING
,
149 TERMINAL(JSON_INTEGER
),
150 ['0' ... '9'] = ERROR
,
156 TERMINAL(JSON_FLOAT
),
157 ['0' ... '9'] = IN_DIGITS
,
161 ['0' ... '9'] = IN_DIGITS
,
167 ['0' ... '9'] = IN_DIGITS
,
170 [IN_MANTISSA_DIGITS
] = {
171 TERMINAL(JSON_FLOAT
),
172 ['0' ... '9'] = IN_MANTISSA_DIGITS
,
178 ['0' ... '9'] = IN_MANTISSA_DIGITS
,
182 [IN_NONZERO_NUMBER
] = {
183 TERMINAL(JSON_INTEGER
),
184 ['0' ... '9'] = IN_NONZERO_NUMBER
,
190 [IN_NEG_NONZERO_NUMBER
] = {
192 ['1' ... '9'] = IN_NONZERO_NUMBER
,
197 TERMINAL(JSON_KEYWORD
),
198 ['a' ... 'z'] = IN_KEYWORD
,
204 [' '] = IN_WHITESPACE
,
205 ['\t'] = IN_WHITESPACE
,
206 ['\r'] = IN_WHITESPACE
,
207 ['\n'] = IN_WHITESPACE
,
211 [IN_OPERATOR_DONE
] = {
212 TERMINAL(JSON_OPERATOR
),
217 TERMINAL(JSON_ESCAPE
),
221 ['d'] = IN_ESCAPE_DONE
,
225 ['d'] = IN_ESCAPE_DONE
,
226 ['l'] = IN_ESCAPE_LL
,
230 ['d'] = IN_ESCAPE_DONE
,
234 ['4'] = IN_ESCAPE_I64
,
238 ['6'] = IN_ESCAPE_I6
,
242 ['d'] = IN_ESCAPE_DONE
,
243 ['i'] = IN_ESCAPE_DONE
,
244 ['p'] = IN_ESCAPE_DONE
,
245 ['s'] = IN_ESCAPE_DONE
,
246 ['f'] = IN_ESCAPE_DONE
,
253 ['"'] = IN_DQ_STRING
,
254 ['\''] = IN_SQ_STRING
,
256 ['1' ... '9'] = IN_NONZERO_NUMBER
,
257 ['-'] = IN_NEG_NONZERO_NUMBER
,
258 ['{'] = IN_OPERATOR_DONE
,
259 ['}'] = IN_OPERATOR_DONE
,
260 ['['] = IN_OPERATOR_DONE
,
261 [']'] = IN_OPERATOR_DONE
,
262 [','] = IN_OPERATOR_DONE
,
263 [':'] = IN_OPERATOR_DONE
,
264 ['a' ... 'z'] = IN_KEYWORD
,
266 [' '] = IN_WHITESPACE
,
267 ['\t'] = IN_WHITESPACE
,
268 ['\r'] = IN_WHITESPACE
,
269 ['\n'] = IN_WHITESPACE
,
273 void json_lexer_init(JSONLexer
*lexer
, JSONLexerEmitter func
)
276 lexer
->state
= IN_START
;
277 lexer
->token
= qstring_new();
280 static int json_lexer_feed_char(JSONLexer
*lexer
, char ch
)
290 lexer
->state
= json_lexer
[lexer
->state
][(uint8_t)ch
];
292 switch (lexer
->state
) {
299 lexer
->emit(lexer
, lexer
->token
, lexer
->state
, lexer
->x
, lexer
->y
);
301 lexer
->state
= json_lexer
[IN_START
][(uint8_t)ch
];
302 QDECREF(lexer
->token
);
303 lexer
->token
= qstring_new();
314 qstring_append(lexer
->token
, buf
);
319 int json_lexer_feed(JSONLexer
*lexer
, const char *buffer
, size_t size
)
323 for (i
= 0; i
< size
; i
++) {
326 err
= json_lexer_feed_char(lexer
, buffer
[i
]);
335 int json_lexer_flush(JSONLexer
*lexer
)
337 return json_lexer_feed_char(lexer
, 0);
340 void json_lexer_destroy(JSONLexer
*lexer
)
342 QDECREF(lexer
->token
);