-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
executable file
·285 lines (206 loc) · 5.79 KB
/
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
"""
Parse .pill files
"""
import os
import model
import config
import ply.yacc
from log import log
from lexer import tokens
#
# YACC
#
class IgnoreFile(Exception):
"Indicator that processing of a file will be skipped"
pass
def p_empty_line(p):
'expression : '
# do nothing
# Words
def p_expression_word(p):
'expression : word'
p[0] = p[1]
def p_word_lang_tokens(p):
'word : LANG tokens'
lang = model.World.lang(p[1])
p[0] = lang.add_word(p[2])
def p_word_tokens(p):
'word : tokens'
lang = model.World.lang(config.default_lang)
p[0] = lang.add_word(p[1])
def p_tokens_tokens_token(p):
'tokens : tokens TOKEN'
p[0] = f"{p[1]} {p[2]}"
def p_tokens_token(p):
'tokens : TOKEN'
p[0] = p[1]
# inline comments
def p_inline_comment_word(p):
'word : word PAREN'
p[0] = p[1]
def p_inline_tokens_comment(p):
'tokens : tokens PAREN'
p[0] = p[1]
def p_inline_comment_tokens(p):
'tokens : PAREN tokens'
p[0] = p[2]
# Groups
def p_expression_group(p):
'expression : group'
p[0] = p[1]
def p_group_expression_comma_expression(p):
'group : expression SEP expression'
if p[3] is None:
raise ValueError("trailing group item is empty")
c = model.Group(p[1], p[3])
p[0] = c
# Relationships
def p_expr_rel(p):
'expression : relationship'
p[0] = p[1]
def p_relationship_word_rel_word(p):
'''relationship : expression DERIVE expression
| expression RELATED expression
| expression EQUALS expression
'''
# NOTE relationship reduces to last expression
# XXX todo check for circularity here
if p[3] is None:
raise ValueError("right side of relationship yields ``None``")
if p[1] == p[3]:
raise ValueError("self-referencing relationship is not allowed")
if p[2] == "->":
model.Derived.add(p[1], p[3])
p[0] = p[3]
elif p[2] == "=":
model.Equals.add(p[1], p[3])
p[0] = p[3]
elif p[2] == "~":
model.Related.add(p[1], p[3])
p[0] = p[3]
def p_expression_word_comment(p):
'word : word COMMENT'
word = p[1]
word.comment(p[2])
p[0] = word
def p_expression_expr_rel_comment_expr(p):
'''expression : expression DERIVE COMMENT expression
| expression EQUALS COMMENT expression
| expression RELATED COMMENT expression
'''
# XXX todo check for circularity here
if p[1] == p[4]:
raise ValueError("self-referencing relationship is not allowed")
comment = p[3]
if p[2] == "->":
p[0] = model.Derived.add(p[1], p[4], comment=comment)
elif p[2] == "=":
p[0] = model.Equals.add(p[1], p[4], comment=comment)
elif p[2] == "~":
p[0] = model.Related.add(p[1], p[4], comment=comment)
p[0] = p[4]
# Unions
_pre_union_default_lang = None
def p_start_union(p):
'start_union : '
global _pre_union_default_lang
_pre_union_default_lang = config.default_lang
config.default_lang = p[-1].lang.name # access grammar symbol to the left
def p_end_union(p):
'end_union : '
config.default_lang = _pre_union_default_lang
def p_word_union(p):
'word : word start_union union end_union'
union = model.Union(p[1], *p[3])
p[1].unions.add(union)
p[0] = p[1]
def p_union(p):
'union : LUNION word plus_words RUNION'
p[3].insert(0, p[2])
p[0] = p[3]
def p_plus_words2(p):
'plus_words : plus_words PLUS word'
p[1].append(p[3])
p[0] = p[1]
def p_plus_words1(p):
'plus_words : PLUS word'
p[0] = [p[2]]
# META is either an user defined comment that will be ignored
# or a pragma directive for the parser
orig_default_lang = None
def handle_meta(meta, p):
meta = meta.strip()
if meta.startswith("SRC"):
src = meta[4:].strip() or None
config.source = src
elif meta.startswith("NOSRC"):
config.source = None
elif meta.startswith("LANG"):
global orig_default_lang # XXX global ...
if not orig_default_lang:
orig_default_lang = config.default_lang
lang = meta[5:]
config.default_lang = lang
elif meta.startswith("IGNORE"):
raise IgnoreFile
def p_meta(p):
'''expression : expression META
| META
'''
if len(p) == 3:
p[0] = p[1]
meta = p[2]
else:
meta = p[1]
handle_meta(meta, p)
def p_tag(p):
'word : word TAG'
p[1].add_tag(p[2])
p[0] = p[1]
def p_error(p):
raise SyntaxError(f"Syntax error at token {p!r}")
def t_eof(t):
pass
precedence = (
('left', 'EQUALS', 'RELATED', 'DERIVE'),
('left', 'LUNION', 'RUNION'),
('left', 'COMMENT'),
('left', 'SEP'),
('left', 'PLUS'),
('left', 'TAG'),
('left', 'LANG'),
('left', 'META'),
('left', 'PAREN'),
)
yacc = ply.yacc.yacc()
def parse_file(fn):
log.debug(f"Parsing: {fn}")
with open(fn) as f:
for line in f.readlines():
try:
yacc.parse(line)
except IgnoreFile:
break
finish_file()
def finish_file():
log.debug("Setting config.source=None")
config.source = None
if orig_default_lang:
log.debug(f"Setting config.default_lang={orig_default_lang}")
config.default_lang = orig_default_lang
def get_pills(path):
for root, dirs, files in os.walk(path):
for f in files:
if f.endswith(".pill"):
yield os.path.join(root, f)
def load_db(db_dir=None):
db_dir = db_dir or os.path.join(os.path.dirname(__file__), "db")
for fn in get_pills(db_dir):
parse_file(fn)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("file")
args = parser.parse_args()
parse_file(args.file)