A bit more refactoring and testing of json.reader
							parent
							
								
									bca998bba5
								
							
						
					
					
						commit
						64cdabf57a
					
				| 
						 | 
					@ -1 +1,2 @@
 | 
				
			||||||
Chris Double
 | 
					Chris Double
 | 
				
			||||||
 | 
					Peter Burns
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
USING: arrays json.reader kernel multiline strings tools.test ;
 | 
					USING: arrays json.reader kernel multiline strings tools.test hashtables ;
 | 
				
			||||||
IN: json.reader.tests
 | 
					IN: json.reader.tests
 | 
				
			||||||
 | 
					
 | 
				
			||||||
{ f } [ "false" json> ] unit-test
 | 
					{ f } [ "false" json> ] unit-test
 | 
				
			||||||
| 
						 | 
					@ -19,14 +19,24 @@ IN: json.reader.tests
 | 
				
			||||||
{ 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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					! not widely supported by javascript, but allowed in the grammar, and a nice
 | 
				
			||||||
 | 
					! feature to get
 | 
				
			||||||
 | 
					{ -0.0 } [ "-0.0" json> ] unit-test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
{ " fuzzy  pickles " } [ <" " fuzzy  pickles " "> json> ] unit-test
 | 
					{ " fuzzy  pickles " } [ <" " fuzzy  pickles " "> json> ] unit-test
 | 
				
			||||||
{ "while 1:\n\tpass" } [ <" "while 1:\n\tpass" "> json> ] unit-test
 | 
					{ "while 1:\n\tpass" } [ <" "while 1:\n\tpass" "> json> ] unit-test
 | 
				
			||||||
 | 
					! unicode is allowed in json
 | 
				
			||||||
 | 
					{ "ß∂¬ƒ˚∆" } [ <" "ß∂¬ƒ˚∆""> json> ] unit-test
 | 
				
			||||||
{ 8 9 10 12 13 34 47 92 } >string 1array [ <" "\b\t\n\f\r\"\/\\" "> json> ] unit-test
 | 
					{ 8 9 10 12 13 34 47 92 } >string 1array [ <" "\b\t\n\f\r\"\/\\" "> json> ] unit-test
 | 
				
			||||||
{ HEX: abcd } >string 1array [ <" "\uaBCd" "> json> ] unit-test
 | 
					{ HEX: abcd } >string 1array [ <" "\uaBCd" "> json> ] unit-test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
{ { } } [ "[]" json> ] unit-test 
 | 
					{ { } } [ "[]" json> ] unit-test 
 | 
				
			||||||
{ { 1 "two" 3.0 } } [ <" [1, "two", 3.0] "> json> ] unit-test
 | 
					{ { 1 "two" 3.0 } } [ <" [1, "two", 3.0] "> json> ] unit-test
 | 
				
			||||||
{ H{ } } [ "{}" json> ] unit-test
 | 
					{ H{ } } [ "{}" json> ] unit-test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					! the returned hashtable should be different every time
 | 
				
			||||||
 | 
					{ H{ } } [ "key" "value" "{}" json> ?set-at "{}" json> swap drop ] unit-test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
{ H{ { "US$" 1.0 } { "EU€" 1.5 } } } [ <" { "US$":1.00, "EU\u20AC":1.50 } "> json> ] unit-test
 | 
					{ H{ { "US$" 1.0 } { "EU€" 1.5 } } } [ <" { "US$":1.00, "EU\u20AC":1.50 } "> json> ] unit-test
 | 
				
			||||||
{ H{
 | 
					{ H{
 | 
				
			||||||
    { "fib" { 1 1 2 3 5 8 H{ { "etc" "etc" } } } }
 | 
					    { "fib" { 1 1 2 3 5 8 H{ { "etc" "etc" } } } }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,11 +1,14 @@
 | 
				
			||||||
! Copyright (C) 2008 Peter Burns.
 | 
					! Copyright (C) 2008 Peter Burns.
 | 
				
			||||||
! 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.private strings math math.functions sequences
 | 
					USING: kernel peg peg.ebnf math.parser math.private strings math math.functions sequences
 | 
				
			||||||
       arrays vectors hashtables prettyprint ;
 | 
					       arrays vectors hashtables assocs prettyprint ;
 | 
				
			||||||
IN: json.reader
 | 
					IN: json.reader
 | 
				
			||||||
 | 
					
 | 
				
			||||||
SINGLETON: json-null
 | 
					SINGLETON: json-null
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					: grammar-list>vector ( seq -- vec ) first2 values swap prefix ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
! Grammar for JSON from RFC 4627
 | 
					! Grammar for JSON from RFC 4627
 | 
				
			||||||
EBNF: json>
 | 
					EBNF: json>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,22 +27,22 @@ char = '\\"'  [[ CHAR: "  ]]
 | 
				
			||||||
     | "\\n"  [[ CHAR: \n ]]
 | 
					     | "\\n"  [[ CHAR: \n ]]
 | 
				
			||||||
     | "\\r"  [[ CHAR: \r ]]
 | 
					     | "\\r"  [[ CHAR: \r ]]
 | 
				
			||||||
     | "\\t"  [[ CHAR: \t ]]
 | 
					     | "\\t"  [[ CHAR: \t ]]
 | 
				
			||||||
     | "\\u" (hex hex hex hex) [[ hex> ]] => [[ 1 swap nth ]]
 | 
					     | "\\u" (hex hex hex hex) [[ hex> ]] => [[ second ]]
 | 
				
			||||||
     | [^"\]
 | 
					     | [^"\]
 | 
				
			||||||
string = '"' char*:cs '"' => [[ cs >string ]]
 | 
					string = '"' char*:cs '"' => [[ cs >string ]]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
sign = ("-" | "+")? => [[ "-" = [ "-" ] [ "" ] if ]]
 | 
					sign = ("-" | "+")? => [[ "-" = "-" "" ? ]]
 | 
				
			||||||
digits = [0-9]+     => [[ >string ]]
 | 
					digits = [0-9]+     => [[ >string ]]
 | 
				
			||||||
decimal = "." digits  => [[ concat ]]
 | 
					decimal = "." digits  => [[ concat ]]
 | 
				
			||||||
exp = ("e" | "E") sign digits => [[ concat ]]
 | 
					exp = ("e" | "E") sign digits => [[ concat ]]
 | 
				
			||||||
number = sign digits decimal? exp? => [[ dup concat swap fourth [ string>float ] [ string>number ] if ]]
 | 
					number = sign digits decimal? exp? => [[ dup concat swap fourth [ string>float ] [ string>number ] if ]]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
elements = value ("," value)* => [[ first2 [ second ] map swap prefix >array ]]
 | 
					elements = value ("," value)* => [[ grammar-list>vector ]]
 | 
				
			||||||
array = "[" elements?:arr "]" => [[ arr { } or ]]
 | 
					array = "[" elements?:arr "]" => [[ arr >array ]]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pair = ws string:key ws ":" value:val => [[ { key val } ]]
 | 
					pair = ws string:key ws ":" value:val => [[ { key val } ]]
 | 
				
			||||||
members = pair ("," pair)* => [[ first2 [ second ] map swap prefix >hashtable ]]
 | 
					members = pair ("," pair)* => [[ grammar-list>vector ]]
 | 
				
			||||||
object = "{" (members)?:hash "}" => [[ hash H{ } or ]]
 | 
					object = "{" members?:hash "}" => [[ hash >hashtable ]]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
val = true
 | 
					val = true
 | 
				
			||||||
    | false
 | 
					    | false
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue