-
Notifications
You must be signed in to change notification settings - Fork 4
/
script_munger.py
120 lines (106 loc) · 4.26 KB
/
script_munger.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
# Copyright (c) 2017, Aeva M. Palecek
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program 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 General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
def find_blocks(src, start=0, nesting=0, scope=None):
if not scope:
scope = []
seek = start;
accumulate = ''
while seek < (len(src)):
chunk = re.match(
r'^(.*?)({|}|\n|\Z|//.*?(?:\n|\Z)|/\*.*?(?:\*/|\Z))',
src[seek:], re.MULTILINE | re.DOTALL)
if chunk:
seek += (chunk.end() - chunk.start())
text, event = chunk.groups()
assert(text.count("}") == 0)
if event.strip():
if event == '{':
if accumulate:
scope.append(accumulate)
accumulate = ''
seek, new_scope = find_blocks(
src, seek, nesting+1, [text + event])
scope.append(new_scope)
continue
accumulate += text
accumulate += event
if event == '}':
if accumulate:
scope.append(accumulate)
if nesting != 0:
return seek, scope
else:
print "unexpected closing parenthesis"
else:
accumulate += text
if event:
accumulate += event
else:
print "error parsing script, returning early"
if accumulate:
scope.append(accumulate)
if nesting == 0:
return scope
else:
return len(src), scope
if accumulate:
scope.append(accumulate)
if nesting == 0:
return scope
else:
return len(src), scope
def find_immediates(src):
"""
Return a flat list of tokens representing commands that could be
executed immediately upon opening the level without any game
state. This includes anything that is not in a conditional block,
and the contents of conditional blocks where the condition is only
one of "playerenters" or "created".
"""
blocks = find_blocks(src)
found = []
pattern = r'^\s*if\s*\(\s*(.*?)\s*\)\s*{\s*$'
for index, block in enumerate(blocks):
if block and type(block) == list:
first = block[0]
if first and type(first) == unicode:
first = first.encode("utf-8")
if first and type(first) == str:
conditional = re.match(pattern, first)
if conditional:
condition = conditional.groups()[0]
parts = re.split(r'(?:&&|\|\||\s+)', condition)
include_block = False
for part in parts:
if part in ['playerenters', 'created', 'isleader']:
include_block = True
else:
include_block = False
break
if include_block:
found.append(block)
if block and type(block) in [str, unicode]:
found.append([block])
reduced = []
for block in found:
tokens = [chunk for chunk in block if type(chunk) == str]
flattened = '\n'.join(tokens)
narrowed = re.match(
r'(?:\A.*?{)(.*)(?:}.*?\Z)', flattened,
re.MULTILINE | re.DOTALL)
if narrowed:
flattened = narrowed.groups()[0]
commands = re.split(r'(?:\n|;)', flattened)
reduced += [command.strip() for command in commands if
command.strip()]
return reduced