Mercurial > repos > tabletprog
comparison 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 |
comparison
equal
deleted
inserted
replaced
366:810b6115c1d4 | 367:e2c1309ab750 |
---|---|
55 after <- aft | 55 after <- aft |
56 } | 56 } |
57 } | 57 } |
58 | 58 |
59 parseStrAt <- :src at { | 59 parseStrAt <- :src at { |
60 //TODO: Deal with escaped characters | 60 //TODO: Deal with unicode escapes |
61 end <- src find: "\"" startingAt: at + 1 else: { src length } | 61 bslash <- "\\" byte: 0 |
62 quote <- "\"" byte: 0 | |
63 escapes <- dict hash | |
64 escapes set: bslash "\\" | |
65 escapes set: quote "\"" | |
66 escapes set: ("n" byte: 0) "\n" | |
67 escapes set: ("t" byte: 0) "\t" | |
68 escapes set: ("r" byte: 0) "\r" | |
69 i <- at + 1 | |
70 chunks <- #[] | |
71 start <- i | |
72 continue <- true | |
73 while: { continue && i < (src byte_length) } do: { | |
74 b <- src byte: i | |
75 if: b = bslash { | |
76 if: i > start { | |
77 chunks append: (src from: start withLength: i-start) | |
78 } | |
79 if: i + 1 < (src byte_length) { | |
80 i <- i + 1 | |
81 b <- src byte: i | |
82 start <- i + 1 | |
83 escapes ifget: b :v { | |
84 chunks append: v | |
85 } else: { | |
86 //not a recognized escape, just copy it raw | |
87 chunks append: (src from: i-1 withLength: 2) | |
88 } | |
89 } | |
90 } else: { | |
91 if: b = quote { | |
92 if: i > start { | |
93 chunks append: (src from: start withLength: i-start) | |
94 } | |
95 continue <- false | |
96 } | |
97 } | |
98 i <- i + 1 | |
99 } | |
62 #{ | 100 #{ |
63 value <- src from: (at + 1) withLength: (end - at - 1) | 101 value <- chunks join: "" |
64 after <- end + 1 | 102 after <- i |
65 } | 103 } |
66 } | 104 } |
67 | 105 |
68 _decode:at <- :text :cur { | 106 _decode:at <- :text :cur { |
69 ret <- false | 107 ret <- false |
99 after <- aft | 137 after <- aft |
100 } | 138 } |
101 } else: { | 139 } else: { |
102 if: b = startObj { | 140 if: b = startObj { |
103 len <- text length | 141 len <- text length |
104 val <- dict linear | 142 val <- dict hash |
105 cur <- cur + 1 | 143 cur <- cur + 1 |
106 aft <- -1 | 144 aft <- -1 |
107 expectKey <- true | 145 expectKey <- true |
108 key <- "" | 146 key <- "" |
109 while: { cur < len } do: { | 147 while: { cur < len } do: { |