-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_parser.py
140 lines (115 loc) · 4.29 KB
/
make_parser.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright 2020 Christian Seberino
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import pgen.add_prods
import pgen.remove_temp
NOTHING = ("___NOTHING___", "___NOTHING___")
def cache(func):
cache_ = globals()[func.__name__ + "_cache"] = {}
def func_(left, right, objects, prods):
if isinstance(right, list):
right_ = tuple(tuple(right))
else:
right_ = right
key = (left, right_, tuple(objects))
if key not in cache_:
cache_[key] = func(left, right, objects, prods)
return cache_[key]
return func_
@cache
def prodizer_one(left, right, tokens, prodizer):
"""
helper function
"""
if (len(tokens) == 0) and (right == NOTHING[0]):
ast = (left, NOTHING)
elif right in prodizer:
ast = prodizer[right](tokens, prodizer)
if ast:
ast = (left, ast)
elif (len(tokens) == 1) and (right == tokens[0][0]):
ast = (left, tokens[0])
else:
ast = None
return ast
@cache
def prodizer_list(left, right, tokens, prodizer):
"""
helper function
"""
ast = None
for e in right:
if isinstance(e, tuple):
ast = prodizer_tuple(left, e, tokens, prodizer)
else:
ast = prodizer_one(left, e, tokens, prodizer)
if ast:
break
return ast
@cache
def prodizer_tuple(left, right, tokens, prodizer):
"""
helper function
"""
asts = []
for e in right:
if tokens:
for i in range(len(tokens), -1, -1):
ast = prodizer_one(e, e, tokens[:i], prodizer)
if ast:
tokens = tokens[i:]
break
else:
ast = prodizer_one(e, e, [], prodizer)
if ast:
asts.append(ast[1])
else:
asts = None
break
ast = (left,) + tuple(asts) if asts and not tokens else None
return ast
def make_prodizer(left, right):
"""
Makes productionizers.
"""
right_ = right
if "|" in right_:
right_ = [e.strip() for e in right_.split("|")]
for i, e in enumerate(right_):
if " " in e:
right_[i] = tuple(e.split())
elif " " in right_:
right_ = tuple(right_.split())
def prodizer(tokens, prodizer):
if isinstance(right_, list):
ast = prodizer_list( left, right_, tokens, prodizer)
elif isinstance(right_, tuple):
ast = prodizer_tuple(left, right_, tokens, prodizer)
else:
ast = prodizer_one( left, right_, tokens, prodizer)
return ast
return prodizer
def make_parser(tokenizer, grammar):
"""
Makes parsers.
"""
names = pgen.add_prods.add_prods(grammar)
prodizer = dict([(e, make_prodizer(e, grammar.prods[e]))
for e in grammar.prods])
def parser(text):
tokens = tokenizer.tokenizer(text)
ast = prodizer[grammar.start](tokens, prodizer)
ast = pgen.remove_temp.remove_temp(ast, names)
return ast
return parser