Mercurial > repos > tabletprog
annotate modules/json.tp @ 365:3d36d69aab7f
Fix JSON decoder to align with language changes
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 07 Aug 2015 21:40:08 -0700 |
parents | eef8a5cea812 |
children | e2c1309ab750 |
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 |
365
3d36d69aab7f
Fix JSON decoder to align with language changes
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
21 parseNumAt <- :str at { |
154 | 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 | |
365
3d36d69aab7f
Fix JSON decoder to align with language changes
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
59 parseStrAt <- :src at { |
154 | 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 { | |
365
3d36d69aab7f
Fix JSON decoder to align with language changes
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
72 parseNumAt: text cur |
154 | 73 } else: { |
74 if: b = quote { | |
365
3d36d69aab7f
Fix JSON decoder to align with language changes
Michael Pavone <pavone@retrodev.com>
parents:
329
diff
changeset
|
75 parseStrAt: text cur |
154 | 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 { |
329
eef8a5cea812
Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents:
286
diff
changeset
|
168 jsonEncoder encode: value |
271
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
169 } |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
170 |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
171 main <- { |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
172 o <- #{ |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
173 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
|
174 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
|
175 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
|
176 arr <- #["pirate" "booty"] |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
177 numbers <- [1 2 3 42 (0-1337) 3.14159] |
272
bb2b4613fdc8
Added support for encoding integers and booleans as JSON
Michael Pavone <pavone@retrodev.com>
parents:
271
diff
changeset
|
178 booleans <- [ |
286
ddf38b66b2e2
Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents:
272
diff
changeset
|
179 true |
272
bb2b4613fdc8
Added support for encoding integers and booleans as JSON
Michael Pavone <pavone@retrodev.com>
parents:
271
diff
changeset
|
180 false |
bb2b4613fdc8
Added support for encoding integers and booleans as JSON
Michael Pavone <pavone@retrodev.com>
parents:
271
diff
changeset
|
181 ] |
271
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
182 } |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
183 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
|
184 0 |
bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents:
169
diff
changeset
|
185 } |
154 | 186 } |
187 } |