-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-tokens.sml
47 lines (43 loc) · 1.16 KB
/
json-tokens.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(* json-tokens.sml
*
* COPYRIGHT (c) 2008 The Fellowship of SML/NJ (http://www.smlnj.org)
* All rights reserved.
*
* The tokens returned by the JSON lexer.
*)
structure JSONTokens =
struct
datatype token
= EOF (* end-of-file *)
| LB | RB (* "[" "]" *)
| LCB | RCB (* "{" "}" *)
| COMMA (* "," *)
| COLON (* ":" *)
| KW_null (* "null" *)
| KW_true (* "true" *)
| KW_false (* "false" *)
| INT of IntInf.int
| FLOAT of real
| STRING of string
fun toString EOF = "<eof>"
| toString LB = "["
| toString RB = "]"
| toString LCB = "{"
| toString RCB = "}"
| toString COMMA = ","
| toString COLON = ":"
| toString KW_null = "null"
| toString KW_true = "true"
| toString KW_false = "false"
| toString (INT i) =
if (i < 0) then "-" ^ IntInf.toString(~i)
else IntInf.toString i
| toString (FLOAT f) =
if (f < 0.0) then "-" ^ Real.toString(~f)
else Real.toString f
| toString (STRING s) = let
fun f (wchr, l) = UTF8.toString wchr :: l
in
String.concat("\"" :: (List.foldr f ["\""] (UTF8.explode s)))
end
end