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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
/*
* JSON parser, yacc/bison based. Manual lexer.
* Mikhail.
*/
%define api.pure full
%{
/* * ** *** ***** ******** ************* ********************* */
#include <tcl.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <json_y.h>
#define TOKEN(tok) TRACE (("TOKEN %s\n", tok))
#define TOKEN1(tok) TRACE (("TOKEN %s (%s)\n", tok, Tcl_GetString(context->obj)))
#define REDUCE(rule) TRACE (("REDUCE %s\n", rule))
#define TRUE_O (Tcl_NewStringObj("true", 4))
#define FALSE_O (Tcl_NewStringObj("false", 5))
#define NULL_O (Tcl_NewStringObj("null", 4))
typedef union YYSTYPE YYSTYPE;
static void jsonerror (struct context *, const char *);
static int jsonlexp (YYSTYPE *lvalp, struct context *context);
#define yylex jsonlexp
#define yyerror jsonerror
/* * ** *** ***** ******** ************* *********************
** User declarations <EOF>
*/
%}
%union {
Tcl_Obj *obj;
struct {
Tcl_Obj *key;
Tcl_Obj *val;
} keyval;
};
%lex-param {struct context* context}
%parse-param {struct context* context}
%token STRING CONSTANT
%type <obj> tree
%type <obj> json
%type <obj> object
%type <obj> list
%type <obj> values
%type <obj> members
%type <obj> value
%type <obj> string
%type <keyval> member
%%
tree : json
{
REDUCE("TREE");
if (context->I) {
Tcl_SetObjResult(context->I, $1);
TRACE ((" RESULT (%s)\n", Tcl_GetString($1)));
}
context->result = TCL_OK;
}
;
json : value
;
object : '{' members '}'
{
$$ = $2;
}
| '{' '}'
{
$$ = Tcl_NewObj();
}
;
list : '[' values ']'
{
$$ = $2;
}
| '[' ']'
{
$$ = Tcl_NewObj();
}
;
values : value
{
$$ = Tcl_NewListObj(1, &$1);
}
| values ',' value
{
Tcl_ListObjAppendElement(NULL, $1, $3);
$$ = $1;
}
;
members : member
{
$$ = Tcl_NewListObj(0, NULL);
Tcl_ListObjAppendElement(NULL, $$, $1.key);
Tcl_ListObjAppendElement(NULL, $$, $1.val);
}
| members ',' member
{
Tcl_ListObjAppendElement(NULL, $1, $3.key);
Tcl_ListObjAppendElement(NULL, $1, $3.val);
$$ = $1;
}
;
member : string ':' value
{
$$.key = $1;
$$.val = $3;
}
;
string : STRING
{
$$ = context->obj;
}
;
value : CONSTANT
{
$$ = context->obj;
}
| string
| object
| list
;
%%
/* * ** *** ***** ******** ************* *********************
** User definitions
*/
void
jsonparse (struct context* context)
{
yyparse (context);
}
#define HAVE(n) (context->remaining >= n)
#define DRAIN(n) context->text += n, context->remaining -= n
#define STORESTRINGSEGMENT() \
if (initialized) { \
if (context->text != bp) { \
Tcl_AppendToObj(context->obj, \
bp, context->text - bp); \
} \
} else { \
context->obj = Tcl_NewStringObj( \
bp, context->text - bp); \
initialized = 1; \
}
void
jsonskip(struct context *context)
{
while (context->remaining) {
switch (*context->text) {
case '\n':
case ' ':
case '\t':
case '\r':
DRAIN(1);
continue;
}
break;
}
}
static int
jsonlexp(YYSTYPE *lvalp, struct context *context)
{
const char *bp = NULL;
/* Question: Why not plain numbers 1,2 for the states
* but these specific hex patterns ?
*/
enum {
PLAIN = 0x0000ff00,
INSTR = 0x00ff0000
} lstate;
double d;
char *end;
const char *p;
int initialized = 0;
/*
* Do not auto-lex beyond a full json structure.
*/
if (context->result == TCL_OK) {
TOKEN ("<<eof>>");
return 0;
}
/*
* Quickly skip and ignore whitespace.
*/
while (context->remaining) {
switch (*context->text) {
case '\n':
case ' ':
case '\t':
case '\r':
DRAIN(1);
continue;
}
break;
}
/*
* Handle the token following the whitespace. Small state machine to
* handle strings and escapes in them, and bare words (various
* contants, and numbers).
*/
for (lstate = PLAIN; context->remaining > 0; DRAIN(1)) {
if (lstate == INSTR) {
if (*context->text == '"') {
/*
* End of quoted string
*/
STORESTRINGSEGMENT();
DRAIN(1);
TOKEN1 ("STRING");
return STRING;
}
if (*context->text == '\\') {
/*
* Escaped sequence. The 9 sequences specified at json.org
* are:
* \" \\ \/ \b \f \n \r \t \uXXXX
*/
char buf[TCL_UTF_MAX];
int len, consumed;
STORESTRINGSEGMENT();
/*
* Perform additional checks to restrict the set of accepted
* escape sequence to what is allowed by json.org instead of
* Tcl_UtfBackslash.
*/
if (!HAVE(1)) {
Tcl_AppendToObj(context->obj, "\\", 1);
yyerror(context,"incomplete escape at <<eof> error");
TOKEN("incomplete escape at <<eof>> error");
return -1;
}
switch (context->text[1]) {
case '"':
case '\\':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
break;
case 'u':
if (!HAVE(5)) {
Tcl_AppendToObj(context->obj, "\\u", 2);
yyerror(context,"incomplete escape at <<eof> error");
TOKEN("incomplete escape at <<eof>> error");
return -1;
}
break;
default:
Tcl_AppendToObj(context->obj, context->text + 1, 1);
yyerror(context,"bad escape");
TOKEN("bad escape");
return -1;
}
/*
* XXX Tcl_UtfBackslash() may be more
* XXX permissive, than JSON standard.
* XXX But that may be a good thing:
* XXX "be generous in what you accept".
*/
len = Tcl_UtfBackslash(context->text,
&consumed, buf);
DRAIN(consumed - 1);
bp = context->text + 1;
Tcl_AppendToObj(context->obj, buf, len);
}
continue;
}
switch (*context->text) {
case ',':
case '{':
case ':':
case '}':
case '[':
case ']':
DRAIN(1);
TOKEN (context->text[-1]);
return context->text[-1];
case 't':
if ((context->remaining < 4) ||
strncmp("rue", context->text + 1, 3))
goto bareword;
DRAIN(4);
context->obj = TRUE_O;
TOKEN1 ("CONSTANT");
return CONSTANT;
case 'f':
if ((context->remaining < 5) ||
strncmp("alse", context->text + 1, 4))
goto bareword;
DRAIN(5);
context->obj = FALSE_O;
TOKEN1 ("CONSTANT");
return CONSTANT;
case 'n':
if ((context->remaining < 4) ||
strncmp("ull", context->text + 1, 3))
goto bareword;
DRAIN(4);
context->obj = NULL_O;
TOKEN1 ("CONSTANT");
return CONSTANT;
case '"':
bp = context->text + 1;
lstate = INSTR;
continue;
case '\\':
yyerror(context,"Escape character outside of string");
TOKEN ("escape error");
return -1;
}
/*
* We already considered the null, true, and false
* above, so it can only be a number now.
*
* NOTE: At this point we do not care about double
* versus integer, nor about the possible integer
* range. We generate a plain string Tcl_Obj and leave
* it to the user of the generated structure to
* convert to a number when actually needed. This
* defered conversion also ensures that the Tcl and
* platform we are building against does not matter
* regarding integer range, only the abilities of the
* Tcl at runtime.
*/
d = strtod(context->text, &end);
if (end == context->text)
goto bareword; /* Nothing parsed */
context->obj = Tcl_NewStringObj (context->text,
end - context->text);
context->remaining -= (end - context->text);
context->text = end;
TOKEN1 ("CONSTANT");
return CONSTANT;
}
TOKEN ("<<eof>>");
return 0;
bareword:
yyerror(context,"Bare word encountered");
TOKEN ("bare word error");
return -1;
}
#if 0
int
jsonlex(struct context *context)
{
const char *bp = NULL;
/* Question: Why not plain numbers 1,2 for the states
* but these specific hex patterns ?
*/
enum {
PLAIN = 0x0000ff00,
INSTR = 0x00ff0000
} lstate;
double d;
char *end;
const char *p;
int initialized = 0;
while (context->remaining) {
/* Iterate over the whole string and check all tokens.
* Nothing else.
*/
/*
* Quickly skip and ignore whitespace.
*/
while (context->remaining) {
switch (*context->text) {
case '\n':
case ' ':
case '\t':
case '\r':
DRAIN(1);
continue;
}
break;
}
/*
* Handle the token following the whitespace. Small state machine to
* handle strings and escapes in them, and bare words (various
* contants, and numbers).
*/
for (lstate = PLAIN; context->remaining > 0; DRAIN(1)) {
if (lstate == INSTR) {
if (*context->text == '"') {
/*
* End of quoted string
*/
DRAIN(1);
goto next_token;
}
if (*context->text == '\\') {
/*
* Escaped sequence
*/
char buf[TCL_UTF_MAX];
int len, consumed;
/*
* XXX Tcl_UtfBackslash() may be more
* XXX permissive, than JSON standard.
* XXX But that may be a good thing:
* XXX "be generous in what you accept".
*/
len = Tcl_UtfBackslash(context->text, &consumed, buf);
DRAIN(consumed - 1);
}
continue;
}
switch (*context->text) {
case ',':
case '{':
case ':':
case '}':
case '[':
case ']':
DRAIN(1);
goto next_token;
case 't':
if ((context->remaining < 4) ||
strncmp("rue", context->text + 1, 3))
return -1; /* bare word */
DRAIN(4);
goto next_token;
case 'f':
if ((context->remaining < 5) ||
strncmp("alse", context->text + 1, 4))
return -1; /* bare word */
DRAIN(5);
goto next_token;
case 'n':
if ((context->remaining < 4) ||
strncmp("ull", context->text + 1, 3))
return -1; /* bare word */
DRAIN(4);
goto next_token;
case '"':
bp = context->text + 1;
lstate = INSTR;
continue;
case '\\':
/* Escape outside string, abort. */
return -1;
}
/*
* We already considered the null, true, and false
* above, so it can only be a number now.
*
* NOTE: At this point we do not care about double
* versus integer, nor about the possible integer
* range. We generate a plain string Tcl_Obj and leave
* it to the user of the generated structure to
* convert to a number when actually needed. This
* defered conversion also ensures that the Tcl and
* platform we are building against does not matter
* regarding integer range, only the abilities of the
* Tcl at runtime.
*/
d = strtod(context->text, &end);
if (end == context->text)
return -1; /* bare word */
context->remaining -= (end - context->text);
context->text = end;
goto next_token;
}
return 0;
next_token:
continue;
}
}
#endif
static void
jsonerror(struct context *context, const char *message)
{
char *fullmessage;
char *yytext;
int yyleng;
if (context->has_error) return;
if (context->obj) {
yytext = Tcl_GetStringFromObj(context->obj, &yyleng);
fullmessage = Tcl_Alloc(strlen(message) + 63 + yyleng);
sprintf(fullmessage, "%s %d bytes before end, around ``%.*s''",
message, context->remaining, yyleng, yytext);
} else {
fullmessage = Tcl_Alloc(strlen(message) + 63);
sprintf(fullmessage, "%s %d bytes before end",
message, context->remaining);
}
TRACE ((">>> %s\n",fullmessage));
Tcl_SetResult (context->I, fullmessage, TCL_DYNAMIC);
Tcl_SetErrorCode (context->I, "JSON", "SYNTAX", NULL);
context->has_error = 1;
}
|