Spaces:
Runtime error
Runtime error
File size: 13,633 Bytes
f0b1638 |
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
def node_to_table(node):
if len(node["entries"]) == node["lualen"]:
lst = []
for kv in node["entries"]:
lst.append(kv[1])
return lst
else:
dct = {}
for kv in node["entries"]:
dct[kv[0]] = kv[1]
return dct
def sorter(kv):
if isinstance(kv[0], int):
return kv[0]
return float("inf")
def node_entries_append(node, key, val):
node["entries"].append([key, val])
node["entries"].sort(key=sorter)
lualen = 0
for kv in node["entries"]:
if kv[0] == lualen + 1:
lualen = lualen + 1
node["lualen"] = lualen
def parse(raw, encoding="utf-8", multival=False, verbose=False):
sbins = raw.encode(encoding)
root = {"entries": [], "lualen": 0, "is_root": True}
node = root
stack = []
state = "SEEK_CHILD"
pos = 0
slen = len(sbins)
byte_quoting_char = None
key = None
escaping = False
comment = None
component_name = None
errmsg = None
while pos <= slen:
byte_current = None
byte_current_is_space = False
if pos < slen:
byte_current = sbins[pos: pos + 1]
byte_current_is_space = (
byte_current == b" "
or byte_current == b"\r"
or byte_current == b"\n"
or byte_current == b"\t"
)
if verbose:
print("[step] pos", pos, byte_current, state, comment, key, node)
if comment == "MULTILINE":
if byte_current == b"]" and sbins[pos: pos + 2] == b"]]":
comment = None
pos = pos + 1
elif comment == "INLINE":
if byte_current == b"\n":
comment = None
elif state == "SEEK_CHILD":
if byte_current is None:
break
if byte_current == b"-" and sbins[pos: pos + 4] == b"--[[":
comment = "MULTILINE"
pos = pos + 3
elif byte_current == b"-" and sbins[pos: pos + 2] == b"--":
comment = "INLINE"
pos = pos + 1
elif not node["is_root"] and (
(byte_current >= b"A" and byte_current <= b"Z")
or (byte_current >= b"a" and byte_current <= b"z")
or byte_current == b"_"
):
state = "KEY_SIMPLE"
pos1 = pos
elif not node["is_root"] and byte_current == b"[":
state = "KEY_EXPRESSION_OPEN"
elif byte_current == b"}":
if len(stack) == 0:
errmsg = (
"unexpected table closing, no matching opening braces found."
)
break
prev_env = stack.pop()
if prev_env["state"] == "KEY_EXPRESSION_OPEN":
key = node_to_table(node)
state = "KEY_END"
elif prev_env["state"] == "VALUE":
node_entries_append(
prev_env["node"],
prev_env["key"],
node_to_table(node),
)
state = "VALUE_END"
key = None
node = prev_env["node"]
elif not byte_current_is_space:
key = node["lualen"] + 1
state = "VALUE"
pos = pos - 1
elif state == "VALUE":
if byte_current is None:
errmsg = "unexpected empty value."
break
if byte_current == b"-" and sbins[pos: pos + 4] == b"--[[":
comment = "MULTILINE"
pos = pos + 3
elif byte_current == b"-" and sbins[pos: pos + 2] == b"--":
comment = "INLINE"
pos = pos + 1
elif byte_current == b'"' or byte_current == b"'":
state = "TEXT"
component_name = "VALUE"
pos1 = pos + 1
byte_quoting_char = byte_current
elif byte_current == b"-" or (
byte_current >= b"0" and byte_current <= b"9"
):
state = "INT"
component_name = "VALUE"
pos1 = pos
elif byte_current == b".":
state = "FLOAT"
component_name = "VALUE"
pos1 = pos
elif byte_current == b"t" and sbins[pos: pos + 4] == b"true":
node_entries_append(node, key, True)
state = "VALUE_END"
key = None
pos = pos + 3
elif byte_current == b"f" and sbins[pos: pos + 5] == b"false":
node_entries_append(node, key, False)
state = "VALUE_END"
key = None
pos = pos + 4
elif byte_current == b"{":
stack.append({"node": node, "state": state, "key": key})
state = "SEEK_CHILD"
node = {"entries": [], "lualen": 0, "is_root": False}
elif state == "TEXT":
if byte_current is None:
errmsg = "unexpected string ending: missing close quote."
break
if escaping:
escaping = False
elif byte_current == b"\\":
escaping = True
elif byte_current == byte_quoting_char:
data = (
sbins[pos1:pos]
.replace(b"\\\n", b"\n")
.replace(b'\\"', b'"')
.replace(b"\\\\", b"\\")
.decode(encoding)
)
if component_name == "KEY":
key = data
state = "KEY_EXPRESSION_FINISH"
elif component_name == "VALUE":
node_entries_append(node, key, data)
state = "VALUE_END"
key = None
data = None
elif state == "INT":
if byte_current == b".":
state = "FLOAT"
elif byte_current is None or byte_current < b"0" or byte_current > b"9":
data = int(sbins[pos1:pos].decode(encoding))
if component_name == "KEY":
key = data
state = "KEY_EXPRESSION_FINISH"
pos = pos - 1
elif component_name == "VALUE":
node_entries_append(node, key, data)
state = "VALUE_END"
key = None
pos = pos - 1
data = None
elif state == "FLOAT":
if byte_current is None or byte_current < b"0" or byte_current > b"9":
if pos == pos1 + 1 and sbins[pos1:pos] == b".":
errmsg = "unexpected dot."
break
else:
data = float(sbins[pos1:pos].decode(encoding))
if component_name == "KEY":
key = data
state = "KEY_EXPRESSION_FINISH"
pos = pos - 1
elif component_name == "VALUE":
node_entries_append(node, key, data)
state = "VALUE_END"
key = None
pos = pos - 1
data = None
elif state == "VALUE_END":
if byte_current is None:
pass
elif byte_current == b"-" and sbins[pos: pos + 4] == b"--[[":
comment = "MULTILINE"
pos = pos + 3
elif byte_current == b"-" and sbins[pos: pos + 2] == b"--":
comment = "INLINE"
pos = pos + 1
elif byte_current == b",":
state = "SEEK_CHILD"
elif byte_current == b"}":
state = "SEEK_CHILD"
pos = pos - 1
elif not byte_current_is_space:
errmsg = "unexpected character."
break
elif state == "KEY_EXPRESSION_OPEN":
if byte_current is None:
errmsg = "key expression expected."
break
if byte_current == b"-" and sbins[pos: pos + 4] == b"--[[":
comment = "MULTILINE"
pos = pos + 3
elif byte_current == b"-" and sbins[pos: pos + 2] == b"--":
comment = "INLINE"
pos = pos + 1
elif byte_current == b'"' or byte_current == b"'":
state = "TEXT"
component_name = "KEY"
pos1 = pos + 1
byte_quoting_char = byte_current
elif byte_current == b"-" or (
byte_current >= b"0" and byte_current <= b"9"
):
state = "INT"
component_name = "KEY"
pos1 = pos
elif byte_current == b".":
state = "FLOAT"
component_name = "KEY"
pos1 = pos
elif byte_current == b"t" and sbins[pos: pos + 4] == b"true":
errmsg = "python do not support bool as dict key."
break
key = True
state = "KEY_EXPRESSION_FINISH"
pos = pos + 3
elif byte_current == b"f" and sbins[pos: pos + 5] == b"false":
errmsg = "python do not support bool variable as dict key."
break
key = False
state = "KEY_EXPRESSION_FINISH"
pos = pos + 4
elif byte_current == b"{":
errmsg = "python do not support lua table variable as dict key."
break
state = "SEEK_CHILD"
stack.push({"node": node, "state": state, "key": key})
node = {"entries": [], "lualen": 0}
elif state == "KEY_EXPRESSION_FINISH":
if byte_current is None:
errmsg = 'unexpected end of table key expression, "]" expected.'
break
if byte_current == b"-" and sbins[pos: pos + 4] == b"--[[":
comment = "MULTILINE"
pos = pos + 3
elif byte_current == b"-" and sbins[pos: pos + 2] == b"--":
comment = "INLINE"
pos = pos + 1
elif byte_current == b"]":
state = "KEY_EXPRESSION_CLOSE"
elif not byte_current_is_space:
errmsg = 'unexpected character, "]" expected.'
break
elif state == "KEY_EXPRESSION_CLOSE":
if byte_current == b"=":
state = "VALUE"
elif byte_current == b"-" and sbins[pos: pos + 4] == b"--[[":
comment = "MULTILINE"
pos = pos + 3
elif byte_current == b"-" and sbins[pos: pos + 2] == b"--":
comment = "INLINE"
pos = pos + 1
elif not byte_current_is_space:
errmsg = 'unexpected character, "=" expected.'
break
elif state == "KEY_SIMPLE":
if not (
(byte_current >= b"A" and byte_current <= b"Z")
or (byte_current >= b"a" and byte_current <= b"z")
or (byte_current >= b"0" and byte_current <= b"9")
or byte_current == b"_"
):
key = sbins[pos1:pos].decode(encoding)
state = "KEY_SIMPLE_END"
pos = pos - 1
elif state == "KEY_SIMPLE_END":
if byte_current_is_space:
pass
elif byte_current == b"-" and sbins[pos: pos + 4] == b"--[[":
comment = "MULTILINE"
pos = pos + 3
elif byte_current == b"-" and sbins[pos: pos + 2] == b"--":
comment = "INLINE"
pos = pos + 1
elif byte_current == b"=":
state = "VALUE"
elif byte_current == b"," or byte_current == b"}":
if key == "true":
node_entries_append(node, node["lualen"] + 1, True)
state = "VALUE_END"
key = None
pos = pos - 1
elif key == "false":
node_entries_append(node, node["lualen"] + 1, False)
state = "VALUE_END"
key = None
pos = pos - 1
else:
key = None
errmsg = "invalied table simple key character."
break
pos += 1
if verbose:
print(" ", pos, " ", state, comment, key, node)
# check if there is any errors
if errmsg is None and len(stack) != 0:
errmsg = 'unexpected end of table, "}" expected.'
if errmsg is None and root["lualen"] == 0:
errmsg = "nothing can be unserialized from input string."
if errmsg is not None:
pos = min(pos, slen)
start_pos = max(0, pos - 4)
end_pos = min(pos + 10, slen)
err_parts = sbins[start_pos:end_pos].decode(encoding)
err_indent = " " * (pos - start_pos)
raise Exception(
"Unserialize luadata failed on pos %d:\n %s\n %s^\n %s"
% (pos, err_parts, err_indent, errmsg)
)
res = []
for kv in root["entries"]:
res.append(kv[1])
if multival:
return tuple(res)
return res[0]
|