-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmvalid.py
More file actions
233 lines (163 loc) · 4 KB
/
mvalid.py
File metadata and controls
233 lines (163 loc) · 4 KB
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
import ply.lex as lex
import ply.yacc as yacc
tokens = ('ID_LINE',
'CREATURE_TYPE_LINE',
'LAND_TYPE_LINE',
'OTHER_TYPE_LINE',
'COST_LINE',
'PT_LINE',
'TEXT_LINE',
'OR_BLOCK',
'BLANK_LINE')
# land creatures are not supported
noncreature_nonland_permanent = r"Artifact|Enchantment|Planeswalker"
creature = r"Creature"
land = r"Land"
nonpermanent = r"Instant|Sorcery"
tribal = r"Tribal"
# creatures always have subtypes
creature_type_line = r"((" + noncreature_nonland_permanent + r")\ )*Creature\ -\ .*\n"
land_type_line = r"((" + noncreature_nonland_permanent + r"|" + tribal + r")\ )*Land\n"
other_type = r"(" + noncreature_nonland_permanent + r"|" + tribal + r"|" + nonpermanent + r")"
other_type_line = other_type + r"(\ " + other_type + r")*(\ -\ .*)?\n"
mana = r"(W|U|B|R|G)"
rarity = r"(C|U|R|M|L)"
def lines(n, t):
t.lexer.lineno += n
@lex.TOKEN(r"\#.*\n")
def t_COMMENT_LINE(t):
pass
@lex.TOKEN(r"(" + mana + r"|C)" + rarity + r"\d+\n")
def t_ID_LINE(t):
lines(1, t)
return t
@lex.TOKEN(creature_type_line)
def t_CREATURE_TYPE_LINE(t):
lines(1, t)
return t
@lex.TOKEN(land_type_line)
def t_LAND_TYPE_LINE(t):
lines(1, t)
return t
@lex.TOKEN(other_type_line)
def t_OTHER_TYPE_LINE(t):
lines(1, t)
return t
@lex.TOKEN(r"(\d+" + mana + r"*|\d*" + mana + r"+)\n")
def t_COST_LINE(t):
lines(1, t)
return t
@lex.TOKEN(r"\d*/\d*\n")
def t_PT_LINE(t):
lines(1, t)
return t
@lex.TOKEN(r".+\n")
def t_TEXT_LINE(t):
lines(1, t)
return t
@lex.TOKEN(r"\n(\#.*\n)*or\n(\#.*\n)*\n")
def t_OR_BLOCK(t):
lines(3, t)
return t
@lex.TOKEN(r"\n")
def t_BLANK_LINE(t):
lines(1, t)
return t
def t_error(t):
print("Can't make token with " + t.value + " (line " + str(t.lexer.lineno) + ")")
lex.lex()
def p_cardfile(p):
'cardfile : cardblock cardfile_more'
p[2].insert(0, p[1])
p[0] = p[2]
def p_cardfile_more(p):
'cardfile_more : BLANK_LINE cardfile'
p[0] = p[2]
def p_cardfile_done(p):
'cardfile_more : empty'
p[0] = []
def p_cardblock(p):
'cardblock : ID_LINE cards'
p[0] = (p[1].strip(), p[2])
def p_cards(p):
'cards : card cards_more'
p[2].insert(0, p[1])
p[0] = p[2]
def p_cards_more(p):
'cards_more : OR_BLOCK cards'
p[0] = p[2]
def p_cards_done(p):
'cards_more : empty'
p[0] = []
def p_card(p):
'''card : creature
| spell
| land'''
p[0] = p[1]
def makedict(**kwargs):
for (k, v) in kwargs.items():
kwargs[k] = v.strip()
return kwargs
def p_creature(p):
'creature : TEXT_LINE COST_LINE CREATURE_TYPE_LINE PT_LINE rules_text'
p[0] = makedict(name=p[1], cost=p[2], types=p[3], pt=p[4], rules_text=p[5])
def p_noncreature(p):
'spell : TEXT_LINE COST_LINE OTHER_TYPE_LINE rules_text'
p[0] = makedict(name=p[1], cost=p[2], types=p[3], rules_text=p[4])
def p_land(p):
'land : TEXT_LINE LAND_TYPE_LINE rules_text'
p[0] = makedict(name=p[1], types=p[2], rules_text=p[3])
def p_rules_text_more(p):
'rules_text : TEXT_LINE rules_text'
p[0] = p[1] + p[2]
def p_rules_text_done(p):
'rules_text : empty'
p[0] = ''
def p_empty(p):
' empty : '
pass
def p_error(p):
print("Error at " + str(p))
# look for the blank line separating block
while 1:
tok = yacc.token()
if not tok:
break
if tok.type == 'BLANK_LINE':
yacc.restart()
yacc.errok()
return yacc.token() # return the next line after the blank
yacc.yacc()
def unit_test():
sample = (
'''WC01
Squire
1W1
Artifact Creature - Human Soldier
1/2
WC02
Squire
1W2
Artifact Creature - Human Soldier
1/2
WC03
Squire
1W
#comment
Artifact Creature - Human Soldier
1/2
#
#
or
Squire
1W
Artifact Creature - Human Soldier
1/2
''')
test = mvalid(sample)
print(test)
def mvalid(filestr):
lex.input(filestr)
return yacc.parse(filestr, debug=0)
if __name__ == "__main__":
unit_test()