1 ! Copyright (C) 2008 Peter Burns.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel peg peg.ebnf math.parser math.private strings math
4 math.functions sequences arrays vectors hashtables assocs
10 : grammar-list>vector ( seq -- vec ) first2 values swap prefix ;
12 ! Grammar for JSON from RFC 4627
15 ws = (" " | "\r" | "\t" | "\n")*
17 true = "true" => [[ t ]]
18 false = "false" => [[ f ]]
19 null = "null" => [[ json-null ]]
22 char = '\\"' [[ CHAR: " ]]
23 | "\\\\" [[ CHAR: \ ]]
27 | "\\n" [[ CHAR: \n ]]
28 | "\\r" [[ CHAR: \r ]]
29 | "\\t" [[ CHAR: \t ]]
30 | "\\u" (hex hex hex hex) [[ hex> ]] => [[ second ]]
32 string = '"' char*:cs '"' => [[ cs >string ]]
34 sign = ("-" | "+")? => [[ "-" = "-" "" ? ]]
35 digits = [0-9]+ => [[ >string ]]
36 decimal = "." digits => [[ concat ]]
37 exp = ("e" | "E") sign digits => [[ concat ]]
38 number = sign digits decimal? exp? => [[ dup concat swap fourth [ string>float ] [ string>number ] if ]]
40 elements = value ("," value)* => [[ grammar-list>vector ]]
41 array = "[" elements?:arr "]" => [[ arr >array ]]
43 pair = ws string:key ws ":" value:val => [[ { key val } ]]
44 members = pair ("," pair)* => [[ grammar-list>vector ]]
45 object = "{" members?:hash "}" => [[ hash >hashtable ]]
55 value = ws val:v ws => [[ v ]]
61 : json> ( string -- object ) (json>) ;