(curr_token)
| 280 | return curr_token, dct |
| 281 | |
| 282 | def parse_kv(curr_token): |
| 283 | dct = defaultdict(list) |
| 284 | while curr_token.category == Pattern.KEYS: |
| 285 | key = curr_token.value |
| 286 | curr_token = next(tokens) |
| 287 | category = curr_token.category |
| 288 | if category == Pattern.REALS or category == Pattern.INTS: |
| 289 | value = curr_token.value |
| 290 | curr_token = next(tokens) |
| 291 | elif category == Pattern.STRINGS: |
| 292 | value = unescape(curr_token.value[1:-1]) |
| 293 | if destringizer: |
| 294 | try: |
| 295 | value = destringizer(value) |
| 296 | except ValueError: |
| 297 | pass |
| 298 | curr_token = next(tokens) |
| 299 | elif category == Pattern.DICT_START: |
| 300 | curr_token, value = parse_dict(curr_token) |
| 301 | else: |
| 302 | if key in ("id", "label", "source", "target"): |
| 303 | try: |
| 304 | # String convert the token value |
| 305 | value = unescape(str(curr_token.value)) |
| 306 | if destringizer: |
| 307 | try: |
| 308 | value = destringizer(value) |
| 309 | except ValueError: |
| 310 | pass |
| 311 | curr_token = next(tokens) |
| 312 | except Exception: |
| 313 | msg = ( |
| 314 | "an int, float, string, '[' or string" |
| 315 | + " convertible ASCII value for node id or label" |
| 316 | ) |
| 317 | unexpected(curr_token, msg) |
| 318 | elif curr_token.value in {"NAN", "INF"}: |
| 319 | value = float(curr_token.value) |
| 320 | curr_token = next(tokens) |
| 321 | else: # Otherwise error out |
| 322 | unexpected(curr_token, "an int, float, string or '['") |
| 323 | dct[key].append(value) |
| 324 | |
| 325 | def clean_dict_value(value): |
| 326 | if not isinstance(value, list): |
| 327 | return value |
| 328 | if len(value) == 1: |
| 329 | return value[0] |
| 330 | if value[0] == LIST_START_VALUE: |
| 331 | return value[1:] |
| 332 | return value |
| 333 | |
| 334 | dct = {key: clean_dict_value(value) for key, value in dct.items()} |
| 335 | return curr_token, dct |
| 336 | |
| 337 | def parse_graph(): |
| 338 | curr_token, dct = parse_kv(next(tokens)) |
no test coverage detected