3939#define BUILTIN_UNDERSCORED_ID json
4040#include " ecma-builtin-internal-routines-template.inc.h"
4141
42- /*
43- * FIXME:
44- * Replace usage of isalpha and isdigit functions in the module with lit_char helpers and remove the functions.
45- *
46- * Related issue: #424
47- */
48-
49- /* * Checks for an alphabetic character. */
50- static int
51- isalpha (int c)
52- {
53- return (c >= ' A' && c <= ' Z' ) || (c >= ' a' && c <= ' z' );
54- }
55-
56- /* * Checks for a digit (0 through 9). */
57- static int
58- isdigit (int c)
59- {
60- return c >= ' 0' && c <= ' 9' ;
61- }
62-
6342/* * \addtogroup ecma ECMA
6443 * @{
6544 *
@@ -126,9 +105,16 @@ ecma_builtin_json_check_id (lit_utf8_byte_t *string_p, /**< start position */
126105 {
127106 string_p++;
128107 id_p++;
129- if (*id_p == ' \0 ' )
130- {
131- return !isalpha (*string_p) && !isdigit (*string_p) && *string_p != ' $' && *string_p != ' _' ;
108+ if (*id_p == LIT_CHAR_NULL)
109+ {
110+ /* JSON lexer accepts input strings such as falsenull and
111+ * returns with multiple tokens (false and null in this case).
112+ * This is different from normal lexers, whose return with a
113+ * single identifier. The falsenull input will still cause an
114+ * error eventually, since a separator must always be present
115+ * between two JSON values regardless of the current expression
116+ * type. */
117+ return true ;
132118 }
133119 }
134120 while (*string_p == *id_p);
@@ -147,49 +133,49 @@ ecma_builtin_json_parse_string (ecma_json_token_t *token_p) /**< token argument
147133
148134 token_p->u .string .start_p = current_p;
149135
150- while (*current_p != ' " ' )
136+ while (*current_p != LIT_CHAR_DOUBLE_QUOTE )
151137 {
152138 if (*current_p <= 0x1f )
153139 {
154140 return ;
155141 }
156- if (*current_p == ' \\ ' )
142+ if (*current_p == LIT_CHAR_BACKSLASH )
157143 {
158144 current_p++;
159145 switch (*current_p)
160146 {
161- case ' " ' :
162- case ' / ' :
163- case ' \\ ' :
147+ case LIT_CHAR_DOUBLE_QUOTE :
148+ case LIT_CHAR_SLASH :
149+ case LIT_CHAR_BACKSLASH :
164150 {
165151 break ;
166152 }
167- case ' b ' :
153+ case LIT_CHAR_LOWERCASE_B :
168154 {
169- *current_p = ' \b ' ;
155+ *current_p = LIT_CHAR_BS ;
170156 break ;
171157 }
172- case ' f ' :
158+ case LIT_CHAR_LOWERCASE_F :
173159 {
174- *current_p = ' \f ' ;
160+ *current_p = LIT_CHAR_FF ;
175161 break ;
176162 }
177- case ' n ' :
163+ case LIT_CHAR_LOWERCASE_N :
178164 {
179- *current_p = ' \n ' ;
165+ *current_p = LIT_CHAR_LF ;
180166 break ;
181167 }
182- case ' r ' :
168+ case LIT_CHAR_LOWERCASE_R :
183169 {
184- *current_p = ' \r ' ;
170+ *current_p = LIT_CHAR_CR ;
185171 break ;
186172 }
187- case ' t ' :
173+ case LIT_CHAR_LOWERCASE_T :
188174 {
189- *current_p = ' \t ' ;
175+ *current_p = LIT_CHAR_TAB ;
190176 break ;
191177 }
192- case ' u ' :
178+ case LIT_CHAR_LOWERCASE_U :
193179 {
194180 lit_code_point_t code_point;
195181
@@ -277,32 +263,32 @@ ecma_builtin_json_parse_number (ecma_json_token_t *token_p) /**< token argument
277263 lit_utf8_byte_t *current_p = token_p->current_p ;
278264 lit_utf8_byte_t *start_p = current_p;
279265
280- if (*current_p == ' - ' )
266+ if (*current_p == LIT_CHAR_MINUS )
281267 {
282268 current_p++;
283269 }
284270
285- if (*current_p == ' 0 ' )
271+ if (*current_p == LIT_CHAR_0 )
286272 {
287273 current_p++;
288- if (isdigit (*current_p))
274+ if (lit_char_is_decimal_digit (*current_p))
289275 {
290276 return ;
291277 }
292278 }
293- else if (isdigit (*current_p))
279+ else if (lit_char_is_decimal_digit (*current_p))
294280 {
295281 do
296282 {
297283 current_p++;
298284 }
299- while (isdigit (*current_p));
285+ while (lit_char_is_decimal_digit (*current_p));
300286 }
301287
302- if (*current_p == ' . ' )
288+ if (*current_p == LIT_CHAR_DOT )
303289 {
304290 current_p++;
305- if (!isdigit (*current_p))
291+ if (!lit_char_is_decimal_digit (*current_p))
306292 {
307293 return ;
308294 }
@@ -311,18 +297,18 @@ ecma_builtin_json_parse_number (ecma_json_token_t *token_p) /**< token argument
311297 {
312298 current_p++;
313299 }
314- while (isdigit (*current_p));
300+ while (lit_char_is_decimal_digit (*current_p));
315301 }
316302
317- if (*current_p == ' e ' || *current_p == ' E ' )
303+ if (*current_p == LIT_CHAR_LOWERCASE_E || *current_p == LIT_CHAR_UPPERCASE_E )
318304 {
319305 current_p++;
320- if (*current_p == ' + ' || *current_p == ' - ' )
306+ if (*current_p == LIT_CHAR_PLUS || *current_p == LIT_CHAR_MINUS )
321307 {
322308 current_p++;
323309 }
324310
325- if (!isdigit (*current_p))
311+ if (!lit_char_is_decimal_digit (*current_p))
326312 {
327313 return ;
328314 }
@@ -331,7 +317,7 @@ ecma_builtin_json_parse_number (ecma_json_token_t *token_p) /**< token argument
331317 {
332318 current_p++;
333319 }
334- while (isdigit (*current_p));
320+ while (lit_char_is_decimal_digit (*current_p));
335321 }
336322 token_p->type = number_token;
337323 token_p->u .number = ecma_utf8_string_to_number (start_p, (lit_utf8_size_t ) (current_p - start_p));
@@ -354,8 +340,8 @@ ecma_builtin_json_parse_next_token (ecma_json_token_t *token_p) /**< token argum
354340 /*
355341 * No need for end check since the string is zero terminated.
356342 */
357- while (*current_p == ' ' || *current_p == ' \r '
358- || *current_p == ' \n ' || *current_p == ' \t ' )
343+ while (*current_p == LIT_CHAR_SP || *current_p == LIT_CHAR_CR
344+ || *current_p == LIT_CHAR_LF || *current_p == LIT_CHAR_TAB )
359345 {
360346 current_p++;
361347 }
@@ -368,43 +354,43 @@ ecma_builtin_json_parse_next_token (ecma_json_token_t *token_p) /**< token argum
368354
369355 switch (*current_p)
370356 {
371- case ' { ' :
357+ case LIT_CHAR_LEFT_BRACE :
372358 {
373359 token_p->type = left_brace_token;
374360 token_p->current_p = current_p + 1 ;
375361 return ;
376362 }
377- case ' } ' :
363+ case LIT_CHAR_RIGHT_BRACE :
378364 {
379365 token_p->type = right_brace_token;
380366 token_p->current_p = current_p + 1 ;
381367 return ;
382368 }
383- case ' [ ' :
369+ case LIT_CHAR_LEFT_SQUARE :
384370 {
385371 token_p->type = left_square_token;
386372 token_p->current_p = current_p + 1 ;
387373 return ;
388374 }
389- case ' , ' :
375+ case LIT_CHAR_COMMA :
390376 {
391377 token_p->type = comma_token;
392378 token_p->current_p = current_p + 1 ;
393379 return ;
394380 }
395- case ' : ' :
381+ case LIT_CHAR_COLON :
396382 {
397383 token_p->type = colon_token;
398384 token_p->current_p = current_p + 1 ;
399385 return ;
400386 }
401- case ' " ' :
387+ case LIT_CHAR_DOUBLE_QUOTE :
402388 {
403389 token_p->current_p = current_p + 1 ;
404390 ecma_builtin_json_parse_string (token_p);
405391 return ;
406392 }
407- case ' n ' :
393+ case LIT_CHAR_LOWERCASE_N :
408394 {
409395 if (ecma_builtin_json_check_id (current_p, " null" ))
410396 {
@@ -414,7 +400,7 @@ ecma_builtin_json_parse_next_token (ecma_json_token_t *token_p) /**< token argum
414400 }
415401 break ;
416402 }
417- case ' t ' :
403+ case LIT_CHAR_LOWERCASE_T :
418404 {
419405 if (ecma_builtin_json_check_id (current_p, " true" ))
420406 {
@@ -424,7 +410,7 @@ ecma_builtin_json_parse_next_token (ecma_json_token_t *token_p) /**< token argum
424410 }
425411 break ;
426412 }
427- case ' f ' :
413+ case LIT_CHAR_LOWERCASE_F :
428414 {
429415 if (ecma_builtin_json_check_id (current_p, " false" ))
430416 {
@@ -436,7 +422,7 @@ ecma_builtin_json_parse_next_token (ecma_json_token_t *token_p) /**< token argum
436422 }
437423 default :
438424 {
439- if (*current_p == ' - ' || isdigit (*current_p))
425+ if (*current_p == LIT_CHAR_MINUS || lit_char_is_decimal_digit (*current_p))
440426 {
441427 token_p->current_p = current_p;
442428 ecma_builtin_json_parse_number (token_p);
@@ -462,15 +448,15 @@ ecma_builtin_json_check_right_square_token (ecma_json_token_t *token_p) /**< tok
462448 /*
463449 * No need for end check since the string is zero terminated.
464450 */
465- while (*current_p == ' ' || *current_p == ' \r '
466- || *current_p == ' \n ' || *current_p == ' \t ' )
451+ while (*current_p == LIT_CHAR_SP || *current_p == LIT_CHAR_CR
452+ || *current_p == LIT_CHAR_LF || *current_p == LIT_CHAR_TAB )
467453 {
468454 current_p++;
469455 }
470456
471457 token_p->current_p = current_p;
472458
473- if (*current_p == ' ] ' )
459+ if (*current_p == LIT_CHAR_RIGHT_SQUARE )
474460 {
475461 token_p->current_p = current_p + 1 ;
476462 return true ;
@@ -1107,7 +1093,7 @@ ecma_builtin_json_stringify (ecma_value_t this_arg __attr_unused___, /**< 'this'
11071093
11081094 for (int32_t i = 0 ; i < space; i++)
11091095 {
1110- space_buff[i] = ' ' ;
1096+ space_buff[i] = LIT_CHAR_SP ;
11111097 }
11121098
11131099 context_p.gap_str_p = ecma_new_ecma_string_from_utf8 ((lit_utf8_byte_t *) space_buff, (lit_utf8_size_t ) space);
@@ -1228,7 +1214,7 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
12281214 lit_utf8_byte_t c = zt_string_buff[i];
12291215
12301216 /* 2.a */
1231- if (c == ' \\ ' || c == ' \" ' )
1217+ if (c == LIT_CHAR_BACKSLASH || c == LIT_CHAR_DOUBLE_QUOTE )
12321218 {
12331219 ecma_string_t *backslash_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BACKSLASH_CHAR);
12341220
@@ -1247,7 +1233,8 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
12471233 product_str_p = tmp_str_p;
12481234 }
12491235 /* 2.b */
1250- else if (c == ' \b ' || c == ' \f ' || c == ' \n ' || c == ' \r ' || c == ' \t ' )
1236+ else if (c == LIT_CHAR_BS || c == LIT_CHAR_FF || c == LIT_CHAR_LF
1237+ || c == LIT_CHAR_CR || c == LIT_CHAR_TAB)
12511238 {
12521239 ecma_string_t *backslash_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BACKSLASH_CHAR);
12531240
@@ -1258,33 +1245,33 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
12581245 product_str_p = tmp_str_p;
12591246
12601247 /* 2.b.ii */
1261- lit_utf8_byte_t abbrev = ' ' ;
1248+ lit_utf8_byte_t abbrev = LIT_CHAR_SP ;
12621249
12631250 switch (c)
12641251 {
1265- case ' \b ' :
1252+ case LIT_CHAR_BS :
12661253 {
1267- abbrev = ' b ' ;
1254+ abbrev = LIT_CHAR_LOWERCASE_B ;
12681255 break ;
12691256 }
1270- case ' \f ' :
1257+ case LIT_CHAR_FF :
12711258 {
1272- abbrev = ' f ' ;
1259+ abbrev = LIT_CHAR_LOWERCASE_F ;
12731260 break ;
12741261 }
1275- case ' \n ' :
1262+ case LIT_CHAR_LF :
12761263 {
1277- abbrev = ' n ' ;
1264+ abbrev = LIT_CHAR_LOWERCASE_N ;
12781265 break ;
12791266 }
1280- case ' \r ' :
1267+ case LIT_CHAR_CR :
12811268 {
1282- abbrev = ' r ' ;
1269+ abbrev = LIT_CHAR_LOWERCASE_R ;
12831270 break ;
12841271 }
1285- case ' \t ' :
1272+ case LIT_CHAR_TAB :
12861273 {
1287- abbrev = ' t ' ;
1274+ abbrev = LIT_CHAR_LOWERCASE_T ;
12881275 break ;
12891276 }
12901277 }
@@ -1309,7 +1296,7 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
13091296 product_str_p = tmp_str_p;
13101297
13111298 /* 2.c.ii */
1312- lit_utf8_byte_t u_ch = ' u ' ;
1299+ lit_utf8_byte_t u_ch = LIT_CHAR_LOWERCASE_U ;
13131300 ecma_string_t *u_ch_str_p = ecma_new_ecma_string_from_utf8 (&u_ch, 1 );
13141301
13151302 tmp_str_p = ecma_concat_ecma_strings (product_str_p, u_ch_str_p);
0 commit comments