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.

104 lines
2.7 KiB

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