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.
 
 

115 line
3.2 KiB

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