Commits

Anonymous committed c20a77f

added parsing of floats

Comments (0)

Files changed (1)

   let extract result = implode (List.nth result 1) in
     parse_compose extract triple
 
+let parse_digit =
+  parse_any_char ['0';'1';'2';'3';'4';'5';'6';'7';'8';'9']
+
 let parse_int =
-  let parse_digit =
-    parse_any_char ['0';'1';'2';'3';'4';'5';'6';'7';'8';'9']
-  in
   parse_compose (compose int_of_string implode) (parse_plus parse_digit)
 
+let parse_float =
+  let dot = parse_compose listify (parse_char '.') in
+  parse_compose (compose float_of_string implode) (
+    parse_combine [parse_plus parse_digit; dot; parse_kleene_star parse_digit])
+						     
 let parse_word w = parse_all (List.map parse_char (explode w))
+
 let parse_wc = 
   let wc = parse_any_char (explode " \t\r\n") in
     parse_compose (fun x -> []) (parse_kleene_star wc)
+
 let parse_ignore p =
   parse_compose (fun x -> []) p
 
 let parse_json_null = parse_compose (fun x -> Null) (parse_word "null")
 let parse_json_string = parse_compose (fun s -> String s) parse_string
 let parse_json_int = parse_compose (fun i -> Int i) parse_int
+let parse_json_float = parse_compose (fun f -> Float f ) parse_float
 
 let parse_delimited (start:char) (last:char) (delim:char) p =
   let parse_delim = parse_char delim in
 		   parse_json_false;
 		   parse_json_null;
 		   parse_json_string;
+		   parse_json_float;
 		   parse_json_int;
 		   parse_json_list;
 		   parse_json_array]); parse_wc]) cs
     | Success (result,rest) -> result
 
 let test =
-  print_endline (dump (loads_json "[true,false,1,    2, 256, {\"a\":1}]"));
+  let s = "[true,false,null,\"hello\", 1   , 2.05 ,  {\"abc\":[1,2,[3]]}]" in
+  print_endline (dump (loads_json s));