blob: e94566c39f1f0f3968c3cc86ba6eb39683e3ae94 [file] [log] [blame]
__author__ = "Dusan (Ph4r05) Klinec"
__copyright__ = "Copyright (C) 2014 Dusan (ph4r05) Klinec"
__license__ = "Apache License, Version 2.0"
__version__ = "1.0"
class Visitor(object):
def __init__(self, verbose=False):
self.verbose = verbose
def __getattr__(self, name):
if not name.startswith('visit_'):
raise AttributeError('name must start with visit_ but was {}'.format(name))
def f(element):
if self.verbose:
msg = 'unimplemented call to {}; ignoring ({})'
print(msg.format(name, element))
return True
return f
# visitor.visit_PackageStatement(self)
# visitor.visit_ImportStatement(self)
# visitor.visit_OptionStatement(self)
# visitor.visit_FieldDirective(self)
# visitor.visit_FieldType(self)
# visitor.visit_FieldDefinition(self)
# visitor.visit_EnumFieldDefinition(self)
# visitor.visit_EnumDefinition(self)
# visitor.visit_MessageDefinition(self)
# visitor.visit_MessageExtension(self)
# visitor.visit_MethodDefinition(self)
# visitor.visit_ServiceDefinition(self)
# visitor.visit_ExtensionsDirective(self)
# visitor.visit_Literal(self)
# visitor.visit_Name(self)
# visitor.visit_Proto(self)
# visitor.visit_LU(self)
class Base(object):
parent = None
lexspan = None
linespan = None
def v(self, obj, visitor):
if obj == None:
return
elif hasattr(obj, "accept"):
obj.accept(visitor)
elif isinstance(obj, list):
for s in obj:
self.v(s, visitor)
pass
pass
@staticmethod
def p(obj, parent):
if isinstance(obj, list):
for s in obj:
Base.p(s, parent)
if hasattr(obj, "parent"):
obj.parent = parent
# Lexical unit - contains lexspan and linespan for later analysis.
class LU(Base):
def __init__(self, p, idx):
self.p = p
self.idx = idx
self.pval = p[idx]
self.lexspan = p.lexspan(idx)
self.linespan = p.linespan(idx)
# If string is in the value (raw value) and start and stop lexspan is the same, add real span
# obtained by string length.
if isinstance(self.pval, str) \
and self.lexspan != None \
and self.lexspan[0] == self.lexspan[1] \
and self.lexspan[0] != 0:
self.lexspan = tuple([self.lexspan[0], self.lexspan[0] + len(self.pval)])
super(LU, self).__init__()
@staticmethod
def i(p, idx):
if isinstance(p[idx], LU): return p[idx]
if isinstance(p[idx], str): return LU(p, idx)
return p[idx]
def describe(self):
return "LU(%s,%s)" % (self.pval, self.lexspan)
def __str__(self):
return self.pval
def __repr__(self):
return self.describe()
def accept(self, visitor):
self.v(self.pval, visitor)
def __iter__(self):
for x in self.pval:
yield x
# Base node
class SourceElement(Base):
'''
A SourceElement is the base class for all elements that occur in a Protocol Buffers
file parsed by plyproto.
'''
def __init__(self, linespan=[], lexspan=[], p=None):
super(SourceElement, self).__init__()
self._fields = [] # ['linespan', 'lexspan']
self.linespan = linespan
self.lexspan = lexspan
self.p = p
def __repr__(self):
equals = ("{0}={1!r}".format(k, getattr(self, k))
for k in self._fields)
args = ", ".join(equals)
return "{0}({1})".format(self.__class__.__name__, args)
def __eq__(self, other):
try:
return self.__dict__ == other.__dict__
except AttributeError:
return False
def __ne__(self, other):
return not self == other
def setLexData(self, linespan, lexspan):
self.linespan = linespan
self.lexspan = lexspan
def setLexObj(self, p):
self.p = p
def accept(self, visitor):
pass
class PackageStatement(SourceElement):
def __init__(self, name, linespan=None, lexspan=None, p=None):
super(PackageStatement, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['name']
self.name = name
Base.p(self.name, self)
def accept(self, visitor):
visitor.visit_PackageStatement(self)
class ImportStatement(SourceElement):
def __init__(self, name, linespan=None, lexspan=None, p=None):
super(ImportStatement, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['name']
self.name = name
Base.p(self.name, self)
def accept(self, visitor):
visitor.visit_ImportStatement(self)
class OptionStatement(SourceElement):
def __init__(self, name, value, linespan=None, lexspan=None, p=None):
super(OptionStatement, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['name', 'value']
self.name = name
Base.p(self.name, self)
self.value = value
Base.p(self.value, self)
def accept(self, visitor):
visitor.visit_OptionStatement(self)
class FieldDirective(SourceElement):
def __init__(self, name, value, linespan=None, lexspan=None, p=None):
super(FieldDirective, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['name', 'value']
self.name = name
Base.p(self.name, self)
self.value = value
Base.p(self.value, self)
def accept(self, visitor):
if visitor.visit_FieldDirective(self):
self.v(self.name, visitor)
self.v(self.value, visitor)
visitor.visit_FieldDirective_post(self)
class FieldType(SourceElement):
def __init__(self, name, linespan=None, lexspan=None, p=None):
super(FieldType, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['name']
self.name = name
Base.p(self.name, self)
def accept(self, visitor):
if visitor.visit_FieldType(self):
self.v(self.name, visitor)
class LinkDefinition(SourceElement):
def __init__(self, link_type, src_port, name, through, dst_port, linespan=None, lexspan=None, p=None):
super(LinkDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['link_type', 'src_port', 'name', 'dst_port', 'through']
self.link_type = link_type
Base.p(self.link_type, self)
self.src_port = src_port
Base.p(self.src_port, self)
self.name = name
Base.p(self.name, self)
self.through = through
Base.p(self.through, self)
self.dst_port = dst_port
Base.p(self.dst_port, self)
def accept(self, visitor):
visitor.visit_LinkDefinition(self)
class FieldDefinition(SourceElement):
def __init__(self, field_modifier, ftype, name, fieldId, fieldDirective, linespan=None, lexspan=None, p=None):
super(FieldDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['field_modifier', 'ftype', 'name', 'fieldId', 'fieldDirective']
self.name = name
Base.p(self.name, self)
self.field_modifier = field_modifier
Base.p(self.field_modifier, self)
self.ftype = ftype
Base.p(self.ftype, self)
self.fieldId = fieldId
Base.p(self.fieldId, self)
self.fieldDirective = fieldDirective
Base.p(self.fieldDirective, self)
def accept(self, visitor):
if visitor.visit_FieldDefinition(self):
self.v(self.name, visitor)
self.v(self.field_modifier, visitor)
self.v(self.ftype, visitor)
self.v(self.fieldId, visitor)
self.v(self.fieldDirective, visitor)
visitor.visit_FieldDefinition_post(self)
class EnumFieldDefinition(SourceElement):
def __init__(self, name, fieldId, linespan=None, lexspan=None, p=None):
super(EnumFieldDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['name', 'fieldId']
self.name = name
Base.p(self.name, self)
self.fieldId = fieldId
Base.p(self.fieldId, self)
def accept(self, visitor):
if visitor.visit_EnumFieldDefinition(self):
self.v(self.name, visitor)
self.v(self.fieldId, visitor)
class EnumDefinition(SourceElement):
def __init__(self, name, body, linespan=None, lexspan=None, p=None):
super(EnumDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['name', 'body']
self.name = name
Base.p(self.name, self)
self.body = body
Base.p(self.body, self)
def accept(self, visitor):
if visitor.visit_EnumDefinition(self):
self.v(self.name, visitor)
self.v(self.body, visitor)
class LinkSpec(SourceElement):
def __init__(self, field_spec, link_spec, linespan=None, lexspan=None, p=None):
super(LinkSpec, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['link_def', 'field_def']
self.link_def = link_spec
Base.p(self.link_def, self)
self.field_def = field_spec
Base.p(self.field_def, self)
def accept(self, visitor):
if visitor.visit_LinkSpec(self):
self.v(self.link_def, visitor)
self.v(self.field_def, visitor)
visitor.visit_LinkSpec_post(self)
class MessageDefinition(SourceElement):
def __init__(self, name, bases, body, linespan=None, lexspan=None, p=None):
super(MessageDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['name', 'bases', 'body']
self.name = name
Base.p(self.name, self)
self.bases = bases
Base.p(self.bases, self)
self.body = body
Base.p(self.body, self)
def accept(self, visitor):
if visitor.visit_MessageDefinition(self):
self.v(self.name, visitor)
self.v(self.bases, visitor)
self.v(self.body, visitor)
visitor.visit_MessageDefinition_post(self)
"""
class MessageDefinition(SourceElement):
def __init__(self, name, body, linespan=None, lexspan=None, p=None):
super(MessageDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['name', 'body']
self.name = name
Base.p(self.name, self)
self.body = body
Base.p(self.body, self)
def accept(self, visitor):
if visitor.visit_MessageDefinition(self):
self.v(self.name, visitor)
self.v(self.body, visitor)
visitor.visit_MessageDefinition_post(self)
"""
class MessageExtension(SourceElement):
def __init__(self, name, body, linespan=None, lexspan=None, p=None):
super(MessageExtension, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['name', 'body']
self.name = name
Base.p(self.name, self)
self.body = body
Base.p(self.body, self)
def accept(self, visitor):
if visitor.visit_MessageExtension(self):
self.v(self.name, visitor)
self.v(self.body, visitor)
visitor.visit_MessageExtension_post(self)
class MethodDefinition(SourceElement):
def __init__(self, name, name2, name3, linespan=None, lexspan=None, p=None):
super(MethodDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['name', 'name2', 'name3']
self.name = name
Base.p(self.name, self)
self.name2 = name2
Base.p(self.name, self)
self.name3 = name3
Base.p(self.name, self)
def accept(self, visitor):
if visitor.visit_MethodDefinition(self):
self.v(self.name, visitor)
self.v(self.name2, visitor)
self.v(self.name3, visitor)
visitor.visit_MethodDefinition_post(self)
class ServiceDefinition(SourceElement):
def __init__(self, name, body, linespan=None, lexspan=None, p=None):
super(ServiceDefinition, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['name', 'body']
self.name = name
Base.p(self.name, self)
self.body = body
Base.p(self.body, self)
def accept(self, visitor):
if visitor.visit_ServiceDefinition(self):
self.v(self.name, visitor)
self.v(self.body, visitor)
visitor.visit_ServiceDefinition_post(self)
class ExtensionsMax(SourceElement):
pass
class ExtensionsDirective(SourceElement):
def __init__(self, fromVal, toVal, linespan=None, lexspan=None, p=None):
super(ExtensionsDirective, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['fromVal', 'toVal']
self.fromVal = fromVal
Base.p(self.fromVal, self)
self.toVal = toVal
Base.p(self.toVal, self)
def accept(self, visitor):
if visitor.visit_ExtensionsDirective(self):
self.v(self.fromVal, visitor)
self.v(self.toVal, visitor)
visitor.visit_ExtensionsDirective_post(self)
class Literal(SourceElement):
def __init__(self, value, linespan=None, lexspan=None, p=None):
super(Literal, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['value']
self.value = value
def accept(self, visitor):
visitor.visit_Literal(self)
class Name(SourceElement):
def __init__(self, value, linespan=None, lexspan=None, p=None):
super(Name, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['value']
self.value = value
self.deriveLex()
def append_name(self, name):
try:
self.value = self.value + '.' + name.value
except:
self.value = self.value + '.' + name
def deriveLex(self):
if hasattr(self.value, "lexspan"):
self.lexspan = self.value.lexspan
self.linespan = self.value.linespan
else:
return
def accept(self, visitor):
visitor.visit_Name(self)
class DotName(Name):
elements = []
def __init__(self, elements, linespan=None, lexspan=None, p=None):
super(DotName, self).__init__('.'.join([str(x) for x in elements]), linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['elements']
self.elements = elements
self.deriveLex()
def deriveLex(self):
if isinstance(self.elements, list) and len(self.elements)>0:
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]))
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]))
elif hasattr(self.elements, "lexspan"):
self.lexspan = self.elements.lexspan
self.linespan = self.elements.linespan
else:
return
def accept(self, visitor):
visitor.visit_DotName(self)
class ProtoFile(SourceElement):
def __init__(self, pkg, body, linespan=None, lexspan=None, p=None):
super(ProtoFile, self).__init__(linespan=linespan, lexspan=lexspan, p=p)
self._fields += ['pkg', 'body']
self.pkg = pkg
Base.p(self.pkg, self)
self.body = body
Base.p(self.body, self)
def accept(self, visitor):
if visitor.visit_Proto(self):
self.v(self.pkg, visitor)
self.v(self.body, visitor)
visitor.visit_Proto_post(self)