(
src: str, pos: Pos, out: Output, header: Key, parse_float: ParseFloat
)
| 321 | |
| 322 | |
| 323 | def key_value_rule( |
| 324 | src: str, pos: Pos, out: Output, header: Key, parse_float: ParseFloat |
| 325 | ) -> Pos: |
| 326 | pos, key, value = parse_key_value_pair(src, pos, parse_float) |
| 327 | key_parent, key_stem = key[:-1], key[-1] |
| 328 | abs_key_parent = header + key_parent |
| 329 | |
| 330 | relative_path_cont_keys = (header + key[:i] for i in range(1, len(key))) |
| 331 | for cont_key in relative_path_cont_keys: |
| 332 | # Check that dotted key syntax does not redefine an existing table |
| 333 | if out.flags.is_(cont_key, Flags.EXPLICIT_NEST): |
| 334 | raise suffixed_err(src, pos, f"Cannot redefine namespace {cont_key}") |
| 335 | # Containers in the relative path can't be opened with the table syntax or |
| 336 | # dotted key/value syntax in following table sections. |
| 337 | out.flags.add_pending(cont_key, Flags.EXPLICIT_NEST) |
| 338 | |
| 339 | if out.flags.is_(abs_key_parent, Flags.FROZEN): |
| 340 | raise suffixed_err( |
| 341 | src, pos, f"Cannot mutate immutable namespace {abs_key_parent}" |
| 342 | ) |
| 343 | |
| 344 | try: |
| 345 | nest = out.data.get_or_create_nest(abs_key_parent) |
| 346 | except KeyError: |
| 347 | raise suffixed_err(src, pos, "Cannot overwrite a value") from None |
| 348 | if key_stem in nest: |
| 349 | raise suffixed_err(src, pos, "Cannot overwrite a value") |
| 350 | # Mark inline table and array namespaces recursively immutable |
| 351 | if isinstance(value, (dict, list)): |
| 352 | out.flags.set(header + key, Flags.FROZEN, recursive=True) |
| 353 | nest[key_stem] = value |
| 354 | return pos |
| 355 | |
| 356 | |
| 357 | def parse_key_value_pair( |
no test coverage detected