-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* split up the tokenizer regex for readability * ignore inter-token whitespace * allow backslash-escapes in strings * disallow control characters in strings * clean up number regex and don't allow . (or the empty string) * fall-through token-matching case is . leaving the parser to validate it. Parser changes: * disallow non-string keys in objects * detect unrecognized tokens, e.g. @ A $ etc. * consistent quoting on case patterns * use ${token:-EOF} in error messages fixed wrong tests (and removed the wrong-test-generator) added new tests. fix test/valid-test.sh to count failures like test/invalid-test.sh removed unnecessary outlog/errlog files (and add to .gitignore)
- Loading branch information
Showing
31 changed files
with
97 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,3 +1,2 @@ | |||
node_modules | test/errlog | ||
node_modules/* | test/outlog | ||
npm_debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,93 +1,90 @@ | |||
|
|
||
throw () { | throw () { | ||
echo $* >&2 | echo "$*" >&2 | ||
exit 1 | exit 1 | ||
} | } | ||
|
|
||
tokenize () { | tokenize () { | ||
egrep -ao '[]|[{}]|:|,|("((\\")|[^"])*")|:|(\-?[0-9]*\.?([0-9]*)?(e?\-?([0-9]*))?)|null|true|false' --color=never | local ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})' | ||
local CHAR='[^[:cntrl:]"\\]' | |||
local STRING="\"$CHAR*($ESCAPE$CHAR*)*\"" | |||
local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?' | |||
local KEYWORD='null|false|true' | |||
local SPACE='[[:space:]]+' | |||
egrep -ao "$STRING|$NUMBER|$KEYWORD|$SPACE|." --color=never | | |||
egrep -v "^$SPACE$" # eat whitespace | |||
} | } | ||
|
|
||
parse_array () { | parse_array () { | ||
local index=0 | local index=0 | ||
local ary='' | local ary='' | ||
read token | read -r token | ||
while true; | while true; | ||
do | do | ||
key=$index | |||
case "$token" in | case "$token" in | ||
']') break ;; | ']') break ;; | ||
*) | |||
parse_value "$1" "$index" | |||
let index=$index+1 | |||
ary="$ary""$value" | |||
read token | |||
case "$token" in | |||
']') break ;; | |||
',') ary="$ary", ;; | |||
*) | |||
if [ "_$token" = _ ]; then token=EOF; fi | |||
throw "EXPECTED ] or , GOT $token" | |||
;; | |||
esac | |||
read token | |||
;; | |||
esac | 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 | done | ||
value=`printf '[%s]' $ary` | value=`printf '[%s]' $ary` | ||
} | } | ||
|
|
||
parse_object () { | parse_object () { | ||
local go=true | local key | ||
local obj='' | local obj='' | ||
local EXPECT_COMMA=0 | read -r token | ||
local EXPECT_COLON=0 | while : | ||
read token | |||
while [ "$go" = true ]; | |||
do | do | ||
case "$token" in | case "$token" in | ||
'}') break ;; | '}') break ;; | ||
*) | '"'*'"') key=$token ;; | ||
|
*) throw "EXPECTED STRING, GOT ${token:-EOF}" ;; | ||
key=$token | esac | ||
read colon | read -r token | ||
if [ "$colon" != ':' ]; then throw "EXPECTED COLON, GOT $colon"; fi | case "$token" in | ||
if [ "_$key" = _ ]; then throw "NULL KEY"; fi | ':') ;; | ||
read token | *) throw "EXPECTED COLON, GOT ${token:-EOF}" ;; | ||
parse_value "$1" "$key" | esac | ||
obj="$obj$key:$value" | read -r token | ||
|
parse_value "$1" "$key" | ||
read token | obj="$obj$key:$value" | ||
case "$token" in | read -r token | ||
'}') break;; | case "$token" in | ||
,) obj="$obj,"; read token ;; | '}') break;; | ||
*) | ',') obj="$obj,"; read -r token ;; | ||
if [ "_$token" = _ ]; then token=EOF; fi | *) throw "EXPECTED , or }, but got ${token:-EOF}" ;; | ||
throw "EXPECTED , or }, but got $token" | |||
;; | |||
esac | |||
;; | |||
esac | esac | ||
done | done | ||
value=`printf '{%s}' "$obj"` | value=`printf '{%s}' "$obj"` | ||
} | } | ||
|
|
||
parse_value () { | parse_value () { | ||
local jpath | local jpath="${1:+$1,}$2" | ||
|
|||
if [ "x$1" = "x" ]; then jpath="$2"; else jpath="$1,$2"; fi | |||
|
|||
case "$token" in | case "$token" in | ||
{) parse_object "$jpath" ;; | '{') parse_object "$jpath" ;; | ||
[) parse_array "$jpath" ;; | '[') parse_array "$jpath" ;; | ||
','|'}'|']') throw "EXPECTED value, GOT $token" ;; | # At this point, the only valid single-character tokens are digits. | ||
*) value=$token | ''|[^0-9]) throw "EXPECTED value, GOT ${token:-EOF}" ;; | ||
;; | *) value=$token ;; | ||
esac | esac | ||
printf "[%s]\t%s\n" "$jpath" "$value" | printf "[%s]\t%s\n" "$jpath" "$value" | ||
} | } | ||
|
|
||
parse () { | parse () { | ||
read token | read -r token | ||
parse_value | parse_value | ||
read -r token | |||
case "$token" in | |||
'') ;; | |||
*) throw "EXPECTED EOF, GOT $token" ;; | |||
esac | |||
} | } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
"hello\u20world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
bareword |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
{[: "bad"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
{ | |||
"hello": : | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
"ab" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
. |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
{false: "bad"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
{null: "bad"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
{5: "bad"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
[1,2,3]' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
{true: "bad"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
"Hello world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
{@: "bad"} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
"hello\tworld" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
[] "hello\tworld" |