From acb4dec96750b0771c63f7b129e0108815e2d724 Mon Sep 17 00:00:00 2001 From: Mark Edgar Date: Wed, 26 Oct 2011 09:43:24 +0200 Subject: [PATCH] Disallow trailing commas. Clean up EXPECTED messages. --- parse.sh | 87 +++++++++++++------------ test/invalid/trailing_array_comma.json | 1 + test/invalid/trailing_object_comma.json | 1 + 3 files changed, 49 insertions(+), 40 deletions(-) create mode 100644 test/invalid/trailing_array_comma.json create mode 100644 test/invalid/trailing_object_comma.json diff --git a/parse.sh b/parse.sh index 661f25e..b469dc5 100644 --- a/parse.sh +++ b/parse.sh @@ -19,22 +19,24 @@ parse_array () { local index=0 local ary='' read -r token - while true; - do - case "$token" in - ']') break ;; - esac - parse_value "$1" "$index" - let index=$index+1 - ary="$ary""$value" - read -r token - case "$token" in - ']') break ;; - ',') ary="$ary", ;; - *) throw "EXPECTED ] or , GOT ${token:-EOF}" ;; - esac - read -r token - done + case "$token" in + ']') ;; + *) + while : + do + parse_value "$1" "$index" + let index=$index+1 + ary="$ary""$value" + read -r token + case "$token" in + ']') break ;; + ',') ary="$ary," ;; + *) throw "EXPECTED , or ] GOT ${token:-EOF}" ;; + esac + read -r token + done + ;; + esac value=`printf '[%s]' $ary` } @@ -42,28 +44,33 @@ parse_object () { local key local obj='' read -r token - while : - do - case "$token" in - '}') break ;; - '"'*'"') key=$token ;; - *) throw "EXPECTED STRING, GOT ${token:-EOF}" ;; - esac - read -r token - case "$token" in - ':') ;; - *) throw "EXPECTED COLON, GOT ${token:-EOF}" ;; - esac - read -r token - parse_value "$1" "$key" - obj="$obj$key:$value" - read -r token - case "$token" in - '}') break;; - ',') obj="$obj,"; read -r token ;; - *) throw "EXPECTED , or }, but got ${token:-EOF}" ;; - esac - done + case "$token" in + '}') ;; + *) + while : + do + case "$token" in + '"'*'"') key=$token ;; + *) throw "EXPECTED string GOT ${token:-EOF}" ;; + esac + read -r token + case "$token" in + ':') ;; + *) throw "EXPECTED : GOT ${token:-EOF}" ;; + esac + read -r token + parse_value "$1" "$key" + obj="$obj$key:$value" + read -r token + case "$token" in + '}') break ;; + ',') obj="$obj," ;; + *) throw "EXPECTED , or } GOT ${token:-EOF}" ;; + esac + read -r token + done + ;; + esac value=`printf '{%s}' "$obj"` } @@ -73,7 +80,7 @@ parse_value () { '{') parse_object "$jpath" ;; '[') parse_array "$jpath" ;; # At this point, the only valid single-character tokens are digits. - ''|[^0-9]) throw "EXPECTED value, GOT ${token:-EOF}" ;; + ''|[^0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;; *) value=$token ;; esac printf "[%s]\t%s\n" "$jpath" "$value" @@ -85,6 +92,6 @@ parse () { read -r token case "$token" in '') ;; - *) throw "EXPECTED EOF, GOT $token" ;; + *) throw "EXPECTED EOF GOT $token" ;; esac } diff --git a/test/invalid/trailing_array_comma.json b/test/invalid/trailing_array_comma.json new file mode 100644 index 0000000..e2ae501 --- /dev/null +++ b/test/invalid/trailing_array_comma.json @@ -0,0 +1 @@ +[1,2,3,] diff --git a/test/invalid/trailing_object_comma.json b/test/invalid/trailing_object_comma.json new file mode 100644 index 0000000..71d0f1b --- /dev/null +++ b/test/invalid/trailing_object_comma.json @@ -0,0 +1 @@ +{"a":1,"b":2,"c":3,}