-
Notifications
You must be signed in to change notification settings - Fork 0
/
transition_parser.py
135 lines (112 loc) · 3.34 KB
/
transition_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
# -*- 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 Sat Oct 18 16:48:32 2014
@author: pavla
"""
from ply import lex, yacc
from extendedExceptions import notSupportedException
import os
tokens = ("OPEN_BRACKET", "CLOSE_BRACKET", "AL_NUM", "WHITESPACE", "NEWLINE",
"OTHER")
literals = r"{}/"
t_OPEN_BRACKET = r"\["
t_CLOSE_BRACKET = r"\]"
t_AL_NUM = r"\w+"
t_WHITESPACE = r"[ \t\r\f\v]+"
t_OTHER = r"[^\[\]{}/\s\w]+"
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_label(p):
"""label : ws events condition c_action t_action
| ws events OPEN_BRACKET A CLOSE_BRACKET ws incorrect_c_action"""
if (len(p) == 6):
p[0] = [p[2], p[3], p[4], p[5]]
def p_events(p):
"""events : AL_NUM ws
| empty"""
p[0] = p[1]
def p_condition(p):
"""condition : OPEN_BRACKET A CLOSE_BRACKET ws
| empty"""
if (len(p) == 5):
p[0] = p[2]
def p_c_action(p):
"""c_action : '{' A '}' ws
| empty"""
if (len(p) == 5):
p[0] = p[2]
def p_t_action(p):
"""t_action : '/' A
| empty"""
if (len(p) == 3):
p[0] = p[2]
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_action(p):
"""A : A2
| empty"""
if p[1] is None:
p[0] = ""
else:
p[0] = p[1]
def p_action_parts(p):
"""A2 : AL_NUM A
| OTHER A
| WHITESPACE A
| NEWLINE A
| '/' A"""
if (p[2] is None):
p[0] = p[1]
else:
p[0] = p[1] + p[2]
def p_brackets(p):
"""A2 : OPEN_BRACKET A CLOSE_BRACKET A
| '{' A '}' A"""
p[0] = p[1] + p[2] + p[3] + p[4]
def p_incorrect_c_action(p):
"""incorrect_c_action : AL_NUM A
| OTHER A"""
raise notSupportedException("Condition actions must be enclosed in curly "
"brackets.")
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)