blob: e94566c39f1f0f3968c3cc86ba6eb39683e3ae94 [file] [log] [blame]
Dusan Klinecccaa0d92014-11-09 03:21:31 +01001__author__ = "Dusan (Ph4r05) Klinec"
2__copyright__ = "Copyright (C) 2014 Dusan (ph4r05) Klinec"
3__license__ = "Apache License, Version 2.0"
4__version__ = "1.0"
5
Dusan Klinece26bb022014-11-09 12:21:37 +01006class Visitor(object):
7
8 def __init__(self, verbose=False):
9 self.verbose = verbose
10
11 def __getattr__(self, name):
12 if not name.startswith('visit_'):
Dusan Klinecc9b031a2014-11-10 13:21:08 +010013 raise AttributeError('name must start with visit_ but was {}'.format(name))
Dusan Klinece26bb022014-11-09 12:21:37 +010014
15 def f(element):
16 if self.verbose:
17 msg = 'unimplemented call to {}; ignoring ({})'
18 print(msg.format(name, element))
19 return True
20 return f
21
22 # visitor.visit_PackageStatement(self)
23 # visitor.visit_ImportStatement(self)
24 # visitor.visit_OptionStatement(self)
25 # visitor.visit_FieldDirective(self)
26 # visitor.visit_FieldType(self)
27 # visitor.visit_FieldDefinition(self)
28 # visitor.visit_EnumFieldDefinition(self)
29 # visitor.visit_EnumDefinition(self)
30 # visitor.visit_MessageDefinition(self)
31 # visitor.visit_MessageExtension(self)
32 # visitor.visit_MethodDefinition(self)
33 # visitor.visit_ServiceDefinition(self)
34 # visitor.visit_ExtensionsDirective(self)
35 # visitor.visit_Literal(self)
36 # visitor.visit_Name(self)
Dusan Klinecc9b031a2014-11-10 13:21:08 +010037 # visitor.visit_Proto(self)
Dusan Klineca9f6d362014-11-10 21:07:08 +010038 # visitor.visit_LU(self)
39
40class Base(object):
41 parent = None
Dusan Klinec00eb5da2014-11-10 21:24:15 +010042 lexspan = None
43 linespan = None
Dusan Klineca9f6d362014-11-10 21:07:08 +010044
45 def v(self, obj, visitor):
46 if obj == None:
47 return
Dusan Klinec1064fb32014-11-10 21:48:50 +010048 elif hasattr(obj, "accept"):
49 obj.accept(visitor)
50 elif isinstance(obj, list):
51 for s in obj:
52 self.v(s, visitor)
53 pass
54 pass
55
56 @staticmethod
57 def p(obj, parent):
58 if isinstance(obj, list):
59 for s in obj:
60 Base.p(s, parent)
61
62 if hasattr(obj, "parent"):
63 obj.parent = parent
Dusan Klinece26bb022014-11-09 12:21:37 +010064
Dusan Klinecaa9ff472014-11-10 18:02:03 +010065# Lexical unit - contains lexspan and linespan for later analysis.
Dusan Klineca9f6d362014-11-10 21:07:08 +010066class LU(Base):
Dusan Klinecaa9ff472014-11-10 18:02:03 +010067 def __init__(self, p, idx):
68 self.p = p
69 self.idx = idx
70 self.pval = p[idx]
71 self.lexspan = p.lexspan(idx)
72 self.linespan = p.linespan(idx)
73
74 # If string is in the value (raw value) and start and stop lexspan is the same, add real span
75 # obtained by string length.
76 if isinstance(self.pval, str) \
77 and self.lexspan != None \
78 and self.lexspan[0] == self.lexspan[1] \
79 and self.lexspan[0] != 0:
80 self.lexspan = tuple([self.lexspan[0], self.lexspan[0] + len(self.pval)])
81 super(LU, self).__init__()
82
83 @staticmethod
84 def i(p, idx):
85 if isinstance(p[idx], LU): return p[idx]
86 if isinstance(p[idx], str): return LU(p, idx)
87 return p[idx]
88
89 def describe(self):
90 return "LU(%s,%s)" % (self.pval, self.lexspan)
91
92 def __str__(self):
93 return self.pval
94
95 def __repr__(self):
96 return self.describe()
97
98 def accept(self, visitor):
Dusan Klineca9f6d362014-11-10 21:07:08 +010099 self.v(self.pval, visitor)
Dusan Klinecaa9ff472014-11-10 18:02:03 +0100100
101 def __iter__(self):
102 for x in self.pval:
103 yield x
104
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100105# Base node
Dusan Klineca9f6d362014-11-10 21:07:08 +0100106class SourceElement(Base):
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100107 '''
Dusan Klinece26bb022014-11-09 12:21:37 +0100108 A SourceElement is the base class for all elements that occur in a Protocol Buffers
109 file parsed by plyproto.
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100110 '''
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100111 def __init__(self, linespan=[], lexspan=[], p=None):
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100112 super(SourceElement, self).__init__()
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100113 self._fields = [] # ['linespan', 'lexspan']
114 self.linespan = linespan
115 self.lexspan = lexspan
116 self.p = p
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100117
118 def __repr__(self):
119 equals = ("{0}={1!r}".format(k, getattr(self, k))
120 for k in self._fields)
121 args = ", ".join(equals)
122 return "{0}({1})".format(self.__class__.__name__, args)
123
124 def __eq__(self, other):
125 try:
126 return self.__dict__ == other.__dict__
127 except AttributeError:
128 return False
129
130 def __ne__(self, other):
131 return not self == other
132
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100133 def setLexData(self, linespan, lexspan):
134 self.linespan = linespan
135 self.lexspan = lexspan
136
137 def setLexObj(self, p):
138 self.p = p
139
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100140 def accept(self, visitor):
141 pass
142
Dusan Klinece26bb022014-11-09 12:21:37 +0100143class PackageStatement(SourceElement):
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100144 def __init__(self, name, linespan=None, lexspan=None, p=None):
145 super(PackageStatement, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
146 self._fields += ['name']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100147 self.name = name
Dusan Klinec1064fb32014-11-10 21:48:50 +0100148 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100149
150 def accept(self, visitor):
Dusan Klinece26bb022014-11-09 12:21:37 +0100151 visitor.visit_PackageStatement(self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100152
153class ImportStatement(SourceElement):
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100154 def __init__(self, name, linespan=None, lexspan=None, p=None):
155 super(ImportStatement, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
156 self._fields += ['name']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100157 self.name = name
Dusan Klinec1064fb32014-11-10 21:48:50 +0100158 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100159
160 def accept(self, visitor):
161 visitor.visit_ImportStatement(self)
162
Dusan Klinece26bb022014-11-09 12:21:37 +0100163class OptionStatement(SourceElement):
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100164 def __init__(self, name, value, linespan=None, lexspan=None, p=None):
165 super(OptionStatement, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
166 self._fields += ['name', 'value']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100167 self.name = name
Dusan Klinec1064fb32014-11-10 21:48:50 +0100168 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100169 self.value = value
Dusan Klinec1064fb32014-11-10 21:48:50 +0100170 Base.p(self.value, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100171
172 def accept(self, visitor):
Dusan Klinece26bb022014-11-09 12:21:37 +0100173 visitor.visit_OptionStatement(self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100174
Dusan Klinece26bb022014-11-09 12:21:37 +0100175class FieldDirective(SourceElement):
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100176 def __init__(self, name, value, linespan=None, lexspan=None, p=None):
177 super(FieldDirective, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
178 self._fields += ['name', 'value']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100179 self.name = name
Dusan Klinec1064fb32014-11-10 21:48:50 +0100180 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100181 self.value = value
Dusan Klinec1064fb32014-11-10 21:48:50 +0100182 Base.p(self.value, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100183
184 def accept(self, visitor):
Dusan Klineca9f6d362014-11-10 21:07:08 +0100185 if visitor.visit_FieldDirective(self):
186 self.v(self.name, visitor)
187 self.v(self.value, visitor)
Sapan Bhatiab1225872017-03-29 20:47:47 +0200188 visitor.visit_FieldDirective_post(self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100189
Dusan Klinece26bb022014-11-09 12:21:37 +0100190class FieldType(SourceElement):
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100191 def __init__(self, name, linespan=None, lexspan=None, p=None):
192 super(FieldType, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
193 self._fields += ['name']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100194 self.name = name
Dusan Klinec1064fb32014-11-10 21:48:50 +0100195 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100196
197 def accept(self, visitor):
Dusan Klineca9f6d362014-11-10 21:07:08 +0100198 if visitor.visit_FieldType(self):
199 self.v(self.name, visitor)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100200
Sapan Bhatiab1225872017-03-29 20:47:47 +0200201class LinkDefinition(SourceElement):
Sapan Bhatia78fee772017-04-21 19:00:48 +0200202 def __init__(self, link_type, src_port, name, through, dst_port, linespan=None, lexspan=None, p=None):
Sapan Bhatiab1225872017-03-29 20:47:47 +0200203 super(LinkDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
Sapan Bhatia78fee772017-04-21 19:00:48 +0200204 self._fields += ['link_type', 'src_port', 'name', 'dst_port', 'through']
Sapan Bhatiab1225872017-03-29 20:47:47 +0200205 self.link_type = link_type
206 Base.p(self.link_type, self)
Sapan Bhatia78fee772017-04-21 19:00:48 +0200207
Sapan Bhatiab1225872017-03-29 20:47:47 +0200208 self.src_port = src_port
209 Base.p(self.src_port, self)
Sapan Bhatia78fee772017-04-21 19:00:48 +0200210
Sapan Bhatiab1225872017-03-29 20:47:47 +0200211 self.name = name
212 Base.p(self.name, self)
Sapan Bhatia78fee772017-04-21 19:00:48 +0200213
214 self.through = through
215 Base.p(self.through, self)
216
Sapan Bhatiab1225872017-03-29 20:47:47 +0200217 self.dst_port = dst_port
218 Base.p(self.dst_port, self)
219
220 def accept(self, visitor):
221 visitor.visit_LinkDefinition(self)
222
Dusan Klinece26bb022014-11-09 12:21:37 +0100223class FieldDefinition(SourceElement):
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100224 def __init__(self, field_modifier, ftype, name, fieldId, fieldDirective, linespan=None, lexspan=None, p=None):
225 super(FieldDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
226 self._fields += ['field_modifier', 'ftype', 'name', 'fieldId', 'fieldDirective']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100227 self.name = name
Dusan Klinec1064fb32014-11-10 21:48:50 +0100228 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100229 self.field_modifier = field_modifier
Dusan Klinec1064fb32014-11-10 21:48:50 +0100230 Base.p(self.field_modifier, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100231 self.ftype = ftype
Dusan Klinec1064fb32014-11-10 21:48:50 +0100232 Base.p(self.ftype, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100233 self.fieldId = fieldId
Dusan Klinec1064fb32014-11-10 21:48:50 +0100234 Base.p(self.fieldId, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100235 self.fieldDirective = fieldDirective
Dusan Klinec1064fb32014-11-10 21:48:50 +0100236 Base.p(self.fieldDirective, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100237
238 def accept(self, visitor):
Dusan Klineca9f6d362014-11-10 21:07:08 +0100239 if visitor.visit_FieldDefinition(self):
240 self.v(self.name, visitor)
241 self.v(self.field_modifier, visitor)
242 self.v(self.ftype, visitor)
243 self.v(self.fieldId, visitor)
Dusan Klinec1064fb32014-11-10 21:48:50 +0100244 self.v(self.fieldDirective, visitor)
Sapan Bhatiab1225872017-03-29 20:47:47 +0200245 visitor.visit_FieldDefinition_post(self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100246
Dusan Klinece26bb022014-11-09 12:21:37 +0100247class EnumFieldDefinition(SourceElement):
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100248 def __init__(self, name, fieldId, linespan=None, lexspan=None, p=None):
249 super(EnumFieldDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
250 self._fields += ['name', 'fieldId']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100251 self.name = name
Dusan Klinec1064fb32014-11-10 21:48:50 +0100252 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100253 self.fieldId = fieldId
Dusan Klinec1064fb32014-11-10 21:48:50 +0100254 Base.p(self.fieldId, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100255
256 def accept(self, visitor):
Dusan Klineca9f6d362014-11-10 21:07:08 +0100257 if visitor.visit_EnumFieldDefinition(self):
258 self.v(self.name, visitor)
259 self.v(self.fieldId, visitor)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100260
Dusan Klinece26bb022014-11-09 12:21:37 +0100261class EnumDefinition(SourceElement):
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100262 def __init__(self, name, body, linespan=None, lexspan=None, p=None):
263 super(EnumDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
264 self._fields += ['name', 'body']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100265 self.name = name
Dusan Klinec1064fb32014-11-10 21:48:50 +0100266 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100267 self.body = body
Dusan Klinec1064fb32014-11-10 21:48:50 +0100268 Base.p(self.body, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100269
270 def accept(self, visitor):
Dusan Klinece26bb022014-11-09 12:21:37 +0100271 if visitor.visit_EnumDefinition(self):
Dusan Klineca9f6d362014-11-10 21:07:08 +0100272 self.v(self.name, visitor)
Dusan Klinec1064fb32014-11-10 21:48:50 +0100273 self.v(self.body, visitor)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100274
Sapan Bhatiab1225872017-03-29 20:47:47 +0200275class LinkSpec(SourceElement):
276 def __init__(self, field_spec, link_spec, linespan=None, lexspan=None, p=None):
277 super(LinkSpec, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
278 self._fields += ['link_def', 'field_def']
279 self.link_def = link_spec
280 Base.p(self.link_def, self)
281 self.field_def = field_spec
282 Base.p(self.field_def, self)
283
284 def accept(self, visitor):
285 if visitor.visit_LinkSpec(self):
286 self.v(self.link_def, visitor)
287 self.v(self.field_def, visitor)
288 visitor.visit_LinkSpec_post(self)
289
290class MessageDefinition(SourceElement):
Sapan Bhatia87792a12017-04-10 19:35:05 -0700291 def __init__(self, name, bases, body, linespan=None, lexspan=None, p=None):
Sapan Bhatiab1225872017-03-29 20:47:47 +0200292 super(MessageDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
Sapan Bhatia87792a12017-04-10 19:35:05 -0700293 self._fields += ['name', 'bases', 'body']
Sapan Bhatiab1225872017-03-29 20:47:47 +0200294 self.name = name
295 Base.p(self.name, self)
Sapan Bhatia87792a12017-04-10 19:35:05 -0700296 self.bases = bases
297 Base.p(self.bases, self)
Sapan Bhatiab1225872017-03-29 20:47:47 +0200298 self.body = body
299 Base.p(self.body, self)
300
301 def accept(self, visitor):
302 if visitor.visit_MessageDefinition(self):
303 self.v(self.name, visitor)
Sapan Bhatia87792a12017-04-10 19:35:05 -0700304 self.v(self.bases, visitor)
Sapan Bhatiab1225872017-03-29 20:47:47 +0200305 self.v(self.body, visitor)
306 visitor.visit_MessageDefinition_post(self)
307
308
309"""
Dusan Klinece26bb022014-11-09 12:21:37 +0100310class MessageDefinition(SourceElement):
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100311 def __init__(self, name, body, linespan=None, lexspan=None, p=None):
312 super(MessageDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
313 self._fields += ['name', 'body']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100314 self.name = name
Dusan Klinec1064fb32014-11-10 21:48:50 +0100315 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100316 self.body = body
Dusan Klinec1064fb32014-11-10 21:48:50 +0100317 Base.p(self.body, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100318
319 def accept(self, visitor):
Dusan Klinece26bb022014-11-09 12:21:37 +0100320 if visitor.visit_MessageDefinition(self):
Dusan Klineca9f6d362014-11-10 21:07:08 +0100321 self.v(self.name, visitor)
Dusan Klinec1064fb32014-11-10 21:48:50 +0100322 self.v(self.body, visitor)
Sapan Bhatiab1225872017-03-29 20:47:47 +0200323 visitor.visit_MessageDefinition_post(self)
324"""
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100325
326class MessageExtension(SourceElement):
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100327 def __init__(self, name, body, linespan=None, lexspan=None, p=None):
328 super(MessageExtension, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
329 self._fields += ['name', 'body']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100330 self.name = name
Dusan Klinec1064fb32014-11-10 21:48:50 +0100331 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100332 self.body = body
Dusan Klinec1064fb32014-11-10 21:48:50 +0100333 Base.p(self.body, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100334
335 def accept(self, visitor):
336 if visitor.visit_MessageExtension(self):
Dusan Klineca9f6d362014-11-10 21:07:08 +0100337 self.v(self.name, visitor)
Dusan Klinec1064fb32014-11-10 21:48:50 +0100338 self.v(self.body, visitor)
Sapan Bhatiab1225872017-03-29 20:47:47 +0200339 visitor.visit_MessageExtension_post(self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100340
341class MethodDefinition(SourceElement):
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100342 def __init__(self, name, name2, name3, linespan=None, lexspan=None, p=None):
343 super(MethodDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
344 self._fields += ['name', 'name2', 'name3']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100345 self.name = name
Dusan Klinec1064fb32014-11-10 21:48:50 +0100346 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100347 self.name2 = name2
Dusan Klinec1064fb32014-11-10 21:48:50 +0100348 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100349 self.name3 = name3
Dusan Klinec1064fb32014-11-10 21:48:50 +0100350 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100351
352 def accept(self, visitor):
Dusan Klineca9f6d362014-11-10 21:07:08 +0100353 if visitor.visit_MethodDefinition(self):
354 self.v(self.name, visitor)
355 self.v(self.name2, visitor)
356 self.v(self.name3, visitor)
Sapan Bhatiab1225872017-03-29 20:47:47 +0200357 visitor.visit_MethodDefinition_post(self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100358
Dusan Klinece26bb022014-11-09 12:21:37 +0100359class ServiceDefinition(SourceElement):
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100360 def __init__(self, name, body, linespan=None, lexspan=None, p=None):
361 super(ServiceDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
362 self._fields += ['name', 'body']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100363 self.name = name
Dusan Klinec1064fb32014-11-10 21:48:50 +0100364 Base.p(self.name, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100365 self.body = body
Dusan Klinec1064fb32014-11-10 21:48:50 +0100366 Base.p(self.body, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100367
368 def accept(self, visitor):
Dusan Klinece26bb022014-11-09 12:21:37 +0100369 if visitor.visit_ServiceDefinition(self):
Dusan Klineca9f6d362014-11-10 21:07:08 +0100370 self.v(self.name, visitor)
Dusan Klinec1064fb32014-11-10 21:48:50 +0100371 self.v(self.body, visitor)
Sapan Bhatiab1225872017-03-29 20:47:47 +0200372 visitor.visit_ServiceDefinition_post(self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100373
374class ExtensionsMax(SourceElement):
375 pass
376
377class ExtensionsDirective(SourceElement):
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100378 def __init__(self, fromVal, toVal, linespan=None, lexspan=None, p=None):
379 super(ExtensionsDirective, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
380 self._fields += ['fromVal', 'toVal']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100381 self.fromVal = fromVal
Dusan Klinec1064fb32014-11-10 21:48:50 +0100382 Base.p(self.fromVal, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100383 self.toVal = toVal
Dusan Klinec1064fb32014-11-10 21:48:50 +0100384 Base.p(self.toVal, self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100385
386 def accept(self, visitor):
Dusan Klineca9f6d362014-11-10 21:07:08 +0100387 if visitor.visit_ExtensionsDirective(self):
388 self.v(self.fromVal, visitor)
389 self.v(self.toVal, visitor)
Sapan Bhatiab1225872017-03-29 20:47:47 +0200390 visitor.visit_ExtensionsDirective_post(self)
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100391
392class Literal(SourceElement):
393
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100394 def __init__(self, value, linespan=None, lexspan=None, p=None):
395 super(Literal, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
396 self._fields += ['value']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100397 self.value = value
398
399 def accept(self, visitor):
400 visitor.visit_Literal(self)
401
402class Name(SourceElement):
403
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100404 def __init__(self, value, linespan=None, lexspan=None, p=None):
405 super(Name, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
406 self._fields += ['value']
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100407 self.value = value
Dusan Klinecaa9ff472014-11-10 18:02:03 +0100408 self.deriveLex()
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100409
410 def append_name(self, name):
411 try:
412 self.value = self.value + '.' + name.value
413 except:
414 self.value = self.value + '.' + name
415
Dusan Klinecaa9ff472014-11-10 18:02:03 +0100416 def deriveLex(self):
417 if hasattr(self.value, "lexspan"):
418 self.lexspan = self.value.lexspan
419 self.linespan = self.value.linespan
420 else:
421 return
422
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100423 def accept(self, visitor):
424 visitor.visit_Name(self)
425
Dusan Klinecaa9ff472014-11-10 18:02:03 +0100426class DotName(Name):
427 elements = []
428 def __init__(self, elements, linespan=None, lexspan=None, p=None):
429 super(DotName, self).__init__('.'.join([str(x) for x in elements]), linespan=linespan, lexspan=lexspan, p=p)
430 self._fields += ['elements']
431 self.elements = elements
432 self.deriveLex()
433
434 def deriveLex(self):
435 if isinstance(self.elements, list) and len(self.elements)>0:
436 self.lexspan = (min([x.lexspan[0] for x in self.elements if x.lexspan[0] != 0]), max([x.lexspan[1] for x in self.elements if x.lexspan[1] != 0]))
437 self.linespan = (min([x.linespan[0] for x in self.elements if x.linespan[0] != 0]), max([x.linespan[1] for x in self.elements if x.linespan[1] != 0]))
438 elif hasattr(self.elements, "lexspan"):
439 self.lexspan = self.elements.lexspan
440 self.linespan = self.elements.linespan
441 else:
442 return
443
444 def accept(self, visitor):
445 visitor.visit_DotName(self)
446
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100447class ProtoFile(SourceElement):
Dusan Klinecccaa0d92014-11-09 03:21:31 +0100448
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100449 def __init__(self, pkg, body, linespan=None, lexspan=None, p=None):
450 super(ProtoFile, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
451 self._fields += ['pkg', 'body']
452 self.pkg = pkg
Dusan Klinec1064fb32014-11-10 21:48:50 +0100453 Base.p(self.pkg, self)
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100454 self.body = body
Dusan Klinec1064fb32014-11-10 21:48:50 +0100455 Base.p(self.body, self)
Dusan Klinecc9b031a2014-11-10 13:21:08 +0100456
457 def accept(self, visitor):
458 if visitor.visit_Proto(self):
Dusan Klineca9f6d362014-11-10 21:07:08 +0100459 self.v(self.pkg, visitor)
Dusan Klinec1064fb32014-11-10 21:48:50 +0100460 self.v(self.body, visitor)
Sapan Bhatiab1225872017-03-29 20:47:47 +0200461 visitor.visit_Proto_post(self)