summaryrefslogtreecommitdiffstats
path: root/parser.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2021-06-20 09:07:45 -0700
committerKaz Kylheku <kaz@kylheku.com>2021-06-20 09:07:45 -0700
commita9835d6c37d2a3c762c4620294d0b2a13d543ccb (patch)
tree7089aff7f2d274ed013115f4152ac311f79c21ea /parser.c
parent7c34d91f30e5a77bc12b811f6c836cea71905529 (diff)
downloadtxr-a9835d6c37d2a3c762c4620294d0b2a13d543ccb.tar.gz
txr-a9835d6c37d2a3c762c4620294d0b2a13d543ccb.tar.bz2
txr-a9835d6c37d2a3c762c4620294d0b2a13d543ccb.zip
read/get-json: reject trailing junk in string input.
* parser.c (lisp_parse_impl): If parsing from string, check for trailing junk and diagnose. JSON parsing doesn't use lookahead because it doesn't have a.b syntax, so the recent_tok gives the last token that actually went into the syntax, and not a lookahead token. So in the case of JSON, we call yylex to see if there is any trailing token. * tests/010/json.tl: Extend get-json tests to more kinds of objects, and then replicate with trailing whitespace and trailing junk to provide coverage for these cases. * tests/012/parse.t: Slew of new read tests and iread also. * txr.1: Documented.
Diffstat (limited to 'parser.c')
-rw-r--r--parser.c18
1 files changed, 16 insertions, 2 deletions
diff --git a/parser.c b/parser.c
index bc01c2bf..fd274ebb 100644
--- a/parser.c
+++ b/parser.c
@@ -636,11 +636,12 @@ static val lisp_parse_impl(val self, enum prime_parser prime,
{
uses_or2;
val source = default_null_arg(source_in);
- val input_stream = if3(stringp(source),
+ val str = stringp(source);
+ val input_stream = if3(str,
make_string_byte_input_stream(source),
or2(source, std_input));
val name = or2(default_null_arg(name_in),
- if3(stringp(source),
+ if3(str,
lit("string"),
stream_get_prop(input_stream, name_k)));
val parser = ensure_parser(input_stream, name);
@@ -676,6 +677,19 @@ static val lisp_parse_impl(val self, enum prime_parser prime,
break;
}
+ if (str) {
+ int junk = 0;
+ if (prime == prime_json) {
+ yystype yyl;
+ junk = yylex(&yyl, pi->scanner);
+ } else {
+ junk = pi->recent_tok.yy_char;
+ }
+
+ if (junk)
+ yyerrorf(pi->scanner, lit("trailing material after expression"), nao);
+ }
+
parsed = t;
uw_unwind {