-
Notifications
You must be signed in to change notification settings - Fork 24
/
parser.py
483 lines (378 loc) · 12.1 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# Copyright (C) 2012 Tim Radvan
#
# This file is part of Kurt.
#
# Kurt 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 3 of the License, or (at your option) any
# later version.
#
# Kurt 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 Kurt. If not, see <http://www.gnu.org/licenses/>.
from ply import yacc
import os
from lexer import tokens, pretty_error
import kurt
DEBUG = False
class Arg(object):
NUMBER = "NUMBER" # ( )
VARIABLE = "VARIABLE"
REPORTER = "REPORTER"
SPECIAL = "SPECIAL"
STRING = "STRING" # [ ]
STRING_DROPDOWN = "STRING_DROPDOWN" # [ v]
BOOL = "BOOL" # < >
COLOR = "COLOR" # [#fff]
# COLOR is only used in `arg_kind`
def __init__(self, kind, value):
self.kind = kind
self.value = value
def __repr__(self):
return "Arg(%r, %r)" % (self.kind, self.value)
class BlockError(Exception):
pass
BLOCK_TEXT_ALIASES = {
'whengfclicked': 'whengreenflagclicked',
'turnleftdegrees': 'turnccwdegrees',
'turnrightdegrees': 'turncwdegrees',
}
# TODO: Symbols
special_variables = {
'all': 'all',
'any': 'any',
'last': 'last',
}
special_strings = {
'mouse-pointer': '_mouse_',
}
SHAPE_ALLOW = {
"boolean": [Arg.BOOL],
"number": [Arg.NUMBER, Arg.VARIABLE],
"number-menu": [Arg.NUMBER, Arg.VARIABLE],
"string": [Arg.STRING, Arg.VARIABLE],
"color": [Arg.COLOR], # Color
"readonly-menu": [Arg.STRING_DROPDOWN, Arg.VARIABLE, Arg.SPECIAL,
Arg.STRING],
}
for accept_args in SHAPE_ALLOW.values():
if Arg.VARIABLE in accept_args:
accept_args.append(Arg.REPORTER)
def fits(arg, insert):
if insert.shape in ('readonly-menu', 'number-menu'):
if arg.value in insert.options():
return True
elif arg.kind == Arg.DROPDOWN:
return True
elif insert.shape in ('number-menu', 'number'):
if arg.kind == Arg.NUMBER:
return True
elif arg.kind in (Arg.STRING, Arg.STRING_DROPDOWN):
# cast DROPDOWN / STRING to NUMBER
if arg.value.strip().isdigit(): # int
return True
elif value.strip().replace('.', '').isdigit(): # float
return True
elif insert.shape == 'string':
if arg.kind in (Arg.STRING, Arg.NUMBER):
return True
elif insert.shape == 'color':
if arg.kind in (Arg.STRING, Arg.STRING_DROPDOWN):
# COLORs are STRINGs at this point
return True
def fits(arg, insert):
need_kinds = SHAPE_ALLOW[insert.shape]
if insert.kind == 'mathOp':
return arg.value in insert.options()
else:
if arg.kind in need_kinds:
return True
else:
if arg.kind in (Arg.STRING, Arg.STRING_DROPDOWN):
if Arg.NUMBER in need_kinds:
# cast DROPDOWN / STRING to NUMBER
if arg.value.strip().isdigit():
return True # int
elif arg.value.strip().replace('.', '').isdigit():
return True # float
elif Arg.COLOR in need_kinds:
# COLORs are STRINGs at this point
return True
elif (arg.kind == Arg.NUMBER and
Arg.STRING in need_kinds):
# cast NUMBER to STRING
return True
return False
def block_from_parts(parts, flag=None):
# flag is currently unused
text = ""
arguments = []
for part in parts:
if isinstance(part, Arg):
arguments.append(part)
else:
text += part
script = None
block = None
text = kurt.BlockType._strip_text(text)
text = BLOCK_TEXT_ALIASES.get(text, text)
poss_types = kurt.plugin.Kurt.blocks_by_text(text)
if not poss_types:
e = "No block type found for %r \n"%text + repr(arguments)
raise BlockError(e)
if len(poss_types) == 1:
bt = poss_types[0]
else:
if text == 'of':
if poss_types[0].has_command('getAttribute:of:'):
poss_types.reverse()
# verify arg types against block.inserts
for bt in poss_types:
for (arg, insert) in zip(arguments, bt.inserts):
if not fits(arg, insert):
break # Not this type
else:
break # Type is ok!
else: # No types matched
bt = poss_types[0]
print "WARNING: wrong args for %r" % bt
print arguments
has_args = not bt.has_command("whenClicked")
allow_vars = not (bt.has_command("whenGreenFlag") or
bt.has_command("whenIreceive"))
block_args = list(bt.defaults)
if has_args:
for i in range(len(arguments)):
arg = arguments[i]
if arg is None:
continue
kind = arg.kind
if kind == Arg.VARIABLE and allow_vars:
value = kurt.Block("readVariable", arg.value)
else:
value = arg.value
if i < len(bt.inserts):
insert = bt.inserts[i]
if kind in (Arg.STRING, Arg.STRING_DROPDOWN):
if insert.shape == 'color':
value = kurt.Color(value)
elif (insert.shape in ('number', 'number-menu')):
# cast DROPDOWN / STRING to NUMBER
if value.strip().isdigit():
value = int(value)
elif value.strip().replace('.', '').isdigit():
value = float(value)
elif (kind == Arg.NUMBER and
insert.shape in ('readonly-menu', 'string')):
# cast NUMBER to STRING
value = unicode(value)
if i >= len(block_args):
block_args.append(value)
else:
block_args[i] = value
block = kurt.Block(bt, *block_args)
return block
def variable_named(variable_name):
if variable_name.endswith(' v'):
variable_name = variable_name[:-2]
# make sure this is a reporter!
if variable_name.lower() in special_variables:
value = special_variables[variable_name].copy()
return Arg(Arg.SPECIAL, value)
else:
return Arg(Arg.VARIABLE, variable_name)
# Scripts
def p_file(t):
"""file : script"""
t[0] = kurt.Script(blocks = t[1])
def p_script_end_newlines(t):
"""script : script NEWLINE"""
t[0] = t[1]
def p_script_begin_newlines(t):
"""script : NEWLINE script"""
t[0] = t[2]
def p_script(t):
"""script : script NEWLINE block
| script NEWLINE block COMMENT"""
block = t[3]
if len(t) > 4:
if block.comment: block.comment += '\n'
block.comment += t[4]
t[0] = t[1] + [ block ]
def p_script_one(t):
"""script : block
| block COMMENT
"""
block = t[1]
if len(t) > 2:
if block.comment: block.comment += '\n'
block.comment += t[2]
t[0] = [ block ]
# C blocks
def p_c_stack(t):
"""block : c_block c_mouth END
| if_block c_mouth END
"""
block = t[1]
block.args[-1] = t[2]
t[0] = block #Block(block.command, *args)
def p_if_else(t):
"""block : if_block c_mouth ELSE c_mouth END
| if_block c_mouth ELSE COMMENT c_mouth END"""
block = t[1]
block.type = kurt.BlockType.get('doIfElse')
block.args = [block.args[0]]
block.args += [ t[2] ]
if len(t) > 6:
block.args += [ t[5] ]
if block.comment: block.comment += '\n'
block.comment += t[4]
else:
block.args += [ t[4] ]
t[0] = block
def p_c_mouth(t):
"""c_mouth : NEWLINE script NEWLINE"""
t[0] = t[2]
def p_c_mouth_empty(t):
"""c_mouth : NEWLINE"""
t[0] = None
def p_c_block(t):
"""c_block : c_block_def
| c_block_def COMMENT
"""
block = t[1]
if len(t) > 2:
if block.comment: block.comment += '\n'
block.comment += t[2]
t[0] = block
def p_c_block_def(t):
"""c_block_def : FOREVER
| FOREVER IF insert
| REPEAT insert
| REPEAT UNTIL insert
"""
t[0] = block_from_parts(t[1:])
def p_if_block(t):
"""if_block : if_block_def
| if_block_def COMMENT
"""
block = t[1]
if len(t) > 2:
if block.comment: block.comment += '\n'
block.comment += t[2]
t[0] = block
def p_if_block_def(t):
"""if_block_def : IF insert
| IF insert THEN
"""
block = block_from_parts(t[1:])
t[0] = block
# Block
def p_block(t):
"""block : parts"""
t[0] = block_from_parts(t[1])
# Parts
def p_parts(t):
"""parts : parts part"""
t[0] = t[1] + [ t[2] ]
def p_part(t):
"""parts : part"""
t[0] = [ t[1] ]
# Part
def p_insert_part(t):
"""part : insert"""
t[0] = t[1]
def p_symbol_part(t):
"""part : SYMBOL"""
t[0] = t[1]
# Args
def p_bool_insert(t):
"""insert : LBOOL parts RBOOL"""
block = block_from_parts(t[2], "b")
t[0] = Arg(Arg.BOOL, block)
def p_empty_bool_insert(t):
"""insert : LBOOL RBOOL"""
t[0] = Arg(Arg.BOOL, False)
def p_string_insert(t):
"""insert : STRING"""
(value, is_dropdown) = t[1]
if value.lower() in special_strings:
value = special_strings[value].copy()
t[0] = Arg(Arg.SPECIAL, value)
else:
if is_dropdown:
t[0] = Arg(Arg.STRING_DROPDOWN, value)
else:
t[0] = Arg(Arg.STRING, value)
def p_reporter_insert(t):
"""insert : LPAREN parts RPAREN"""
parts = t[2]
try: # Reporter block.
block = block_from_parts(parts, "r")
t[0] = Arg(Arg.REPORTER, block)
except BlockError, e:
# Maybe it's a variable? Check there are no args.
for part in parts:
if isinstance(part, Arg):
raise e # Give up: not a block or a variable.
# Variable!
t[0] = variable_named(' '.join(parts))
# TODO: allow multiple spaces in var names
def p_empty_reporter_insert(t):
"""insert : LPAREN RPAREN"""
t[0] = Arg(Arg.REPORTER, None)
def p_number_insert(t):
"""insert : LPAREN number RPAREN
| LPAREN number DROPDOWN
"""
t[0] = Arg(Arg.NUMBER, t[2])
def p_variable_insert(t):
"""insert : LPAREN SYMBOL RPAREN
| LPAREN SYMBOL DROPDOWN
"""
variable_name = t[2]
try: # Might be a reporter like 'costume #'
block = block_from_parts(t[2], "r")
t[0] = Arg(Arg.REPORTER, block)
except BlockError:
t[0] = variable_named(variable_name)
def p_number_int(t):
"""number : INT"""
t[0] = t[1]
def p_number_float(t):
"""number : FLOAT"""
t[0] = t[1]
# Override to make lt/gt < > work.
#
# This assumes )>( and )>( are never valid in any other context -- you can never
# have a boolean insert directly followed by a reporter, or vice versa.
def p_boolean_expr(t):
"""parts : insert LBOOL insert
| insert RBOOL insert
"""
t[0] = [ t[1], t[2], t[3] ]
# Keywords sometimes used in block text
def p_keyword_part(t):
"""part : IF
| UNTIL
"""
t[0] = t[1]
# Syntax errors.
def p_error(p):
if p:
err_msg = "Unexpected %(type)s: %(value)s " % p.__dict__
pretty_error(p, err_msg)
else:
# EOF
raise SyntaxError("invalid syntax")
# Build the parser.
path_to_file = os.path.join(os.getcwd(), __file__)
path_to_folder = os.path.split(path_to_file)[0]
block_plugin_parser = yacc.yacc(debug=DEBUG, write_tables=0)
#outputdir=path_to_folder,
# TODO: figure out how to make pip/setup.py be nice to PLY
# so parsetab.py can be writeable