Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Disallow trailing commas.

Clean up EXPECTED messages.
  • Loading branch information...
commit acb4dec96750b0771c63f7b129e0108815e2d724 1 parent e028db5
@medgar123 medgar123 authored
View
87 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
}
View
1  test/invalid/trailing_array_comma.json
@@ -0,0 +1 @@
+[1,2,3,]
View
1  test/invalid/trailing_object_comma.json
@@ -0,0 +1 @@
+{"a":1,"b":2,"c":3,}
Please sign in to comment.
Something went wrong with that request. Please try again.