annotate modules/json.tp @ 367:e2c1309ab750

Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
author Michael Pavone <pavone@retrodev.com>
date Sun, 09 Aug 2015 18:07:23 -0700
parents 3d36d69aab7f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1 {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
2 startArr <- "[" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
3 endArr <- "]" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
4 startObj <- "{" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
5 endObj <- "}" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6 quote <- "\"" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7 esc <- "\\" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
8 zero <- "0" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
9 nine <- "9" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
10 neg <- "-" byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
11 space <- " " byte: 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
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
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
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
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
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
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
22 num <- 0
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
23 l <- str length
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
24 minus <- false
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
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
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 while: { at < l } do: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 b <- str byte: at
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
29 if: b = neg {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
30 minus <- true
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
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
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
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
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
43 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
44 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
45 at <- at + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
46 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47 if: aft < 0 {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
48 aft <- at
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 if: minus {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 num <- 0 - num
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 #{
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 value <- num
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
55 after <- aft
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58
365
3d36d69aab7f Fix JSON decoder to align with language changes
Michael Pavone <pavone@retrodev.com>
parents: 329
diff changeset
59 parseStrAt <- :src at {
367
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
60 //TODO: Deal with unicode escapes
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
61 bslash <- "\\" byte: 0
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
62 quote <- "\"" byte: 0
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
63 escapes <- dict hash
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
64 escapes set: bslash "\\"
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
65 escapes set: quote "\""
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
66 escapes set: ("n" byte: 0) "\n"
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
67 escapes set: ("t" byte: 0) "\t"
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
68 escapes set: ("r" byte: 0) "\r"
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
69 i <- at + 1
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
70 chunks <- #[]
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
71 start <- i
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
72 continue <- true
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
73 while: { continue && i < (src byte_length) } do: {
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
74 b <- src byte: i
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
75 if: b = bslash {
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
76 if: i > start {
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
77 chunks append: (src from: start withLength: i-start)
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
78 }
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
79 if: i + 1 < (src byte_length) {
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
80 i <- i + 1
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
81 b <- src byte: i
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
82 start <- i + 1
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
83 escapes ifget: b :v {
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
84 chunks append: v
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
85 } else: {
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
86 //not a recognized escape, just copy it raw
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
87 chunks append: (src from: i-1 withLength: 2)
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
88 }
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
89 }
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
90 } else: {
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
91 if: b = quote {
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
92 if: i > start {
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
93 chunks append: (src from: start withLength: i-start)
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
94 }
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
95 continue <- false
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
96 }
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
97 }
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
98 i <- i + 1
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
99 }
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
100 #{
367
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
101 value <- chunks join: ""
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
102 after <- i
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
103 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
104 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
105
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
106 _decode:at <- :text :cur {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
107 ret <- false
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
108 b <- text byte: cur
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
109 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
110 parseNumAt: text cur
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
111 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
112 if: b = quote {
365
3d36d69aab7f Fix JSON decoder to align with language changes
Michael Pavone <pavone@retrodev.com>
parents: 329
diff changeset
113 parseStrAt: text cur
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
114 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
115 if: b = startArr {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
116 len <- text length
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
117 val <- #[]
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
118 cur <- cur + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
119 aft <- -1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
120 while: { cur < len } do: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
121 b <- text byte: cur
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
122 if: b = endArr {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
123 aft <- cur + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
124 cur <- len
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
125 } else: {
166
e7642715d575 Handle newlines and carriage returns in JSON decoder
Mike Pavone <pavone@retrodev.com>
parents: 165
diff changeset
126 if: b = comma || b = space || b = tab || b = nl || b = cr {
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
127 cur <- cur + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
128 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
129 el <- _decode: text at: cur
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
130 cur <- el after
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
131 val append: (el value)
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
132 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
133 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
134 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
135 #{
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
136 value <- val
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
137 after <- aft
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
138 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
139 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
140 if: b = startObj {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
141 len <- text length
367
e2c1309ab750 Use dict hash instead of dict linear in JSON parser. Implement basic string escapes in JSON parser.
Michael Pavone <pavone@retrodev.com>
parents: 365
diff changeset
142 val <- dict hash
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
143 cur <- cur + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
144 aft <- -1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
145 expectKey <- true
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
146 key <- ""
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
147 while: { cur < len } do: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
148 b <- text byte: cur
166
e7642715d575 Handle newlines and carriage returns in JSON decoder
Mike Pavone <pavone@retrodev.com>
parents: 165
diff changeset
149 if: b = comma || b = space || b = tab || b = colon || b = nl || b = cr {
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
150 cur <- cur + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
151 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
152 if: expectKey {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
153 if: b = endObj {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
154 aft <- cur + 1
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
155 cur <- len
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
156 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
157 kd <- _decode: text at: cur
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
158 key <- kd value
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
159 cur <- kd after
167
5a6a55592c45 Fix some JSON parser bugs
Mike Pavone <pavone@retrodev.com>
parents: 166
diff changeset
160
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
161 expectKey <- false
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
162 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
163 } else: {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
164 el <- _decode: text at: cur
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
165 val set: key (el value)
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
166 cur <- el after
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
167 expectKey <- true
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
168 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
169 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
170 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
171 #{
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
172 after <- aft
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
173 value <- val
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
174 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
175 } else: {
165
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
176 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
177 #{
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
178 value <- true
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
179 after <- cur + 4
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
180 }
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
181 } else: {
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
182 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
183 #{
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
184 value <- false
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
185 after <- cur + 5
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
186 }
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
187 } else: {
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
188 #{
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
189 value <- "foobar"
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
190 after <- (text length)
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
191 }
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
192 }
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
193 }
165
fe816637fcc4 Add support for parsing true and false to JSON parser
Mike Pavone <pavone@retrodev.com>
parents: 154
diff changeset
194
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
195 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
196 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
197 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
198 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
199 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
200 #{
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
201 decode <- :text {
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
202 (_decode: text at: 0) value
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
203 }
271
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
204
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
205 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
206 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
207 }
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
208
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
209 main <- {
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
210 o <- #{
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
211 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
212 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
213 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
214 arr <- #["pirate" "booty"]
286
ddf38b66b2e2 Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents: 272
diff changeset
215 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
216 booleans <- [
286
ddf38b66b2e2 Finish support for floating point numbers in C backend
Michael Pavone <pavone@retrodev.com>
parents: 272
diff changeset
217 true
272
bb2b4613fdc8 Added support for encoding integers and booleans as JSON
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
218 false
bb2b4613fdc8 Added support for encoding integers and booleans as JSON
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
219 ]
271
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
220 }
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
221 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
222 0
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 169
diff changeset
223 }
154
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
224 }
6e579a75a0a9 Add JSON parser and sample
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
225 }