mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
GH-111485: Separate out parsing, analysis and code-gen phases of tier 1 code generator (GH-112299)
This commit is contained in:
parent
3d712a9f4c
commit
b449415b2f
15 changed files with 1675 additions and 526 deletions
55
Tools/cases_generator/parser.py
Normal file
55
Tools/cases_generator/parser.py
Normal file
|
@ -0,0 +1,55 @@
|
|||
from parsing import (
|
||||
InstDef,
|
||||
Macro,
|
||||
Pseudo,
|
||||
Family,
|
||||
Parser,
|
||||
Context,
|
||||
CacheEffect,
|
||||
StackEffect,
|
||||
OpName,
|
||||
AstNode,
|
||||
)
|
||||
from formatting import prettify_filename
|
||||
|
||||
|
||||
BEGIN_MARKER = "// BEGIN BYTECODES //"
|
||||
END_MARKER = "// END BYTECODES //"
|
||||
|
||||
|
||||
def parse_files(filenames: list[str]) -> list[AstNode]:
|
||||
result: list[AstNode] = []
|
||||
for filename in filenames:
|
||||
with open(filename) as file:
|
||||
src = file.read()
|
||||
|
||||
psr = Parser(src, filename=prettify_filename(filename))
|
||||
|
||||
# Skip until begin marker
|
||||
while tkn := psr.next(raw=True):
|
||||
if tkn.text == BEGIN_MARKER:
|
||||
break
|
||||
else:
|
||||
raise psr.make_syntax_error(
|
||||
f"Couldn't find {BEGIN_MARKER!r} in {psr.filename}"
|
||||
)
|
||||
start = psr.getpos()
|
||||
|
||||
# Find end marker, then delete everything after it
|
||||
while tkn := psr.next(raw=True):
|
||||
if tkn.text == END_MARKER:
|
||||
break
|
||||
del psr.tokens[psr.getpos() - 1 :]
|
||||
|
||||
# Parse from start
|
||||
psr.setpos(start)
|
||||
thing_first_token = psr.peek()
|
||||
while node := psr.definition():
|
||||
assert node is not None
|
||||
result.append(node) # type: ignore[arg-type]
|
||||
if not psr.eof():
|
||||
psr.backup()
|
||||
raise psr.make_syntax_error(
|
||||
f"Extra stuff at the end of {filename}", psr.next(True)
|
||||
)
|
||||
return result
|
Loading…
Add table
Add a link
Reference in a new issue