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.

107 lines
2.7 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', 'empty_indices'
  32. def __init__(self, keep_all_tokens=False, expand1=False, priority=None, empty_indices=()):
  33. self.keep_all_tokens = keep_all_tokens
  34. self.expand1 = expand1
  35. self.priority = priority
  36. self.empty_indices = empty_indices
  37. def __repr__(self):
  38. return 'RuleOptions(%r, %r, %r)' % (
  39. self.keep_all_tokens,
  40. self.expand1,
  41. self.priority,
  42. )
  43. class Rule(Serialize):
  44. """
  45. origin : a symbol
  46. expansion : a list of symbols
  47. order : index of this expansion amongst all rules of the same name
  48. """
  49. __slots__ = ('origin', 'expansion', 'alias', 'options', 'order', '_hash')
  50. __serialize_fields__ = 'origin', 'expansion', 'order', 'alias', 'options'
  51. __serialize_namespace__ = Terminal, NonTerminal, RuleOptions
  52. def __init__(self, origin, expansion, order=0, alias=None, options=None):
  53. self.origin = origin
  54. self.expansion = expansion
  55. self.alias = alias
  56. self.order = order
  57. self.options = options or RuleOptions()
  58. self._hash = hash((self.origin, tuple(self.expansion)))
  59. def _deserialize(self):
  60. self._hash = hash((self.origin, tuple(self.expansion)))
  61. def __str__(self):
  62. return '<%s : %s>' % (self.origin.name, ' '.join(x.name for x in self.expansion))
  63. def __repr__(self):
  64. return 'Rule(%r, %r, %r, %r)' % (self.origin, self.expansion, self.alias, self.options)
  65. def __hash__(self):
  66. return self._hash
  67. def __eq__(self, other):
  68. if not isinstance(other, Rule):
  69. return False
  70. return self.origin == other.origin and self.expansion == other.expansion
  71. ###}