vex-ast 0.2.5__py3-none-any.whl → 0.2.7__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.
- vex_ast/README.md +101 -51
- vex_ast/READMEAPI.md +133 -318
- vex_ast/__init__.py +81 -81
- vex_ast/ast/README.md +87 -87
- vex_ast/ast/__init__.py +74 -74
- vex_ast/ast/core.py +71 -71
- vex_ast/ast/expressions.py +276 -276
- vex_ast/ast/interfaces.py +208 -208
- vex_ast/ast/literals.py +80 -80
- vex_ast/ast/navigator.py +241 -225
- vex_ast/ast/operators.py +135 -135
- vex_ast/ast/statements.py +351 -351
- vex_ast/ast/validators.py +121 -121
- vex_ast/ast/vex_nodes.py +279 -279
- vex_ast/parser/README.md +47 -47
- vex_ast/parser/__init__.py +26 -26
- vex_ast/parser/factory.py +190 -190
- vex_ast/parser/interfaces.py +34 -34
- vex_ast/parser/python_parser.py +831 -831
- vex_ast/registry/README.md +107 -29
- vex_ast/registry/__init__.py +51 -51
- vex_ast/registry/api.py +190 -155
- vex_ast/registry/categories.py +179 -136
- vex_ast/registry/functions/__init__.py +10 -10
- vex_ast/registry/functions/constructors.py +71 -35
- vex_ast/registry/functions/display.py +146 -146
- vex_ast/registry/functions/drivetrain.py +163 -163
- vex_ast/registry/functions/initialize.py +31 -31
- vex_ast/registry/functions/motor.py +140 -140
- vex_ast/registry/functions/sensors.py +194 -194
- vex_ast/registry/functions/timing.py +103 -103
- vex_ast/registry/language_map.py +77 -77
- vex_ast/registry/registry.py +164 -153
- vex_ast/registry/signature.py +269 -191
- vex_ast/registry/simulation_behavior.py +8 -8
- vex_ast/registry/validation.py +43 -43
- vex_ast/serialization/__init__.py +37 -37
- vex_ast/serialization/json_deserializer.py +284 -284
- vex_ast/serialization/json_serializer.py +148 -148
- vex_ast/serialization/schema.py +492 -492
- vex_ast/types/README.md +78 -26
- vex_ast/types/__init__.py +140 -140
- vex_ast/types/base.py +83 -83
- vex_ast/types/enums.py +122 -122
- vex_ast/types/objects.py +64 -64
- vex_ast/types/primitives.py +68 -68
- vex_ast/types/type_checker.py +31 -31
- vex_ast/utils/README.md +39 -39
- vex_ast/utils/__init__.py +37 -37
- vex_ast/utils/errors.py +112 -112
- vex_ast/utils/source_location.py +38 -38
- vex_ast/utils/type_definitions.py +8 -8
- vex_ast/visitors/README.md +49 -49
- vex_ast/visitors/__init__.py +27 -27
- vex_ast/visitors/analyzer.py +102 -102
- vex_ast/visitors/base.py +133 -133
- vex_ast/visitors/printer.py +196 -196
- {vex_ast-0.2.5.dist-info → vex_ast-0.2.7.dist-info}/METADATA +206 -174
- vex_ast-0.2.7.dist-info/RECORD +64 -0
- vex_ast-0.2.5.dist-info/RECORD +0 -64
- {vex_ast-0.2.5.dist-info → vex_ast-0.2.7.dist-info}/WHEEL +0 -0
- {vex_ast-0.2.5.dist-info → vex_ast-0.2.7.dist-info}/licenses/LICENSE +0 -0
- {vex_ast-0.2.5.dist-info → vex_ast-0.2.7.dist-info}/top_level.txt +0 -0
@@ -1,148 +1,148 @@
|
|
1
|
-
"""
|
2
|
-
JSON serialization for AST nodes.
|
3
|
-
|
4
|
-
This module provides functionality to convert AST nodes to JSON format.
|
5
|
-
"""
|
6
|
-
|
7
|
-
import json
|
8
|
-
from typing import Any, Dict, List, Optional, Union, cast
|
9
|
-
|
10
|
-
from ..ast.interfaces import IAstNode
|
11
|
-
from ..visitors.base import AstVisitor
|
12
|
-
from ..utils.source_location import SourceLocation
|
13
|
-
|
14
|
-
|
15
|
-
class SerializationVisitor(AstVisitor[Dict[str, Any]]):
|
16
|
-
"""
|
17
|
-
Visitor that converts AST nodes to dictionary representations.
|
18
|
-
|
19
|
-
This visitor traverses the AST and converts each node to a dictionary
|
20
|
-
that can be serialized to JSON. The dictionaries include node type
|
21
|
-
information and all relevant attributes.
|
22
|
-
"""
|
23
|
-
|
24
|
-
def generic_visit(self, node: IAstNode) -> Dict[str, Any]:
|
25
|
-
"""
|
26
|
-
Default serialization logic for all node types.
|
27
|
-
|
28
|
-
Args:
|
29
|
-
node: The AST node to serialize
|
30
|
-
|
31
|
-
Returns:
|
32
|
-
A dictionary representation of the node
|
33
|
-
"""
|
34
|
-
# Get the node's class name for type information
|
35
|
-
node_type = node.__class__.__name__
|
36
|
-
|
37
|
-
# Start with basic node information
|
38
|
-
result = {
|
39
|
-
"type": node_type,
|
40
|
-
}
|
41
|
-
|
42
|
-
# Add source location if available
|
43
|
-
if node.location:
|
44
|
-
result["location"] = self._serialize_location(node.location)
|
45
|
-
|
46
|
-
# Add all attributes from the node
|
47
|
-
attributes = node.get_attributes()
|
48
|
-
for name, value in attributes.items():
|
49
|
-
# Skip internal attributes and parent reference
|
50
|
-
if name.startswith('_'):
|
51
|
-
continue
|
52
|
-
|
53
|
-
# Handle different attribute types
|
54
|
-
result[name] = self._serialize_attribute(value)
|
55
|
-
|
56
|
-
return result
|
57
|
-
|
58
|
-
def _serialize_location(self, location: SourceLocation) -> Dict[str, Any]:
|
59
|
-
"""
|
60
|
-
Serialize a source location to a dictionary.
|
61
|
-
|
62
|
-
Args:
|
63
|
-
location: The source location to serialize
|
64
|
-
|
65
|
-
Returns:
|
66
|
-
A dictionary representation of the source location
|
67
|
-
"""
|
68
|
-
result = {
|
69
|
-
"line": location.line,
|
70
|
-
"column": location.column,
|
71
|
-
}
|
72
|
-
|
73
|
-
if location.end_line is not None:
|
74
|
-
result["end_line"] = location.end_line
|
75
|
-
|
76
|
-
if location.end_column is not None:
|
77
|
-
result["end_column"] = location.end_column
|
78
|
-
|
79
|
-
if location.filename:
|
80
|
-
result["filename"] = location.filename
|
81
|
-
|
82
|
-
return result
|
83
|
-
|
84
|
-
def _serialize_attribute(self, value: Any) -> Any:
|
85
|
-
"""
|
86
|
-
Serialize an attribute value based on its type.
|
87
|
-
|
88
|
-
Args:
|
89
|
-
value: The attribute value to serialize
|
90
|
-
|
91
|
-
Returns:
|
92
|
-
A serialized representation of the value
|
93
|
-
"""
|
94
|
-
# Handle None
|
95
|
-
if value is None:
|
96
|
-
return None
|
97
|
-
|
98
|
-
# Handle AST nodes
|
99
|
-
if isinstance(value, IAstNode):
|
100
|
-
return self.visit(value)
|
101
|
-
|
102
|
-
# Handle lists of values
|
103
|
-
if isinstance(value, list):
|
104
|
-
return [self._serialize_attribute(item) for item in value]
|
105
|
-
|
106
|
-
# Handle dictionaries
|
107
|
-
if isinstance(value, dict):
|
108
|
-
return {k: self._serialize_attribute(v) for k, v in value.items()}
|
109
|
-
|
110
|
-
# Handle basic types (strings, numbers, booleans)
|
111
|
-
if isinstance(value, (str, int, float, bool)):
|
112
|
-
return value
|
113
|
-
|
114
|
-
# Handle Operator enum values
|
115
|
-
if hasattr(value, '__module__') and 'operators' in value.__module__ and hasattr(value, 'value'):
|
116
|
-
return value.value
|
117
|
-
|
118
|
-
# For other types, convert to string
|
119
|
-
return str(value)
|
120
|
-
|
121
|
-
|
122
|
-
def serialize_ast_to_dict(ast: IAstNode) -> Dict[str, Any]:
|
123
|
-
"""
|
124
|
-
Convert an AST node to a dictionary representation.
|
125
|
-
|
126
|
-
Args:
|
127
|
-
ast: The AST node to serialize
|
128
|
-
|
129
|
-
Returns:
|
130
|
-
A dictionary representation of the AST
|
131
|
-
"""
|
132
|
-
visitor = SerializationVisitor()
|
133
|
-
return visitor.visit(ast)
|
134
|
-
|
135
|
-
|
136
|
-
def serialize_ast_to_json(ast: IAstNode, indent: Optional[int] = None) -> str:
|
137
|
-
"""
|
138
|
-
Convert an AST node to a JSON string.
|
139
|
-
|
140
|
-
Args:
|
141
|
-
ast: The AST node to serialize
|
142
|
-
indent: Optional indentation level for pretty-printing
|
143
|
-
|
144
|
-
Returns:
|
145
|
-
A JSON string representation of the AST
|
146
|
-
"""
|
147
|
-
data = serialize_ast_to_dict(ast)
|
148
|
-
return json.dumps(data, indent=indent)
|
1
|
+
"""
|
2
|
+
JSON serialization for AST nodes.
|
3
|
+
|
4
|
+
This module provides functionality to convert AST nodes to JSON format.
|
5
|
+
"""
|
6
|
+
|
7
|
+
import json
|
8
|
+
from typing import Any, Dict, List, Optional, Union, cast
|
9
|
+
|
10
|
+
from ..ast.interfaces import IAstNode
|
11
|
+
from ..visitors.base import AstVisitor
|
12
|
+
from ..utils.source_location import SourceLocation
|
13
|
+
|
14
|
+
|
15
|
+
class SerializationVisitor(AstVisitor[Dict[str, Any]]):
|
16
|
+
"""
|
17
|
+
Visitor that converts AST nodes to dictionary representations.
|
18
|
+
|
19
|
+
This visitor traverses the AST and converts each node to a dictionary
|
20
|
+
that can be serialized to JSON. The dictionaries include node type
|
21
|
+
information and all relevant attributes.
|
22
|
+
"""
|
23
|
+
|
24
|
+
def generic_visit(self, node: IAstNode) -> Dict[str, Any]:
|
25
|
+
"""
|
26
|
+
Default serialization logic for all node types.
|
27
|
+
|
28
|
+
Args:
|
29
|
+
node: The AST node to serialize
|
30
|
+
|
31
|
+
Returns:
|
32
|
+
A dictionary representation of the node
|
33
|
+
"""
|
34
|
+
# Get the node's class name for type information
|
35
|
+
node_type = node.__class__.__name__
|
36
|
+
|
37
|
+
# Start with basic node information
|
38
|
+
result = {
|
39
|
+
"type": node_type,
|
40
|
+
}
|
41
|
+
|
42
|
+
# Add source location if available
|
43
|
+
if node.location:
|
44
|
+
result["location"] = self._serialize_location(node.location)
|
45
|
+
|
46
|
+
# Add all attributes from the node
|
47
|
+
attributes = node.get_attributes()
|
48
|
+
for name, value in attributes.items():
|
49
|
+
# Skip internal attributes and parent reference
|
50
|
+
if name.startswith('_'):
|
51
|
+
continue
|
52
|
+
|
53
|
+
# Handle different attribute types
|
54
|
+
result[name] = self._serialize_attribute(value)
|
55
|
+
|
56
|
+
return result
|
57
|
+
|
58
|
+
def _serialize_location(self, location: SourceLocation) -> Dict[str, Any]:
|
59
|
+
"""
|
60
|
+
Serialize a source location to a dictionary.
|
61
|
+
|
62
|
+
Args:
|
63
|
+
location: The source location to serialize
|
64
|
+
|
65
|
+
Returns:
|
66
|
+
A dictionary representation of the source location
|
67
|
+
"""
|
68
|
+
result = {
|
69
|
+
"line": location.line,
|
70
|
+
"column": location.column,
|
71
|
+
}
|
72
|
+
|
73
|
+
if location.end_line is not None:
|
74
|
+
result["end_line"] = location.end_line
|
75
|
+
|
76
|
+
if location.end_column is not None:
|
77
|
+
result["end_column"] = location.end_column
|
78
|
+
|
79
|
+
if location.filename:
|
80
|
+
result["filename"] = location.filename
|
81
|
+
|
82
|
+
return result
|
83
|
+
|
84
|
+
def _serialize_attribute(self, value: Any) -> Any:
|
85
|
+
"""
|
86
|
+
Serialize an attribute value based on its type.
|
87
|
+
|
88
|
+
Args:
|
89
|
+
value: The attribute value to serialize
|
90
|
+
|
91
|
+
Returns:
|
92
|
+
A serialized representation of the value
|
93
|
+
"""
|
94
|
+
# Handle None
|
95
|
+
if value is None:
|
96
|
+
return None
|
97
|
+
|
98
|
+
# Handle AST nodes
|
99
|
+
if isinstance(value, IAstNode):
|
100
|
+
return self.visit(value)
|
101
|
+
|
102
|
+
# Handle lists of values
|
103
|
+
if isinstance(value, list):
|
104
|
+
return [self._serialize_attribute(item) for item in value]
|
105
|
+
|
106
|
+
# Handle dictionaries
|
107
|
+
if isinstance(value, dict):
|
108
|
+
return {k: self._serialize_attribute(v) for k, v in value.items()}
|
109
|
+
|
110
|
+
# Handle basic types (strings, numbers, booleans)
|
111
|
+
if isinstance(value, (str, int, float, bool)):
|
112
|
+
return value
|
113
|
+
|
114
|
+
# Handle Operator enum values
|
115
|
+
if hasattr(value, '__module__') and 'operators' in value.__module__ and hasattr(value, 'value'):
|
116
|
+
return value.value
|
117
|
+
|
118
|
+
# For other types, convert to string
|
119
|
+
return str(value)
|
120
|
+
|
121
|
+
|
122
|
+
def serialize_ast_to_dict(ast: IAstNode) -> Dict[str, Any]:
|
123
|
+
"""
|
124
|
+
Convert an AST node to a dictionary representation.
|
125
|
+
|
126
|
+
Args:
|
127
|
+
ast: The AST node to serialize
|
128
|
+
|
129
|
+
Returns:
|
130
|
+
A dictionary representation of the AST
|
131
|
+
"""
|
132
|
+
visitor = SerializationVisitor()
|
133
|
+
return visitor.visit(ast)
|
134
|
+
|
135
|
+
|
136
|
+
def serialize_ast_to_json(ast: IAstNode, indent: Optional[int] = None) -> str:
|
137
|
+
"""
|
138
|
+
Convert an AST node to a JSON string.
|
139
|
+
|
140
|
+
Args:
|
141
|
+
ast: The AST node to serialize
|
142
|
+
indent: Optional indentation level for pretty-printing
|
143
|
+
|
144
|
+
Returns:
|
145
|
+
A JSON string representation of the AST
|
146
|
+
"""
|
147
|
+
data = serialize_ast_to_dict(ast)
|
148
|
+
return json.dumps(data, indent=indent)
|