Use special eof char to mark end of parsing

pull/13171/head
Bo Yang 7 years ago
parent d4caefaade
commit a8d1a5933d
  1. 107
      upb/json/parser.c
  2. 17
      upb/json/parser.rl

@ -63,6 +63,8 @@ static void end_wrapper_object(upb_json_parser *p);
static bool start_subobject(upb_json_parser *p); static bool start_subobject(upb_json_parser *p);
static void end_subobject(upb_json_parser *p); static void end_subobject(upb_json_parser *p);
static const char eof_ch = 'e';
typedef struct { typedef struct {
upb_sink sink; upb_sink sink;
@ -130,9 +132,6 @@ struct upb_json_parser {
/* Whether to proceed if unknown field is met. */ /* Whether to proceed if unknown field is met. */
bool ignore_json_unknown; bool ignore_json_unknown;
/* Whether to end parsing. */
bool ready_to_end;
}; };
struct upb_json_parsermethod { struct upb_json_parsermethod {
@ -1489,11 +1488,11 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) {
* final state once, when the closing '"' is seen. */ * final state once, when the closing '"' is seen. */
#line 1592 "upb/json/parser.rl" #line 1587 "upb/json/parser.rl"
#line 1497 "upb/json/parser.c" #line 1496 "upb/json/parser.c"
static const char _json_actions[] = { static const char _json_actions[] = {
0, 1, 0, 1, 1, 1, 3, 1, 0, 1, 0, 1, 1, 1, 3, 1,
4, 1, 6, 1, 7, 1, 8, 1, 4, 1, 6, 1, 7, 1, 8, 1,
@ -1686,7 +1685,7 @@ static const int json_en_value_machine = 41;
static const int json_en_main = 1; static const int json_en_main = 1;
#line 1595 "upb/json/parser.rl" #line 1590 "upb/json/parser.rl"
size_t parse(void *closure, const void *hd, const char *buf, size_t size, size_t parse(void *closure, const void *hd, const char *buf, size_t size,
const upb_bufhandle *handle) { const upb_bufhandle *handle) {
@ -1699,7 +1698,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size,
const char *p = buf; const char *p = buf;
const char *pe = buf + size; const char *pe = buf + size;
const char *eof = pe; const char *eof = &eof_ch;
parser->handle = handle; parser->handle = handle;
@ -1709,7 +1708,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size,
capture_resume(parser, buf); capture_resume(parser, buf);
#line 1713 "upb/json/parser.c" #line 1712 "upb/json/parser.c"
{ {
int _klen; int _klen;
unsigned int _trans; unsigned int _trans;
@ -1784,118 +1783,118 @@ _match:
switch ( *_acts++ ) switch ( *_acts++ )
{ {
case 1: case 1:
#line 1506 "upb/json/parser.rl" #line 1501 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} } { p--; {cs = stack[--top]; goto _again;} }
break; break;
case 2: case 2:
#line 1508 "upb/json/parser.rl" #line 1503 "upb/json/parser.rl"
{ p--; {stack[top++] = cs; cs = 24; goto _again;} } { p--; {stack[top++] = cs; cs = 24; goto _again;} }
break; break;
case 3: case 3:
#line 1512 "upb/json/parser.rl" #line 1507 "upb/json/parser.rl"
{ start_text(parser, p); } { start_text(parser, p); }
break; break;
case 4: case 4:
#line 1513 "upb/json/parser.rl" #line 1508 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_text(parser, p)); } { CHECK_RETURN_TOP(end_text(parser, p)); }
break; break;
case 5: case 5:
#line 1519 "upb/json/parser.rl" #line 1514 "upb/json/parser.rl"
{ start_hex(parser); } { start_hex(parser); }
break; break;
case 6: case 6:
#line 1520 "upb/json/parser.rl" #line 1515 "upb/json/parser.rl"
{ hexdigit(parser, p); } { hexdigit(parser, p); }
break; break;
case 7: case 7:
#line 1521 "upb/json/parser.rl" #line 1516 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_hex(parser)); } { CHECK_RETURN_TOP(end_hex(parser)); }
break; break;
case 8: case 8:
#line 1527 "upb/json/parser.rl" #line 1522 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(escape(parser, p)); } { CHECK_RETURN_TOP(escape(parser, p)); }
break; break;
case 9: case 9:
#line 1533 "upb/json/parser.rl" #line 1528 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} } { p--; {cs = stack[--top]; goto _again;} }
break; break;
case 10: case 10:
#line 1536 "upb/json/parser.rl" #line 1531 "upb/json/parser.rl"
{ {stack[top++] = cs; cs = 33; goto _again;} } { {stack[top++] = cs; cs = 33; goto _again;} }
break; break;
case 11: case 11:
#line 1538 "upb/json/parser.rl" #line 1533 "upb/json/parser.rl"
{ p--; {stack[top++] = cs; cs = 41; goto _again;} } { p--; {stack[top++] = cs; cs = 41; goto _again;} }
break; break;
case 12: case 12:
#line 1543 "upb/json/parser.rl" #line 1538 "upb/json/parser.rl"
{ start_member(parser); } { start_member(parser); }
break; break;
case 13: case 13:
#line 1544 "upb/json/parser.rl" #line 1539 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_membername(parser)); } { CHECK_RETURN_TOP(end_membername(parser)); }
break; break;
case 14: case 14:
#line 1547 "upb/json/parser.rl" #line 1542 "upb/json/parser.rl"
{ end_member(parser); } { end_member(parser); }
break; break;
case 15: case 15:
#line 1553 "upb/json/parser.rl" #line 1548 "upb/json/parser.rl"
{ start_object(parser); } { start_object(parser); }
break; break;
case 16: case 16:
#line 1556 "upb/json/parser.rl" #line 1551 "upb/json/parser.rl"
{ end_object(parser); } { end_object(parser); }
break; break;
case 17: case 17:
#line 1562 "upb/json/parser.rl" #line 1557 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_array(parser)); } { CHECK_RETURN_TOP(start_array(parser)); }
break; break;
case 18: case 18:
#line 1566 "upb/json/parser.rl" #line 1561 "upb/json/parser.rl"
{ end_array(parser); } { end_array(parser); }
break; break;
case 19: case 19:
#line 1571 "upb/json/parser.rl" #line 1566 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_number(parser, p)); } { CHECK_RETURN_TOP(start_number(parser, p)); }
break; break;
case 20: case 20:
#line 1572 "upb/json/parser.rl" #line 1567 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_number(parser, p)); } { CHECK_RETURN_TOP(end_number(parser, p)); }
break; break;
case 21: case 21:
#line 1574 "upb/json/parser.rl" #line 1569 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_stringval(parser)); } { CHECK_RETURN_TOP(start_stringval(parser)); }
break; break;
case 22: case 22:
#line 1575 "upb/json/parser.rl" #line 1570 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_stringval(parser)); } { CHECK_RETURN_TOP(end_stringval(parser)); }
break; break;
case 23: case 23:
#line 1577 "upb/json/parser.rl" #line 1572 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, true)); } { CHECK_RETURN_TOP(end_bool(parser, true)); }
break; break;
case 24: case 24:
#line 1579 "upb/json/parser.rl" #line 1574 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, false)); } { CHECK_RETURN_TOP(end_bool(parser, false)); }
break; break;
case 25: case 25:
#line 1581 "upb/json/parser.rl" #line 1576 "upb/json/parser.rl"
{ /* null value */ } { /* null value */ }
break; break;
case 26: case 26:
#line 1583 "upb/json/parser.rl" #line 1578 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(start_subobject(parser)); } { CHECK_RETURN_TOP(start_subobject(parser)); }
break; break;
case 27: case 27:
#line 1584 "upb/json/parser.rl" #line 1579 "upb/json/parser.rl"
{ end_subobject(parser); } { end_subobject(parser); }
break; break;
case 28: case 28:
#line 1589 "upb/json/parser.rl" #line 1584 "upb/json/parser.rl"
{ p--; {cs = stack[--top]; goto _again;} } { p--; {cs = stack[--top]; goto _again;} }
break; break;
#line 1899 "upb/json/parser.c" #line 1898 "upb/json/parser.c"
} }
} }
@ -1912,34 +1911,30 @@ _again:
while ( __nacts-- > 0 ) { while ( __nacts-- > 0 ) {
switch ( *__acts++ ) { switch ( *__acts++ ) {
case 0: case 0:
#line 1500 "upb/json/parser.rl" #line 1499 "upb/json/parser.rl"
{ { p--; {cs = stack[--top]; goto _again;} }
if (parser->ready_to_end) {
p--; {cs = stack[--top]; goto _again;}
}
}
break; break;
case 20: case 20:
#line 1572 "upb/json/parser.rl" #line 1567 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_number(parser, p)); } { CHECK_RETURN_TOP(end_number(parser, p)); }
break; break;
case 23: case 23:
#line 1577 "upb/json/parser.rl" #line 1572 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, true)); } { CHECK_RETURN_TOP(end_bool(parser, true)); }
break; break;
case 24: case 24:
#line 1579 "upb/json/parser.rl" #line 1574 "upb/json/parser.rl"
{ CHECK_RETURN_TOP(end_bool(parser, false)); } { CHECK_RETURN_TOP(end_bool(parser, false)); }
break; break;
case 25: case 25:
#line 1581 "upb/json/parser.rl" #line 1576 "upb/json/parser.rl"
{ /* null value */ } { /* null value */ }
break; break;
case 27: case 27:
#line 1584 "upb/json/parser.rl" #line 1579 "upb/json/parser.rl"
{ end_subobject(parser); } { end_subobject(parser); }
break; break;
#line 1943 "upb/json/parser.c" #line 1938 "upb/json/parser.c"
} }
} }
} }
@ -1947,7 +1942,7 @@ _again:
_out: {} _out: {}
} }
#line 1617 "upb/json/parser.rl" #line 1612 "upb/json/parser.rl"
if (p != pe) { if (p != pe) {
upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p);
@ -1974,13 +1969,12 @@ bool end(void *closure, const void *hd) {
UPB_UNUSED(json_en_value_machine); UPB_UNUSED(json_en_value_machine);
UPB_UNUSED(json_en_main); UPB_UNUSED(json_en_main);
parser->ready_to_end = true; parse(parser, hd, &eof_ch, 0, NULL);
parse(parser, hd, NULL, 0, NULL);
return parser->current_state >= return parser->current_state >=
#line 1982 "upb/json/parser.c" #line 1976 "upb/json/parser.c"
70 70
#line 1646 "upb/json/parser.rl" #line 1640 "upb/json/parser.rl"
; ;
} }
@ -1995,13 +1989,13 @@ static void json_parser_reset(upb_json_parser *p) {
/* Emit Ragel initialization of the parser. */ /* Emit Ragel initialization of the parser. */
#line 1999 "upb/json/parser.c" #line 1993 "upb/json/parser.c"
{ {
cs = json_start; cs = json_start;
top = 0; top = 0;
} }
#line 1660 "upb/json/parser.rl" #line 1654 "upb/json/parser.rl"
p->current_state = cs; p->current_state = cs;
p->parser_top = top; p->parser_top = top;
accumulate_clear(p); accumulate_clear(p);
@ -2109,7 +2103,6 @@ upb_json_parser *upb_json_parser_create(upb_env *env,
set_name_table(p, p->top); set_name_table(p, p->top);
p->ignore_json_unknown = ignore_json_unknown; p->ignore_json_unknown = ignore_json_unknown;
p->ready_to_end = false;
/* If this fails, uncomment and increase the value in parser.h. */ /* If this fails, uncomment and increase the value in parser.h. */
/* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */ /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */

@ -61,6 +61,8 @@ static void end_wrapper_object(upb_json_parser *p);
static bool start_subobject(upb_json_parser *p); static bool start_subobject(upb_json_parser *p);
static void end_subobject(upb_json_parser *p); static void end_subobject(upb_json_parser *p);
static const char eof_ch = 'e';
typedef struct { typedef struct {
upb_sink sink; upb_sink sink;
@ -128,9 +130,6 @@ struct upb_json_parser {
/* Whether to proceed if unknown field is met. */ /* Whether to proceed if unknown field is met. */
bool ignore_json_unknown; bool ignore_json_unknown;
/* Whether to end parsing. */
bool ready_to_end;
}; };
struct upb_json_parsermethod { struct upb_json_parsermethod {
@ -1497,11 +1496,7 @@ static bool is_boolean_wrapper_object(upb_json_parser *p) {
number_machine := number_machine :=
("-"? integer decimal? exponent?) ("-"? integer decimal? exponent?)
%/{ %/{ fhold; fret; }
if (parser->ready_to_end) {
fhold; fret;
}
}
<: any <: any
>{ fhold; fret; } >{ fhold; fret; }
; ;
@ -1604,7 +1599,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size,
const char *p = buf; const char *p = buf;
const char *pe = buf + size; const char *pe = buf + size;
const char *eof = pe; const char *eof = &eof_ch;
parser->handle = handle; parser->handle = handle;
@ -1640,8 +1635,7 @@ bool end(void *closure, const void *hd) {
UPB_UNUSED(json_en_value_machine); UPB_UNUSED(json_en_value_machine);
UPB_UNUSED(json_en_main); UPB_UNUSED(json_en_main);
parser->ready_to_end = true; parse(parser, hd, &eof_ch, 0, NULL);
parse(parser, hd, NULL, 0, NULL);
return parser->current_state >= %%{ write first_final; }%%; return parser->current_state >= %%{ write first_final; }%%;
} }
@ -1764,7 +1758,6 @@ upb_json_parser *upb_json_parser_create(upb_env *env,
set_name_table(p, p->top); set_name_table(p, p->top);
p->ignore_json_unknown = ignore_json_unknown; p->ignore_json_unknown = ignore_json_unknown;
p->ready_to_end = false;
/* If this fails, uncomment and increase the value in parser.h. */ /* If this fails, uncomment and increase the value in parser.h. */
/* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */ /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */

Loading…
Cancel
Save