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) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 188 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 189 | class FieldType(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 190 | def __init__(self, name, linespan=None, lexspan=None, p=None): |
| 191 | super(FieldType, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 192 | self._fields += ['name'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 193 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 194 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 195 | |
| 196 | def accept(self, visitor): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 197 | if visitor.visit_FieldType(self): |
| 198 | self.v(self.name, visitor) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 199 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 200 | class FieldDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 201 | def __init__(self, field_modifier, ftype, name, fieldId, fieldDirective, linespan=None, lexspan=None, p=None): |
| 202 | super(FieldDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 203 | self._fields += ['field_modifier', 'ftype', 'name', 'fieldId', 'fieldDirective'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 204 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 205 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 206 | self.field_modifier = field_modifier |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 207 | Base.p(self.field_modifier, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 208 | self.ftype = ftype |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 209 | Base.p(self.ftype, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 210 | self.fieldId = fieldId |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 211 | Base.p(self.fieldId, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 212 | self.fieldDirective = fieldDirective |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 213 | Base.p(self.fieldDirective, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 214 | |
| 215 | def accept(self, visitor): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 216 | if visitor.visit_FieldDefinition(self): |
| 217 | self.v(self.name, visitor) |
| 218 | self.v(self.field_modifier, visitor) |
| 219 | self.v(self.ftype, visitor) |
| 220 | self.v(self.fieldId, visitor) |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 221 | self.v(self.fieldDirective, visitor) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 222 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 223 | class EnumFieldDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 224 | def __init__(self, name, fieldId, linespan=None, lexspan=None, p=None): |
| 225 | super(EnumFieldDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 226 | self._fields += ['name', 'fieldId'] |
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.fieldId = fieldId |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 230 | Base.p(self.fieldId, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 231 | |
| 232 | def accept(self, visitor): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 233 | if visitor.visit_EnumFieldDefinition(self): |
| 234 | self.v(self.name, visitor) |
| 235 | self.v(self.fieldId, visitor) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 236 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 237 | class EnumDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 238 | def __init__(self, name, body, linespan=None, lexspan=None, p=None): |
| 239 | super(EnumDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 240 | self._fields += ['name', 'body'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 241 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 242 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 243 | self.body = body |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 244 | Base.p(self.body, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 245 | |
| 246 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 247 | if visitor.visit_EnumDefinition(self): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 248 | self.v(self.name, visitor) |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 249 | self.v(self.body, visitor) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 250 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 251 | class MessageDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 252 | def __init__(self, name, body, linespan=None, lexspan=None, p=None): |
| 253 | super(MessageDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 254 | self._fields += ['name', 'body'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 255 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 256 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 257 | self.body = body |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 258 | Base.p(self.body, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 259 | |
| 260 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 261 | if visitor.visit_MessageDefinition(self): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 262 | self.v(self.name, visitor) |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 263 | self.v(self.body, visitor) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 264 | |
| 265 | class MessageExtension(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 266 | def __init__(self, name, body, linespan=None, lexspan=None, p=None): |
| 267 | super(MessageExtension, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 268 | self._fields += ['name', 'body'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 269 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 270 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 271 | self.body = body |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 272 | Base.p(self.body, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 273 | |
| 274 | def accept(self, visitor): |
| 275 | if visitor.visit_MessageExtension(self): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 276 | self.v(self.name, visitor) |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 277 | self.v(self.body, visitor) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 278 | |
| 279 | class MethodDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 280 | def __init__(self, name, name2, name3, linespan=None, lexspan=None, p=None): |
| 281 | super(MethodDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 282 | self._fields += ['name', 'name2', 'name3'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 283 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 284 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 285 | self.name2 = name2 |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 286 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 287 | self.name3 = name3 |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 288 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 289 | |
| 290 | def accept(self, visitor): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 291 | if visitor.visit_MethodDefinition(self): |
| 292 | self.v(self.name, visitor) |
| 293 | self.v(self.name2, visitor) |
| 294 | self.v(self.name3, visitor) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 295 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 296 | class ServiceDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 297 | def __init__(self, name, body, linespan=None, lexspan=None, p=None): |
| 298 | super(ServiceDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 299 | self._fields += ['name', 'body'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 300 | self.name = name |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 301 | Base.p(self.name, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 302 | self.body = body |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 303 | Base.p(self.body, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 304 | |
| 305 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 306 | if visitor.visit_ServiceDefinition(self): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 307 | self.v(self.name, visitor) |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 308 | self.v(self.body, visitor) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 309 | |
| 310 | class ExtensionsMax(SourceElement): |
| 311 | pass |
| 312 | |
| 313 | class ExtensionsDirective(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 314 | def __init__(self, fromVal, toVal, linespan=None, lexspan=None, p=None): |
| 315 | super(ExtensionsDirective, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 316 | self._fields += ['fromVal', 'toVal'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 317 | self.fromVal = fromVal |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 318 | Base.p(self.fromVal, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 319 | self.toVal = toVal |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 320 | Base.p(self.toVal, self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 321 | |
| 322 | def accept(self, visitor): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 323 | if visitor.visit_ExtensionsDirective(self): |
| 324 | self.v(self.fromVal, visitor) |
| 325 | self.v(self.toVal, visitor) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 326 | |
| 327 | class Literal(SourceElement): |
| 328 | |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 329 | def __init__(self, value, linespan=None, lexspan=None, p=None): |
| 330 | super(Literal, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 331 | self._fields += ['value'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 332 | self.value = value |
| 333 | |
| 334 | def accept(self, visitor): |
| 335 | visitor.visit_Literal(self) |
| 336 | |
| 337 | class Name(SourceElement): |
| 338 | |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 339 | def __init__(self, value, linespan=None, lexspan=None, p=None): |
| 340 | super(Name, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 341 | self._fields += ['value'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 342 | self.value = value |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 343 | self.deriveLex() |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 344 | |
| 345 | def append_name(self, name): |
| 346 | try: |
| 347 | self.value = self.value + '.' + name.value |
| 348 | except: |
| 349 | self.value = self.value + '.' + name |
| 350 | |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 351 | def deriveLex(self): |
| 352 | if hasattr(self.value, "lexspan"): |
| 353 | self.lexspan = self.value.lexspan |
| 354 | self.linespan = self.value.linespan |
| 355 | else: |
| 356 | return |
| 357 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 358 | def accept(self, visitor): |
| 359 | visitor.visit_Name(self) |
| 360 | |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 361 | class DotName(Name): |
| 362 | elements = [] |
| 363 | def __init__(self, elements, linespan=None, lexspan=None, p=None): |
| 364 | super(DotName, self).__init__('.'.join([str(x) for x in elements]), linespan=linespan, lexspan=lexspan, p=p) |
| 365 | self._fields += ['elements'] |
| 366 | self.elements = elements |
| 367 | self.deriveLex() |
| 368 | |
| 369 | def deriveLex(self): |
| 370 | if isinstance(self.elements, list) and len(self.elements)>0: |
| 371 | 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])) |
| 372 | 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])) |
| 373 | elif hasattr(self.elements, "lexspan"): |
| 374 | self.lexspan = self.elements.lexspan |
| 375 | self.linespan = self.elements.linespan |
| 376 | else: |
| 377 | return |
| 378 | |
| 379 | def accept(self, visitor): |
| 380 | visitor.visit_DotName(self) |
| 381 | |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 382 | class ProtoFile(SourceElement): |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 383 | |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 384 | def __init__(self, pkg, body, linespan=None, lexspan=None, p=None): |
| 385 | super(ProtoFile, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 386 | self._fields += ['pkg', 'body'] |
| 387 | self.pkg = pkg |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 388 | Base.p(self.pkg, self) |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 389 | self.body = body |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 390 | Base.p(self.body, self) |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 391 | |
| 392 | def accept(self, visitor): |
| 393 | if visitor.visit_Proto(self): |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 394 | self.v(self.pkg, visitor) |
Dusan Klinec | 1064fb3 | 2014-11-10 21:48:50 +0100 | [diff] [blame] | 395 | self.v(self.body, visitor) |