Skip to content

Commit

Permalink
Disallow trailing commas.
Browse files Browse the repository at this point in the history
Clean up EXPECTED messages.
  • Loading branch information
medgar123 committed Oct 26, 2011
1 parent e028db5 commit acb4dec
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
87 changes: 47 additions & 40 deletions parse.sh
Expand Up @@ -19,51 +19,58 @@ 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`
}

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"`
}

Expand All @@ -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"
Expand All @@ -85,6 +92,6 @@ parse () {
read -r token
case "$token" in
'') ;;
*) throw "EXPECTED EOF, GOT $token" ;;
*) throw "EXPECTED EOF GOT $token" ;;
esac
}
1 change: 1 addition & 0 deletions test/invalid/trailing_array_comma.json
@@ -0,0 +1 @@
[1,2,3,]
1 change: 1 addition & 0 deletions test/invalid/trailing_object_comma.json
@@ -0,0 +1 @@
{"a":1,"b":2,"c":3,}

0 comments on commit acb4dec

Please sign in to comment.