vex-ast 0.1.0__py3-none-any.whl → 0.2.1__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 +51 -0
- vex_ast/READMEAPI.md +318 -0
- vex_ast/__init__.py +20 -17
- vex_ast/ast/README.md +87 -0
- vex_ast/ast/__init__.py +1 -1
- vex_ast/ast/navigator.py +12 -0
- vex_ast/parser/README.md +47 -0
- vex_ast/parser/__init__.py +27 -0
- vex_ast/registry/README.md +29 -0
- vex_ast/registry/functions/__init__.py +11 -0
- vex_ast/registry/functions/display.py +147 -0
- vex_ast/registry/functions/drivetrain.py +163 -0
- vex_ast/registry/functions/initialize.py +28 -0
- vex_ast/registry/functions/motor.py +140 -0
- vex_ast/registry/functions/sensors.py +195 -0
- vex_ast/registry/functions/timing.py +104 -0
- vex_ast/types/README.md +26 -0
- vex_ast/types/__init__.py +140 -0
- vex_ast/types/base.py +84 -0
- vex_ast/types/enums.py +97 -0
- vex_ast/types/objects.py +64 -0
- vex_ast/types/primitives.py +69 -0
- vex_ast/types/type_checker.py +32 -0
- vex_ast/utils/README.md +39 -0
- vex_ast/utils/__init__.py +38 -0
- vex_ast/utils/type_definitions.py +9 -0
- vex_ast/visitors/README.md +49 -0
- vex_ast/visitors/__init__.py +28 -0
- {vex_ast-0.1.0.dist-info → vex_ast-0.2.1.dist-info}/METADATA +9 -11
- vex_ast-0.2.1.dist-info/RECORD +63 -0
- vex_ast-0.2.1.dist-info/licenses/LICENSE +1 -0
- vex_ast-0.1.0.dist-info/RECORD +0 -41
- {vex_ast-0.1.0.dist-info → vex_ast-0.2.1.dist-info}/WHEEL +0 -0
- {vex_ast-0.1.0.dist-info → vex_ast-0.2.1.dist-info}/top_level.txt +0 -0
vex_ast/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
VEX AST Package (vex_ast)
|
2
|
+
|
3
|
+
This directory is the root of the vex_ast Python package. It orchestrates the different components of the VEX AST generation and processing system.
|
4
|
+
|
5
|
+
Purpose
|
6
|
+
|
7
|
+
The vex_ast package provides a unified interface for parsing VEX V5 Python code into an Abstract Syntax Tree (AST) and tools for working with that AST.
|
8
|
+
|
9
|
+
Structure
|
10
|
+
|
11
|
+
The package is organized into several sub-packages:
|
12
|
+
|
13
|
+
ast/: Contains the definitions for all AST node types, representing the structure of the parsed code. See vex_ast/ast/README.md.
|
14
|
+
|
15
|
+
parser/: Includes the parsing logic responsible for converting source code text into an AST instance. See vex_ast/parser/README.md.
|
16
|
+
|
17
|
+
visitors/: Provides implementations of the Visitor pattern for traversing, analyzing, or transforming the AST. See vex_ast/visitors/README.md.
|
18
|
+
|
19
|
+
utils/: Contains utility classes and functions, primarily for error handling and source location tracking. See vex_ast/utils/README.md.
|
20
|
+
|
21
|
+
Core Exports
|
22
|
+
|
23
|
+
The main components are exposed through the vex_ast/__init__.py file, making them easily accessible:
|
24
|
+
|
25
|
+
parse_string(source, ...): Parses Python code from a string.
|
26
|
+
|
27
|
+
parse_file(filepath, ...): Parses Python code from a file.
|
28
|
+
|
29
|
+
Program: The root node type of the generated AST.
|
30
|
+
|
31
|
+
PrintVisitor: A visitor to generate a string representation of the AST.
|
32
|
+
|
33
|
+
NodeCounter: A visitor to count nodes in the AST.
|
34
|
+
|
35
|
+
VariableCollector: A visitor to collect variable names used in the AST.
|
36
|
+
|
37
|
+
ErrorHandler: Class for managing errors during parsing and processing.
|
38
|
+
|
39
|
+
VexSyntaxError, VexAstError: Custom exception types.
|
40
|
+
|
41
|
+
Workflow
|
42
|
+
|
43
|
+
The typical workflow involves:
|
44
|
+
|
45
|
+
Using parse_string or parse_file from this package to generate an AST (Program object) from source code.
|
46
|
+
|
47
|
+
Instantiating one or more visitors from vex_ast.visitors (or custom ones).
|
48
|
+
|
49
|
+
Calling the visit method of the visitor with the root Program node to perform analysis, transformation, or other operations on the AST.
|
50
|
+
|
51
|
+
Using the ErrorHandler to manage and inspect any errors encountered during the process.
|
vex_ast/READMEAPI.md
ADDED
@@ -0,0 +1,318 @@
|
|
1
|
+
# VEX AST (`vex_ast`) - Public API Reference
|
2
|
+
|
3
|
+
This document describes the main functions and classes exposed by the `vex_ast` package, intended for users who want to parse VEX V5 Python code and interact with the resulting Abstract Syntax Tree (AST).
|
4
|
+
|
5
|
+
## Overview
|
6
|
+
|
7
|
+
The core workflow typically involves:
|
8
|
+
|
9
|
+
1. Parsing source code (from a string or file) using `parse_string` or `parse_file`. This requires an optional `ErrorHandler` instance.
|
10
|
+
2. Receiving a `Program` object, which is the root of the generated AST.
|
11
|
+
3. Instantiating one or more `AstVisitor` subclasses (like `PrintVisitor`, `NodeCounter`, `VariableCollector`, or custom ones).
|
12
|
+
4. Calling the visitor's `visit()` method with the `Program` node to traverse the AST and perform actions.
|
13
|
+
5. Checking the `ErrorHandler` instance for any reported errors during parsing or visiting.
|
14
|
+
|
15
|
+
---
|
16
|
+
|
17
|
+
## Parsing Functions
|
18
|
+
|
19
|
+
These functions are the entry points for converting source code into an AST.
|
20
|
+
|
21
|
+
### `parse_string`
|
22
|
+
|
23
|
+
Parses VEX V5 Python code provided as a string.
|
24
|
+
|
25
|
+
**Signature:**
|
26
|
+
|
27
|
+
```python
|
28
|
+
def parse_string(
|
29
|
+
source: str,
|
30
|
+
filename: str = "<string>",
|
31
|
+
error_handler: Optional[ErrorHandler] = None
|
32
|
+
) -> Program:
|
33
|
+
|
34
|
+
|
35
|
+
Arguments:
|
36
|
+
|
37
|
+
source (str): The string containing the Python code to parse.
|
38
|
+
|
39
|
+
filename (str, optional): The name to associate with the source code, used in error messages and source locations. Defaults to "<string>".
|
40
|
+
|
41
|
+
error_handler (Optional[ErrorHandler], optional): An instance of ErrorHandler to collect parsing errors. If None, a default ErrorHandler is created internally (which will raise VexSyntaxError on the first syntax error). It's recommended to provide your own handler to manage errors more flexibly.
|
42
|
+
|
43
|
+
Returns:
|
44
|
+
|
45
|
+
Program: The root node of the generated Abstract Syntax Tree.
|
46
|
+
|
47
|
+
Raises:
|
48
|
+
|
49
|
+
VexSyntaxError: If a syntax error is encountered during parsing and the error_handler is configured to raise errors immediately (or if no handler is provided).
|
50
|
+
|
51
|
+
VexAstError: For other internal parsing or AST conversion issues if the error_handler is configured to raise errors.
|
52
|
+
|
53
|
+
parse_file
|
54
|
+
|
55
|
+
Parses VEX V5 Python code read from a specified file.
|
56
|
+
|
57
|
+
Signature:
|
58
|
+
|
59
|
+
def parse_file(
|
60
|
+
filepath: str,
|
61
|
+
error_handler: Optional[ErrorHandler] = None
|
62
|
+
) -> Program:
|
63
|
+
IGNORE_WHEN_COPYING_START
|
64
|
+
content_copy
|
65
|
+
download
|
66
|
+
Use code with caution.
|
67
|
+
Python
|
68
|
+
IGNORE_WHEN_COPYING_END
|
69
|
+
|
70
|
+
Arguments:
|
71
|
+
|
72
|
+
filepath (str): The path to the Python file to be parsed.
|
73
|
+
|
74
|
+
error_handler (Optional[ErrorHandler], optional): An instance of ErrorHandler to collect parsing errors. See parse_string for details.
|
75
|
+
|
76
|
+
Returns:
|
77
|
+
|
78
|
+
Program: The root node of the generated Abstract Syntax Tree.
|
79
|
+
|
80
|
+
Raises:
|
81
|
+
|
82
|
+
FileNotFoundError: If the specified filepath does not exist.
|
83
|
+
|
84
|
+
IOError: If there is an error reading the file.
|
85
|
+
|
86
|
+
VexSyntaxError: If a syntax error is encountered (see parse_string).
|
87
|
+
|
88
|
+
VexAstError: For other internal parsing errors (see parse_string).
|
89
|
+
|
90
|
+
Core AST Node
|
91
|
+
Program
|
92
|
+
|
93
|
+
Represents the root node of the entire Abstract Syntax Tree.
|
94
|
+
|
95
|
+
Description:
|
96
|
+
|
97
|
+
The Program node is the object returned by parse_string and parse_file. It serves as the entry point for traversing the AST using visitors.
|
98
|
+
|
99
|
+
Key Attribute:
|
100
|
+
|
101
|
+
body (List[IStatement]): A list containing the top-level statements (like function definitions, assignments, expression statements) found in the parsed code.
|
102
|
+
|
103
|
+
Usage:
|
104
|
+
|
105
|
+
You typically don't instantiate Program directly. You receive it from a parsing function and pass it to the visit() method of an AstVisitor.
|
106
|
+
|
107
|
+
ast_root: Program = parse_string("x = 1")
|
108
|
+
printer = PrintVisitor()
|
109
|
+
printer.visit(ast_root) # Pass the Program node to the visitor
|
110
|
+
IGNORE_WHEN_COPYING_START
|
111
|
+
content_copy
|
112
|
+
download
|
113
|
+
Use code with caution.
|
114
|
+
Python
|
115
|
+
IGNORE_WHEN_COPYING_END
|
116
|
+
Standard Visitors
|
117
|
+
|
118
|
+
Visitors provide mechanisms to traverse and process the AST. You instantiate a visitor and then call its visit method on an AST node (usually the Program root).
|
119
|
+
|
120
|
+
PrintVisitor
|
121
|
+
|
122
|
+
Generates a formatted, indented string representation of the AST structure. Useful for debugging and inspection.
|
123
|
+
|
124
|
+
Instantiation:
|
125
|
+
|
126
|
+
printer = PrintVisitor()
|
127
|
+
IGNORE_WHEN_COPYING_START
|
128
|
+
content_copy
|
129
|
+
download
|
130
|
+
Use code with caution.
|
131
|
+
Python
|
132
|
+
IGNORE_WHEN_COPYING_END
|
133
|
+
|
134
|
+
Core Method:
|
135
|
+
|
136
|
+
visit(node: IAstNode) -> str: Traverses the AST starting from node (typically the Program root) and returns a multi-line string representing the tree.
|
137
|
+
|
138
|
+
Example:
|
139
|
+
|
140
|
+
ast_root = parse_string("y = a + 5")
|
141
|
+
visitor = PrintVisitor()
|
142
|
+
ast_string_representation = visitor.visit(ast_root)
|
143
|
+
print(ast_string_representation)
|
144
|
+
IGNORE_WHEN_COPYING_START
|
145
|
+
content_copy
|
146
|
+
download
|
147
|
+
Use code with caution.
|
148
|
+
Python
|
149
|
+
IGNORE_WHEN_COPYING_END
|
150
|
+
NodeCounter
|
151
|
+
|
152
|
+
Counts the number of nodes in the AST.
|
153
|
+
|
154
|
+
Instantiation:
|
155
|
+
|
156
|
+
counter = NodeCounter()
|
157
|
+
IGNORE_WHEN_COPYING_START
|
158
|
+
content_copy
|
159
|
+
download
|
160
|
+
Use code with caution.
|
161
|
+
Python
|
162
|
+
IGNORE_WHEN_COPYING_END
|
163
|
+
|
164
|
+
Core Method:
|
165
|
+
|
166
|
+
visit(node: IAstNode) -> int: Traverses the AST starting from node and returns the total count of nodes visited.
|
167
|
+
|
168
|
+
Additional Attribute:
|
169
|
+
|
170
|
+
counts_by_type (Dict[str, int]): After calling visit, this dictionary holds the counts for each specific node type encountered (e.g., {'Assignment': 1, 'BinaryOperation': 1, ...}).
|
171
|
+
|
172
|
+
Example:
|
173
|
+
|
174
|
+
ast_root = parse_string("def f():\n return 10")
|
175
|
+
visitor = NodeCounter()
|
176
|
+
total_nodes = visitor.visit(ast_root)
|
177
|
+
print(f"Total nodes: {total_nodes}")
|
178
|
+
print(f"Nodes by type: {visitor.counts_by_type}")
|
179
|
+
IGNORE_WHEN_COPYING_START
|
180
|
+
content_copy
|
181
|
+
download
|
182
|
+
Use code with caution.
|
183
|
+
Python
|
184
|
+
IGNORE_WHEN_COPYING_END
|
185
|
+
VariableCollector
|
186
|
+
|
187
|
+
Collects the names of all variables referenced (read from) in the AST. Does not include variables only assigned to or function/class names being defined.
|
188
|
+
|
189
|
+
Instantiation:
|
190
|
+
|
191
|
+
collector = VariableCollector()
|
192
|
+
IGNORE_WHEN_COPYING_START
|
193
|
+
content_copy
|
194
|
+
download
|
195
|
+
Use code with caution.
|
196
|
+
Python
|
197
|
+
IGNORE_WHEN_COPYING_END
|
198
|
+
|
199
|
+
Core Method:
|
200
|
+
|
201
|
+
visit(node: IAstNode) -> Set[str]: Traverses the AST starting from node and returns a set containing the names of all referenced variables (identifiers used in a Load context).
|
202
|
+
|
203
|
+
Example:
|
204
|
+
|
205
|
+
code = """
|
206
|
+
x = 10
|
207
|
+
y = x + z
|
208
|
+
def my_func(p):
|
209
|
+
q = p
|
210
|
+
"""
|
211
|
+
ast_root = parse_string(code)
|
212
|
+
visitor = VariableCollector()
|
213
|
+
referenced_vars = visitor.visit(ast_root)
|
214
|
+
print(f"Referenced variables: {referenced_vars}") # Output: {'x', 'z', 'p'}
|
215
|
+
IGNORE_WHEN_COPYING_START
|
216
|
+
content_copy
|
217
|
+
download
|
218
|
+
Use code with caution.
|
219
|
+
Python
|
220
|
+
IGNORE_WHEN_COPYING_END
|
221
|
+
Error Handling
|
222
|
+
|
223
|
+
Components for managing and reporting errors during parsing and processing.
|
224
|
+
|
225
|
+
ErrorHandler
|
226
|
+
|
227
|
+
Manages the collection and reporting of errors.
|
228
|
+
|
229
|
+
Instantiation:
|
230
|
+
|
231
|
+
# Option 1: Collect errors, don't raise exceptions immediately
|
232
|
+
handler = ErrorHandler(raise_on_error=False)
|
233
|
+
|
234
|
+
# Option 2: Raise VexSyntaxError/VexAstError on the first error
|
235
|
+
handler = ErrorHandler(raise_on_error=True) # Default behavior if omitted
|
236
|
+
IGNORE_WHEN_COPYING_START
|
237
|
+
content_copy
|
238
|
+
download
|
239
|
+
Use code with caution.
|
240
|
+
Python
|
241
|
+
IGNORE_WHEN_COPYING_END
|
242
|
+
|
243
|
+
Key Methods:
|
244
|
+
|
245
|
+
get_errors() -> List[Error]: Returns a list of all Error objects collected so far.
|
246
|
+
|
247
|
+
has_errors() -> bool: Returns True if any errors have been collected, False otherwise. Useful after parsing with raise_on_error=False.
|
248
|
+
|
249
|
+
clear_errors() -> None: Removes all collected errors.
|
250
|
+
|
251
|
+
add_observer(observer: ErrorObserver) -> None: (Advanced) Registers a custom object to be notified whenever an error is added.
|
252
|
+
|
253
|
+
remove_observer(observer: ErrorObserver) -> None: (Advanced) Unregisters an observer.
|
254
|
+
|
255
|
+
Usage:
|
256
|
+
|
257
|
+
Typically, you create an ErrorHandler instance and pass it to parse_string or parse_file.
|
258
|
+
|
259
|
+
error_handler = ErrorHandler(raise_on_error=False)
|
260
|
+
try:
|
261
|
+
ast = parse_string("x = 1 + ", error_handler=error_handler)
|
262
|
+
if error_handler.has_errors():
|
263
|
+
print("Parsing completed with non-fatal errors:")
|
264
|
+
for err in error_handler.get_errors():
|
265
|
+
print(f"- {err}")
|
266
|
+
else:
|
267
|
+
print("Parsing successful.")
|
268
|
+
# Proceed with AST processing...
|
269
|
+
except VexAstError as e:
|
270
|
+
# This shouldn't happen if raise_on_error=False, but good practice
|
271
|
+
print(f"Unexpected VexAstError: {e}")
|
272
|
+
IGNORE_WHEN_COPYING_START
|
273
|
+
content_copy
|
274
|
+
download
|
275
|
+
Use code with caution.
|
276
|
+
Python
|
277
|
+
IGNORE_WHEN_COPYING_END
|
278
|
+
ErrorType
|
279
|
+
|
280
|
+
An Enum used within Error objects to categorize the type of error.
|
281
|
+
|
282
|
+
Values:
|
283
|
+
|
284
|
+
LEXER_ERROR
|
285
|
+
|
286
|
+
PARSER_ERROR
|
287
|
+
|
288
|
+
TYPE_ERROR
|
289
|
+
|
290
|
+
SEMANTIC_ERROR
|
291
|
+
|
292
|
+
INTERNAL_ERROR
|
293
|
+
|
294
|
+
Usage:
|
295
|
+
|
296
|
+
You typically check the error_type attribute of Error objects retrieved from ErrorHandler.get_errors().
|
297
|
+
|
298
|
+
VexSyntaxError
|
299
|
+
|
300
|
+
Exception raised specifically for syntax errors encountered during parsing when the ErrorHandler is configured to raise errors.
|
301
|
+
|
302
|
+
Inheritance: VexSyntaxError -> VexAstError -> Exception
|
303
|
+
|
304
|
+
Attribute:
|
305
|
+
|
306
|
+
location (Optional[SourceLocation]): May contain the location (line, column) where the syntax error occurred.
|
307
|
+
|
308
|
+
VexAstError
|
309
|
+
|
310
|
+
The base exception class for all errors originating from the vex_ast library. VexSyntaxError is a subclass.
|
311
|
+
|
312
|
+
This reference covers the main components needed to use the vex_ast library effectively for parsing and basic AST interaction. For more advanced use cases, you might need to delve into the specific AST node types in vex_ast.ast or create custom visitors inheriting from vex_ast.visitors.AstVisitor.
|
313
|
+
|
314
|
+
IGNORE_WHEN_COPYING_START
|
315
|
+
content_copy
|
316
|
+
download
|
317
|
+
Use code with caution.
|
318
|
+
IGNORE_WHEN_COPYING_END
|
vex_ast/__init__.py
CHANGED
@@ -4,13 +4,25 @@ VEX AST Generator Package.
|
|
4
4
|
Provides tools for parsing VEX V5 code and generating an Abstract Syntax Tree (AST).
|
5
5
|
"""
|
6
6
|
|
7
|
+
# Core functionality
|
7
8
|
from .ast.core import Program
|
8
|
-
from .ast.navigator import AstNavigator
|
9
9
|
from .parser.python_parser import parse_string, parse_file
|
10
|
+
|
11
|
+
# AST Navigation
|
12
|
+
from .ast.navigator import AstNavigator, create_navigator
|
13
|
+
|
14
|
+
# Visitors
|
10
15
|
from .visitors.printer import PrintVisitor
|
11
16
|
from .visitors.analyzer import NodeCounter, VariableCollector
|
12
|
-
|
13
|
-
|
17
|
+
|
18
|
+
# Error handling
|
19
|
+
from .utils.errors import ErrorHandler, ErrorType, VexSyntaxError, VexAstError, Error
|
20
|
+
|
21
|
+
# Registry
|
22
|
+
from .registry.api import registry_api
|
23
|
+
from .registry import initialize
|
24
|
+
|
25
|
+
# Serialization
|
14
26
|
from .serialization.json_serializer import serialize_ast_to_dict, serialize_ast_to_json
|
15
27
|
from .serialization.json_deserializer import deserialize_ast_from_dict, deserialize_ast_from_json
|
16
28
|
from .serialization.schema import generate_ast_schema, export_schema_to_file
|
@@ -18,18 +30,7 @@ from .serialization.schema import generate_ast_schema, export_schema_to_file
|
|
18
30
|
__version__ = "0.2.0"
|
19
31
|
|
20
32
|
# Initialize the registry with default functions
|
21
|
-
|
22
|
-
|
23
|
-
def create_navigator(ast: Program) -> AstNavigator:
|
24
|
-
"""Create an AST navigator for the given AST.
|
25
|
-
|
26
|
-
Args:
|
27
|
-
ast: The AST to navigate
|
28
|
-
|
29
|
-
Returns:
|
30
|
-
An AST navigator for traversing and querying the AST
|
31
|
-
"""
|
32
|
-
return AstNavigator(ast)
|
33
|
+
initialize()
|
33
34
|
|
34
35
|
__all__ = [
|
35
36
|
# Core functionality
|
@@ -50,10 +51,12 @@ __all__ = [
|
|
50
51
|
"ErrorHandler",
|
51
52
|
"ErrorType",
|
52
53
|
"VexSyntaxError",
|
54
|
+
"VexAstError",
|
55
|
+
"Error",
|
53
56
|
|
54
57
|
# Registry
|
55
58
|
"registry_api",
|
56
|
-
"
|
59
|
+
"initialize",
|
57
60
|
|
58
61
|
# Serialization
|
59
62
|
"serialize_ast_to_dict",
|
@@ -62,4 +65,4 @@ __all__ = [
|
|
62
65
|
"deserialize_ast_from_json",
|
63
66
|
"generate_ast_schema",
|
64
67
|
"export_schema_to_file"
|
65
|
-
]
|
68
|
+
]
|
vex_ast/ast/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
AST Node Definitions (vex_ast.ast)
|
2
|
+
|
3
|
+
This directory defines the structure and types of nodes used in the Abstract Syntax Tree (AST) representation of VEX V5 Python code.
|
4
|
+
|
5
|
+
Purpose
|
6
|
+
|
7
|
+
The AST provides a structured, hierarchical representation of the source code, abstracting away the specific syntax details. This structure is the foundation for analysis, transformation, and interpretation of the code.
|
8
|
+
|
9
|
+
Core Concepts
|
10
|
+
|
11
|
+
Interfaces (interfaces.py): Defines the fundamental protocols (IAstNode, IExpression, IStatement, ILiteral, IVisitor) that all nodes and visitors adhere to. This ensures a consistent structure.
|
12
|
+
|
13
|
+
Base Classes (core.py): Provides abstract base classes (AstNode, Expression, Statement) that implement common functionality like location tracking and the basic accept method for the Visitor pattern. Program is the root node type.
|
14
|
+
|
15
|
+
Visitor Pattern: Each node implements an accept(visitor) method, allowing external IVisitor objects to operate on the AST without modifying the node classes themselves.
|
16
|
+
|
17
|
+
Child Nodes: Each node provides a get_children() method to facilitate traversal of the tree.
|
18
|
+
|
19
|
+
Source Location: Nodes can store optional SourceLocation information (from vex_ast.utils) linking them back to the original code.
|
20
|
+
|
21
|
+
Node Categories
|
22
|
+
|
23
|
+
The AST nodes are organized into logical categories:
|
24
|
+
|
25
|
+
Expressions (expressions.py): Represent code constructs that evaluate to a value.
|
26
|
+
|
27
|
+
Identifier: Variable names, function names.
|
28
|
+
|
29
|
+
VariableReference: Usage of a variable.
|
30
|
+
|
31
|
+
AttributeAccess: Accessing attributes (e.g., motor.spin).
|
32
|
+
|
33
|
+
BinaryOperation: Operations like +, -, *, /, ==, and, or.
|
34
|
+
|
35
|
+
UnaryOperation: Operations like - (negation), not.
|
36
|
+
|
37
|
+
FunctionCall: Calling functions or methods.
|
38
|
+
|
39
|
+
KeywordArgument: Named arguments in function calls (e.g., speed=50).
|
40
|
+
|
41
|
+
Literals (literals.py): Represent constant values.
|
42
|
+
|
43
|
+
NumberLiteral: Integers and floats.
|
44
|
+
|
45
|
+
StringLiteral: Text strings.
|
46
|
+
|
47
|
+
BooleanLiteral: True or False.
|
48
|
+
|
49
|
+
NoneLiteral: The None value.
|
50
|
+
|
51
|
+
Statements (statements.py): Represent actions or control flow constructs.
|
52
|
+
|
53
|
+
ExpressionStatement: An expression used on its own line (e.g., a function call).
|
54
|
+
|
55
|
+
Assignment: Assigning a value to a variable (=).
|
56
|
+
|
57
|
+
IfStatement: Conditional execution (if/elif/else).
|
58
|
+
|
59
|
+
WhileLoop: Looping based on a condition.
|
60
|
+
|
61
|
+
ForLoop: Iterating over a sequence.
|
62
|
+
|
63
|
+
FunctionDefinition: Defining a function (def).
|
64
|
+
|
65
|
+
Argument: Parameters in a function definition.
|
66
|
+
|
67
|
+
ReturnStatement: Returning a value from a function.
|
68
|
+
|
69
|
+
BreakStatement, ContinueStatement: Loop control.
|
70
|
+
|
71
|
+
Operators (operators.py): Defines Operator enums used within BinaryOperation and UnaryOperation nodes. Includes mappings from Python operators.
|
72
|
+
|
73
|
+
VEX-Specific Nodes (vex_nodes.py): Subclasses of FunctionCall tailored to represent common VEX API patterns.
|
74
|
+
|
75
|
+
VexAPICall: Base class for VEX calls.
|
76
|
+
|
77
|
+
MotorControl: Calls related to motors (e.g., motor.spin).
|
78
|
+
|
79
|
+
SensorReading: Calls related to sensors (e.g., sensor.value).
|
80
|
+
|
81
|
+
TimingControl: Calls like wait.
|
82
|
+
|
83
|
+
DisplayOutput: Calls related to screen output (e.g., brain.screen.print).
|
84
|
+
|
85
|
+
Usage
|
86
|
+
|
87
|
+
These node classes are typically instantiated by the parser (vex_ast.parser) during the code parsing process. Visitors (vex_ast.visitors) then interact with these node objects to perform tasks.
|
vex_ast/ast/__init__.py
CHANGED
vex_ast/ast/navigator.py
CHANGED
@@ -211,3 +211,15 @@ class AstNavigator:
|
|
211
211
|
if hasattr(access.object, 'name') and getattr(access.object, 'name') == object_name:
|
212
212
|
result.append(access)
|
213
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)
|
vex_ast/parser/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
Code Parser (vex_ast.parser)
|
2
|
+
|
3
|
+
This directory contains the components responsible for parsing VEX V5 Python source code and constructing the Abstract Syntax Tree (AST).
|
4
|
+
|
5
|
+
Purpose
|
6
|
+
|
7
|
+
The parser's role is to take raw source code (as a string or from a file) and transform it into the structured AST representation defined in vex_ast.ast. It handles syntax rules, identifies different code constructs, and builds the corresponding tree of nodes.
|
8
|
+
|
9
|
+
Key Components
|
10
|
+
|
11
|
+
Interfaces (interfaces.py):
|
12
|
+
|
13
|
+
IParser: Protocol defining the essential parse() method that all parser implementations must provide.
|
14
|
+
|
15
|
+
BaseParser: An abstract base class providing common functionality, such as integrating with the ErrorHandler.
|
16
|
+
|
17
|
+
Node Factory (factory.py):
|
18
|
+
|
19
|
+
NodeFactory: Implements the Factory pattern. It provides methods (create_identifier, create_binary_operation, etc.) to instantiate specific AST node types defined in vex_ast.ast. This decouples the main parsing logic from the details of node creation and allows for easier management of node instantiation, including attaching source locations and potentially handling errors during creation.
|
20
|
+
|
21
|
+
Python Parser (python_parser.py):
|
22
|
+
|
23
|
+
PythonParser: The concrete implementation of IParser. It leverages Python's built-in ast module to parse the input Python code into a standard Python AST.
|
24
|
+
|
25
|
+
It then traverses the Python AST, using the NodeFactory to convert the standard Python nodes (ast.Assign, ast.Call, ast.BinOp, etc.) into the corresponding custom VEX AST nodes (Assignment, FunctionCall, BinaryOperation, etc.).
|
26
|
+
|
27
|
+
Provides convenience functions parse_string and parse_file.
|
28
|
+
|
29
|
+
Workflow
|
30
|
+
|
31
|
+
The user calls parse_string or parse_file.
|
32
|
+
|
33
|
+
An instance of PythonParser is created with the source code and an optional ErrorHandler.
|
34
|
+
|
35
|
+
The PythonParser uses Python's ast.parse to generate a standard Python AST.
|
36
|
+
|
37
|
+
The PythonParser walks through the Python AST. For each Python ast node, it determines the corresponding VEX AST node type.
|
38
|
+
|
39
|
+
It calls the appropriate create_... method on its internal NodeFactory instance, passing the necessary components (sub-expressions, statements, names, values) converted recursively.
|
40
|
+
|
41
|
+
The NodeFactory creates the VEX AST node, potentially adding source location information extracted from the original Python ast node.
|
42
|
+
|
43
|
+
This process continues recursively until the entire Python AST is converted into the custom VEX AST (Program node).
|
44
|
+
|
45
|
+
Any syntax errors during Python parsing or unsupported constructs during conversion are reported via the ErrorHandler.
|
46
|
+
|
47
|
+
The final Program node representing the VEX AST is returned.
|
vex_ast/parser/__init__.py
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
"""
|
2
|
+
Parser package for VEX AST.
|
3
|
+
|
4
|
+
This package provides functionality for parsing VEX code into an Abstract Syntax Tree.
|
5
|
+
"""
|
6
|
+
|
7
|
+
from .interfaces import (
|
8
|
+
IParser,
|
9
|
+
BaseParser
|
10
|
+
)
|
11
|
+
from .factory import NodeFactory, default_factory
|
12
|
+
from .python_parser import parse_string, parse_file, PythonParser
|
13
|
+
|
14
|
+
__all__ = [
|
15
|
+
# Interfaces
|
16
|
+
"IParser",
|
17
|
+
"BaseParser",
|
18
|
+
|
19
|
+
# Factory
|
20
|
+
"NodeFactory",
|
21
|
+
"default_factory",
|
22
|
+
|
23
|
+
# Python parser
|
24
|
+
"parse_string",
|
25
|
+
"parse_file",
|
26
|
+
"PythonParser"
|
27
|
+
]
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# VEX AST Registry Package (vex_ast.registry)
|
2
|
+
|
3
|
+
This package manages the registration and organization of VEX V5 Robot Python language features, including functions, objects, and other elements.
|
4
|
+
|
5
|
+
Purpose
|
6
|
+
|
7
|
+
The registry provides a centralized system for defining and accessing information about VEX-specific language constructs. This information is used by the parser, type checker, and other tools to understand and process VEX code.
|
8
|
+
|
9
|
+
Structure
|
10
|
+
|
11
|
+
The package is organized into several modules:
|
12
|
+
|
13
|
+
* `categories.py`: Defines categories for organizing registry entries.
|
14
|
+
* `language_map.py`: Maps VEX language features to their Python equivalents.
|
15
|
+
* `registry.py`: Implements the core registry logic.
|
16
|
+
* `signature.py`: Defines the structure for function signatures.
|
17
|
+
* `simulation_behavior.py`: Specifies how VEX features should behave in a simulation environment.
|
18
|
+
* `validation.py`: Provides validation rules for registry entries.
|
19
|
+
* `functions/`: Contains submodules for specific function categories (e.g., drivetrain, motor, sensors).
|
20
|
+
|
21
|
+
Key Concepts
|
22
|
+
|
23
|
+
* Registration: VEX language features are registered with the registry, providing metadata about their syntax, semantics, and simulation behavior.
|
24
|
+
* Organization: The registry organizes features into categories and subcategories for easy access and management.
|
25
|
+
* Validation: Registry entries are validated to ensure consistency and correctness.
|
26
|
+
|
27
|
+
Usage
|
28
|
+
|
29
|
+
The registry is used internally by the VEX AST parser and other tools. It is not typically accessed directly by users.
|