-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_module.py
269 lines (217 loc) · 11.7 KB
/
parser_module.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
import re
import os
import generic_regexes
import parser_instructions
import flow_chart_graphics
import remi.gui as gui
"""
This parses dat and src files
analyzed instructions
"""
def fread_lines(file_path_and_name):
content = None
with open(file_path_and_name, "r") as f:
content = f.readlines()
return content
class MouseNavArea(gui.Container):
zoom_absolute_position = 1.0
zoom_min_value = 0.0
def __init__(self, *args, **kwargs):
gui.Container.__init__(self, *args, **kwargs)
self.style['overflow'] = 'hidden'
self.style['outline'] = '1px solid gray'
self.style['zoom'] = '1.0'
self.onmousemove.do(self.center_view)
self.onwheel.do(self.zoom, js_prevent_default=True, js_stop_propagation=True)
@gui.decorate_set_on_listener("(self, emitter, deltaY)")
@gui.decorate_event_js("var params={};" \
"params['deltaY']=event.deltaY;" \
"remi.sendCallbackParam('%(emitter_identifier)s','%(event_name)s',params);")
def onwheel(self, deltaY):
"""Called when the mouse cursor moves inside the Widget.
Args:
deltaY (float): the relative scroll value
"""
return (deltaY,)
def center_view(self, emitter, x, y):
x = float(x)
wself = float(gui.from_pix(self.css_width))
y = float(y)
hself = float(gui.from_pix(self.css_height))
offset_x = wself * self.zoom_absolute_position
offset_y = hself * self.zoom_absolute_position
for c in self.children.values():
c.css_position = 'relative'
wchild = wself
try:
wchild = gui.from_pix(c.css_width)
except:
wchild = float(c.attr_width)
wchild = wchild * self.zoom_absolute_position
if wself < wchild:
left = offset_x/2 -(wchild+offset_x-wself) * (x/wself)
#left = left + offset/2 - offset * (x/wself)
c.css_left = gui.to_pix( left / self.zoom_absolute_position )
else:
c.css_left = "0px"
hchild = hself
try:
hchild = gui.from_pix(c.css_height)
except:
hchild = float(c.attr_height)
hchild = hchild * self.zoom_absolute_position
if hself < hchild:
top = offset_y/2-(hchild+offset_y-hself) * (y/hself)
#top = top + offset/2 - offset * (y/hself)
c.css_top = gui.to_pix( top / self.zoom_absolute_position )
else:
c.css_top = "0px"
def zoom(self, emitter, relative_value):
self.zoom_absolute_position = min(max(self.zoom_min_value, self.zoom_absolute_position - float(relative_value)*0.0003), 2.0)
self.set_zoom( self.zoom_absolute_position )
def set_zoom(self, value):
self.zoom_absolute_position = value
for c in self.children.values():
c.style['zoom'] = str(value)
class KRLModuleSrcFileParser(parser_instructions.KRLGenericParser, gui.HBox):
file_path_name = '' # the str path and file
def __init__(self, file_path_name):
# read all instructions, parse and collect definitions
self.krl_procedures_and_functions_list = []
self.indent_comments = False
self.file_path_name = file_path_name
permissible_instructions = ['procedure begin', 'function begin']
permissible_instructions_dictionary = {k:v for k,v in parser_instructions.instructions_defs.items() if k in permissible_instructions}
parser_instructions.KRLGenericParser.__init__(self, permissible_instructions_dictionary)
gui.HBox.__init__(self)
self.css_align_items = 'flex-start'
self.style['outline'] = '2px solid gray'
#self.css_background_color = 'lightgray'
self.append(gui.ListView(width=200, style={'margin': '0px'}), 'list')
self.append(MouseNavArea(width=1000, height=650), 'container')
self.children['list'].onselection.do(self.on_proc_list_selected)
def on_proc_list_selected(self, widget, selected_key):
w = gui.from_pix(self.children['container'].css_width)
h = gui.from_pix(self.children['container'].css_height)
self.children['container'].append(widget.children[selected_key].node, 'proc_to_view')
widget.children[selected_key].node.draw()
best_zoom = min(w/float(widget.children[selected_key].node.attr_width), h/float(widget.children[selected_key].node.attr_height))
self.children['container'].set_zoom(best_zoom)
self.children['container'].zoom_min_value = best_zoom
def parse_single_instruction(self, code_line_original, code_line, instruction_name, match_groups, file_lines):
translation_result_tmp = []
if instruction_name == 'procedure begin':
param_list = code_line.split('(')[1].split(')')[0].split(',')
def filter_zero_sized(v):
return len(v.strip()) > 0
param_list = list(filter(filter_zero_sized,param_list))
param_names = [x.split(':')[0].strip() for x in param_list]
param_direction = [x.split(':')[1].strip() for x in param_list]
param_names = [re.sub(generic_regexes.index_3d, '', x) for x in param_names]
is_global = not match_groups[0] is None
procedure_name = match_groups[2]
#translation_result_tmp.append("@global_defs.interruptable_function_decorator")
node = parser_instructions.KRLProcedureParser( procedure_name, param_names )
#self.append(node)
li = gui.ListItem(node.name)
li.node = node
self.children['list'].append(li)
_translation_result_tmp, file_lines = node.parse(file_lines)
proc_def = "void " + procedure_name + "("
parameters_def = []
index = 0
for param in param_names:
if len(param.strip()):
if param in node.local_variables.keys():
parameters_def.append( node.local_variables[param] + ("&" if param_direction[index].lower() == 'out' else "") + " " + param )
index += 1
proc_def += ", ".join(parameters_def)
proc_def += "){"
translation_result_tmp.append( proc_def )
if len(_translation_result_tmp):
translation_result_tmp.extend(_translation_result_tmp)
#translation_result_tmp.extend("}")
if is_global:
parser_instructions.add_user_global_def('\n'.join(translation_result_tmp))
if instruction_name == 'function begin':
param_list = code_line.split('(')[1].split(')')[0].split(',')
def filter_zero_sized(v):
return len(v.strip()) > 0
param_list = list(filter(filter_zero_sized,param_list))
param_names = [x.split(':')[0].strip() for x in param_list]
param_direction = [x.split(':')[1].strip() for x in param_list]
param_names = [re.sub(generic_regexes.index_3d, '', x) for x in param_names]
procedure_name = match_groups[3]
is_global = not match_groups[0] is None
return_value_type_name = match_groups[2]
#translation_result_tmp.append("@global_defs.interruptable_function_decorator")
#translation_result_tmp.append( "def " + procedure_name + "(" + ", ".join(param_names) + "): #function returns %s"%return_value_type_name )
node = parser_instructions.KRLFunctionParser( procedure_name, param_names, return_value_type_name )
li = gui.ListItem(node.name)
li.node = node
self.children['list'].append(li)
_translation_result_tmp, file_lines = node.parse(file_lines)
func_def = return_value_type_name + " " + procedure_name + "("
parameters_def = []
index = 0
for param in param_names:
if len(param.strip()):
if param in node.local_variables.keys():
parameters_def.append( node.local_variables[param] + ("&" if param_direction[index].lower() == 'out' else "") + " " + param )
index += 1
func_def += ", ".join(parameters_def)
func_def += "){"
translation_result_tmp.append( func_def )
if len(_translation_result_tmp):
translation_result_tmp.extend(_translation_result_tmp)
#translation_result_tmp.extend("}")
if is_global:
parser_instructions.add_user_global_def('\n'.join(translation_result_tmp))
_translation_result_tmp, file_lines = parser_instructions.KRLGenericParser.parse_single_instruction(self, code_line_original, code_line, instruction_name, match_groups, file_lines)
if len(_translation_result_tmp):
translation_result_tmp.extend(_translation_result_tmp)
return translation_result_tmp, file_lines
class KRLModuleDatFileParser(parser_instructions.KRLGenericParser):
file_path_name = '' # the str path and file
def __init__(self, file_path_name):
# read all instructions, parse and collect definitions
self.indent_comments = False
self.file_path_name = file_path_name
permissible_instructions = ['dat begin', 'dat end', 'enum definition', 'struc declaration']
permissible_instructions_dictionary = {k:v for k,v in parser_instructions.instructions_defs.items() if k in permissible_instructions}
parser_instructions.KRLGenericParser.__init__(self, permissible_instructions_dictionary)
permissible_instructions = ['variable declaration', 'variable assignment']
permissible_instructions_dictionary = {k:v for k,v in parser_instructions.instructions_defs.items() if k in permissible_instructions}
self.permissible_instructions_dictionary.update(permissible_instructions_dictionary)
class KRLModule(gui.VBox):
name = ''
module_dat = None # KRLDat instance
module_src = None # KRLSrc instance
def __init__(self, module_name, dat_path_and_file = '', src_path_and_file = '', imports_to_prepend = '', *args, **kwargs):
super(KRLModule, self).__init__(*args, **kwargs)
self.css_align_items = 'flex-start'
self.append(gui.Label("MODULE: %s"%module_name, style={'font-weight':'bolder', 'font-size':'20px'}))
self.name = module_name
if len(dat_path_and_file):
self.module_dat = KRLModuleDatFileParser(dat_path_and_file)
#it seems to have no relevance in flowcharts
#self.module_dat.text = "DAT %s"%module_name
#self.append(self.module_dat)
file_lines = fread_lines(dat_path_and_file)
translation_result, file_lines = self.module_dat.parse(file_lines)
with open(os.path.dirname(os.path.abspath(__file__)) + "/%s.h"%self.name, 'w+') as f:
f.write(imports_to_prepend)
for l in translation_result:
f.write(l + '\n')
if len(src_path_and_file):
has_dat = not (self.module_dat is None)
self.module_src = KRLModuleSrcFileParser(src_path_and_file)
self.append(self.module_src)
file_lines = fread_lines(src_path_and_file)
translation_result, file_lines = self.module_src.parse(file_lines)
with open(os.path.dirname(os.path.abspath(__file__)) + "/%s.c"%self.name, 'w+') as f:
if not has_dat:
f.write(imports_to_prepend)
f.write('#include "%s.h"\n'%self.name)
for l in translation_result:
f.write(l + '\n')