| 1 |
from parsing import ( # noqa: F401 |
| 2 |
InstDef, |
| 3 |
Macro, |
| 4 |
Pseudo, |
| 5 |
Family, |
| 6 |
LabelDef, |
| 7 |
Parser, |
| 8 |
Context, |
| 9 |
CacheEffect, |
| 10 |
StackEffect, |
| 11 |
InputEffect, |
| 12 |
OpName, |
| 13 |
AstNode, |
| 14 |
Stmt, |
| 15 |
SimpleStmt, |
| 16 |
IfStmt, |
| 17 |
ForStmt, |
| 18 |
WhileStmt, |
| 19 |
BlockStmt, |
| 20 |
MacroIfStmt, |
| 21 |
) |
| 22 |
|
| 23 |
import pprint |
| 24 |
|
| 25 |
CodeDef = InstDef | LabelDef |
| 26 |
|
| 27 |
def prettify_filename(filename: str) -> str: |
| 28 |
# Make filename more user-friendly and less platform-specific, |
| 29 |
# it is only used for error reporting at this point. |
| 30 |
filename = filename.replace("\\", "/") |
| 31 |
if filename.startswith("./"): |
| 32 |
filename = filename[2:] |
| 33 |
if filename.endswith(".new"): |
| 34 |
filename = filename[:-4] |
| 35 |
return filename |
| 36 |
|
| 37 |
|
| 38 |
BEGIN_MARKER = "// BEGIN BYTECODES //" |
| 39 |
END_MARKER = "// END BYTECODES //" |
| 40 |
|
| 41 |
|
| 42 |
def parse_files(filenames: list[str]) -> list[AstNode]: |
| 43 |
result: list[AstNode] = [] |
| 44 |
for filename in filenames: |
| 45 |
with open(filename) as file: |
| 46 |
src = file.read() |
| 47 |
|
| 48 |
psr = Parser(src, filename=prettify_filename(filename)) |
| 49 |
|
| 50 |
# Skip until begin marker |
| 51 |
while tkn := psr.next(raw=True): |
| 52 |
if tkn.text == BEGIN_MARKER: |
| 53 |
break |
| 54 |
else: |
| 55 |
raise psr.make_syntax_error( |
| 56 |
f"Couldn't find {BEGIN_MARKER!r} in {psr.filename}" |
| 57 |
) |
| 58 |
start = psr.getpos() |
| 59 |
|
| 60 |
# Find end marker, then delete everything after it |
| 61 |
while tkn := psr.next(raw=True): |
| 62 |
if tkn.text == END_MARKER: |
| 63 |
break |
| 64 |
del psr.tokens[psr.getpos() - 1 :] |
| 65 |
|
| 66 |
# Parse from start |
| 67 |
psr.setpos(start) |
| 68 |
thing_first_token = psr.peek() |
| 69 |
while node := psr.definition(): |
| 70 |
assert node is not None |
| 71 |
result.append(node) # type: ignore[arg-type] |
| 72 |
if not psr.eof(): |
| 73 |
pprint.pprint(result) |
| 74 |
psr.backup() |
| 75 |
raise psr.make_syntax_error( |
| 76 |
f"Extra stuff at the end of {filename}", psr.next(True) |
| 77 |
) |
| 78 |
return result |