Repository navigation
Expand file tree
/
Copy pathtext.py
More file actions
483 lines (407 loc) · 14.1 KB
/
Copy pathtext.py
File metadata and controls
483 lines (407 loc) · 14.1 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
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/>.
"""Experimental text parser for scripts.
The original parser used block plugin syntax, the same as the ``scratchblocks``
tag used on the Scratch Forums and Wiki. See:
http://wiki.scratch.mit.edu/wiki/Block_Plugin
This parser supports most of the block plugin syntax. Most notably, using < >
for boolean shaped blocks is not supported.
"""
import re
from collections import OrderedDict
import kurt
#-- Tokens --#
class Token(object):
def __init__(self, value):
self.value = value
def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.value)
class symbol(Token):
lbp = 0
class literal(Token):
def nud(self):
return self.value
class number(Token):
def __init__(self, value):
value = float(value)
if int(value) == value:
value = int(value)
self.value = value
def nud(self):
return self
class string(Token):
lbp = 0
def nud(self):
return self
class color(Token):
def nud(self):
self.value = kurt.Color(self.value)
return self
class lparen(Token):
lbp = 0
def nud(self):
global token
contents = expression()
if isinstance(contents, rparen): # empty brackets
return
if not isinstance(token, rparen):
raise SyntaxError("Expected bracket to match %s" % self.value)
token = next()
return contents
class rparen(Token):
lbp = 0
def nud(self):
return self
class newline(symbol):
name = "EOL"
lbp = 3
def nud(self):
return []
def led(self, left):
if isinstance(left, kurt.Block):
left = [left]
return left
class end_token(Token):
name = "EOF"
lbp = 0
def __init__(self):
self.value = None
def __repr__(self):
return "%s()" % self.__class__.__name__
#-- Identity Token --#
# block kinds:
# - infix
# - postfix ("_ sensor value")
# - insert transformations ("var", "list", "param", "{}")
def inline_blocks():
for block in kurt.plugin.Kurt.blocks:
if len(block.inserts) == 1 and block.inserts[0].shape == "inline":
yield block
def all_the_blocks():
for block in kurt.plugin.Kurt.blocks:
done_text = set()
for block in block.conversions:
if block.text in done_text:
continue
yield block
done_text.add(block.text)
def match_part(part, block_part):
if isinstance(block_part, kurt.Insert):
return (isinstance(part, (Token, kurt.Block, list)))
else:
return (block_part.strip() == part)
def blocks_starting_with(parts):
suppress_blocks = set(suppress_block_names())
for block in all_the_blocks():
if len(parts) > len(block.parts):
continue
if (isinstance(block.parts[0], basestring)
and block.parts[0].strip() in suppress_blocks):
continue
for (p, bp) in zip(parts, block.parts):
if not match_part(p, bp):
break
else:
yield block
def next_block_part(parts):
for block in blocks_starting_with(parts):
next_part = (block.parts[len(parts)]
if len(block.parts) > len(parts)
else None)
if isinstance(next_part, basestring):
next_part = next_part.strip()
yield next_part
def blocks_by_parts(parts):
for block in all_the_blocks():
if len(parts) != len(block.parts):
continue
for (p, bp) in zip(parts, block.parts):
if not match_part(p, bp):
break
else:
yield block
def block_from_parts(parts):
args = []
for part in parts:
if isinstance(part, (Token, kurt.Block, list)):
args.append(part)
failure = ""
for block in blocks_by_parts(parts):
block_args = []
for (arg, insert) in zip(args, block.inserts):
if isinstance(arg, Token):
if (isinstance(arg, iden) and
not insert.shape in ("number-menu", "readonly-menu")):
arg = kurt.Block(arg.value)
elif (isinstance(arg, iden) and
insert.shape in ("number-menu", "readonly-menu") and
arg not in insert.options()):
try:
arg = kurt.Block(arg.value)
except kurt.UnknownBlock:
arg = arg.value
else:
arg = arg.value
elif (isinstance(arg, kurt.Block) and
arg.type.text in insert.options(context)):
arg = arg.type.text
ok = False
if insert.shape == "string" or insert.kind == "broadcast":
if isinstance(arg, (int, long, float, complex, str)):
arg = unicode(arg)
ok = isinstance(arg, (unicode, kurt.Block))
elif insert.shape == "color":
ok = isinstance(arg, kurt.Color)
elif insert.shape == "boolean":
ok = arg is None or (isinstance(arg, kurt.Block)
and arg.type.shape == "boolean")
elif insert.shape == "stack":
ok = isinstance(arg, list)
elif insert.shape in ("number", "number-menu", "readonly-menu"):
if insert.shape in ("number", "number-menu"):
if isinstance(arg, basestring):
try:
arg = float(arg)
arg = int(arg) if int(arg) == arg else arg
except ValueError:
pass
if isinstance(arg, (int, long, float, complex,
kurt.Block)):
ok = True
if insert.shape in ("readonly-menu", "number-menu"):
if (str(arg) in insert.options(context)
or isinstance(arg, kurt.Block)):
ok = True
if not ok:
failure = "%r doesn't fit %s" % (arg, insert.shape)
if insert.kind:
failure += " " + insert.kind
break
block_args.append(arg)
else:
return kurt.Block(block, *block_args)
else:
throw("Wrong type of arguments to block: %s" % failure, repr(parts))
class iden(Token):
@property
def lbp(self):
return PRECEDENCE.get(self.value, 100)
def nud(self):
try:
return self.parse_block([self.value])
except SyntaxError:
for block in inline_blocks():
if self.value in block.inserts[0].options(context):
return kurt.Block(block, self.value)
if len(self.parts) == 1 and self.value in set(make_menu_tokens()):
return iden(self.value)
else:
raise
def led(self, left):
if isinstance(left, list):
return left + [self.parse_block([self.value])]
else:
return self.parse_block([left, self.value])
def parse_block(self, parts):
global token
self.parts = parts
while 1:
part = self.parse_one_part(parts)
if part is not None:
if isinstance(part, rparen):
part = False
parts.append(part)
else:
block = block_from_parts(parts)
if isinstance(token, end_token):
return block
if block.type.has_insert("stack"):
if not token.value == "end":
throw("Expected 'end' after C mouth")
token = next()
return block
def parse_one_part(self, parts):
global token, next
expect = set(next_block_part(parts))
if not expect:
self.parts = parts
throw("Can't find block %r" % parts)
if expect == set(['']):
return ''
all_inserts = filter(lambda p: isinstance(p, kurt.Insert), expect)
if isinstance(token, iden):
text_segments = filter(lambda p: isinstance(p, basestring), expect)
menu_inserts = filter(lambda i: i.options(context), all_inserts)
if token.value in text_segments:
part = token.value
token = next()
return part
for insert in menu_inserts:
if token.value in insert.options(context):
part = token
token = next()
return part
if None in expect:
return None
stack_inserts = filter(lambda i: i.shape == 'stack', all_inserts)
if stack_inserts:
if isinstance(token, end_token):
part = []
else:
part = expression(1)
assert isinstance(part, list)
return part
if isinstance(token, (newline, end_token)):
throw("Unexpected EOL", expected=expect)
if all_inserts:
return expression(self.lbp)
throw("Wrong argument", expected=expect)
#-- Tokenizer --#
PRECEDENCE = {
"*": 130,
"/": 130,
"+": 120,
"-": 120,
"<": 110,
"=": 110,
">": 110,
"else": 0,
}
TOKENS = [(re.compile(r), cls) for (r, cls) in [
(r'end', symbol),
(r'(-?[0-9]+(\.[0-9]+)?)', number),
(r'\[(#[A-Fa-f0-9]{3,6})\]', color),
(r'\[([^\]]+?)( v)?\]', string),
(r'\"([^"]+)\"', string),
(r"\'([^']+)\'", string),
(r'\(', lparen),
(r'\)', rparen),
# (r'\<', lparen),
# (r'\>', rparen),
(r'\n|\r|\r\n', newline),
]]
NEWLINE_PAT = re.compile(r'\n|\r|\r\n')
SEPARATOR_PAT = re.compile(r'[^A-Za-z:#%+*-,=<?>]')
WHITESPACE_PAT = re.compile(r'[ \t]+')
SEGMENT_ALIASES = {
"when green flag clicked": "when @greenFlag clicked",
"when gf clicked": "when @greenFlag clicked",
"turn left": "turn @turnLeft",
"turn right": "turn @turnRight",
"turn ccw": "turn @turnRight",
"turn cw": "turn @turnRight",
}
def make_block_tokens():
for block in kurt.plugin.Kurt.blocks:
for part in block.parts:
if isinstance(part, basestring):
yield part.strip()
for alias in SEGMENT_ALIASES:
yield alias
def make_menu_tokens():
global context
for kind in kurt.Insert.KIND_OPTIONS:
if kind == "broadcast": continue
for o in kurt.Insert(None, kind).options(context):
yield str(o)
def suppress_block_names():
for block in kurt.plugin.Kurt.blocks:
if isinstance(block.parts[0], kurt.Insert):
for o in block.parts[0].options(context):
yield o
def tokenize(program):
block_tokens = sorted(set(make_block_tokens()) | set(make_menu_tokens()))
block_tokens.sort(key=len, reverse=True)
block_tokens = filter(lambda x: not x.isdigit(), block_tokens)
block_tokens = filter(None, block_tokens)
global remain, lineno
remain = program
lineno = 1
while remain:
m = WHITESPACE_PAT.match(remain)
if m:
remain = remain[m.end():]
if not remain:
break
for (pat, cls) in TOKENS:
m = pat.match(remain)
if m:
if m.groups():
contents = m.group(1)
else:
contents = m.group(0).strip()
yield cls(contents)
remain = remain[m.end():]
if cls == newline:
lineno += 1
break
else:
for value in block_tokens:
if remain.startswith(value):
after_value = remain[len(value):]
if not after_value or SEPARATOR_PAT.match(after_value[0]):
value = SEGMENT_ALIASES.get(value, value)
yield iden(value)
remain = after_value
break
else:
throw("Unknown token at %r" % remain.split("\n")[0])
yield end_token()
#-- Parser --#
p_input = ""
def expression(rbp=0):
global token
t = token
token = next()
left = t.nud()
if not hasattr(token, "lbp"):
throw("Not an operator: %r" % token)
while rbp < token.lbp:
t = token
token = next()
left = t.led(left)
if not hasattr(token, "lbp"):
throw("Not an operator: %r" % token)
return left
def parse(program, scriptable):
global token, next, context, p_input
# for errors
p_input = program
context = scriptable
next = tokenize(program).next
token = next()
result = expression()
if not isinstance(token, end_token):
throw("Expected end of input")
if isinstance(result, kurt.Block):
result = [result]
if not isinstance(result, list):
throw("Result does not evaluate to a block")
return kurt.Script(result)
def throw(msg, hint=None, expected=None):
global remain
if expected:
repr_expected = map(repr, expected)
hint = "Expected %s" % (repr_expected[0] if len(repr_expected) == 1
else "one of " + ", ".join(repr_expected))
if hint:
msg += ". " + hint
line = NEWLINE_PAT.split(p_input)[lineno - 1]
offset = len(p_input) - len(remain) #- len(token.value)
err = SyntaxError(msg, ('<string>', lineno, offset, line))
err.expected = expected
raise err