This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

117 linhas
3.2 KiB

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