-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor.py
More file actions
40 lines (33 loc) · 1021 Bytes
/
constructor.py
File metadata and controls
40 lines (33 loc) · 1021 Bytes
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
# SPDX-License-Identifier: Apache-2.0
"""
Final loading stage. Python object construction from MOON AST.
"""
from typing import Any, Dict, Iterable
from moon.hooks.tags import resolve_tag
from moon.schemas import (
ASTNode,
DuplicateIdentifierNode,
TagNode,
UnexpectedNode,
UnknownNode,
)
def construct(ast: Iterable[ASTNode]) -> Dict[str, Any]:
"""
Builds Python dict from MOON AST.
:param ast: MOON AST node.
:return: Python dict.
"""
res = dict()
for node in ast:
if isinstance(node, TagNode):
hook = resolve_tag(node.tag)
if not hook:
raise UnknownNode(f"Unexpected AST node: {node}")
if node.name in res:
raise DuplicateIdentifierNode(f"Duplicate identifier: {node.name}")
res[node.name] = hook.construct(node)
else:
raise UnexpectedNode(
f"Unexpected AST node: {node}. Only tag nodes allowed on top level."
)
return res