Mercurial > repos > tabletprog
annotate modules/json.tp @ 277:2b58eafa360b
Add SDL bindings for creating a renderer, clearing it, presenting it and destroying it
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 20 Jul 2014 17:30:46 -0700 |
parents | bb2b4613fdc8 |
children | ddf38b66b2e2 |
rev | line source |
---|---|
154 | 1 { |
2 startArr <- "[" byte: 0 | |
3 endArr <- "]" byte: 0 | |
4 startObj <- "{" byte: 0 | |
5 endObj <- "}" byte: 0 | |
6 quote <- "\"" byte: 0 | |
7 esc <- "\\" byte: 0 | |
8 zero <- "0" byte: 0 | |
9 nine <- "9" byte: 0 | |
10 neg <- "-" byte: 0 | |
11 space <- " " byte: 0 | |
12 comma <- "," byte: 0 | |
169
9d8ae39e8e67
Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
167
diff
changeset
|
13 period <- "." byte: 0 |
167
5a6a55592c45
Fix some JSON parser bugs
Mike Pavone <pavone@retrodev.com>
parents:
166
diff
changeset
|
14 tab <- " " byte: 0 |
166
e7642715d575
Handle newlines and carriage returns in JSON decoder
Mike Pavone <pavone@retrodev.com>
parents:
165
diff
changeset
|
15 nl <- "\n" byte: 0 |
e7642715d575
Handle newlines and carriage returns in JSON decoder
Mike Pavone <pavone@retrodev.com>
parents:
165
diff
changeset
|
16 cr <- "\r" byte: 0 |
154 | 17 colon <- ":" byte: 0 |
165
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
18 t <- "t" byte: 0 |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
19 f <- "f" byte: 0 |
154 | 20 |
21 parseNumAt <- :str :at :recvResult { | |
22 num <- 0 | |
23 l <- str length | |
24 minus <- false | |
25 aft <- -1 | |
169
9d8ae39e8e67
Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
167
diff
changeset
|
26 ignore <- false |
154 | 27 while: { at < l } do: { |
28 b <- str byte: at | |
29 if: b = neg { | |
30 minus <- true | |
31 } else: { | |
169
9d8ae39e8e67
Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
167
diff
changeset
|
32 if: b = period { |
9d8ae39e8e67
Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
167
diff
changeset
|
33 ignore <- true |
154 | 34 } else: { |
169
9d8ae39e8e67
Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
167
diff
changeset
|
35 if: b >= zero && b <= nine { |
9d8ae39e8e67
Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
167
diff
changeset
|
36 if: (not: ignore) { |
9d8ae39e8e67
Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
167
diff
changeset
|
37 num <- num * 10 + (str byte: at) - zero |
9d8ae39e8e67
Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
167
diff
changeset
|
38 } |
9d8ae39e8e67
Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
167
diff
changeset
|
39 } else: { |
9d8ae39e8e67
Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
167
diff
changeset
|
40 aft <- at |
9d8ae39e8e67
Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
167
diff
changeset
|
41 at <- l |
9d8ae39e8e67
Handle floating point numbers in JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
167
diff
changeset
|
42 } |
154 | 43 } |
44 } | |
45 at <- at + 1 | |
46 } | |
47 if: aft < 0 { | |
48 aft <- at | |
49 } | |
50 if: minus { | |
51 num <- 0 - num | |
52 } | |
53 #{ | |
54 value <- num | |
55 after <- aft | |
56 } | |
57 } | |
58 | |
59 parseStrAt <- :src :at :recvResult { | |
60 //TODO: Deal with escaped characters | |
61 end <- src find: "\"" startingAt: at + 1 else: { src length } | |
62 #{ | |
63 value <- src from: (at + 1) withLength: (end - at - 1) | |
64 after <- end + 1 | |
65 } | |
66 } | |
67 | |
68 _decode:at <- :text :cur { | |
69 ret <- false | |
70 b <- text byte: cur | |
71 if: b = neg || b >= zero && b <= nine { | |
72 text parseNumAt: cur | |
73 } else: { | |
74 if: b = quote { | |
75 text parseStrAt: cur | |
76 } else: { | |
77 if: b = startArr { | |
78 len <- text length | |
79 val <- #[] | |
80 cur <- cur + 1 | |
81 aft <- -1 | |
82 while: { cur < len } do: { | |
83 b <- text byte: cur | |
84 if: b = endArr { | |
85 aft <- cur + 1 | |
86 cur <- len | |
87 } else: { | |
166
e7642715d575
Handle newlines and carriage returns in JSON decoder
Mike Pavone <pavone@retrodev.com>
parents:
165
diff
changeset
|
88 if: b = comma || b = space || b = tab || b = nl || b = cr { |
154 | 89 cur <- cur + 1 |
90 } else: { | |
91 el <- _decode: text at: cur | |
92 cur <- el after | |
93 val append: (el value) | |
94 } | |
95 } | |
96 } | |
97 #{ | |
98 value <- val | |
99 after <- aft | |
100 } | |
101 } else: { | |
102 if: b = startObj { | |
103 len <- text length | |
104 val <- dict linear | |
105 cur <- cur + 1 | |
106 aft <- -1 | |
107 expectKey <- true | |
108 key <- "" | |
109 while: { cur < len } do: { | |
110 b <- text byte: cur | |
166
e7642715d575
Handle newlines and carriage returns in JSON decoder
Mike Pavone <pavone@retrodev.com>
parents:
165
diff
changeset
|
111 if: b = comma || b = space || b = tab || b = colon || b = nl || b = cr { |
154 | 112 cur <- cur + 1 |
113 } else: { | |
114 if: expectKey { | |
115 if: b = endObj { | |
116 aft <- cur + 1 | |
117 cur <- len | |
118 } else: { | |
119 kd <- _decode: text at: cur | |
120 key <- kd value | |
121 cur <- kd after | |
167
5a6a55592c45
Fix some JSON parser bugs
Mike Pavone <pavone@retrodev.com>
parents:
166
diff
changeset
|
122 |
154 | 123 expectKey <- false |
124 } | |
125 } else: { | |
126 el <- _decode: text at: cur | |
127 val set: key (el value) | |
128 cur <- el after | |
129 expectKey <- true | |
130 } | |
131 } | |
132 } | |
133 #{ | |
134 after <- aft | |
135 value <- val | |
136 } | |
137 } else: { | |
165
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
138 if: b = t && (text from: cur withLength: 4) = "true" { |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
139 #{ |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
140 value <- true |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
141 after <- cur + 4 |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
142 } |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
143 } else: { |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
144 if: b = f && (text from: cur withLength: 5) = "false" { |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
145 #{ |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
146 value <- false |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
147 after <- cur + 5 |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
148 } |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
149 } else: { |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
150 #{ |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
151 value <- "foobar" |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
152 after <- (text length) |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
153 } |
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
154 } |
154 | 155 } |
165
fe816637fcc4
Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents:
154
diff
changeset
|
156 |
154 | 157 } |
158 } | |
159 } | |
160 } | |
161 } | |
162 #{ | |
163 decode <- :text { | |
164 (_decode: text at: 0) value | |
165 } | |
271
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
166 |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
167 encode <- :value { |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
168 if: (object does: value understand?: "jsonEncode") { |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
169 value jsonEncode |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
170 } else: { |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
171 toEncode <- #[] |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
172 if: (object does: value understand?: "serializeFields") { |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
173 toEncode <- value serializeFields |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
174 } else: { |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
175 toEncode <- object propertiesOf: value |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
176 } |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
177 parts <- #[] |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
178 foreach: toEncode :idx field { |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
179 fieldVal <- object sendMessage: field to: value |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
180 parts append: (field jsonEncode) . ":" . (encode: fieldVal) |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
181 } |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
182 "{" . (parts join: ",") . "}" |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
183 } |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
184 } |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
185 |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
186 main <- { |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
187 o <- #{ |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
188 foo <- "bar" |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
189 baz <- ["fizz" "buzz" "buzzzz"] |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
190 qux <- ((dict hash) set: "fo" "shizzle") set: "my" "nizzle" |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
191 arr <- #["pirate" "booty"] |
272
bb2b4613fdc8
Added support for encoding integers and booleans as JSON
Michael Pavone <pavone@retrodev.com>
parents:
271
diff
changeset
|
192 numbers <- [1 2 3 42 (0-1337)] |
bb2b4613fdc8
Added support for encoding integers and booleans as JSON
Michael Pavone <pavone@retrodev.com>
parents:
271
diff
changeset
|
193 booleans <- [ |
bb2b4613fdc8
Added support for encoding integers and booleans as JSON
Michael Pavone <pavone@retrodev.com>
parents:
271
diff
changeset
|
194 true |
bb2b4613fdc8
Added support for encoding integers and booleans as JSON
Michael Pavone <pavone@retrodev.com>
parents:
271
diff
changeset
|
195 false |
bb2b4613fdc8
Added support for encoding integers and booleans as JSON
Michael Pavone <pavone@retrodev.com>
parents:
271
diff
changeset
|
196 ] |
271
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
197 } |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
198 print: (encode: o) . "\n" |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
199 0 |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
200 } |
154 | 201 } |
202 } |