Add a hand parser. Improves speed from 23 seconds to 0.03 seconds when parsing a 123Kb string.
parent
4cead52ba6
commit
7922628abb
|
@ -1 +1,3 @@
|
||||||
Chris Double
|
Chris Double
|
||||||
|
Peter Burns
|
||||||
|
Philipp Winkler
|
||||||
|
|
|
@ -19,6 +19,8 @@ IN: json.reader.tests
|
||||||
{ 10.25 } [ "1025e-2" json> ] unit-test
|
{ 10.25 } [ "1025e-2" json> ] unit-test
|
||||||
{ 0.125 } [ "0.125" json> ] unit-test
|
{ 0.125 } [ "0.125" json> ] unit-test
|
||||||
{ -0.125 } [ "-0.125" json> ] unit-test
|
{ -0.125 } [ "-0.125" json> ] unit-test
|
||||||
|
{ -0.00125 } [ "-0.125e-2" json> ] unit-test
|
||||||
|
{ -012.5 } [ "-0.125e+2" json> ] unit-test
|
||||||
|
|
||||||
! not widely supported by javascript, but allowed in the grammar, and a nice
|
! not widely supported by javascript, but allowed in the grammar, and a nice
|
||||||
! feature to get
|
! feature to get
|
||||||
|
|
|
@ -1,61 +1,97 @@
|
||||||
! Copyright (C) 2008 Peter Burns.
|
! Copyright (C) 2008 Peter Burns, 2009 Philipp Winkler
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: kernel peg peg.ebnf math.parser math.parser.private strings math
|
USING: arrays assocs combinators io io.streams.string json
|
||||||
math.functions sequences arrays vectors hashtables assocs
|
kernel math math.parser math.parser.private sequences strings ;
|
||||||
prettyprint json ;
|
|
||||||
IN: json.reader
|
IN: json.reader
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
: value ( char -- num char )
|
||||||
|
1string " \t\r\n,:}]" read-until
|
||||||
|
[
|
||||||
|
append
|
||||||
|
[ string>float ]
|
||||||
|
[ [ "eE." index ] any? [ >integer ] unless ] bi
|
||||||
|
] dip ;
|
||||||
|
|
||||||
: grammar-list>vector ( seq -- vec ) first2 values swap prefix ;
|
DEFER: j-string
|
||||||
|
|
||||||
|
: convert-string ( str -- str )
|
||||||
|
read1
|
||||||
|
{
|
||||||
|
{ CHAR: b [ 8 ] }
|
||||||
|
{ CHAR: f [ 12 ] }
|
||||||
|
{ CHAR: n [ CHAR: \n ] }
|
||||||
|
{ CHAR: r [ CHAR: \r ] }
|
||||||
|
{ CHAR: t [ CHAR: \t ] }
|
||||||
|
{ CHAR: u [ 4 read hex> ] }
|
||||||
|
[ ]
|
||||||
|
} case
|
||||||
|
dup
|
||||||
|
[ 1string append j-string append ]
|
||||||
|
[ drop ] if ;
|
||||||
|
|
||||||
|
: j-string ( -- str )
|
||||||
|
"\\\"" read-until CHAR: \" =
|
||||||
|
[ convert-string ] unless ;
|
||||||
|
|
||||||
|
: second-last ( seq -- second-last )
|
||||||
|
[ length 2 - ] keep nth ; inline
|
||||||
|
|
||||||
! Grammar for JSON from RFC 4627
|
: third-last ( seq -- third-last )
|
||||||
EBNF: (json>)
|
[ length 3 - ] keep nth ; inline
|
||||||
|
|
||||||
|
: last2 ( seq -- second-last last )
|
||||||
|
[ second-last ] [ last ] bi ; inline
|
||||||
|
|
||||||
ws = (" " | "\r" | "\t" | "\n")*
|
: last3 ( seq -- third-last second-last last )
|
||||||
|
[ third-last ] [ last2 ] bi ; inline
|
||||||
|
|
||||||
true = "true" => [[ t ]]
|
: v-over-push ( vec -- vec' )
|
||||||
false = "false" => [[ f ]]
|
dup length 2 >=
|
||||||
null = "null" => [[ json-null ]]
|
[
|
||||||
|
dup
|
||||||
|
[ pop ]
|
||||||
|
[ last ] bi push
|
||||||
|
] when ;
|
||||||
|
|
||||||
hex = [0-9a-fA-F]
|
: v-pick-push ( vec -- vec' )
|
||||||
char = '\\"' [[ CHAR: " ]]
|
dup length 3 >=
|
||||||
| "\\\\" [[ CHAR: \ ]]
|
[
|
||||||
| "\\/" [[ CHAR: / ]]
|
dup
|
||||||
| "\\b" [[ 8 ]]
|
[ pop ]
|
||||||
| "\\f" [[ 12 ]]
|
[ second-last ] bi push
|
||||||
| "\\n" [[ CHAR: \n ]]
|
] when ;
|
||||||
| "\\r" [[ CHAR: \r ]]
|
|
||||||
| "\\t" [[ CHAR: \t ]]
|
|
||||||
| "\\u" (hex hex hex hex) [[ hex> ]] => [[ second ]]
|
|
||||||
| [^"\]
|
|
||||||
string = '"' char*:cs '"' => [[ cs >string ]]
|
|
||||||
|
|
||||||
sign = ("-" | "+")? => [[ "-" = "-" "" ? ]]
|
: (close-hash) ( accum -- accum' )
|
||||||
digits = [0-9]+ => [[ >string ]]
|
dup length 3 >= [ v-over-push ] when
|
||||||
decimal = "." digits => [[ concat ]]
|
dup dup [ pop ] dip pop swap
|
||||||
exp = ("e" | "E") sign digits => [[ concat ]]
|
zip H{ } assoc-clone-like over push ;
|
||||||
number = sign digits decimal? exp? => [[ dup concat swap fourth [ string>float ] [ string>number ] if ]]
|
|
||||||
|
: scan ( accum char -- accum )
|
||||||
elements = value ("," value)* => [[ grammar-list>vector ]]
|
[
|
||||||
array = "[" elements?:arr "]" => [[ arr >array ]]
|
{
|
||||||
|
{ CHAR: \" [ j-string over push ] }
|
||||||
pair = ws string:key ws ":" value:val => [[ { key val } ]]
|
{ CHAR: [ [ V{ } clone over push ] }
|
||||||
members = pair ("," pair)* => [[ grammar-list>vector ]]
|
{ CHAR: , [ v-over-push ] }
|
||||||
object = "{" members?:hash "}" => [[ hash >hashtable ]]
|
{ CHAR: ] [ v-over-push dup pop >array over push ] }
|
||||||
|
{ CHAR: { [ 2 [ V{ } clone over push ] times ] }
|
||||||
val = true
|
{ CHAR: : [ v-pick-push ] }
|
||||||
| false
|
{ CHAR: } [ (close-hash) ] }
|
||||||
| null
|
{ CHAR: \u000020 [ ] }
|
||||||
| string
|
{ CHAR: \t [ ] }
|
||||||
| number
|
{ CHAR: \r [ ] }
|
||||||
| array
|
{ CHAR: \n [ ] }
|
||||||
| object
|
{ CHAR: t [ 3 read drop t over push ] }
|
||||||
|
{ CHAR: f [ 4 read drop f over push ] }
|
||||||
value = ws val:v ws => [[ v ]]
|
{ CHAR: n [ 3 read drop json-null over push ] }
|
||||||
|
[ value [ over push ] dip [ scan ] when* ]
|
||||||
;EBNF
|
} case
|
||||||
|
] when* ;
|
||||||
|
|
||||||
|
: (json-parser>) ( string -- object )
|
||||||
|
[ V{ } clone [ read1 dup ] [ scan ] while drop first ] with-string-reader ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
: json> ( string -- object ) (json>) ;
|
: json> ( string -- object )
|
||||||
|
(json-parser>) ;
|
Loading…
Reference in New Issue