abilian-astero 0.1.0__tar.gz
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.
- abilian_astero-0.1.0/LICENSE +202 -0
- abilian_astero-0.1.0/PKG-INFO +163 -0
- abilian_astero-0.1.0/README.md +141 -0
- abilian_astero-0.1.0/pyproject.toml +149 -0
- abilian_astero-0.1.0/src/astero/__init__.py +59 -0
- abilian_astero-0.1.0/src/astero/coverage.py +165 -0
- abilian_astero-0.1.0/src/astero/emit.py +270 -0
- abilian_astero-0.1.0/src/astero/emit_py.py +422 -0
- abilian_astero-0.1.0/src/astero/emit_rules.py +570 -0
- abilian_astero-0.1.0/src/astero/generate.py +148 -0
- abilian_astero-0.1.0/src/astero/grammar.py +561 -0
- abilian_astero-0.1.0/src/astero/hygiene.py +439 -0
- abilian_astero-0.1.0/src/astero/lang_postpile_runtime.py +69 -0
- abilian_astero-0.1.0/src/astero/lang_py.py +380 -0
- abilian_astero-0.1.0/src/astero/lang_ssa.py +170 -0
- abilian_astero-0.1.0/src/astero/py.typed +0 -0
- abilian_astero-0.1.0/src/astero/rewriting.py +848 -0
- abilian_astero-0.1.0/src/astero/scopes.py +436 -0
- abilian_astero-0.1.0/src/astero/tables.py +201 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Stefane Fermigier
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: abilian-astero
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: astero: declared grammars for ASTs and IRs, and the transformations derived from them.
|
|
5
|
+
Keywords: compiler,ast,grammar,transpiler,code-generation
|
|
6
|
+
Author: Stefane Fermigier
|
|
7
|
+
Author-email: Stefane Fermigier <sf@abilian.com>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
17
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Project-URL: Documentation, https://astero.lab.abilian.com
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# astero
|
|
24
|
+
|
|
25
|
+
**Write compilers, transpilers and code analysers without hand-maintaining the tables they depend on.**
|
|
26
|
+
|
|
27
|
+
Every program that transforms code needs to know the same structural facts about its language: which positions introduce a variable, which read one, which fields a traversal must enter, how tightly each operator binds. Most projects write those facts out by hand, once per pass. They drift. When they drift the symptom is wrong output, not a crash.
|
|
28
|
+
|
|
29
|
+
astero takes one declaration of your syntax tree and computes those facts on demand. Python's grammar ships with the library, so for Python source there is nothing to declare.
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install abilian-astero # or: uv add abilian-astero
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The distribution is called **abilian-astero**, because PyPI already had `astero`. The import name is `astero`:
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from astero.lang_py import PY, VARS
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Python 3.11 through 3.15. No runtime dependencies. Apache-2.0.
|
|
42
|
+
|
|
43
|
+
## The problem it solves
|
|
44
|
+
|
|
45
|
+
Fifteen positions in Python can hold a variable name. Here are four of them:
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
def f(x): # a parameter
|
|
49
|
+
import os as x # an import alias
|
|
50
|
+
try:
|
|
51
|
+
pass
|
|
52
|
+
except E as x: # an exception name
|
|
53
|
+
pass
|
|
54
|
+
return [x for x in xs] # a comprehension target
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
A renamer written with `ast.NodeTransformer` and a `visit_Name` method reaches one. That is not a hypothetical: it is the shape of a defect found in three separate compilers, and none of the three crashed. One never bound `**kwargs`, one inlined a function so that `[99 for 99 in xs]` came out, one set an assignment context by hand and produced JavaScript that computed `NaN` where Python gave `5`.
|
|
58
|
+
|
|
59
|
+
With astero the list is a query, and the same query serves every pass that needs it:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from astero.hygiene import rename
|
|
63
|
+
from astero.lang_py import PY, VARS
|
|
64
|
+
|
|
65
|
+
rename(tree, {"x": "y"}, PY, VARS) # reaches all fifteen
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The set changes between Python releases. Version 3.12 added three positions. Code written against the query follows the language without being edited.
|
|
69
|
+
|
|
70
|
+
## What you get
|
|
71
|
+
|
|
72
|
+
Declare a **role** for each field of each production: whether it introduces a name, refers to one, or holds a subtree. Six modules read that declaration.
|
|
73
|
+
|
|
74
|
+
| module | what it gives you |
|
|
75
|
+
| --- | --- |
|
|
76
|
+
| `grammar` | which fields bind a name, which read one, which hold a bare identifier, which a traversal must enter, which productions can appear |
|
|
77
|
+
| `rewriting` | tree rewriting from `pattern => result` rules, with every `Name`'s context recomputed for you |
|
|
78
|
+
| `scopes` | the scope tree of a module, and the names each scope binds |
|
|
79
|
+
| `hygiene` | renaming, and substitution that cannot capture a name |
|
|
80
|
+
| `emit_rules` | a code generator written as templates, with brackets derived from a precedence table |
|
|
81
|
+
| `coverage` | a test that fails when your dispatch table misses a production |
|
|
82
|
+
|
|
83
|
+
Add a binding form to your language, edit the declaration, and all six follow.
|
|
84
|
+
|
|
85
|
+
### Rewriting, without touching contexts
|
|
86
|
+
|
|
87
|
+
A rule is one string with `=>` between pattern and replacement:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from astero import Pass, rules
|
|
91
|
+
|
|
92
|
+
fold = Pass("fold", rules("""
|
|
93
|
+
_x + 0 => _x
|
|
94
|
+
_x * 1 => _x
|
|
95
|
+
"""))
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Identifiers spelled `_name` are metavariables, `_` matches anything without binding, and `*_xs` splices the rest of a list. Rules never mention `ctx` or source positions, because both are recomputed from the shape of the result. That is what makes the `AugAssign` defect above impossible to write.
|
|
99
|
+
|
|
100
|
+
### Your own IR, not just Python's
|
|
101
|
+
|
|
102
|
+
A grammar carries each production's constructor, so a rewrite builds your node classes. If your AST is generated from CPython's, the whole declaration is one line:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
PRESCRYPT = lang_py.build(module=prescrypt.front.ast.ast, name="prescrypt")
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
If your IR is annotated dataclasses, `from_dataclasses` reads the fields and sorts off the annotations, leaving you only the roles to write. The [PL/0 tutorial](https://astero.lab.abilian.com/tutorial-pl0/) builds a complete compiler that way, for a language with no relationship to Python.
|
|
109
|
+
|
|
110
|
+
## Why you can rely on it
|
|
111
|
+
|
|
112
|
+
Every derivation is compared against an independent source of truth, over the whole Python standard library, on Python 3.11 through 3.15.
|
|
113
|
+
|
|
114
|
+
| derived | compared against | result |
|
|
115
|
+
| --- | --- | --- |
|
|
116
|
+
| assignment contexts | what CPython's parser produces | every position in the standard library, no disagreements |
|
|
117
|
+
| scopes and their names | CPython's `symtable` | 99.98% of blocks agree |
|
|
118
|
+
| emitted source | reparsing the emitted text | 1,797 modules, 2,266,043 expressions |
|
|
119
|
+
| operand positions of an SSA IR | that compiler's own declaration | exact match |
|
|
120
|
+
|
|
121
|
+
For assignment contexts that is ten authored role entries reconstructing all 1,457,931 positions across the 1,761 files of the 3.13 standard library. The tests hold the ratio, not the number. A regression fails; an improvement does not have to be chased.
|
|
122
|
+
|
|
123
|
+
The rewrite engine was first validated by reimplementing all six of [latexify_py](https://github.com/google/latexify_py)'s tree transformations as rule sets. Differential testing against the originals found 121 output differences, **every one a latent bug in the hand-written version**.
|
|
124
|
+
|
|
125
|
+
Three compilers are built on it: a Python-to-C compiler, a Python-to-WebAssembly compiler, and a Python-to-JavaScript transpiler.
|
|
126
|
+
|
|
127
|
+
## What astero is not
|
|
128
|
+
|
|
129
|
+
- **Not a parser.** Bring your own AST. astero starts from a tree.
|
|
130
|
+
- **Not a code generator you run.** Nothing is written to disk, and there is no build step. Every entry point is a function call at run time.
|
|
131
|
+
- **Not a framework.** It does not own your pipeline, your IR, or your `main`. Adopt one query in one function and leave the rest alone.
|
|
132
|
+
|
|
133
|
+
Lowering and cost models stay yours. astero answers questions about a declaration; it does not decide what your compiler should do with the answers.
|
|
134
|
+
|
|
135
|
+
## Documentation
|
|
136
|
+
|
|
137
|
+
- **[Getting started](https://astero.lab.abilian.com/getting-started/)**: install it and run three queries against real code.
|
|
138
|
+
- **[TinyPy tutorial](https://astero.lab.abilian.com/tutorial/)**: a working compiler for a Python subset, with two back ends, in about 170 lines.
|
|
139
|
+
- **[PL/0 tutorial](https://astero.lab.abilian.com/tutorial-pl0/)**: the same, for a language that is not Python.
|
|
140
|
+
- **[User guide](https://astero.lab.abilian.com/guides/user-guide/)**: every module, its API, and when to reach for it.
|
|
141
|
+
- **[Adopting astero](https://astero.lab.abilian.com/guides/adopting/)**: fitting it into a compiler you already have.
|
|
142
|
+
- **[API reference](https://astero.lab.abilian.com/api/grammar/)**: generated from the source.
|
|
143
|
+
|
|
144
|
+
## Status
|
|
145
|
+
|
|
146
|
+
Version 0.1.x. The API may still change between minor versions; the changelog says what moved. Every derivation is checked against CPython on five interpreters, and three compilers are built on it.
|
|
147
|
+
|
|
148
|
+
## Development
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
make test # pytest
|
|
152
|
+
make lint # ruff, ruff format, ty, pyrefly, zuban, mypy
|
|
153
|
+
make docs # build the documentation site
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
This code is version-sensitive, so a green run on one interpreter says little about the others. `nox` runs the suite on each:
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
nox -s tests # 3.11 through 3.15
|
|
160
|
+
nox -s check # everything `make lint` runs
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Contributions are welcome. `docs/src/guides/developer-guide.md` describes how the library is organised and what a new derivation has to prove before it lands.
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# astero
|
|
2
|
+
|
|
3
|
+
**Write compilers, transpilers and code analysers without hand-maintaining the tables they depend on.**
|
|
4
|
+
|
|
5
|
+
Every program that transforms code needs to know the same structural facts about its language: which positions introduce a variable, which read one, which fields a traversal must enter, how tightly each operator binds. Most projects write those facts out by hand, once per pass. They drift. When they drift the symptom is wrong output, not a crash.
|
|
6
|
+
|
|
7
|
+
astero takes one declaration of your syntax tree and computes those facts on demand. Python's grammar ships with the library, so for Python source there is nothing to declare.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install abilian-astero # or: uv add abilian-astero
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The distribution is called **abilian-astero**, because PyPI already had `astero`. The import name is `astero`:
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from astero.lang_py import PY, VARS
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Python 3.11 through 3.15. No runtime dependencies. Apache-2.0.
|
|
20
|
+
|
|
21
|
+
## The problem it solves
|
|
22
|
+
|
|
23
|
+
Fifteen positions in Python can hold a variable name. Here are four of them:
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
def f(x): # a parameter
|
|
27
|
+
import os as x # an import alias
|
|
28
|
+
try:
|
|
29
|
+
pass
|
|
30
|
+
except E as x: # an exception name
|
|
31
|
+
pass
|
|
32
|
+
return [x for x in xs] # a comprehension target
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
A renamer written with `ast.NodeTransformer` and a `visit_Name` method reaches one. That is not a hypothetical: it is the shape of a defect found in three separate compilers, and none of the three crashed. One never bound `**kwargs`, one inlined a function so that `[99 for 99 in xs]` came out, one set an assignment context by hand and produced JavaScript that computed `NaN` where Python gave `5`.
|
|
36
|
+
|
|
37
|
+
With astero the list is a query, and the same query serves every pass that needs it:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from astero.hygiene import rename
|
|
41
|
+
from astero.lang_py import PY, VARS
|
|
42
|
+
|
|
43
|
+
rename(tree, {"x": "y"}, PY, VARS) # reaches all fifteen
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The set changes between Python releases. Version 3.12 added three positions. Code written against the query follows the language without being edited.
|
|
47
|
+
|
|
48
|
+
## What you get
|
|
49
|
+
|
|
50
|
+
Declare a **role** for each field of each production: whether it introduces a name, refers to one, or holds a subtree. Six modules read that declaration.
|
|
51
|
+
|
|
52
|
+
| module | what it gives you |
|
|
53
|
+
| --- | --- |
|
|
54
|
+
| `grammar` | which fields bind a name, which read one, which hold a bare identifier, which a traversal must enter, which productions can appear |
|
|
55
|
+
| `rewriting` | tree rewriting from `pattern => result` rules, with every `Name`'s context recomputed for you |
|
|
56
|
+
| `scopes` | the scope tree of a module, and the names each scope binds |
|
|
57
|
+
| `hygiene` | renaming, and substitution that cannot capture a name |
|
|
58
|
+
| `emit_rules` | a code generator written as templates, with brackets derived from a precedence table |
|
|
59
|
+
| `coverage` | a test that fails when your dispatch table misses a production |
|
|
60
|
+
|
|
61
|
+
Add a binding form to your language, edit the declaration, and all six follow.
|
|
62
|
+
|
|
63
|
+
### Rewriting, without touching contexts
|
|
64
|
+
|
|
65
|
+
A rule is one string with `=>` between pattern and replacement:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from astero import Pass, rules
|
|
69
|
+
|
|
70
|
+
fold = Pass("fold", rules("""
|
|
71
|
+
_x + 0 => _x
|
|
72
|
+
_x * 1 => _x
|
|
73
|
+
"""))
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Identifiers spelled `_name` are metavariables, `_` matches anything without binding, and `*_xs` splices the rest of a list. Rules never mention `ctx` or source positions, because both are recomputed from the shape of the result. That is what makes the `AugAssign` defect above impossible to write.
|
|
77
|
+
|
|
78
|
+
### Your own IR, not just Python's
|
|
79
|
+
|
|
80
|
+
A grammar carries each production's constructor, so a rewrite builds your node classes. If your AST is generated from CPython's, the whole declaration is one line:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
PRESCRYPT = lang_py.build(module=prescrypt.front.ast.ast, name="prescrypt")
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
If your IR is annotated dataclasses, `from_dataclasses` reads the fields and sorts off the annotations, leaving you only the roles to write. The [PL/0 tutorial](https://astero.lab.abilian.com/tutorial-pl0/) builds a complete compiler that way, for a language with no relationship to Python.
|
|
87
|
+
|
|
88
|
+
## Why you can rely on it
|
|
89
|
+
|
|
90
|
+
Every derivation is compared against an independent source of truth, over the whole Python standard library, on Python 3.11 through 3.15.
|
|
91
|
+
|
|
92
|
+
| derived | compared against | result |
|
|
93
|
+
| --- | --- | --- |
|
|
94
|
+
| assignment contexts | what CPython's parser produces | every position in the standard library, no disagreements |
|
|
95
|
+
| scopes and their names | CPython's `symtable` | 99.98% of blocks agree |
|
|
96
|
+
| emitted source | reparsing the emitted text | 1,797 modules, 2,266,043 expressions |
|
|
97
|
+
| operand positions of an SSA IR | that compiler's own declaration | exact match |
|
|
98
|
+
|
|
99
|
+
For assignment contexts that is ten authored role entries reconstructing all 1,457,931 positions across the 1,761 files of the 3.13 standard library. The tests hold the ratio, not the number. A regression fails; an improvement does not have to be chased.
|
|
100
|
+
|
|
101
|
+
The rewrite engine was first validated by reimplementing all six of [latexify_py](https://github.com/google/latexify_py)'s tree transformations as rule sets. Differential testing against the originals found 121 output differences, **every one a latent bug in the hand-written version**.
|
|
102
|
+
|
|
103
|
+
Three compilers are built on it: a Python-to-C compiler, a Python-to-WebAssembly compiler, and a Python-to-JavaScript transpiler.
|
|
104
|
+
|
|
105
|
+
## What astero is not
|
|
106
|
+
|
|
107
|
+
- **Not a parser.** Bring your own AST. astero starts from a tree.
|
|
108
|
+
- **Not a code generator you run.** Nothing is written to disk, and there is no build step. Every entry point is a function call at run time.
|
|
109
|
+
- **Not a framework.** It does not own your pipeline, your IR, or your `main`. Adopt one query in one function and leave the rest alone.
|
|
110
|
+
|
|
111
|
+
Lowering and cost models stay yours. astero answers questions about a declaration; it does not decide what your compiler should do with the answers.
|
|
112
|
+
|
|
113
|
+
## Documentation
|
|
114
|
+
|
|
115
|
+
- **[Getting started](https://astero.lab.abilian.com/getting-started/)**: install it and run three queries against real code.
|
|
116
|
+
- **[TinyPy tutorial](https://astero.lab.abilian.com/tutorial/)**: a working compiler for a Python subset, with two back ends, in about 170 lines.
|
|
117
|
+
- **[PL/0 tutorial](https://astero.lab.abilian.com/tutorial-pl0/)**: the same, for a language that is not Python.
|
|
118
|
+
- **[User guide](https://astero.lab.abilian.com/guides/user-guide/)**: every module, its API, and when to reach for it.
|
|
119
|
+
- **[Adopting astero](https://astero.lab.abilian.com/guides/adopting/)**: fitting it into a compiler you already have.
|
|
120
|
+
- **[API reference](https://astero.lab.abilian.com/api/grammar/)**: generated from the source.
|
|
121
|
+
|
|
122
|
+
## Status
|
|
123
|
+
|
|
124
|
+
Version 0.1.x. The API may still change between minor versions; the changelog says what moved. Every derivation is checked against CPython on five interpreters, and three compilers are built on it.
|
|
125
|
+
|
|
126
|
+
## Development
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
make test # pytest
|
|
130
|
+
make lint # ruff, ruff format, ty, pyrefly, zuban, mypy
|
|
131
|
+
make docs # build the documentation site
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
This code is version-sensitive, so a green run on one interpreter says little about the others. `nox` runs the suite on each:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
nox -s tests # 3.11 through 3.15
|
|
138
|
+
nox -s check # everything `make lint` runs
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Contributions are welcome. `docs/src/guides/developer-guide.md` describes how the library is organised and what a new derivation has to prove before it lands.
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "abilian-astero"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "astero: declared grammars for ASTs and IRs, and the transformations derived from them."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Stefane Fermigier", email = "sf@abilian.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = []
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
keywords = ["compiler", "ast", "grammar", "transpiler", "code-generation"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Programming Language :: Python :: 3.14",
|
|
21
|
+
"Topic :: Software Development :: Compilers",
|
|
22
|
+
"Topic :: Software Development :: Code Generators",
|
|
23
|
+
"Typing :: Typed",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
Documentation = "https://astero.lab.abilian.com"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
[build-system]
|
|
31
|
+
requires = ["uv_build>=0.11.33,<0.12.0"]
|
|
32
|
+
build-backend = "uv_build"
|
|
33
|
+
|
|
34
|
+
[tool.uv.build-backend]
|
|
35
|
+
# The distribution is `abilian-astero` because PyPI would not give up
|
|
36
|
+
# `astero`, but the import name is and stays `astero`. Without this the
|
|
37
|
+
# backend derives the module path from the project name and looks for
|
|
38
|
+
# `src/abilian_astero/`.
|
|
39
|
+
module-name = "astero"
|
|
40
|
+
|
|
41
|
+
[dependency-groups]
|
|
42
|
+
docs = [
|
|
43
|
+
# Zensical's published versions are 0.0.x, and it needs a recent Python.
|
|
44
|
+
# This project's floor is 3.11, so the group is marked rather than pinned:
|
|
45
|
+
# `make docs` on an older interpreter is the only thing it affects.
|
|
46
|
+
"zensical>=0.0.54; python_version >= '3.13' and python_version < '3.15'",
|
|
47
|
+
# The API reference is generated from the source's own docstrings, so it
|
|
48
|
+
# cannot drift. Zensical shims the mkdocs plugin.
|
|
49
|
+
"mkdocstrings[python]>=0.27; python_version >= '3.13' and python_version < '3.15'",
|
|
50
|
+
]
|
|
51
|
+
# What running the suite needs, and nothing else. Separate from `dev`
|
|
52
|
+
# because the version matrix installs it on every supported interpreter, and
|
|
53
|
+
# `pre-commit` pulls in `pyyaml`, which does not build on 3.15.
|
|
54
|
+
test = [
|
|
55
|
+
"pytest>=9.1.1",
|
|
56
|
+
"pytest-cov>=7.1.0",
|
|
57
|
+
]
|
|
58
|
+
dev = [
|
|
59
|
+
{ include-group = "test" },
|
|
60
|
+
# postpile's own dependency, and the only thing standing between the SSA
|
|
61
|
+
# oracles and running. It does not make postpile a dependency: those
|
|
62
|
+
# tests still locate a checkout (`POSTPILE_SRC`, or a sibling directory)
|
|
63
|
+
# and skip when there is none. Without this they skipped on the import
|
|
64
|
+
# instead, so the oracle never ran and nothing said so.
|
|
65
|
+
"postyp>=0.3.0",
|
|
66
|
+
"mypy>=2.3.0",
|
|
67
|
+
"nox>=2026.7.11",
|
|
68
|
+
"pre-commit>=4.6.1",
|
|
69
|
+
"pyrefly>=1.2.0",
|
|
70
|
+
"ruff>=0.16.2",
|
|
71
|
+
"ty>=0.0.69",
|
|
72
|
+
"zuban>=0.9.1",
|
|
73
|
+
# tepyd needs 3.12; this project's floor is 3.11, so the marker keeps the
|
|
74
|
+
# lock resolvable there. Same shape as the `docs` group's zensical pin.
|
|
75
|
+
"tepyd; python_version >= '3.12'",
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
[tool.pytest.ini_options]
|
|
79
|
+
testpaths = ["tests"]
|
|
80
|
+
markers = [
|
|
81
|
+
"unit: fast, isolated unit tests",
|
|
82
|
+
"integration: component-interaction tests",
|
|
83
|
+
"e2e: end-to-end tests",
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
[tool.ty.environment]
|
|
87
|
+
# The two worked examples are standalone programs, not packages: their modules
|
|
88
|
+
# import each other by bare name (`import syntax`), and the tests put the
|
|
89
|
+
# directory on `sys.path` to run them. That is the shape a reader copies, so
|
|
90
|
+
# the type checker is told where to look rather than the examples being made
|
|
91
|
+
# into packages nobody would write.
|
|
92
|
+
extra-paths = ["examples/pl0", "examples/tinypy"]
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
[tool.mypy]
|
|
96
|
+
# zuban reads mypy's configuration. `mypy_path` is the same fact as
|
|
97
|
+
# `[tool.ty.environment] extra-paths` and pyrefly's `search-path`: the worked
|
|
98
|
+
# examples import each other by bare name. `make lint` runs mypy over `src`
|
|
99
|
+
# only, so this changes nothing for it and everything for zuban.
|
|
100
|
+
mypy_path = "src:examples/pl0:examples/tinypy"
|
|
101
|
+
|
|
102
|
+
[[tool.mypy.overrides]]
|
|
103
|
+
# zuban reads `operator.mod` as returning `None`, which typeshed does not
|
|
104
|
+
# say and mypy does not agree with. Scoped to the one rule and the one
|
|
105
|
+
# module rather than worked around in the code.
|
|
106
|
+
module = ["vm"]
|
|
107
|
+
disable_error_code = ["dict-item"]
|
|
108
|
+
|
|
109
|
+
[tool.pyrefly]
|
|
110
|
+
project-includes = [
|
|
111
|
+
"**/*.py*",
|
|
112
|
+
"**/*.ipynb",
|
|
113
|
+
]
|
|
114
|
+
# Same reason as `[tool.ty.environment]` above: the worked examples are
|
|
115
|
+
# standalone programs whose modules import each other by bare name.
|
|
116
|
+
search-path = ["src", "examples/pl0", "examples/tinypy"]
|
|
117
|
+
|
|
118
|
+
[tool.tepyd]
|
|
119
|
+
# Generated by `tepyd init` — review and adjust.
|
|
120
|
+
src_root = "src/astero"
|
|
121
|
+
src_package = "astero"
|
|
122
|
+
units = ["*.py"]
|
|
123
|
+
# counter = "internal" # or "cloc"
|
|
124
|
+
|
|
125
|
+
# Source units to exclude from analysis (a reason is required):
|
|
126
|
+
# [tool.tepyd.exclude]
|
|
127
|
+
# faker = "why this is not tested"
|
|
128
|
+
|
|
129
|
+
# Tiers, cheapest first. Add `expects = [...]` to a tier to scope it to
|
|
130
|
+
# the source units it should test (glob patterns over unit names); units
|
|
131
|
+
# outside the scope are reported n/a, not as gaps. Use it when layers map
|
|
132
|
+
# to tiers — e.g. integration `expects = ["repositories/*", "infra/*"]`,
|
|
133
|
+
# e2e `expects = ["web/*"]`. Omit it and the tier is checked against all.
|
|
134
|
+
|
|
135
|
+
[[tool.tepyd.tiers]]
|
|
136
|
+
name = "a_unit"
|
|
137
|
+
root = "tests/a_unit"
|
|
138
|
+
label = "unit"
|
|
139
|
+
target_share = 0.6
|
|
140
|
+
|
|
141
|
+
[[tool.tepyd.tiers]]
|
|
142
|
+
name = "b_integration"
|
|
143
|
+
root = "tests/b_integration"
|
|
144
|
+
label = "integration"
|
|
145
|
+
|
|
146
|
+
[[tool.tepyd.tiers]]
|
|
147
|
+
name = "c_e2e"
|
|
148
|
+
root = "tests/c_e2e"
|
|
149
|
+
label = "e2e"
|