vex-ast 0.2.4__py3-none-any.whl → 0.2.6__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.
Files changed (63) hide show
  1. vex_ast/README.md +101 -51
  2. vex_ast/READMEAPI.md +133 -318
  3. vex_ast/__init__.py +81 -72
  4. vex_ast/ast/README.md +87 -87
  5. vex_ast/ast/__init__.py +74 -74
  6. vex_ast/ast/core.py +71 -71
  7. vex_ast/ast/expressions.py +276 -276
  8. vex_ast/ast/interfaces.py +208 -208
  9. vex_ast/ast/literals.py +80 -80
  10. vex_ast/ast/navigator.py +225 -225
  11. vex_ast/ast/operators.py +135 -135
  12. vex_ast/ast/statements.py +351 -351
  13. vex_ast/ast/validators.py +121 -120
  14. vex_ast/ast/vex_nodes.py +279 -279
  15. vex_ast/parser/README.md +47 -47
  16. vex_ast/parser/__init__.py +26 -26
  17. vex_ast/parser/factory.py +190 -190
  18. vex_ast/parser/interfaces.py +34 -34
  19. vex_ast/parser/python_parser.py +831 -786
  20. vex_ast/registry/README.md +107 -29
  21. vex_ast/registry/__init__.py +51 -51
  22. vex_ast/registry/api.py +190 -155
  23. vex_ast/registry/categories.py +179 -136
  24. vex_ast/registry/functions/__init__.py +10 -10
  25. vex_ast/registry/functions/constructors.py +71 -0
  26. vex_ast/registry/functions/display.py +146 -146
  27. vex_ast/registry/functions/drivetrain.py +163 -163
  28. vex_ast/registry/functions/initialize.py +31 -28
  29. vex_ast/registry/functions/motor.py +140 -140
  30. vex_ast/registry/functions/sensors.py +194 -194
  31. vex_ast/registry/functions/timing.py +103 -103
  32. vex_ast/registry/language_map.py +77 -77
  33. vex_ast/registry/registry.py +164 -153
  34. vex_ast/registry/signature.py +269 -191
  35. vex_ast/registry/simulation_behavior.py +8 -8
  36. vex_ast/registry/validation.py +43 -43
  37. vex_ast/serialization/__init__.py +37 -37
  38. vex_ast/serialization/json_deserializer.py +284 -275
  39. vex_ast/serialization/json_serializer.py +148 -148
  40. vex_ast/serialization/schema.py +492 -470
  41. vex_ast/types/README.md +78 -26
  42. vex_ast/types/__init__.py +140 -140
  43. vex_ast/types/base.py +83 -83
  44. vex_ast/types/enums.py +122 -97
  45. vex_ast/types/objects.py +64 -64
  46. vex_ast/types/primitives.py +68 -68
  47. vex_ast/types/type_checker.py +31 -31
  48. vex_ast/utils/README.md +39 -39
  49. vex_ast/utils/__init__.py +37 -37
  50. vex_ast/utils/errors.py +112 -112
  51. vex_ast/utils/source_location.py +38 -38
  52. vex_ast/utils/type_definitions.py +8 -8
  53. vex_ast/visitors/README.md +49 -49
  54. vex_ast/visitors/__init__.py +27 -27
  55. vex_ast/visitors/analyzer.py +102 -102
  56. vex_ast/visitors/base.py +133 -133
  57. vex_ast/visitors/printer.py +196 -146
  58. {vex_ast-0.2.4.dist-info → vex_ast-0.2.6.dist-info}/METADATA +206 -174
  59. vex_ast-0.2.6.dist-info/RECORD +64 -0
  60. vex_ast-0.2.4.dist-info/RECORD +0 -63
  61. {vex_ast-0.2.4.dist-info → vex_ast-0.2.6.dist-info}/WHEEL +0 -0
  62. {vex_ast-0.2.4.dist-info → vex_ast-0.2.6.dist-info}/licenses/LICENSE +0 -0
  63. {vex_ast-0.2.4.dist-info → vex_ast-0.2.6.dist-info}/top_level.txt +0 -0
vex_ast/ast/navigator.py CHANGED
@@ -1,225 +1,225 @@
1
- """AST navigation utilities.
2
-
3
- This module provides utilities for navigating the AST structure,
4
- hiding implementation details and providing a more stable interface.
5
- """
6
-
7
- from typing import Dict, List, Optional, Set, Iterator, TypeVar, Generic, Type, cast, Any, Union
8
-
9
- from .interfaces import IAstNode, IExpression, IStatement, ILiteral, IIdentifier, IFunctionCall
10
- from .core import Program
11
- from .expressions import (
12
- Identifier, VariableReference, AttributeAccess,
13
- BinaryOperation, UnaryOperation, FunctionCall
14
- )
15
- from .statements import (
16
- ExpressionStatement, Assignment, FunctionDefinition,
17
- IfStatement, WhileLoop, ForLoop, ReturnStatement
18
- )
19
- from .literals import NumberLiteral, StringLiteral, BooleanLiteral
20
- from .vex_nodes import VexAPICall
21
-
22
- T = TypeVar('T', bound=IAstNode)
23
-
24
- class AstNavigator:
25
- """Navigator for traversing and querying the AST."""
26
-
27
- def __init__(self, root: IAstNode):
28
- """Initialize with a root node."""
29
- self.root = root
30
-
31
- def find_all(self, node_type: Type[T]) -> List[T]:
32
- """Find all nodes of a specific type in the AST.
33
-
34
- Args:
35
- node_type: The type of node to find
36
-
37
- Returns:
38
- List of nodes matching the type
39
- """
40
- result: List[T] = []
41
- self._find_all_recursive(self.root, node_type, result)
42
- return result
43
-
44
- def _find_all_recursive(self, node: IAstNode, node_type: Type[T], result: List[T]) -> None:
45
- """Recursively find all nodes of a specific type."""
46
- if isinstance(node, node_type):
47
- result.append(cast(T, node))
48
-
49
- for child in node.get_children():
50
- self._find_all_recursive(child, node_type, result)
51
-
52
- def find_first(self, node_type: Type[T]) -> Optional[T]:
53
- """Find the first node of a specific type in the AST.
54
-
55
- Args:
56
- node_type: The type of node to find
57
-
58
- Returns:
59
- The first node matching the type, or None if not found
60
- """
61
- return next(self.find_iter(node_type), None)
62
-
63
- def find_iter(self, node_type: Type[T]) -> Iterator[T]:
64
- """Find all nodes of a specific type as an iterator.
65
-
66
- Args:
67
- node_type: The type of node to find
68
-
69
- Returns:
70
- Iterator of nodes matching the type
71
- """
72
- for node in self._traverse_iter(self.root):
73
- if isinstance(node, node_type):
74
- yield cast(T, node)
75
-
76
- def _traverse_iter(self, node: IAstNode) -> Iterator[IAstNode]:
77
- """Traverse the AST in pre-order."""
78
- yield node
79
- for child in node.get_children():
80
- yield from self._traverse_iter(child)
81
-
82
- def find_parent(self, node: IAstNode) -> Optional[IAstNode]:
83
- """Find the parent of a node.
84
-
85
- Args:
86
- node: The node to find the parent of
87
-
88
- Returns:
89
- The parent node, or None if not found or if the node is the root
90
- """
91
- return node.get_parent()
92
-
93
- def find_ancestors(self, node: IAstNode) -> List[IAstNode]:
94
- """Find all ancestors of a node.
95
-
96
- Args:
97
- node: The node to find ancestors of
98
-
99
- Returns:
100
- List of ancestor nodes, from immediate parent to root
101
- """
102
- result: List[IAstNode] = []
103
- current = node.get_parent()
104
- while current:
105
- result.append(current)
106
- current = current.get_parent()
107
- return result
108
-
109
- def find_siblings(self, node: IAstNode) -> List[IAstNode]:
110
- """Find all siblings of a node.
111
-
112
- Args:
113
- node: The node to find siblings of
114
-
115
- Returns:
116
- List of sibling nodes, excluding the node itself
117
- """
118
- parent = node.get_parent()
119
- if not parent:
120
- return []
121
-
122
- return [child for child in parent.get_children() if child is not node]
123
-
124
- # Specific node type finders
125
-
126
- def find_identifiers(self) -> List[IIdentifier]:
127
- """Find all identifiers in the AST."""
128
- return self.find_all(Identifier)
129
-
130
- def find_function_calls(self) -> List[IFunctionCall]:
131
- """Find all function calls in the AST."""
132
- return self.find_all(FunctionCall)
133
-
134
- def find_vex_api_calls(self) -> List[VexAPICall]:
135
- """Find all VEX API calls in the AST."""
136
- return self.find_all(VexAPICall)
137
-
138
- def find_assignments(self) -> List[Assignment]:
139
- """Find all assignments in the AST."""
140
- return self.find_all(Assignment)
141
-
142
- def find_function_definitions(self) -> List[FunctionDefinition]:
143
- """Find all function definitions in the AST."""
144
- return self.find_all(FunctionDefinition)
145
-
146
- def find_if_statements(self) -> List[IfStatement]:
147
- """Find all if statements in the AST."""
148
- return self.find_all(IfStatement)
149
-
150
- def find_loops(self) -> List[Union[WhileLoop, ForLoop]]:
151
- """Find all loops in the AST."""
152
- while_loops = self.find_all(WhileLoop)
153
- for_loops = self.find_all(ForLoop)
154
- return cast(List[Union[WhileLoop, ForLoop]], while_loops + for_loops)
155
-
156
- def find_return_statements(self) -> List[ReturnStatement]:
157
- """Find all return statements in the AST."""
158
- return self.find_all(ReturnStatement)
159
-
160
- def find_literals(self) -> List[ILiteral]:
161
- """Find all literals in the AST."""
162
- return cast(List[ILiteral],
163
- self.find_all(NumberLiteral) +
164
- self.find_all(StringLiteral) +
165
- self.find_all(BooleanLiteral))
166
-
167
- # Program-specific methods
168
-
169
- def get_statements(self) -> List[IStatement]:
170
- """Get all top-level statements in the program."""
171
- if isinstance(self.root, Program):
172
- return self.root.get_statements()
173
- return []
174
-
175
- def get_function_by_name(self, name: str) -> Optional[FunctionDefinition]:
176
- """Find a function definition by name.
177
-
178
- Args:
179
- name: The function name
180
-
181
- Returns:
182
- The function definition, or None if not found
183
- """
184
- for func in self.find_function_definitions():
185
- if func.get_name() == name:
186
- return func
187
- return None
188
-
189
- def get_variable_references(self, name: str) -> List[VariableReference]:
190
- """Find all references to a variable.
191
-
192
- Args:
193
- name: The variable name
194
-
195
- Returns:
196
- List of variable references
197
- """
198
- return [ref for ref in self.find_all(VariableReference) if ref.name == name]
199
-
200
- def get_attribute_accesses(self, object_name: str) -> List[AttributeAccess]:
201
- """Find all attribute accesses on an object.
202
-
203
- Args:
204
- object_name: The object name
205
-
206
- Returns:
207
- List of attribute accesses
208
- """
209
- result = []
210
- for access in self.find_all(AttributeAccess):
211
- if hasattr(access.object, 'name') and getattr(access.object, 'name') == object_name:
212
- result.append(access)
213
- return result
214
-
215
-
216
- def create_navigator(ast_node: IAstNode) -> AstNavigator:
217
- """Create a new AST navigator for the given AST node.
218
-
219
- Args:
220
- ast_node: The AST node to navigate
221
-
222
- Returns:
223
- A new AstNavigator instance
224
- """
225
- return AstNavigator(ast_node)
1
+ """AST navigation utilities.
2
+
3
+ This module provides utilities for navigating the AST structure,
4
+ hiding implementation details and providing a more stable interface.
5
+ """
6
+
7
+ from typing import Dict, List, Optional, Set, Iterator, TypeVar, Generic, Type, cast, Any, Union
8
+
9
+ from .interfaces import IAstNode, IExpression, IStatement, ILiteral, IIdentifier, IFunctionCall
10
+ from .core import Program
11
+ from .expressions import (
12
+ Identifier, VariableReference, AttributeAccess,
13
+ BinaryOperation, UnaryOperation, FunctionCall
14
+ )
15
+ from .statements import (
16
+ ExpressionStatement, Assignment, FunctionDefinition,
17
+ IfStatement, WhileLoop, ForLoop, ReturnStatement
18
+ )
19
+ from .literals import NumberLiteral, StringLiteral, BooleanLiteral
20
+ from .vex_nodes import VexAPICall
21
+
22
+ T = TypeVar('T', bound=IAstNode)
23
+
24
+ class AstNavigator:
25
+ """Navigator for traversing and querying the AST."""
26
+
27
+ def __init__(self, root: IAstNode):
28
+ """Initialize with a root node."""
29
+ self.root = root
30
+
31
+ def find_all(self, node_type: Type[T]) -> List[T]:
32
+ """Find all nodes of a specific type in the AST.
33
+
34
+ Args:
35
+ node_type: The type of node to find
36
+
37
+ Returns:
38
+ List of nodes matching the type
39
+ """
40
+ result: List[T] = []
41
+ self._find_all_recursive(self.root, node_type, result)
42
+ return result
43
+
44
+ def _find_all_recursive(self, node: IAstNode, node_type: Type[T], result: List[T]) -> None:
45
+ """Recursively find all nodes of a specific type."""
46
+ if isinstance(node, node_type):
47
+ result.append(cast(T, node))
48
+
49
+ for child in node.get_children():
50
+ self._find_all_recursive(child, node_type, result)
51
+
52
+ def find_first(self, node_type: Type[T]) -> Optional[T]:
53
+ """Find the first node of a specific type in the AST.
54
+
55
+ Args:
56
+ node_type: The type of node to find
57
+
58
+ Returns:
59
+ The first node matching the type, or None if not found
60
+ """
61
+ return next(self.find_iter(node_type), None)
62
+
63
+ def find_iter(self, node_type: Type[T]) -> Iterator[T]:
64
+ """Find all nodes of a specific type as an iterator.
65
+
66
+ Args:
67
+ node_type: The type of node to find
68
+
69
+ Returns:
70
+ Iterator of nodes matching the type
71
+ """
72
+ for node in self._traverse_iter(self.root):
73
+ if isinstance(node, node_type):
74
+ yield cast(T, node)
75
+
76
+ def _traverse_iter(self, node: IAstNode) -> Iterator[IAstNode]:
77
+ """Traverse the AST in pre-order."""
78
+ yield node
79
+ for child in node.get_children():
80
+ yield from self._traverse_iter(child)
81
+
82
+ def find_parent(self, node: IAstNode) -> Optional[IAstNode]:
83
+ """Find the parent of a node.
84
+
85
+ Args:
86
+ node: The node to find the parent of
87
+
88
+ Returns:
89
+ The parent node, or None if not found or if the node is the root
90
+ """
91
+ return node.get_parent()
92
+
93
+ def find_ancestors(self, node: IAstNode) -> List[IAstNode]:
94
+ """Find all ancestors of a node.
95
+
96
+ Args:
97
+ node: The node to find ancestors of
98
+
99
+ Returns:
100
+ List of ancestor nodes, from immediate parent to root
101
+ """
102
+ result: List[IAstNode] = []
103
+ current = node.get_parent()
104
+ while current:
105
+ result.append(current)
106
+ current = current.get_parent()
107
+ return result
108
+
109
+ def find_siblings(self, node: IAstNode) -> List[IAstNode]:
110
+ """Find all siblings of a node.
111
+
112
+ Args:
113
+ node: The node to find siblings of
114
+
115
+ Returns:
116
+ List of sibling nodes, excluding the node itself
117
+ """
118
+ parent = node.get_parent()
119
+ if not parent:
120
+ return []
121
+
122
+ return [child for child in parent.get_children() if child is not node]
123
+
124
+ # Specific node type finders
125
+
126
+ def find_identifiers(self) -> List[IIdentifier]:
127
+ """Find all identifiers in the AST."""
128
+ return self.find_all(Identifier)
129
+
130
+ def find_function_calls(self) -> List[IFunctionCall]:
131
+ """Find all function calls in the AST."""
132
+ return self.find_all(FunctionCall)
133
+
134
+ def find_vex_api_calls(self) -> List[VexAPICall]:
135
+ """Find all VEX API calls in the AST."""
136
+ return self.find_all(VexAPICall)
137
+
138
+ def find_assignments(self) -> List[Assignment]:
139
+ """Find all assignments in the AST."""
140
+ return self.find_all(Assignment)
141
+
142
+ def find_function_definitions(self) -> List[FunctionDefinition]:
143
+ """Find all function definitions in the AST."""
144
+ return self.find_all(FunctionDefinition)
145
+
146
+ def find_if_statements(self) -> List[IfStatement]:
147
+ """Find all if statements in the AST."""
148
+ return self.find_all(IfStatement)
149
+
150
+ def find_loops(self) -> List[Union[WhileLoop, ForLoop]]:
151
+ """Find all loops in the AST."""
152
+ while_loops = self.find_all(WhileLoop)
153
+ for_loops = self.find_all(ForLoop)
154
+ return cast(List[Union[WhileLoop, ForLoop]], while_loops + for_loops)
155
+
156
+ def find_return_statements(self) -> List[ReturnStatement]:
157
+ """Find all return statements in the AST."""
158
+ return self.find_all(ReturnStatement)
159
+
160
+ def find_literals(self) -> List[ILiteral]:
161
+ """Find all literals in the AST."""
162
+ return cast(List[ILiteral],
163
+ self.find_all(NumberLiteral) +
164
+ self.find_all(StringLiteral) +
165
+ self.find_all(BooleanLiteral))
166
+
167
+ # Program-specific methods
168
+
169
+ def get_statements(self) -> List[IStatement]:
170
+ """Get all top-level statements in the program."""
171
+ if isinstance(self.root, Program):
172
+ return self.root.get_statements()
173
+ return []
174
+
175
+ def get_function_by_name(self, name: str) -> Optional[FunctionDefinition]:
176
+ """Find a function definition by name.
177
+
178
+ Args:
179
+ name: The function name
180
+
181
+ Returns:
182
+ The function definition, or None if not found
183
+ """
184
+ for func in self.find_function_definitions():
185
+ if func.get_name() == name:
186
+ return func
187
+ return None
188
+
189
+ def get_variable_references(self, name: str) -> List[VariableReference]:
190
+ """Find all references to a variable.
191
+
192
+ Args:
193
+ name: The variable name
194
+
195
+ Returns:
196
+ List of variable references
197
+ """
198
+ return [ref for ref in self.find_all(VariableReference) if ref.name == name]
199
+
200
+ def get_attribute_accesses(self, object_name: str) -> List[AttributeAccess]:
201
+ """Find all attribute accesses on an object.
202
+
203
+ Args:
204
+ object_name: The object name
205
+
206
+ Returns:
207
+ List of attribute accesses
208
+ """
209
+ result = []
210
+ for access in self.find_all(AttributeAccess):
211
+ if hasattr(access.object, 'name') and getattr(access.object, 'name') == object_name:
212
+ result.append(access)
213
+ return result
214
+
215
+
216
+ def create_navigator(ast_node: IAstNode) -> AstNavigator:
217
+ """Create a new AST navigator for the given AST node.
218
+
219
+ Args:
220
+ ast_node: The AST node to navigate
221
+
222
+ Returns:
223
+ A new AstNavigator instance
224
+ """
225
+ return AstNavigator(ast_node)