blob: f05c2bc83c450b48b41129579aae6b3a349086b4 [file] [log] [blame]
Sapan Bhatia3cfdf632017-06-08 05:14:03 +02001import plyxproto.model as m
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -07002import pdb
3import argparse
Sapan Bhatia3cfdf632017-06-08 05:14:03 +02004import plyxproto.parser as plyxproto
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -07005import traceback
6import sys
7import jinja2
8import os
9
10class Stack(list):
11 def push(self,x):
12 self.append(x)
13
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020014def str_to_dict(s):
15 lst = s.rsplit('.',1)
16 name = lst[-1]
17
18 if len(lst)==2:
19 package = lst[0]
20 else:
21 package = ''
22
23 return {'name': name, 'package': package, 'fqn': s}
24
Sapan Bhatiaae9645c2017-05-05 15:35:54 +020025def replace_link(obj):
26 try:
27 link = obj.link
28 try:
29 through = link['through']
30 except KeyError:
31 through = None
32
33 try:
34 through_str = through[1:-1]
35 except TypeError:
36 through_str = None
37
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020038 if through_str:
39 through_dict = str_to_dict(through_str)
40 else:
41 through_dict = {}
42
43 model_dict = str_to_dict(link['model'][1:-1])
44
45 ls = m.LinkSpec(obj, m.LinkDefinition(link['link'][1:-1],obj.name,model_dict,link['port'][1:-1],through_dict))
Sapan Bhatiaae9645c2017-05-05 15:35:54 +020046 return ls
47 except:
48 return obj
49
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070050class Proto2XProto(m.Visitor):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070051 def __init__(self):
52 super(Proto2XProto, self).__init__()
53
54 self.stack = Stack()
55 self.count_stack = Stack()
56 self.content=""
57 self.offset=0
58 self.statementsChanged=0
59 self.message_options = {}
60 self.options = {}
61 self.current_message_name = None
62
63 self.xproto_message_options = ['bases']
64 self.xproto_field_options = ['model']
65 self.verbose = 0
66 self.first_field = True
67 self.first_method = True
Sapan Bhatiaae9645c2017-05-05 15:35:54 +020068
69 def proto_to_xproto_field(self, obj):
70 try:
71 opts = {}
72 for fd in obj.fieldDirective:
73 k = fd.pval.name.value.pval
74 v = fd.pval.value.value.pval
75 opts[k]=v
76
77 if ('model' in opts and 'link' in opts and 'port' in opts):
78 obj.link = opts
79 pass
80 except KeyError:
81 raise
82
83 def proto_to_xproto_message(self, obj):
84 try:
85 bases = self.message_options['bases'].split(',')
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020086 bases = map(lambda x:str_to_dict(x[1:-1]), bases)
Sapan Bhatiaae9645c2017-05-05 15:35:54 +020087 obj.bases = bases
88 except KeyError:
89 raise
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070090
91 def map_field(self, obj, s):
92 if 'model' in s:
93 link = m.LinkDefinition('onetoone','src','name','dst', obj.linespan, obj.lexspan, obj.p)
94 lspec = m.LinkSpec(link, obj)
95 else:
96 lspec = obj
97 return lspec
98
99
100 def get_stack(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700101 return self.stack
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700102
103 def visit_PackageStatement(self, obj):
104 '''Ignore'''
105 return True
106
107 def visit_ImportStatement(self, obj):
108 '''Ignore'''
109 return True
110
111 def visit_OptionStatement(self, obj):
Sapan Bhatiaae9645c2017-05-05 15:35:54 +0200112 if (self.current_message_name):
113 k = obj.name.value.pval
114 self.message_options[k] = obj.value.value.pval
115 if (k in self.xproto_message_options):
116 obj.mark_for_deletion = True
117 else:
118 self.options[obj.name.value.pval] = obj.value.value.pval
119
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700120 return True
121
122 def visit_LU(self, obj):
123 return True
124
125 def visit_default(self, obj):
126 return True
127
128 def visit_FieldDirective(self, obj):
129 return True
130
131 def visit_FieldDirective_post(self, obj):
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700132 return True
133
134 def visit_FieldType(self, obj):
135 return True
136
137 def visit_LinkDefinition(self, obj):
138 return True
139
140 def visit_FieldDefinition(self, obj):
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700141 return True
142
143 def visit_FieldDefinition_post(self, obj):
Sapan Bhatiaae9645c2017-05-05 15:35:54 +0200144 self.proto_to_xproto_field(obj)
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700145 return True
146
147 def visit_EnumFieldDefinition(self, obj):
148 return True
149
150 def visit_EnumDefinition(self, obj):
151 return True
152
153 def visit_MessageDefinition(self, obj):
Sapan Bhatiaae9645c2017-05-05 15:35:54 +0200154 self.current_message_name = obj.name.value.pval
155 self.message_options = {}
156
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700157 return True
158
159 def visit_MessageDefinition_post(self, obj):
Sapan Bhatiaae9645c2017-05-05 15:35:54 +0200160 self.proto_to_xproto_message(obj)
161 obj.body = filter(lambda x:not hasattr(x, 'mark_for_deletion'), obj.body)
162 obj.body = map(replace_link, obj.body)
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700163
Sapan Bhatiaae9645c2017-05-05 15:35:54 +0200164 self.current_message_name = None
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700165 return True
166
167 def visit_MessageExtension(self, obj):
168 return True
169
170 def visit_MethodDefinition(self, obj):
171 return True
172
173 def visit_ServiceDefinition(self, obj):
174 return True
175
176 def visit_ExtensionsDirective(self, obj):
177 return True
178
179 def visit_Literal(self, obj):
180 return True
181
182 def visit_Name(self, obj):
183 return True
184
185 def visit_DotName(self, obj):
186 return True
187
188 def visit_Proto(self, obj):
189 self.count_stack.push(len(obj.body))
190 return True
191
192 def visit_Proto_post(self, obj):
193 return True
194
195 def visit_LinkSpec(self, obj):
196 return False