factor/basis/json/reader/reader.factor

54 lines
1.5 KiB
Factor
Raw Normal View History

2008-11-07 00:45:24 -05:00
! Copyright (C) 2008 Peter Burns.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: kernel peg peg.ebnf math.parser math.private strings math math.functions sequences
arrays vectors hashtables prettyprint ;
2007-09-20 18:09:08 -04:00
IN: json.reader
SINGLETON: json-null
! Grammar for JSON from RFC 4627
2008-11-07 00:45:24 -05:00
EBNF: json>
2007-09-20 18:09:08 -04:00
2008-11-07 00:45:24 -05:00
ws = (" " | "\r" | "\t" | "\n")*
2007-09-20 18:09:08 -04:00
true = "true" => [[ t ]]
false = "false" => [[ f ]]
null = "null" => [[ json-null ]]
2007-09-20 18:09:08 -04:00
hex = [0-9a-fA-F]
char = '\\"' [[ CHAR: " ]]
| "\\\\" [[ CHAR: \ ]]
| "\\/" [[ CHAR: / ]]
| "\\b" [[ 8 ]]
| "\\f" [[ 12 ]]
| "\\n" [[ CHAR: \n ]]
| "\\r" [[ CHAR: \r ]]
| "\\t" [[ CHAR: \t ]]
2008-11-07 00:45:24 -05:00
| "\\u" (hex hex hex hex) [[ hex> ]] => [[ 1 swap nth ]]
| [^"\]
string = '"' char*:cs '"' => [[ cs >string ]]
2007-09-20 18:09:08 -04:00
sign = ("-" | "+")? => [[ "-" = [ "-" ] [ "" ] if ]]
digits = [0-9]+ => [[ >string ]]
decimal = "." digits => [[ concat ]]
exp = ("e" | "E") sign digits => [[ concat ]]
number = sign digits decimal? exp? => [[ dup concat swap fourth [ string>float ] [ string>number ] if ]]
2007-09-20 18:09:08 -04:00
elements = value ("," value)* => [[ first2 [ second ] map swap prefix >array ]]
array = "[" elements?:arr "]" => [[ arr { } or ]]
2007-09-20 18:09:08 -04:00
2008-11-07 00:45:24 -05:00
pair = ws string:key ws ":" value:val => [[ { key val } ]]
members = pair ("," pair)* => [[ first2 [ second ] map swap prefix >hashtable ]]
object = "{" (members)?:hash "}" => [[ hash H{ } or ]]
2007-09-20 18:09:08 -04:00
val = true
| false
| null
| string
2008-11-07 00:45:24 -05:00
| number
| array
| object
2007-09-20 18:09:08 -04:00
2008-11-07 00:45:24 -05:00
value = ws val:v ws => [[ v ]]
2007-09-20 18:09:08 -04:00
2008-11-07 00:45:24 -05:00
;EBNF