Skip to content

bpo-40939: Clean and adapt the peg_generator directory after deleting the old parser #20822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions Tools/peg_generator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ data/xxl.py:

build: peg_extension/parse.c

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
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
$(PYTHON) -m pegen -q c $(GRAMMAR) $(TOKENS) -o peg_extension/parse.c --compile-extension

clean:
Expand Down Expand Up @@ -70,18 +70,10 @@ stats: peg_extension/parse.c data/xxl.py
time: time_compile

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

time_parse: venv data/xxl.py
$(VENVPYTHON) scripts/benchmark.py --parser=new --target=xxl parse

time_old: time_old_compile

time_old_compile: venv data/xxl.py
$(VENVPYTHON) scripts/benchmark.py --parser=old --target=xxl compile

time_old_parse: venv data/xxl.py
$(VENVPYTHON) scripts/benchmark.py --parser=old --target=xxl parse
$(VENVPYTHON) scripts/benchmark.py --target=xxl parse

time_peg_dir: venv
$(VENVPYTHON) scripts/test_parse_directory.py \
Expand Down
8 changes: 2 additions & 6 deletions Tools/peg_generator/pegen/keywordgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ def main():
"grammar", type=str, help="The file with the grammar definition in PEG format"
)
parser.add_argument(
"tokens_file",
type=argparse.FileType("r"),
help="The file with the token definitions"
"tokens_file", type=argparse.FileType("r"), help="The file with the token definitions"
)
parser.add_argument(
"keyword_file",
Expand All @@ -61,9 +59,7 @@ def main():
gen.collect_todo()

with args.keyword_file as thefile:
all_keywords = sorted(
list(gen.callmakervisitor.keyword_cache.keys()) + EXTRA_KEYWORDS
)
all_keywords = sorted(list(gen.callmakervisitor.keyword_cache.keys()) + EXTRA_KEYWORDS)

keywords = ",\n ".join(map(repr, all_keywords))
thefile.write(TEMPLATE.format(keywords=keywords))
Expand Down
66 changes: 16 additions & 50 deletions Tools/peg_generator/scripts/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import os
from time import time

import _peg_parser

try:
import memory_profiler
except ModuleNotFoundError:
print("Please run `make venv` to create a virtual environment and install"
" all the dependencies, before running this script.")
print(
"Please run `make venv` to create a virtual environment and install"
" all the dependencies, before running this script."
)
sys.exit(1)

sys.path.insert(0, os.getcwd())
Expand All @@ -21,13 +21,6 @@
argparser = argparse.ArgumentParser(
prog="benchmark", description="Reproduce the various pegen benchmarks"
)
argparser.add_argument(
"--parser",
action="store",
choices=["new", "old"],
default="pegen",
help="Which parser to benchmark (default is pegen)",
)
argparser.add_argument(
"--target",
action="store",
Expand All @@ -40,12 +33,7 @@
command_compile = subcommands.add_parser(
"compile", help="Benchmark parsing and compiling to bytecode"
)
command_parse = subcommands.add_parser(
"parse", help="Benchmark parsing and generating an ast.AST"
)
command_notree = subcommands.add_parser(
"notree", help="Benchmark parsing and dumping the tree"
)
command_parse = subcommands.add_parser("parse", help="Benchmark parsing and generating an ast.AST")


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


@benchmark
def time_compile(source, parser):
if parser == "old":
return _peg_parser.compile_string(
source,
oldparser=True,
)
else:
return _peg_parser.compile_string(source)


@benchmark
def time_parse(source, parser):
if parser == "old":
return _peg_parser.parse_string(source, oldparser=True)
else:
return _peg_parser.parse_string(source)
def time_compile(source):
return compile(source, "<string>", "exec")


@benchmark
def time_notree(source, parser):
if parser == "old":
return _peg_parser.parse_string(source, oldparser=True, ast=False)
else:
return _peg_parser.parse_string(source, ast=False)
def time_parse(source):
return ast.parse(source)


def run_benchmark_xxl(subcommand, parser, source):
def run_benchmark_xxl(subcommand, source):
if subcommand == "compile":
time_compile(source, parser)
time_compile(source)
elif subcommand == "parse":
time_parse(source, parser)
elif subcommand == "notree":
time_notree(source, parser)
time_parse(source)


def run_benchmark_stdlib(subcommand, parser):
modes = {"compile": 2, "parse": 1, "notree": 0}
def run_benchmark_stdlib(subcommand):
modes = {"compile": 2, "parse": 1}
for _ in range(3):
parse_directory(
"../../Lib",
verbose=False,
excluded_files=["*/bad*", "*/lib2to3/tests/data/*",],
tree_arg=0,
short=True,
mode=modes[subcommand],
oldparser=(parser == "old"),
)


def main():
args = argparser.parse_args()
subcommand = args.subcommand
parser = args.parser
target = args.target

if subcommand is None:
Expand All @@ -127,9 +93,9 @@ def main():
if target == "xxl":
with open(os.path.join("data", "xxl.py"), "r") as f:
source = f.read()
run_benchmark_xxl(subcommand, parser, source)
run_benchmark_xxl(subcommand, source)
elif target == "stdlib":
run_benchmark_stdlib(subcommand, parser)
run_benchmark_stdlib(subcommand)


if __name__ == "__main__":
Expand Down
6 changes: 2 additions & 4 deletions Tools/peg_generator/scripts/find_max_nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
Usage: python -m scripts.find_max_nesting
"""
import sys

from _peg_parser import parse_string
import ast

GRAMMAR_FILE = "data/python.gram"
INITIAL_NESTING_DEPTH = 10
Expand All @@ -28,9 +27,8 @@

def check_nested_expr(nesting_depth: int) -> bool:
expr = f"{'(' * nesting_depth}0{')' * nesting_depth}"

try:
parse_string(expr)
ast.parse(expr)
print(f"Nesting depth of {nesting_depth} is successful")
return True
except Exception as err:
Expand Down
121 changes: 0 additions & 121 deletions Tools/peg_generator/scripts/show_parse.py

This file was deleted.

Loading