-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_parser.py
173 lines (146 loc) · 3.92 KB
/
state_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
# -*- coding: utf-8 -*-
# This file is part of sf2dve.
#
# sf2dve is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or
# (at your option) any later version.
#
# sf2dve 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with sf2dve. If not, see <http://www.gnu.org/licenses/>.
"""
Created on Thu Oct 23 15:51:31 2014
@author: pavla
"""
from ply import lex, yacc
import os
keywords = {
"en" : "EN",
"du" : "DU",
"ex" : "EX",
"entry" : "ENTRY",
"during" : "DURING",
"exit" : "EXIT",
"bind" : "BIND",
"on" : "ON",
"after" : "AFTER",
"at" : "AT",
"before" : "BEFORE",
"every" : "EVERY"
}
tokens = (["WHITESPACE", "NEWLINE", "AL_NUM", "OTHER"] + list(keywords.values()))
literals = ",;:()"
t_WHITESPACE = r"[ \t\r\f\v]+"
t_OTHER = r"[^\s,;:()\w]+"
def t_AL_NUM(t):
r"\w+"
t.type = keywords.get(t.value, "AL_NUM")
return t
def t_NEWLINE(t):
r"\n+"
t.lexer.lineno += len(t.value)
return t
def t_error(t):
raise TypeError("Unknown text '%s'" % t.value)
def p_start(p):
"start : ws label"
p[0] = p[2]
def p_label(p):
"""label : action_keywords actions label
| bind actions label
| on actions label
| empty"""
if len(p) == 4:
if p[3] is None:
p[0] = [[p[1], p[2]]]
else:
p[0] = [[p[1], p[2]]] + p[3]
else:
p[0] = []
def p_empty(p):
"empty :"
pass
def p_ws(p):
"""ws : WHITESPACE ws
| NEWLINE ws
| empty"""
if p[1] is None:
p[0] = ""
else:
p[0] = p[1] + p[2]
def p_keywords(p):
"""action_keywords : action_keyword separator action_keywords
| action_keyword ws ':'"""
if p[3] != ':':
p[0] = [p[1]] + p[3]
else:
p[0] = [p[1]]
def p_keyword(p):
"""action_keyword : EN
| DU
| EX
| ENTRY
| DURING
| EXIT"""
p[0] = p[1]
def p_bind(p):
"bind : BIND ws ':'"
p[0] = [p[1]]
def p_on(p):
"on : ON ws event ws ':'"
p[0] = [p[1], p[3]]
def p_separator(p):
"""separator : ws ',' ws
| ws ';' ws"""
p[0] = p[2]
def p_event(p):
"""event : AL_NUM
| temporal '(' ws AL_NUM ws ',' ws AL_NUM ws ')'"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = [p[1], p[4], p[8]]
def p_temporal(p):
"""temporal : AFTER
| AT
| BEFORE
| EVERY"""
p[0] = p[1]
def p_actions(p):
"""actions : anything actions
| anything"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + p[2]
def p_anything(p):
"""anything : WHITESPACE
| NEWLINE
| AL_NUM
| OTHER
| ','
| ';'
| ':'
| '('
| ')'"""
p[0] = p[1]
def p_error(p):
if p is None:
raise ValueError("Unknown error")
raise ValueError("Syntax error, line %s: %s" % (p.lineno, p.type))
directory = os.path.join(os.path.dirname(__file__),
"parser_tables",
os.path.basename(__file__).rsplit('.', 1)[0])
try:
os.makedirs(directory, exist_ok=True)
except OSError:
pass
lexer = lex.lex(debug=False, optimize=True, outputdir=directory)
parser = yacc.yacc(debug=False, optimize=True, outputdir=directory)
def parse(text, lexer=lexer):
return parser.parse(text, lexer)