This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.8 KiB

  1. from .utils import Serialize
  2. ###{standalone
  3. class Symbol(Serialize):
  4. __slots__ = ('name',)
  5. is_term = NotImplemented
  6. def __init__(self, name):
  7. self.name = name
  8. def __eq__(self, other):
  9. assert isinstance(other, Symbol), other
  10. return self.is_term == other.is_term and self.name == other.name
  11. def __ne__(self, other):
  12. return not (self == other)
  13. def __hash__(self):
  14. return hash(self.name)
  15. def __repr__(self):
  16. return '%s(%r)' % (type(self).__name__, self.name)
  17. fullrepr = property(__repr__)
  18. class Terminal(Symbol):
  19. __serialize_fields__ = 'name', 'filter_out'
  20. is_term = True
  21. def __init__(self, name, filter_out=False):
  22. self.name = name
  23. self.filter_out = filter_out
  24. @property
  25. def fullrepr(self):
  26. return '%s(%r, %r)' % (type(self).__name__, self.name, self.filter_out)
  27. class NonTerminal(Symbol):
  28. __serialize_fields__ = 'name',
  29. is_term = False
  30. class RuleOptions(Serialize):
  31. __serialize_fields__ = 'keep_all_tokens', 'expand1', 'priority', 'template_source', 'empty_indices'
  32. def __init__(self, keep_all_tokens=False, expand1=False, priority=None, template_source=None, empty_indices=()):
  33. self.keep_all_tokens = keep_all_tokens
  34. self.expand1 = expand1
  35. self.priority = priority
  36. self.template_source = template_source
  37. self.empty_indices = empty_indices
  38. def __repr__(self):
  39. return 'RuleOptions(%r, %r, %r, %r)' % (
  40. self.keep_all_tokens,
  41. self.expand1,
  42. self.priority,
  43. self.template_source
  44. )
  45. class Rule(Serialize):
  46. """
  47. origin : a symbol
  48. expansion : a list of symbols
  49. order : index of this expansion amongst all rules of the same name
  50. """
  51. __slots__ = ('origin', 'expansion', 'alias', 'options', 'order', '_hash')
  52. __serialize_fields__ = 'origin', 'expansion', 'order', 'alias', 'options'
  53. __serialize_namespace__ = Terminal, NonTerminal, RuleOptions
  54. def __init__(self, origin, expansion, order=0, alias=None, options=None):
  55. self.origin = origin
  56. self.expansion = expansion
  57. self.alias = alias
  58. self.order = order
  59. self.options = options or RuleOptions()
  60. self._hash = hash((self.origin, tuple(self.expansion)))
  61. def _deserialize(self):
  62. self._hash = hash((self.origin, tuple(self.expansion)))
  63. def __str__(self):
  64. return '<%s : %s>' % (self.origin.name, ' '.join(x.name for x in self.expansion))
  65. def __repr__(self):
  66. return 'Rule(%r, %r, %r, %r)' % (self.origin, self.expansion, self.alias, self.options)
  67. def __hash__(self):
  68. return self._hash
  69. def __eq__(self, other):
  70. if not isinstance(other, Rule):
  71. return False
  72. return self.origin == other.origin and self.expansion == other.expansion
  73. ###}