]> gitweb.factorcode.org Git - factor.git/commitdiff
Refactored json.reader, added a couple more unit tests, no more rationals
authorPeter Burns <peter@metaweb.com>
Sat, 8 Nov 2008 04:00:19 +0000 (20:00 -0800)
committerPeter Burns <peter@metaweb.com>
Sat, 8 Nov 2008 04:00:19 +0000 (20:00 -0800)
basis/json/reader/reader-tests.factor
basis/json/reader/reader.factor

index 995ae0e0b8b8027014d4c1303a5585f62330e3b1..84f22f9282c0b88c7cd0fca749b04c8c2b468af9 100644 (file)
@@ -8,12 +8,14 @@ IN: json.reader.tests
 { 102 } [ "102" json> ] unit-test
 { -102 } [ "-102" json> ] unit-test
 { 102 } [ "+102" json> ] unit-test
+{ 1000.0 } [ "1.0e3" json> ] unit-test
+{ 1000.0 } [ "10e2" json> ] unit-test
 { 102.0 } [ "102.0" json> ] unit-test
 { 102.5 } [ "102.5" json> ] unit-test
 { 102.5 } [ "102.50" json> ] unit-test
 { -10250.0 } [ "-102.5e2" json> ] unit-test
 { -10250.0 } [ "-102.5E+2" json> ] unit-test
-{ 10+1/4 } [ "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
 
@@ -22,7 +24,9 @@ IN: json.reader.tests
 { 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
 
+{ { } } [ "[]" json> ] unit-test 
 { { 1 "two" 3.0 } } [ <" [1, "two", 3.0] "> json> ] unit-test
+{ H{ } } [ "{}" json> ] unit-test
 { H{ { "US$" 1.0 } { "EU€" 1.5 } } } [ <" { "US$":1.00, "EU\u20AC":1.50 } "> json> ] unit-test
 { H{
     { "fib" { 1 1 2 3 5 8 H{ { "etc" "etc" } } } }
index 0feed3750d7e5651a9abc349ddf5f1bdc33a2285..9b50a6bf38001e8998175ada14615ba74c8a499d 100644 (file)
@@ -1,55 +1,53 @@
 ! Copyright (C) 2008 Peter Burns.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: kernel peg peg.ebnf math.parser strings math math.functions sequences
-       arrays vectors hashtables ;
+USING: kernel peg peg.ebnf math.parser math.private strings math math.functions sequences
+       arrays vectors hashtables prettyprint ;
 IN: json.reader
 
-! Grammar for JSON from RFC 4627
-
 SINGLETON: json-null
 
+! Grammar for JSON from RFC 4627
 EBNF: json>
 
 ws = (" " | "\r" | "\t" | "\n")*
 
-hex = [0-9a-fA-F]
+true = "true" => [[ t ]]
+false = "false" => [[ f ]]
+null = "null" => [[ json-null ]]
 
-char = '\\"'  [[ drop CHAR: "  ]]
-     | "\\\\" [[ drop CHAR: \  ]]
-     | "\\/"  [[ drop CHAR: /  ]]
-     | "\\b"  [[ drop 8        ]]
-     | "\\f"  [[ drop 12       ]]
-     | "\\n"  [[ drop CHAR: \n ]]
-     | "\\r"  [[ drop CHAR: \r ]]
-     | "\\t"  [[ drop CHAR: \t ]]
+hex = [0-9a-fA-F]
+char = '\\"'  [[ CHAR: "  ]]
+     | "\\\\" [[ CHAR: \  ]]
+     | "\\/"  [[ CHAR: /  ]]
+     | "\\b"  [[ 8        ]]
+     | "\\f"  [[ 12       ]]
+     | "\\n"  [[ CHAR: \n ]]
+     | "\\r"  [[ CHAR: \r ]]
+     | "\\t"  [[ CHAR: \t ]]
      | "\\u" (hex hex hex hex) [[ hex> ]] => [[ 1 swap nth ]]
      | [^"\]
-
 string = '"' char*:cs '"' => [[ cs >string ]]
 
-number = base:base exp?:exp            => [[ base exp [ exp * ] when ]]
-base   = sign?:s float:f               => [[ f s "-" = [ neg ] when ]]
-float  = digits:int ("." digits)?:frac => [[ int frac [ frac concat append ] when string>number ]]
-digits = [0-9]+                        => [[ >string ]]
-
-exp  = ("e" | "E") (sign)?:s digits:ex => [[ ex string>number s "-" = [ neg ] when 10 swap ^ ]]
-sign = "-" | "+"
-
+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 ]]
 
-array = "[" elements*:vec "]" => [[ 0 vec nth <reversed> >array ]]
-elements = value:head ("," elements)?:tail => [[ head tail [ 1 tail nth ?push ] [ f ?push ] if ]]
+elements = value ("," value)* => [[ first2 [ second ] map swap prefix >array ]]
+array = "[" elements?:arr "]" => [[ arr { } or ]]
 
-object = "{" (members)*:assoc "}" => [[ 0 assoc nth >hashtable ]]
-members = pair:head ("," members)?:tail => [[ head tail [ 1 tail nth ?push ] [ f ?push ] if ]]
 pair = ws string:key ws ":" value:val => [[ { key val } ]]
+members = pair ("," pair)* => [[ first2 [ second ] map swap prefix >hashtable ]]
+object = "{" (members)?:hash "}" => [[ hash H{ } or ]]
 
-val = string
+val = true
+    | false
+    | null
+    | string
     | number
-    | object
     | array
-    | "true"      [[ drop t ]]
-    | "false"     [[ drop f ]]
-    | "null"      [[ drop json-null ]]
+    | object
 
 value = ws val:v ws => [[ v ]]