-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscrib_util.py
More file actions
executable file
·273 lines (220 loc) · 8.39 KB
/
Copy pathscrib_util.py
File metadata and controls
executable file
·273 lines (220 loc) · 8.39 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
import os
import sys
from antlr3 import (
ANTLRFileStream as ANTLRFileStream,
CommonTokenStream as CommonTokenStream,
RecognitionException as RecognitionException
)
from ScribbleLexer import ScribbleLexer as ScribbleLexer
from ScribbleParser import ScribbleParser as ScribbleParser
import scrib_constants as constants
from visit.commonerrornodechecker import \
CommonErrorNodeChecker as CommonErrorNodeChecker
from ast.globel.globalprotocoldef import \
pretty_print as globalprotocoldef_pretty_print
from ast.globel.globalprotocolblock import \
pretty_print as globalprotocolblock_pretty_print
from ast.globel.globalchoice import \
pretty_print as globalchoice_pretty_print
from ast.globel.globalcontinue import \
pretty_print as globalcontinue_pretty_print
from ast.globel.globaldo import \
pretty_print as globaldo_pretty_print
from ast.globel.globalinterruptible import \
pretty_print as globalinterruptible_pretty_print
from ast.globel.globalmessagetransfer import \
pretty_print as globalmessagetransfer_pretty_print
from ast.globel.globalparallel import \
pretty_print as globalparallel_pretty_print
from ast.globel.globalrecursion import \
pretty_print as globalrecursion_pretty_print
from ast.local.localsend import pretty_print as localsend_pretty_print
from ast.local.localreceive import pretty_print as localreceive_pretty_print
"""from ast.local.localchoice import \
pretty_print as localchoice_pretty_print
from ast.local.localcontinue import \
pretty_print as localcontinue_pretty_print
from ast.local.localdo import \
pretty_print as localdo_pretty_print
from ast.local.localinterruptible import \
pretty_print as localinterruptible_pretty_print
from ast.local.localmessagetransfer import \
pretty_print as localmessagetransfer_pretty_print
from ast.local.localparallel import \
pretty_print as localparallel_pretty_print
from ast.local.localrecursion import \
pretty_print as localrecursion_pretty_print"""
from visit.unfolder import Unfolder as Unfolder
"""def getCurrentFunctionName():
return sys._getframe(1).f_code.co_name"""
# AST node "type" functions
def get_node_type(node_):
return node_.getText() # gets the token text?
def set_node_type(node_, type_):
node_.token.setText(type_)
def filter_node_types(nodes, type_):
res = []
for node_ in nodes:
if get_node_type(node_) == type_:
res.append(node_)
return res
# Duplicate an ANTLR node (shallow clone -- and may be currently incomplete)
def antlr_dupnode_and_replace_children(node_, children):
clone = node_.dupNode()
for child in children:
clone.addChild(child)
clone.setParent(node_.getParent())
# How about text and token fields?
return clone
##
# Tool feedback
def report_warning(message):
print message
def report_error(message):
print message
sys.exit(0) # Not a failure
def search_importpath_for_file(importpath, filepath):
path = None
found = False
for ip in importpath:
path = ip + '/' + filepath
if os.path.isfile(path):
try:
open(path)
found = True
return path
except IOError as e:
pass
if not found:
report_error("File not found: " + filepath)
# Read Scribble file
def load_file_and_parse_module(filepath):
try:
char_stream = ANTLRFileStream(filepath)
lexer = ScribbleLexer(char_stream)
tokens = CommonTokenStream(lexer)
parser = ScribbleParser(tokens)
tree = parser.module().tree
checker = CommonErrorNodeChecker()
checker._check_for_errornode(tree)
return tree
except IOError as e:
print e
sys.exit(1)
except RecognitionException as e:
print e
sys.exit(1)
# except Exception as e: # Failed parse doesn't seem to throw any exceptions
# traceback.print_stack()
def write_to_file(filepath, text):
try:
if filepath.find('/') != -1 or filepath.find('\\') != -1:
d = os.path.dirname(filepath)
if not os.path.exists(d):
os.makedirs(d)
with open(filepath, 'w') as file:
file.write(text)
except Exception as e:
print "Write to file failed: ", e
sys.exit(1)
##
# Filepath helpers
def parse_file_extension_from_filepath(filepath):
return filepath[filepath.rfind('.') + 1:]
def parse_directory_from_filepath(filepath):
if filepath.find('/') != -1:
return filepath[:filepath.rfind('/')]
elif filepath.find('\\') != -1:
return filepath[:filepath.rfind('\\')]
else:
return '.'
##
# Conversion between module names and filepaths
def convert_full_module_name_to_filepath(fmn):
return fmn.replace('.', '/') + '.' + constants.SCRIBBLE_FILE_EXTENSION
def parse_simple_module_name_from_filepath(filepath):
ext = parse_file_extension_from_filepath(filepath)
if ext != constants.SCRIBBLE_FILE_EXTENSION:
report_error("Bad file extension: " + ext)
dirsep = '/'
if filepath.find('\\') != -1: # HACK: for Windows paths
dirsep = '\\'
module = filepath[filepath.rfind(dirsep) + 1:filepath.rfind('.')]
return module
##
# module and member name helpers
def get_simple_module_name_from_full_module_name(fullname):
if fullname.find('.') == -1:
return fullname
else:
return fullname[fullname.rfind('.') + 1:]
def get_full_module_name_from_full_member_name(fullname):
return fullname[:fullname.rfind('.')]
def get_simple_member_name_from_full_member_name(fullname):
return fullname[fullname.rfind('.') + 1:]
# converse to globaldo.get_projected_member_name
#
# FIXME: broken for names with underscores
def get_global_module_name_from_projected_member_name(localname):
tmp = get_full_module_name_from_full_member_name(localname)
tmp = tmp[:tmp.rfind('_')]
tmp = tmp[:tmp.rfind('_')]
return tmp
# Assembles a list of "name nodes" into one dot-separated name (the name "parse"
# is not suitable)
def parse_dot_separated_name_from_node_list(nodes):
name = nodes[0].getText()
for n in nodes[1:]:
name = name + '.' + n.getText()
return name
# TODO: make a proper PrettyPrint visitor, with support for indenting
def pretty_print(node_):
ntype = get_node_type(node_)
if ntype == constants.GLOBAL_PROTOCOL_DEF_NODE_TYPE:
return globalprotocoldef_pretty_print(node_)
elif ntype == constants.GLOBAL_PROTOCOL_BLOCK_NODE_TYPE:
return globalprotocolblock_pretty_print(node_)
elif ntype == constants.GLOBAL_MESSAGE_TRANSFER_NODE_TYPE:
return globalmessagetransfer_pretty_print(node_)
elif ntype == constants.GLOBAL_CHOICE_NODE_TYPE:
return globalchoice_pretty_print(node_)
elif ntype == constants.GLOBAL_RECURSION_NODE_TYPE:
return globalrecursion_pretty_print(node_)
elif ntype == constants.GLOBAL_CONTINUE_NODE_TYPE:
return globalcontinue_pretty_print(node_)
elif ntype == constants.GLOBAL_PARALLEL_NODE_TYPE:
return globalparallel_pretty_print(node_)
elif ntype == constants.GLOBAL_DO_NODE_TYPE:
return globaldo_pretty_print(node_)
elif ntype == constants.GLOBAL_INTERRUPTIBLE_NODE_TYPE:
return globalinterruptible_pretty_print(node_)
elif ntype == constants.LOCAL_SEND_NODE_TYPE:
return localsend_pretty_print(node_)
elif ntype == constants.LOCAL_RECEIVE_NODE_TYPE:
return localreceive_pretty_print(node_)
elif ntype == constants.LOCAL_CHOICE_NODE_TYPE:
return localchoice_pretty_print(node_)
elif ntype == constants.LOCAL_RECURSION_NODE_TYPE:
return localrecursion_pretty_print(node_)
elif ntype == constants.LOCAL_CONTINUE_NODE_TYPE:
return localcontinue_pretty_print(node_)
elif ntype == constants.LOCAL_PARALLEL_NODE_TYPE:
return localparallel_pretty_print(node_)
elif ntype == constants.LOCAL_DO_NODE_TYPE:
return localdo_pretty_print(node_)
elif ntype == constants.LOCAL_INTERRUPTIBLE_NODE_TYPE:
return localinterruptible_pretty_print(node_)
else:
raise Exception("TODO: ", ntype)
# Misc
def replace_in_list(l, map):
ll = []
for x in l:
if x in map.keys():
ll.append(map[x])
else:
ll.append(x)
return ll
def unfold_once(context_, node_):
unfolder = Unfolder(context_) # Context will be updated
return unfolder.once_unfold_all(node_)