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.

56 lines
1.8 KiB

  1. from .utils import Serialize
  2. from .lexer import TerminalDef, Token
  3. ###{standalone
  4. from types import ModuleType
  5. from typing import Any, Callable, Collection, Dict, Optional, TYPE_CHECKING
  6. if TYPE_CHECKING:
  7. from .lark import PostLex
  8. _Callback = Callable[[Token], Token]
  9. class LexerConf(Serialize):
  10. __serialize_fields__ = 'terminals', 'ignore', 'g_regex_flags', 'use_bytes', 'lexer_type'
  11. __serialize_namespace__ = TerminalDef,
  12. terminals: Collection[TerminalDef]
  13. re_module: ModuleType
  14. ignore: Collection[str] = ()
  15. postlex: 'PostLex' = None
  16. callbacks: Optional[Dict[str, _Callback]] = None
  17. g_regex_flags: int = 0
  18. skip_validation: bool = False
  19. use_bytes: bool = False
  20. def __init__(self, terminals: Collection[TerminalDef], re_module: ModuleType, ignore: Collection[str]=(), postlex: 'PostLex'=None, callbacks: Optional[Dict[str, _Callback]]=None, g_regex_flags: int=0, skip_validation: bool=False, use_bytes: bool=False):
  21. self.terminals = terminals
  22. self.terminals_by_name = {t.name: t for t in self.terminals}
  23. assert len(self.terminals) == len(self.terminals_by_name)
  24. self.ignore = ignore
  25. self.postlex = postlex
  26. self.callbacks = callbacks or {}
  27. self.g_regex_flags = g_regex_flags
  28. self.re_module = re_module
  29. self.skip_validation = skip_validation
  30. self.use_bytes = use_bytes
  31. self.lexer_type = None
  32. def _deserialize(self):
  33. self.terminals_by_name = {t.name: t for t in self.terminals}
  34. class ParserConf(Serialize):
  35. __serialize_fields__ = 'rules', 'start', 'parser_type'
  36. def __init__(self, rules, callbacks, start):
  37. assert isinstance(start, list)
  38. self.rules = rules
  39. self.callbacks = callbacks
  40. self.start = start
  41. self.parser_type = None
  42. ###}