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.

60 lines
1.7 KiB

  1. class Symbol(object):
  2. is_term = NotImplemented
  3. def __init__(self, name):
  4. self.name = name
  5. def __eq__(self, other):
  6. assert isinstance(other, Symbol), other
  7. return self.is_term == other.is_term and self.name == other.name
  8. def __hash__(self):
  9. return hash(self.name)
  10. class Terminal(Symbol):
  11. is_term = True
  12. @property
  13. def filter_out(self):
  14. return self.name.startswith('_')
  15. class NonTerminal(Symbol):
  16. is_term = False
  17. class Rule(object):
  18. """
  19. origin : a symbol
  20. expansion : a list of symbols
  21. """
  22. def __init__(self, origin, expansion, alias=None, options=None):
  23. self.origin = origin
  24. self.expansion = expansion
  25. self.alias = alias
  26. self.options = options
  27. def __str__(self):
  28. return '<%s : %s>' % (self.origin, ' '.join(map(str,self.expansion)))
  29. def __repr__(self):
  30. return 'Rule(%r, %r, %r, %r)' % (self.origin, self.expansion, self.alias, self.options)
  31. class RuleOptions:
  32. def __init__(self, keep_all_tokens=False, expand1=False, create_token=None, filter_out=False, priority=None):
  33. self.keep_all_tokens = keep_all_tokens
  34. self.expand1 = expand1
  35. self.create_token = create_token # used for scanless postprocessing
  36. self.priority = priority
  37. self.filter_out = filter_out # remove this rule from the tree
  38. # used for "token"-rules in scanless
  39. def __repr__(self):
  40. return 'RuleOptions(%r, %r, %r, %r, %r)' % (
  41. self.keep_all_tokens,
  42. self.expand1,
  43. self.create_token,
  44. self.priority,
  45. self.filter_out
  46. )