Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 1 | __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 Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 6 | class 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 Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 13 | raise AttributeError('name must start with visit_ but was {}'.format(name)) |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 14 | |
| 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 Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 37 | # visitor.visit_Proto(self) |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 38 | # visitor.visit_LU(self) |
| 39 | |
| 40 | class Base(object): |
| 41 | parent = None |
Dusan Klinec | 00eb5da | 2014-11-10 21:24:15 +0100 | [diff] [blame] | 42 | lexspan = None |
| 43 | linespan = None |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 44 | |
| 45 | def v(self, obj, visitor): |
| 46 | if obj == None: |
| 47 | return |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 48 | 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 Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 64 | |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 65 | # Lexical unit - contains lexspan and linespan for later analysis. |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 66 | class LU(Base): |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 67 | 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 Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 99 | self.v(self.pval, visitor) |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 100 | |
| 101 | def __iter__(self): |
| 102 | for x in self.pval: |
| 103 | yield x |
| 104 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 105 | # Base node |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 106 | class SourceElement(Base): |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 107 | ''' |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 108 | A SourceElement is the base class for all elements that occur in a Protocol Buffers |
| 109 | file parsed by plyproto. |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 110 | ''' |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 111 | def __init__(self, linespan=[], lexspan=[], p=None): |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 112 | super(SourceElement, self).__init__() |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 113 | self._fields = [] # ['linespan', 'lexspan'] |
| 114 | self.linespan = linespan |
| 115 | self.lexspan = lexspan |
| 116 | self.p = p |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 117 | |
| 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 Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 133 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 140 | def accept(self, visitor): |
| 141 | pass |
| 142 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 143 | class PackageStatement(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 144 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 147 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 148 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 149 | |
| 150 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 151 | visitor.visit_PackageStatement(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 152 | |
| 153 | class ImportStatement(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 154 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 157 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 158 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 159 | |
| 160 | def accept(self, visitor): |
| 161 | visitor.visit_ImportStatement(self) |
| 162 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 163 | class OptionStatement(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 164 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 167 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 168 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 169 | self.value = value |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 170 | Base.p(self.value, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 171 | |
| 172 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 173 | visitor.visit_OptionStatement(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 174 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 175 | class FieldDirective(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 176 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 179 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 180 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 181 | self.value = value |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 182 | Base.p(self.value, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 183 | |
| 184 | def accept(self, visitor): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 185 | if visitor.visit_FieldDirective(self): |
| 186 | self.v(self.name, visitor) |
| 187 | self.v(self.value, visitor) |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 188 | visitor.visit_FieldDirective_post(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 189 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 190 | class FieldType(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 191 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 194 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 195 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 196 | |
| 197 | def accept(self, visitor): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 198 | if visitor.visit_FieldType(self): |
| 199 | self.v(self.name, visitor) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 200 | |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 201 | class LinkDefinition(SourceElement): |
Sapan Bhatia | 78fee77 | 2017-04-21 19:00:48 +0200 | [diff] [blame] | 202 | def __init__(self, link_type, src_port, name, through, dst_port, linespan=None, lexspan=None, p=None): |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 203 | super(LinkDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
Sapan Bhatia | 78fee77 | 2017-04-21 19:00:48 +0200 | [diff] [blame] | 204 | self._fields += ['link_type', 'src_port', 'name', 'dst_port', 'through'] |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 205 | self.link_type = link_type |
| 206 | Base.p(self.link_type, self) |
Sapan Bhatia | 78fee77 | 2017-04-21 19:00:48 +0200 | [diff] [blame] | 207 | |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 208 | self.src_port = src_port |
| 209 | Base.p(self.src_port, self) |
Sapan Bhatia | 78fee77 | 2017-04-21 19:00:48 +0200 | [diff] [blame] | 210 | |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 211 | self.name = name |
| 212 | Base.p(self.name, self) |
Sapan Bhatia | 78fee77 | 2017-04-21 19:00:48 +0200 | [diff] [blame] | 213 | |
| 214 | self.through = through |
| 215 | Base.p(self.through, self) |
| 216 | |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 217 | 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 Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 223 | class FieldDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 224 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 227 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 228 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 229 | self.field_modifier = field_modifier |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 230 | Base.p(self.field_modifier, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 231 | self.ftype = ftype |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 232 | Base.p(self.ftype, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 233 | self.fieldId = fieldId |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 234 | Base.p(self.fieldId, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 235 | self.fieldDirective = fieldDirective |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 236 | Base.p(self.fieldDirective, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 237 | |
| 238 | def accept(self, visitor): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 239 | 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 Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 244 | self.v(self.fieldDirective, visitor) |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 245 | visitor.visit_FieldDefinition_post(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 246 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 247 | class EnumFieldDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 248 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 251 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 252 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 253 | self.fieldId = fieldId |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 254 | Base.p(self.fieldId, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 255 | |
| 256 | def accept(self, visitor): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 257 | if visitor.visit_EnumFieldDefinition(self): |
| 258 | self.v(self.name, visitor) |
| 259 | self.v(self.fieldId, visitor) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 260 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 261 | class EnumDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 262 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 265 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 266 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 267 | self.body = body |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 268 | Base.p(self.body, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 269 | |
| 270 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 271 | if visitor.visit_EnumDefinition(self): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 272 | self.v(self.name, visitor) |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 273 | self.v(self.body, visitor) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 274 | |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 275 | class 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 | |
| 290 | class MessageDefinition(SourceElement): |
Sapan Bhatia | 87792a1 | 2017-04-10 19:35:05 -0700 | [diff] [blame] | 291 | def __init__(self, name, bases, body, linespan=None, lexspan=None, p=None): |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 292 | super(MessageDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
Sapan Bhatia | 87792a1 | 2017-04-10 19:35:05 -0700 | [diff] [blame] | 293 | self._fields += ['name', 'bases', 'body'] |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 294 | self.name = name |
| 295 | Base.p(self.name, self) |
Sapan Bhatia | 87792a1 | 2017-04-10 19:35:05 -0700 | [diff] [blame] | 296 | self.bases = bases |
| 297 | Base.p(self.bases, self) |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 298 | 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 Bhatia | 87792a1 | 2017-04-10 19:35:05 -0700 | [diff] [blame] | 304 | self.v(self.bases, visitor) |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 305 | self.v(self.body, visitor) |
| 306 | visitor.visit_MessageDefinition_post(self) |
| 307 | |
| 308 | |
| 309 | """ |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 310 | class MessageDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 311 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 314 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 315 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 316 | self.body = body |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 317 | Base.p(self.body, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 318 | |
| 319 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 320 | if visitor.visit_MessageDefinition(self): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 321 | self.v(self.name, visitor) |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 322 | self.v(self.body, visitor) |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 323 | visitor.visit_MessageDefinition_post(self) |
| 324 | """ |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 325 | |
| 326 | class MessageExtension(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 327 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 330 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 331 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 332 | self.body = body |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 333 | Base.p(self.body, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 334 | |
| 335 | def accept(self, visitor): |
| 336 | if visitor.visit_MessageExtension(self): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 337 | self.v(self.name, visitor) |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 338 | self.v(self.body, visitor) |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 339 | visitor.visit_MessageExtension_post(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 340 | |
| 341 | class MethodDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 342 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 345 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 346 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 347 | self.name2 = name2 |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 348 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 349 | self.name3 = name3 |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 350 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 351 | |
| 352 | def accept(self, visitor): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 353 | if visitor.visit_MethodDefinition(self): |
| 354 | self.v(self.name, visitor) |
| 355 | self.v(self.name2, visitor) |
| 356 | self.v(self.name3, visitor) |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 357 | visitor.visit_MethodDefinition_post(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 358 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 359 | class ServiceDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 360 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 363 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 364 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 365 | self.body = body |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 366 | Base.p(self.body, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 367 | |
| 368 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 369 | if visitor.visit_ServiceDefinition(self): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 370 | self.v(self.name, visitor) |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 371 | self.v(self.body, visitor) |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 372 | visitor.visit_ServiceDefinition_post(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 373 | |
| 374 | class ExtensionsMax(SourceElement): |
| 375 | pass |
| 376 | |
| 377 | class ExtensionsDirective(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 378 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 381 | self.fromVal = fromVal |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 382 | Base.p(self.fromVal, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 383 | self.toVal = toVal |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 384 | Base.p(self.toVal, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 385 | |
| 386 | def accept(self, visitor): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 387 | if visitor.visit_ExtensionsDirective(self): |
| 388 | self.v(self.fromVal, visitor) |
| 389 | self.v(self.toVal, visitor) |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 390 | visitor.visit_ExtensionsDirective_post(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 391 | |
| 392 | class Literal(SourceElement): |
| 393 | |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 394 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 397 | self.value = value |
| 398 | |
| 399 | def accept(self, visitor): |
| 400 | visitor.visit_Literal(self) |
| 401 | |
| 402 | class Name(SourceElement): |
| 403 | |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 404 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 407 | self.value = value |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 408 | self.deriveLex() |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 409 | |
| 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 Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 416 | 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 Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 423 | def accept(self, visitor): |
| 424 | visitor.visit_Name(self) |
| 425 | |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 426 | class 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 Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 447 | class ProtoFile(SourceElement): |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 448 | |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 449 | 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 Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 453 | Base.p(self.pkg, self) |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 454 | self.body = body |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 455 | Base.p(self.body, self) |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 456 | |
| 457 | def accept(self, visitor): |
| 458 | if visitor.visit_Proto(self): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 459 | self.v(self.pkg, visitor) |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 460 | self.v(self.body, visitor) |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 461 | visitor.visit_Proto_post(self) |