From a9835d6c37d2a3c762c4620294d0b2a13d543ccb Mon Sep 17 00:00:00 2001
From: Kaz Kylheku <kaz@kylheku.com>
Date: Sun, 20 Jun 2021 09:07:45 -0700
Subject: 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.
---
 tests/012/parse.tl | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

(limited to 'tests/012')

diff --git a/tests/012/parse.tl b/tests/012/parse.tl
index 2bce20dc..6d091b00 100644
--- a/tests/012/parse.tl
+++ b/tests/012/parse.tl
@@ -12,3 +12,55 @@
 
   (test (regex-parse (str-buf #b'F3FF'))
         (compound #\xDCF3 #\xDCFF)))
+
+(mtest
+  (read "0") 0
+  (read "0x") 0x
+  (read "a") a
+  (read "abc") abc
+  (read "abc.def") abc.def
+  (read "(1 2 3)") (1 2 3)
+  (read "#;(1 2 3) 4") 4
+  (read "#;(1 2 3) #; a.b 4") 4
+  (read "0 ") 0
+  (read "0x ") 0x
+  (read "a ") a
+  (read "abc ") abc
+  (read "abc.def ") abc.def
+  (read "(1 2 3) ") (1 2 3)
+  (read "#;(1 2 3) 4 ") 4
+  (read "#;(1 2 3) #; a.b 4 ") 4
+  (read "0,") :error
+  (read "0x,") :error
+  (read "a,") :error
+  (read "abc,") :error
+  (read "abc.def,") :error
+  (read "(1 2 3),") :error
+  (read "#;(1 2 3) 4,") :error
+  (read "#;(1 2 3) #; a.b 4,") :error)
+
+(mtest
+  (iread "0") 0
+  (iread "0x") 0x
+  (iread "a") a
+  (iread "abc") abc
+  (iread "abc.def") abc
+  (iread "(1 2 3)") (1 2 3)
+  (iread "#;(1 2 3) 4") 4
+  (iread "#;(1 2 3) #; a.b 4") .b
+  (iread "0 ") 0
+  (iread "0x ") 0x
+  (iread "a ") a
+  (iread "abc ") abc
+  (iread "abc.def ") abc
+  (iread "(1 2 3) ") (1 2 3)
+  (iread "#;(1 2 3) 4 ") 4
+  (iread "#;(1 2 3) #; a.b 4 ") .b
+  (iread "0,") 0
+  (iread "0x,") 0x
+  (iread "a,") a
+  (iread "abc,") abc
+  (iread "abc.def,") abc
+  (iread "(1 2 3),") (1 2 3)
+  (iread "#;(1 2 3) 4,") 4
+  (iread "#;(1 2 3) #; a.b 4,") .b)
-- 
cgit v1.2.3