blob: 3723e7cf094b4ce3062fac186e3b20e06a7f4e8d [file] [log] [blame]
Sapan Bhatia3cfdf632017-06-08 05:14:03 +02001import plyxproto.model as m
Sapan Bhatiadb183c22017-06-23 02:47:42 -07002from plyxproto.helpers import Visitor
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -07003import pdb
4import argparse
Sapan Bhatia3cfdf632017-06-08 05:14:03 +02005import plyxproto.parser as plyxproto
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -07006import traceback
7import sys
8import jinja2
9import os
10
11class Stack(list):
12 def push(self,x):
13 self.append(x)
14
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020015def str_to_dict(s):
16 lst = s.rsplit('.',1)
17 name = lst[-1]
18
19 if len(lst)==2:
20 package = lst[0]
21 else:
22 package = ''
23
24 return {'name': name, 'package': package, 'fqn': s}
25
Sapan Bhatiaae9645c2017-05-05 15:35:54 +020026def replace_link(obj):
27 try:
28 link = obj.link
29 try:
30 through = link['through']
31 except KeyError:
32 through = None
33
34 try:
35 through_str = through[1:-1]
36 except TypeError:
37 through_str = None
38
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020039 if through_str:
40 through_dict = str_to_dict(through_str)
41 else:
42 through_dict = {}
43
44 model_dict = str_to_dict(link['model'][1:-1])
45
46 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 +020047 return ls
48 except:
49 return obj
50
Sapan Bhatiadb183c22017-06-23 02:47:42 -070051class Proto2XProto(Visitor):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070052 def __init__(self):
53 super(Proto2XProto, self).__init__()
54
55 self.stack = Stack()
56 self.count_stack = Stack()
57 self.content=""
58 self.offset=0
59 self.statementsChanged=0
60 self.message_options = {}
61 self.options = {}
62 self.current_message_name = None
63
64 self.xproto_message_options = ['bases']
65 self.xproto_field_options = ['model']
66 self.verbose = 0
67 self.first_field = True
68 self.first_method = True
Sapan Bhatiaae9645c2017-05-05 15:35:54 +020069
70 def proto_to_xproto_field(self, obj):
71 try:
72 opts = {}
73 for fd in obj.fieldDirective:
74 k = fd.pval.name.value.pval
75 v = fd.pval.value.value.pval
76 opts[k]=v
77
78 if ('model' in opts and 'link' in opts and 'port' in opts):
79 obj.link = opts
80 pass
81 except KeyError:
82 raise
83
84 def proto_to_xproto_message(self, obj):
85 try:
86 bases = self.message_options['bases'].split(',')
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020087 bases = map(lambda x:str_to_dict(x[1:-1]), bases)
Sapan Bhatiaae9645c2017-05-05 15:35:54 +020088 obj.bases = bases
89 except KeyError:
90 raise
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070091
92 def map_field(self, obj, s):
93 if 'model' in s:
94 link = m.LinkDefinition('onetoone','src','name','dst', obj.linespan, obj.lexspan, obj.p)
95 lspec = m.LinkSpec(link, obj)
96 else:
97 lspec = obj
98 return lspec
99
100
101 def get_stack(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700102 return self.stack
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700103
104 def visit_PackageStatement(self, obj):
105 '''Ignore'''
106 return True
107
108 def visit_ImportStatement(self, obj):
109 '''Ignore'''
110 return True
111
112 def visit_OptionStatement(self, obj):
Sapan Bhatiaae9645c2017-05-05 15:35:54 +0200113 if (self.current_message_name):
114 k = obj.name.value.pval
115 self.message_options[k] = obj.value.value.pval
116 if (k in self.xproto_message_options):
117 obj.mark_for_deletion = True
118 else:
119 self.options[obj.name.value.pval] = obj.value.value.pval
120
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700121 return True
122
123 def visit_LU(self, obj):
124 return True
125
126 def visit_default(self, obj):
127 return True
128
129 def visit_FieldDirective(self, obj):
130 return True
131
132 def visit_FieldDirective_post(self, obj):
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700133 return True
134
135 def visit_FieldType(self, obj):
136 return True
137
138 def visit_LinkDefinition(self, obj):
139 return True
140
141 def visit_FieldDefinition(self, obj):
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700142 return True
143
144 def visit_FieldDefinition_post(self, obj):
Sapan Bhatiaae9645c2017-05-05 15:35:54 +0200145 self.proto_to_xproto_field(obj)
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700146 return True
147
148 def visit_EnumFieldDefinition(self, obj):
149 return True
150
151 def visit_EnumDefinition(self, obj):
152 return True
153
154 def visit_MessageDefinition(self, obj):
Sapan Bhatiaae9645c2017-05-05 15:35:54 +0200155 self.current_message_name = obj.name.value.pval
156 self.message_options = {}
157
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700158 return True
159
160 def visit_MessageDefinition_post(self, obj):
Sapan Bhatiaae9645c2017-05-05 15:35:54 +0200161 self.proto_to_xproto_message(obj)
162 obj.body = filter(lambda x:not hasattr(x, 'mark_for_deletion'), obj.body)
163 obj.body = map(replace_link, obj.body)
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700164
Sapan Bhatiaae9645c2017-05-05 15:35:54 +0200165 self.current_message_name = None
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -0700166 return True
167
168 def visit_MessageExtension(self, obj):
169 return True
170
171 def visit_MethodDefinition(self, obj):
172 return True
173
174 def visit_ServiceDefinition(self, obj):
175 return True
176
177 def visit_ExtensionsDirective(self, obj):
178 return True
179
180 def visit_Literal(self, obj):
181 return True
182
183 def visit_Name(self, obj):
184 return True
185
186 def visit_DotName(self, obj):
187 return True
188
189 def visit_Proto(self, obj):
190 self.count_stack.push(len(obj.body))
191 return True
192
193 def visit_Proto_post(self, obj):
194 return True
195
196 def visit_LinkSpec(self, obj):
197 return False