Skip to content

Commit 756180b

Browse files
authored
bpo-40939: Clean and adapt the peg_generator directory after deleting the old parser (GH-20822)
1 parent b4282dd commit 756180b

File tree

6 files changed

+30
-304
lines changed

6 files changed

+30
-304
lines changed

Tools/peg_generator/Makefile

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ data/xxl.py:
2222

2323
build: peg_extension/parse.c
2424

25-
peg_extension/parse.c: $(GRAMMAR) $(TOKENS) pegen/*.py peg_extension/peg_extension.c ../../Parser/pegen/pegen.c ../../Parser/pegen/parse_string.c ../../Parser/pegen/*.h pegen/grammar_parser.py
25+
peg_extension/parse.c: $(GRAMMAR) $(TOKENS) pegen/*.py peg_extension/peg_extension.c ../../Parser/pegen.c ../../Parser/string_parser.c ../../Parser/*.h pegen/grammar_parser.py
2626
$(PYTHON) -m pegen -q c $(GRAMMAR) $(TOKENS) -o peg_extension/parse.c --compile-extension
2727

2828
clean:
@@ -70,18 +70,10 @@ stats: peg_extension/parse.c data/xxl.py
7070
time: time_compile
7171

7272
time_compile: venv data/xxl.py
73-
$(VENVPYTHON) scripts/benchmark.py --parser=new --target=xxl compile
73+
$(VENVPYTHON) scripts/benchmark.py --target=xxl compile
7474

7575
time_parse: venv data/xxl.py
76-
$(VENVPYTHON) scripts/benchmark.py --parser=new --target=xxl parse
77-
78-
time_old: time_old_compile
79-
80-
time_old_compile: venv data/xxl.py
81-
$(VENVPYTHON) scripts/benchmark.py --parser=old --target=xxl compile
82-
83-
time_old_parse: venv data/xxl.py
84-
$(VENVPYTHON) scripts/benchmark.py --parser=old --target=xxl parse
76+
$(VENVPYTHON) scripts/benchmark.py --target=xxl parse
8577

8678
time_peg_dir: venv
8779
$(VENVPYTHON) scripts/test_parse_directory.py \

Tools/peg_generator/pegen/keywordgen.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ def main():
4141
"grammar", type=str, help="The file with the grammar definition in PEG format"
4242
)
4343
parser.add_argument(
44-
"tokens_file",
45-
type=argparse.FileType("r"),
46-
help="The file with the token definitions"
44+
"tokens_file", type=argparse.FileType("r"), help="The file with the token definitions"
4745
)
4846
parser.add_argument(
4947
"keyword_file",
@@ -61,9 +59,7 @@ def main():
6159
gen.collect_todo()
6260

6361
with args.keyword_file as thefile:
64-
all_keywords = sorted(
65-
list(gen.callmakervisitor.keyword_cache.keys()) + EXTRA_KEYWORDS
66-
)
62+
all_keywords = sorted(list(gen.callmakervisitor.keyword_cache.keys()) + EXTRA_KEYWORDS)
6763

6864
keywords = ",\n ".join(map(repr, all_keywords))
6965
thefile.write(TEMPLATE.format(keywords=keywords))

Tools/peg_generator/scripts/benchmark.py

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
import os
77
from time import time
88

9-
import _peg_parser
10-
119
try:
1210
import memory_profiler
1311
except ModuleNotFoundError:
14-
print("Please run `make venv` to create a virtual environment and install"
15-
" all the dependencies, before running this script.")
12+
print(
13+
"Please run `make venv` to create a virtual environment and install"
14+
" all the dependencies, before running this script."
15+
)
1616
sys.exit(1)
1717

1818
sys.path.insert(0, os.getcwd())
@@ -21,13 +21,6 @@
2121
argparser = argparse.ArgumentParser(
2222
prog="benchmark", description="Reproduce the various pegen benchmarks"
2323
)
24-
argparser.add_argument(
25-
"--parser",
26-
action="store",
27-
choices=["new", "old"],
28-
default="pegen",
29-
help="Which parser to benchmark (default is pegen)",
30-
)
3124
argparser.add_argument(
3225
"--target",
3326
action="store",
@@ -40,12 +33,7 @@
4033
command_compile = subcommands.add_parser(
4134
"compile", help="Benchmark parsing and compiling to bytecode"
4235
)
43-
command_parse = subcommands.add_parser(
44-
"parse", help="Benchmark parsing and generating an ast.AST"
45-
)
46-
command_notree = subcommands.add_parser(
47-
"notree", help="Benchmark parsing and dumping the tree"
48-
)
36+
command_parse = subcommands.add_parser("parse", help="Benchmark parsing and generating an ast.AST")
4937

5038

5139
def benchmark(func):
@@ -66,59 +54,37 @@ def wrapper(*args):
6654

6755

6856
@benchmark
69-
def time_compile(source, parser):
70-
if parser == "old":
71-
return _peg_parser.compile_string(
72-
source,
73-
oldparser=True,
74-
)
75-
else:
76-
return _peg_parser.compile_string(source)
77-
78-
79-
@benchmark
80-
def time_parse(source, parser):
81-
if parser == "old":
82-
return _peg_parser.parse_string(source, oldparser=True)
83-
else:
84-
return _peg_parser.parse_string(source)
57+
def time_compile(source):
58+
return compile(source, "<string>", "exec")
8559

8660

8761
@benchmark
88-
def time_notree(source, parser):
89-
if parser == "old":
90-
return _peg_parser.parse_string(source, oldparser=True, ast=False)
91-
else:
92-
return _peg_parser.parse_string(source, ast=False)
62+
def time_parse(source):
63+
return ast.parse(source)
9364

9465

95-
def run_benchmark_xxl(subcommand, parser, source):
66+
def run_benchmark_xxl(subcommand, source):
9667
if subcommand == "compile":
97-
time_compile(source, parser)
68+
time_compile(source)
9869
elif subcommand == "parse":
99-
time_parse(source, parser)
100-
elif subcommand == "notree":
101-
time_notree(source, parser)
70+
time_parse(source)
10271

10372

104-
def run_benchmark_stdlib(subcommand, parser):
105-
modes = {"compile": 2, "parse": 1, "notree": 0}
73+
def run_benchmark_stdlib(subcommand):
74+
modes = {"compile": 2, "parse": 1}
10675
for _ in range(3):
10776
parse_directory(
10877
"../../Lib",
10978
verbose=False,
11079
excluded_files=["*/bad*", "*/lib2to3/tests/data/*",],
111-
tree_arg=0,
11280
short=True,
11381
mode=modes[subcommand],
114-
oldparser=(parser == "old"),
11582
)
11683

11784

11885
def main():
11986
args = argparser.parse_args()
12087
subcommand = args.subcommand
121-
parser = args.parser
12288
target = args.target
12389

12490
if subcommand is None:
@@ -127,9 +93,9 @@ def main():
12793
if target == "xxl":
12894
with open(os.path.join("data", "xxl.py"), "r") as f:
12995
source = f.read()
130-
run_benchmark_xxl(subcommand, parser, source)
96+
run_benchmark_xxl(subcommand, source)
13197
elif target == "stdlib":
132-
run_benchmark_stdlib(subcommand, parser)
98+
run_benchmark_stdlib(subcommand)
13399

134100

135101
if __name__ == "__main__":

Tools/peg_generator/scripts/find_max_nesting.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
Usage: python -m scripts.find_max_nesting
1515
"""
1616
import sys
17-
18-
from _peg_parser import parse_string
17+
import ast
1918

2019
GRAMMAR_FILE = "data/python.gram"
2120
INITIAL_NESTING_DEPTH = 10
@@ -28,9 +27,8 @@
2827

2928
def check_nested_expr(nesting_depth: int) -> bool:
3029
expr = f"{'(' * nesting_depth}0{')' * nesting_depth}"
31-
3230
try:
33-
parse_string(expr)
31+
ast.parse(expr)
3432
print(f"Nesting depth of {nesting_depth} is successful")
3533
return True
3634
except Exception as err:

Tools/peg_generator/scripts/show_parse.py

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)