summaryrefslogtreecommitdiffstats
path: root/Tools/peg_generator/Makefile
blob: e7a190c1bcd13d89021b0cc58a1f3cf2e742e40d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
	PYTHON ?= ../../python
endif
ifeq ($(UNAME_S),Darwin)
	PYTHON ?= ../../python.exe
endif
VENVDIR ?= ./venv
VENVPYTHON ?= $(VENVDIR)/bin/python
CPYTHON ?= ../../Lib
MYPY ?= mypy

GRAMMAR = ../../Grammar/python.gram
TOKENS = ../../Grammar/Tokens
TESTFILE = data/cprog.py
TIMEFILE = data/xxl.py
TESTDIR = .
TESTFLAGS = --short

data/xxl.py:
	$(PYTHON) -m zipfile -e data/xxl.zip data

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

clean:
	-rm -f peg_extension/*.o peg_extension/*.so peg_extension/parse.c
	-rm -f data/xxl.py
	-rm -rf $(VENVDIR)

dump: peg_extension/parse.c
	cat -n $(TESTFILE)
	$(PYTHON) -c "from peg_extension import parse; import ast; t = parse.parse_file('$(TESTFILE)', mode=1); print(ast.dump(t))"

regen-metaparser: pegen/metagrammar.gram pegen/*.py
	$(PYTHON) -m pegen -q python pegen/metagrammar.gram -o pegen/grammar_parser.py

# Note: These targets really depend on the generated shared object in peg_extension/parse.*.so but
# this has different names in different systems so we are abusing the implicit dependency on
# parse.c by the use of --compile-extension.

.PHONY: test

venv:
	$(PYTHON) -m venv $(VENVDIR)
	$(VENVPYTHON) -m pip install -U pip setuptools
	$(VENVPYTHON) -m pip install -r requirements.pip
	@echo "The venv has been created in the $(VENVDIR) directory"

test: run

run: peg_extension/parse.c
	$(PYTHON) -c "from peg_extension import parse; t = parse.parse_file('$(TESTFILE)'); exec(t)"

compile: peg_extension/parse.c
	$(PYTHON) -c "from peg_extension import parse; t = parse.parse_file('$(TESTFILE)', mode=2)"

parse: peg_extension/parse.c
	$(PYTHON) -c "from peg_extension import parse; t = parse.parse_file('$(TESTFILE)', mode=1)"

check: peg_extension/parse.c
	$(PYTHON) -c "from peg_extension import parse; t = parse.parse_file('$(TESTFILE)', mode=0)"

stats: peg_extension/parse.c data/xxl.py
	$(PYTHON) -c "from peg_extension import parse; t = parse.parse_file('$(TIMEFILE)', mode=0); parse.dump_memo_stats()" >@data
	$(PYTHON) scripts/joinstats.py @data

time: time_compile

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

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

time_old: time_old_compile

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

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

time_peg_dir: venv
	$(VENVPYTHON) scripts/test_parse_directory.py \
		--grammar-file $(GRAMMAR) \
		--tokens-file $(TOKENS) \
		-d $(TESTDIR) \
		$(TESTFLAGS) \
		--exclude "*/failset/*" \
		--exclude "*/failset/**" \
		--exclude "*/failset/**/*"

time_stdlib: $(CPYTHON) venv
	$(VENVPYTHON) scripts/test_parse_directory.py \
		--grammar-file $(GRAMMAR) \
		--tokens-file $(TOKENS) \
		-d $(CPYTHON) \
		$(TESTFLAGS) \
		--exclude "*/test2to3/*" \
		--exclude "*/test2to3/**/*" \
		--exclude "*/bad*" \
		--exclude "*/lib2to3/tests/data/*"

mypy: regen-metaparser
	$(MYPY)  # For list of files, see mypy.ini

format-python:
	black pegen scripts

format: format-python

find_max_nesting:
	$(PYTHON) scripts/find_max_nesting.py

tags: TAGS

TAGS: pegen/*.py test/test_pegen.py
	etags pegen/*.py test/test_pegen.py