kdlquery 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kdlquery/__init__.py +83 -0
- kdlquery/dict_reader.py +97 -0
- kdlquery/document.py +178 -0
- kdlquery/parser.py +1217 -0
- kdlquery/reader.py +730 -0
- kdlquery/selector.py +750 -0
- kdlquery/types.py +112 -0
- kdlquery-1.0.0.dist-info/METADATA +302 -0
- kdlquery-1.0.0.dist-info/RECORD +11 -0
- kdlquery-1.0.0.dist-info/WHEEL +4 -0
- kdlquery-1.0.0.dist-info/licenses/LICENSE +21 -0
kdlquery/__init__.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
from .types import (
|
|
2
|
+
CSTArgEntry,
|
|
3
|
+
CSTDocument,
|
|
4
|
+
CSTEntry,
|
|
5
|
+
CSTIdentifier,
|
|
6
|
+
CSTNode,
|
|
7
|
+
CSTPropEntry,
|
|
8
|
+
CSTTypeAnnotation,
|
|
9
|
+
CSTValue,
|
|
10
|
+
KDLParseError,
|
|
11
|
+
Position,
|
|
12
|
+
Span,
|
|
13
|
+
Token,
|
|
14
|
+
TokenType,
|
|
15
|
+
)
|
|
16
|
+
from .parser import KDL2CSTParser, KDLLexer
|
|
17
|
+
from .reader import (
|
|
18
|
+
DiagnosticCollector,
|
|
19
|
+
KdlValue,
|
|
20
|
+
KdlNode,
|
|
21
|
+
ReadDiagnostic,
|
|
22
|
+
Reader,
|
|
23
|
+
Severity,
|
|
24
|
+
WalkContext,
|
|
25
|
+
Walker,
|
|
26
|
+
parse_into,
|
|
27
|
+
)
|
|
28
|
+
from .document import KdlDocument
|
|
29
|
+
from .selector import SelectorError
|
|
30
|
+
from .dict_reader import DictReader
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def parse(source: str) -> KdlDocument:
|
|
34
|
+
"""Parse a KDL 2.0 string into a KdlDocument tree.
|
|
35
|
+
|
|
36
|
+
This is the simplest entry point for most use cases. It parses the
|
|
37
|
+
source into a CST, converts it to a KdlNode tree, and builds the
|
|
38
|
+
parent and depth maps for structural queries.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
source: A KDL 2.0 document as a string.
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
A KdlDocument with parent, depth, and sibling maps ready for
|
|
45
|
+
querying.
|
|
46
|
+
|
|
47
|
+
Raises:
|
|
48
|
+
KDLParseError: If the source is not valid KDL 2.0.
|
|
49
|
+
"""
|
|
50
|
+
cst = KDL2CSTParser().parse(source)
|
|
51
|
+
return KdlDocument.from_cst(cst)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
__all__ = [
|
|
55
|
+
"CSTArgEntry",
|
|
56
|
+
"CSTDocument",
|
|
57
|
+
"CSTEntry",
|
|
58
|
+
"CSTIdentifier",
|
|
59
|
+
"CSTNode",
|
|
60
|
+
"CSTPropEntry",
|
|
61
|
+
"CSTTypeAnnotation",
|
|
62
|
+
"CSTValue",
|
|
63
|
+
"KDL2CSTParser",
|
|
64
|
+
"KDLParseError",
|
|
65
|
+
"KDLLexer",
|
|
66
|
+
"Position",
|
|
67
|
+
"Span",
|
|
68
|
+
"Token",
|
|
69
|
+
"TokenType",
|
|
70
|
+
"DiagnosticCollector",
|
|
71
|
+
"KdlValue",
|
|
72
|
+
"KdlNode",
|
|
73
|
+
"KdlDocument",
|
|
74
|
+
"ReadDiagnostic",
|
|
75
|
+
"Reader",
|
|
76
|
+
"Severity",
|
|
77
|
+
"WalkContext",
|
|
78
|
+
"Walker",
|
|
79
|
+
"parse_into",
|
|
80
|
+
"parse",
|
|
81
|
+
"SelectorError",
|
|
82
|
+
"DictReader",
|
|
83
|
+
]
|
kdlquery/dict_reader.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, TypedDict
|
|
4
|
+
|
|
5
|
+
from .reader import (
|
|
6
|
+
KdlNode,
|
|
7
|
+
ReadDiagnostic,
|
|
8
|
+
Reader,
|
|
9
|
+
WalkContext,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Node(TypedDict):
|
|
14
|
+
"""A plain-dict representation of a KDL node.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
name: Node identifier string.
|
|
18
|
+
args: Tuple of unwrapped positional argument values.
|
|
19
|
+
props: Dict of unwrapped named property values.
|
|
20
|
+
children: List of child Node dicts.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
name: str
|
|
24
|
+
args: tuple[Any, ...]
|
|
25
|
+
props: dict[str, Any]
|
|
26
|
+
children: list[Node]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class DictReader(Reader[Node, list[Node]]):
|
|
30
|
+
"""Reader that converts a KDL document tree into nested dicts.
|
|
31
|
+
|
|
32
|
+
Each node becomes a ``Node`` TypedDict with unwrapped argument and
|
|
33
|
+
property values. Children are processed recursively.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def on_node(
|
|
37
|
+
self,
|
|
38
|
+
node: KdlNode,
|
|
39
|
+
ctx: WalkContext[Node],
|
|
40
|
+
) -> Node:
|
|
41
|
+
"""Convert a KDL node into a plain dict.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
node: The KDL node to convert.
|
|
45
|
+
ctx: Walk context used to recurse into children.
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
A Node dict with unwrapped values and recursively
|
|
49
|
+
processed children.
|
|
50
|
+
"""
|
|
51
|
+
child_nodes = ctx.walk_children()
|
|
52
|
+
return Node(
|
|
53
|
+
name=node.name,
|
|
54
|
+
args=tuple(a.value for a in node.args),
|
|
55
|
+
props={k: v.value for k, v in node.properties.items()},
|
|
56
|
+
children=child_nodes,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
def error_node(
|
|
60
|
+
self,
|
|
61
|
+
node: KdlNode,
|
|
62
|
+
message: str,
|
|
63
|
+
ctx: WalkContext[Node],
|
|
64
|
+
) -> Node:
|
|
65
|
+
"""Produce a minimal fallback dict for an errored node.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
node: The KDL node that caused the error.
|
|
69
|
+
message: String representation of the exception.
|
|
70
|
+
ctx: Walk context for the errored node.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
A Node dict with the node name but empty args, props,
|
|
74
|
+
and children.
|
|
75
|
+
"""
|
|
76
|
+
return Node(
|
|
77
|
+
name=node.name,
|
|
78
|
+
args=(),
|
|
79
|
+
props={},
|
|
80
|
+
children=[],
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
def finalize(
|
|
84
|
+
self,
|
|
85
|
+
nodes: list[Node],
|
|
86
|
+
diagnostics: list[ReadDiagnostic],
|
|
87
|
+
) -> list[Node]:
|
|
88
|
+
"""Return the list of root-level node dicts unchanged.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
nodes: Root-level Node dicts in source order.
|
|
92
|
+
diagnostics: Diagnostics collected during traversal.
|
|
93
|
+
|
|
94
|
+
Returns:
|
|
95
|
+
The same list of Node dicts.
|
|
96
|
+
"""
|
|
97
|
+
return nodes
|
kdlquery/document.py
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Iterator
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
|
|
6
|
+
from .types import CSTDocument, Span
|
|
7
|
+
from .reader import KdlNode
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class KdlDocument:
|
|
12
|
+
"""Owns a parsed KDL document tree and provides structural queries.
|
|
13
|
+
|
|
14
|
+
Maintains internal parent/depth maps keyed by node identity, enabling
|
|
15
|
+
parent, sibling, depth, and index lookups without mutating the
|
|
16
|
+
frozen KdlNode tree.
|
|
17
|
+
|
|
18
|
+
Attributes:
|
|
19
|
+
nodes: Top-level nodes of the document, in source order.
|
|
20
|
+
span: Source location spanning the entire document.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
nodes: tuple[KdlNode, ...]
|
|
24
|
+
span: Span
|
|
25
|
+
_parent_map: dict[int, KdlNode] = field(
|
|
26
|
+
default_factory=dict,
|
|
27
|
+
init=False,
|
|
28
|
+
repr=False,
|
|
29
|
+
)
|
|
30
|
+
_depth_map: dict[int, int] = field(
|
|
31
|
+
default_factory=dict,
|
|
32
|
+
init=False,
|
|
33
|
+
repr=False,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
@classmethod
|
|
37
|
+
def from_cst(cls, cst_doc: CSTDocument) -> KdlDocument:
|
|
38
|
+
"""Construct a KdlDocument from a CST document.
|
|
39
|
+
|
|
40
|
+
Converts all CST nodes to KdlNode instances and builds the
|
|
41
|
+
internal parent and depth maps in a single pass.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
cst_doc: The CST document produced by ``KDL2CSTParser.parse``.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
A fully initialized KdlDocument with parent and depth maps.
|
|
48
|
+
"""
|
|
49
|
+
nodes = tuple(KdlNode.from_cst(n) for n in cst_doc.nodes)
|
|
50
|
+
doc = cls(nodes=nodes, span=cst_doc.span)
|
|
51
|
+
doc._build_maps()
|
|
52
|
+
return doc
|
|
53
|
+
|
|
54
|
+
def _build_maps(self) -> None:
|
|
55
|
+
"""Populate the parent and depth maps via iterative pre-order traversal."""
|
|
56
|
+
stack: list[tuple[KdlNode, KdlNode | None, int]] = [
|
|
57
|
+
(n, None, 0) for n in reversed(self.nodes)
|
|
58
|
+
]
|
|
59
|
+
while stack:
|
|
60
|
+
node, parent, depth = stack.pop()
|
|
61
|
+
if parent is not None:
|
|
62
|
+
self._parent_map[id(node)] = parent
|
|
63
|
+
self._depth_map[id(node)] = depth
|
|
64
|
+
for child in reversed(node.children):
|
|
65
|
+
stack.append((child, node, depth + 1))
|
|
66
|
+
|
|
67
|
+
def parent_of(self, node: KdlNode) -> KdlNode | None:
|
|
68
|
+
"""Return the parent of a node.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
node: The node whose parent to look up. Must be a node
|
|
72
|
+
owned by this document.
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
The parent node, or ``None`` if this is a root-level node.
|
|
76
|
+
"""
|
|
77
|
+
return self._parent_map.get(id(node))
|
|
78
|
+
|
|
79
|
+
def depth_of(self, node: KdlNode) -> int:
|
|
80
|
+
"""Return the depth of a node in the tree.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
node: The node whose depth to look up. Must be a node
|
|
84
|
+
owned by this document.
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
Depth value where ``0`` means root-level.
|
|
88
|
+
"""
|
|
89
|
+
return self._depth_map.get(id(node), 0)
|
|
90
|
+
|
|
91
|
+
def index_of(self, node: KdlNode) -> int:
|
|
92
|
+
"""Return the position of a node among its siblings.
|
|
93
|
+
|
|
94
|
+
Args:
|
|
95
|
+
node: The node whose index to look up. Must be a node
|
|
96
|
+
owned by this document.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
Zero-based index among siblings, or ``-1`` if not found.
|
|
100
|
+
"""
|
|
101
|
+
parent = self.parent_of(node)
|
|
102
|
+
siblings = parent.children if parent is not None else self.nodes
|
|
103
|
+
for i, s in enumerate(siblings):
|
|
104
|
+
if s is node:
|
|
105
|
+
return i
|
|
106
|
+
return -1
|
|
107
|
+
|
|
108
|
+
def siblings_of(self, node: KdlNode) -> tuple[KdlNode, ...]:
|
|
109
|
+
"""Return the sibling tuple containing the given node.
|
|
110
|
+
|
|
111
|
+
For root-level nodes, returns ``self.nodes``. For child nodes,
|
|
112
|
+
returns ``parent.children``.
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
node: The node whose siblings to look up. Must be a node
|
|
116
|
+
owned by this document.
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
Tuple of sibling nodes (includes the node itself).
|
|
120
|
+
"""
|
|
121
|
+
parent = self.parent_of(node)
|
|
122
|
+
if parent is not None:
|
|
123
|
+
return parent.children
|
|
124
|
+
return self.nodes
|
|
125
|
+
|
|
126
|
+
def iter_nodes(self) -> Iterator[KdlNode]:
|
|
127
|
+
"""Iterate all nodes in the document in pre-order (depth-first).
|
|
128
|
+
|
|
129
|
+
Yields:
|
|
130
|
+
Each KdlNode in the tree, parent before children.
|
|
131
|
+
"""
|
|
132
|
+
stack: list[KdlNode] = list(reversed(self.nodes))
|
|
133
|
+
while stack:
|
|
134
|
+
node = stack.pop()
|
|
135
|
+
yield node
|
|
136
|
+
for child in reversed(node.children):
|
|
137
|
+
stack.append(child)
|
|
138
|
+
|
|
139
|
+
def select(self, selector: str) -> list[KdlNode]:
|
|
140
|
+
"""Select nodes matching a CSS3-like selector.
|
|
141
|
+
|
|
142
|
+
Args:
|
|
143
|
+
selector: A CSS3-compatible selector string.
|
|
144
|
+
|
|
145
|
+
Returns:
|
|
146
|
+
List of matching nodes in document order.
|
|
147
|
+
|
|
148
|
+
Raises:
|
|
149
|
+
SelectorError: If the selector syntax is invalid.
|
|
150
|
+
"""
|
|
151
|
+
from .selector import SelectorMatcher, _parse_selector
|
|
152
|
+
|
|
153
|
+
sel = _parse_selector(selector)
|
|
154
|
+
return SelectorMatcher(self).match(sel)
|
|
155
|
+
|
|
156
|
+
def select_one(self, selector: str) -> KdlNode | None:
|
|
157
|
+
"""Return the first node matching a selector, or None.
|
|
158
|
+
|
|
159
|
+
Lazily evaluates — stops iteration at the first match.
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
selector: A CSS3-compatible selector string.
|
|
163
|
+
|
|
164
|
+
Returns:
|
|
165
|
+
The first matching node in document order, or ``None``.
|
|
166
|
+
|
|
167
|
+
Raises:
|
|
168
|
+
SelectorError: If the selector syntax is invalid.
|
|
169
|
+
"""
|
|
170
|
+
from .selector import SelectorMatcher, _parse_selector
|
|
171
|
+
|
|
172
|
+
sel = _parse_selector(selector)
|
|
173
|
+
return SelectorMatcher(self).match_one(sel)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
__all__ = [
|
|
177
|
+
"KdlDocument",
|
|
178
|
+
]
|