| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''YAML Parser with column and line information.''' |
| 5 |
|
| 6 |
import yaml |
| 7 |
from yaml.events import (StreamStartEvent, |
| 8 |
DocumentStartEvent, DocumentEndEvent, |
| 9 |
MappingStartEvent, MappingEndEvent, |
| 10 |
SequenceStartEvent, SequenceEndEvent, |
| 11 |
ScalarEvent, AliasEvent) |
| 12 |
|
| 13 |
from .value_with_trace import ValueWithTrace |
| 14 |
from .errors import CrazySchemaValidationError |
| 15 |
|
| 16 |
|
| 17 |
_error = CrazySchemaValidationError |
| 18 |
|
| 19 |
|
| 20 |
class ExtendedYAMLParser: |
| 21 |
'''Parse YAML with the ability to trace the origin of parsed values.''' |
| 22 |
|
| 23 |
def __init__(self): |
| 24 |
self.src = None |
| 25 |
self.data = [] |
| 26 |
self.current_stack = [] |
| 27 |
self.current_key = None |
| 28 |
|
| 29 |
def parse(self, stream): |
| 30 |
""" |
| 31 |
Parses the given YAML stream in a SAX-like way and reconstructs the |
| 32 |
structure. |
| 33 |
|
| 34 |
Raises: |
| 35 |
- yaml.parser.ParserError |
| 36 |
- errors.CrazySchemaValidationError |
| 37 |
""" |
| 38 |
self.src = stream |
| 39 |
self.data = [] |
| 40 |
self.current_stack = [] |
| 41 |
self.current_key = None |
| 42 |
|
| 43 |
loader = yaml.Loader(stream) |
| 44 |
try: |
| 45 |
while True: |
| 46 |
event = loader.get_event() |
| 47 |
if event is None: |
| 48 |
break |
| 49 |
self.handle_event(event) |
| 50 |
finally: |
| 51 |
loader.dispose() |
| 52 |
|
| 53 |
return self.data |
| 54 |
|
| 55 |
def handle_event(self, event): |
| 56 |
""" |
| 57 |
Handle each YAML parsing event and construct the corresponding data |
| 58 |
structure. |
| 59 |
""" |
| 60 |
if isinstance(event, StreamStartEvent): |
| 61 |
pass |
| 62 |
elif isinstance(event, DocumentStartEvent): |
| 63 |
pass |
| 64 |
elif isinstance(event, DocumentEndEvent): |
| 65 |
if self.current_stack: |
| 66 |
self.data.append(self.current_stack.pop()) |
| 67 |
elif isinstance(event, MappingStartEvent): |
| 68 |
new_map = ValueWithTrace.from_yaml_event({}, self.src, event) |
| 69 |
self.add_to_current_structure(self.current_key, new_map) |
| 70 |
self.current_stack.append(new_map) |
| 71 |
self.current_key = None |
| 72 |
elif isinstance(event, SequenceStartEvent): |
| 73 |
new_seq = ValueWithTrace.from_yaml_event([], self.src, event) |
| 74 |
self.add_to_current_structure(self.current_key, new_seq) |
| 75 |
self.current_stack.append(new_seq) |
| 76 |
self.current_key = None |
| 77 |
elif isinstance(event, ScalarEvent): |
| 78 |
value = self.convert_scalar(event.value, event.implicit) |
| 79 |
value = ValueWithTrace.from_yaml_event(value, self.src, event) |
| 80 |
|
| 81 |
if self.current_stack and isinstance(self.current_stack[-1].value, list): |
| 82 |
self.add_to_current_structure(None, value) |
| 83 |
else: |
| 84 |
if self.current_key is None: |
| 85 |
self.current_key = value |
| 86 |
else: |
| 87 |
self.add_to_current_structure(self.current_key, value) |
| 88 |
self.current_key = None |
| 89 |
elif isinstance(event, (MappingEndEvent, SequenceEndEvent)): |
| 90 |
self.current_stack.pop() |
| 91 |
elif isinstance(event, AliasEvent): |
| 92 |
value = ValueWithTrace.from_yaml_event(None, self.src, event) |
| 93 |
raise _error('Anchors not supported', value) |
| 94 |
|
| 95 |
def add_to_current_structure(self, key, value): |
| 96 |
""" |
| 97 |
Add a value to the current structure (either dict or list). |
| 98 |
""" |
| 99 |
if not self.current_stack: |
| 100 |
# Root-level element |
| 101 |
self.current_stack.append(value) |
| 102 |
return |
| 103 |
|
| 104 |
current = self.current_stack[-1] |
| 105 |
if isinstance(current.value, dict): |
| 106 |
if key is None: |
| 107 |
raise _error("Missing key for mapping in YAML", value) |
| 108 |
current.value[key] = value |
| 109 |
elif isinstance(current.value, list): |
| 110 |
current.value.append(value) |
| 111 |
else: |
| 112 |
raise _error("Invalid structure in YAML", value) |
| 113 |
|
| 114 |
def convert_scalar(self, value, implicit): |
| 115 |
""" |
| 116 |
Converts a scalar based on its type hints (implicit tuple). |
| 117 |
""" |
| 118 |
if implicit[0]: |
| 119 |
lower = value.lower() |
| 120 |
|
| 121 |
if lower in ("null", "~", ""): |
| 122 |
return None |
| 123 |
|
| 124 |
if lower in ("true", "on", "yes"): |
| 125 |
return True |
| 126 |
|
| 127 |
if lower in ("false", "off", "no"): |
| 128 |
return False |
| 129 |
|
| 130 |
try: |
| 131 |
return int(value) |
| 132 |
except ValueError: |
| 133 |
pass |
| 134 |
|
| 135 |
try: |
| 136 |
return float(value) |
| 137 |
except ValueError: |
| 138 |
pass |
| 139 |
|
| 140 |
return value |