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 | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 38 | |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 39 | # Lexical unit - contains lexspan and linespan for later analysis. |
| 40 | class LU(object): |
| 41 | def __init__(self, p, idx): |
| 42 | self.p = p |
| 43 | self.idx = idx |
| 44 | self.pval = p[idx] |
| 45 | self.lexspan = p.lexspan(idx) |
| 46 | self.linespan = p.linespan(idx) |
| 47 | |
| 48 | # If string is in the value (raw value) and start and stop lexspan is the same, add real span |
| 49 | # obtained by string length. |
| 50 | if isinstance(self.pval, str) \ |
| 51 | and self.lexspan != None \ |
| 52 | and self.lexspan[0] == self.lexspan[1] \ |
| 53 | and self.lexspan[0] != 0: |
| 54 | self.lexspan = tuple([self.lexspan[0], self.lexspan[0] + len(self.pval)]) |
| 55 | super(LU, self).__init__() |
| 56 | |
| 57 | @staticmethod |
| 58 | def i(p, idx): |
| 59 | if isinstance(p[idx], LU): return p[idx] |
| 60 | if isinstance(p[idx], str): return LU(p, idx) |
| 61 | return p[idx] |
| 62 | |
| 63 | def describe(self): |
| 64 | return "LU(%s,%s)" % (self.pval, self.lexspan) |
| 65 | |
| 66 | def __str__(self): |
| 67 | return self.pval |
| 68 | |
| 69 | def __repr__(self): |
| 70 | return self.describe() |
| 71 | |
| 72 | def accept(self, visitor): |
| 73 | pass |
| 74 | |
| 75 | def __iter__(self): |
| 76 | for x in self.pval: |
| 77 | yield x |
| 78 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 79 | # Base node |
| 80 | class SourceElement(object): |
| 81 | ''' |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 82 | A SourceElement is the base class for all elements that occur in a Protocol Buffers |
| 83 | file parsed by plyproto. |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 84 | ''' |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 85 | def __init__(self, linespan=[], lexspan=[], p=None): |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 86 | super(SourceElement, self).__init__() |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 87 | self._fields = [] # ['linespan', 'lexspan'] |
| 88 | self.linespan = linespan |
| 89 | self.lexspan = lexspan |
| 90 | self.p = p |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 91 | |
| 92 | def __repr__(self): |
| 93 | equals = ("{0}={1!r}".format(k, getattr(self, k)) |
| 94 | for k in self._fields) |
| 95 | args = ", ".join(equals) |
| 96 | return "{0}({1})".format(self.__class__.__name__, args) |
| 97 | |
| 98 | def __eq__(self, other): |
| 99 | try: |
| 100 | return self.__dict__ == other.__dict__ |
| 101 | except AttributeError: |
| 102 | return False |
| 103 | |
| 104 | def __ne__(self, other): |
| 105 | return not self == other |
| 106 | |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 107 | def setLexData(self, linespan, lexspan): |
| 108 | self.linespan = linespan |
| 109 | self.lexspan = lexspan |
| 110 | |
| 111 | def setLexObj(self, p): |
| 112 | self.p = p |
| 113 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 114 | def accept(self, visitor): |
| 115 | pass |
| 116 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 117 | class PackageStatement(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 118 | def __init__(self, name, linespan=None, lexspan=None, p=None): |
| 119 | super(PackageStatement, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 120 | self._fields += ['name'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 121 | self.name = name |
| 122 | |
| 123 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 124 | visitor.visit_PackageStatement(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 125 | |
| 126 | class ImportStatement(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 127 | def __init__(self, name, linespan=None, lexspan=None, p=None): |
| 128 | super(ImportStatement, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 129 | self._fields += ['name'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 130 | self.name = name |
| 131 | |
| 132 | def accept(self, visitor): |
| 133 | visitor.visit_ImportStatement(self) |
| 134 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 135 | class OptionStatement(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 136 | def __init__(self, name, value, linespan=None, lexspan=None, p=None): |
| 137 | super(OptionStatement, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 138 | self._fields += ['name', 'value'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 139 | self.name = name |
| 140 | self.value = value |
| 141 | |
| 142 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 143 | visitor.visit_OptionStatement(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 144 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 145 | class FieldDirective(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 146 | def __init__(self, name, value, linespan=None, lexspan=None, p=None): |
| 147 | super(FieldDirective, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 148 | self._fields += ['name', 'value'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 149 | self.name = name |
| 150 | self.value = value |
| 151 | |
| 152 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 153 | visitor.visit_FieldDirective(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 154 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 155 | class FieldType(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 156 | def __init__(self, name, linespan=None, lexspan=None, p=None): |
| 157 | super(FieldType, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 158 | self._fields += ['name'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 159 | self.name = name |
| 160 | |
| 161 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 162 | visitor.visit_FieldType(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 163 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 164 | class FieldDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 165 | def __init__(self, field_modifier, ftype, name, fieldId, fieldDirective, linespan=None, lexspan=None, p=None): |
| 166 | super(FieldDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 167 | self._fields += ['field_modifier', 'ftype', 'name', 'fieldId', 'fieldDirective'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 168 | self.name = name |
| 169 | self.field_modifier = field_modifier |
| 170 | self.ftype = ftype |
| 171 | self.fieldId = fieldId |
| 172 | self.fieldDirective = fieldDirective |
| 173 | |
| 174 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 175 | visitor.visit_FieldDefinition(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 176 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 177 | class EnumFieldDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 178 | def __init__(self, name, fieldId, linespan=None, lexspan=None, p=None): |
| 179 | super(EnumFieldDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 180 | self._fields += ['name', 'fieldId'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 181 | self.name = name |
| 182 | self.fieldId = fieldId |
| 183 | |
| 184 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 185 | visitor.visit_EnumFieldDefinition(self) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 186 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 187 | class EnumDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 188 | def __init__(self, name, body, linespan=None, lexspan=None, p=None): |
| 189 | super(EnumDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 190 | self._fields += ['name', 'body'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 191 | self.name = name |
| 192 | self.body = body |
| 193 | |
| 194 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 195 | if visitor.visit_EnumDefinition(self): |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 196 | for s in self.body: |
| 197 | s.accept(visitor) |
| 198 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 199 | class MessageDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 200 | def __init__(self, name, body, linespan=None, lexspan=None, p=None): |
| 201 | super(MessageDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 202 | self._fields += ['name', 'body'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 203 | self.name = name |
| 204 | self.body = body |
| 205 | |
| 206 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 207 | if visitor.visit_MessageDefinition(self): |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 208 | for s in self.body: |
| 209 | s.accept(visitor) |
| 210 | |
| 211 | class MessageExtension(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 212 | def __init__(self, name, body, linespan=None, lexspan=None, p=None): |
| 213 | super(MessageExtension, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 214 | self._fields += ['name', 'body'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 215 | self.name = name |
| 216 | self.body = body |
| 217 | |
| 218 | def accept(self, visitor): |
| 219 | if visitor.visit_MessageExtension(self): |
| 220 | for s in self.body: |
| 221 | s.accept(visitor) |
| 222 | |
| 223 | class MethodDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 224 | def __init__(self, name, name2, name3, linespan=None, lexspan=None, p=None): |
| 225 | super(MethodDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 226 | self._fields += ['name', 'name2', 'name3'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 227 | self.name = name |
| 228 | self.name2 = name2 |
| 229 | self.name3 = name3 |
| 230 | |
| 231 | def accept(self, visitor): |
| 232 | visitor.visit_MethodDefinition(self) |
| 233 | |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 234 | class ServiceDefinition(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 235 | def __init__(self, name, body, linespan=None, lexspan=None, p=None): |
| 236 | super(ServiceDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 237 | self._fields += ['name', 'body'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 238 | self.name = name |
| 239 | self.body = body |
| 240 | |
| 241 | def accept(self, visitor): |
Dusan Klinec | e26bb02 | 2014-11-09 12:21:37 +0100 | [diff] [blame] | 242 | if visitor.visit_ServiceDefinition(self): |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 243 | for s in self.body: |
| 244 | s.accept(visitor) |
| 245 | |
| 246 | class ExtensionsMax(SourceElement): |
| 247 | pass |
| 248 | |
| 249 | class ExtensionsDirective(SourceElement): |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 250 | def __init__(self, fromVal, toVal, linespan=None, lexspan=None, p=None): |
| 251 | super(ExtensionsDirective, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 252 | self._fields += ['fromVal', 'toVal'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 253 | self.fromVal = fromVal |
| 254 | self.toVal = toVal |
| 255 | |
| 256 | def accept(self, visitor): |
| 257 | visitor.visit_ExtensionsDirective(self) |
| 258 | |
| 259 | class Literal(SourceElement): |
| 260 | |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 261 | def __init__(self, value, linespan=None, lexspan=None, p=None): |
| 262 | super(Literal, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 263 | self._fields += ['value'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 264 | self.value = value |
| 265 | |
| 266 | def accept(self, visitor): |
| 267 | visitor.visit_Literal(self) |
| 268 | |
| 269 | class Name(SourceElement): |
| 270 | |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 271 | def __init__(self, value, linespan=None, lexspan=None, p=None): |
| 272 | super(Name, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 273 | self._fields += ['value'] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 274 | self.value = value |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 275 | self.deriveLex() |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 276 | |
| 277 | def append_name(self, name): |
| 278 | try: |
| 279 | self.value = self.value + '.' + name.value |
| 280 | except: |
| 281 | self.value = self.value + '.' + name |
| 282 | |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 283 | def deriveLex(self): |
| 284 | if hasattr(self.value, "lexspan"): |
| 285 | self.lexspan = self.value.lexspan |
| 286 | self.linespan = self.value.linespan |
| 287 | else: |
| 288 | return |
| 289 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 290 | def accept(self, visitor): |
| 291 | visitor.visit_Name(self) |
| 292 | |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 293 | class DotName(Name): |
| 294 | elements = [] |
| 295 | def __init__(self, elements, linespan=None, lexspan=None, p=None): |
| 296 | super(DotName, self).__init__('.'.join([str(x) for x in elements]), linespan=linespan, lexspan=lexspan, p=p) |
| 297 | self._fields += ['elements'] |
| 298 | self.elements = elements |
| 299 | self.deriveLex() |
| 300 | |
| 301 | def deriveLex(self): |
| 302 | if isinstance(self.elements, list) and len(self.elements)>0: |
| 303 | 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])) |
| 304 | 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])) |
| 305 | elif hasattr(self.elements, "lexspan"): |
| 306 | self.lexspan = self.elements.lexspan |
| 307 | self.linespan = self.elements.linespan |
| 308 | else: |
| 309 | return |
| 310 | |
| 311 | def accept(self, visitor): |
| 312 | visitor.visit_DotName(self) |
| 313 | |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 314 | class ProtoFile(SourceElement): |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 315 | |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 316 | def __init__(self, pkg, body, linespan=None, lexspan=None, p=None): |
| 317 | super(ProtoFile, self).__init__(linespan=linespan, lexspan=lexspan, p=p) |
| 318 | self._fields += ['pkg', 'body'] |
| 319 | self.pkg = pkg |
| 320 | self.body = body |
| 321 | |
| 322 | def accept(self, visitor): |
| 323 | if visitor.visit_Proto(self): |
| 324 | for s in self.body: |
| 325 | s.accept(visitor) |