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.

160 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. from types import ModuleType
  3. from typing import (
  4. TypeVar, Type, Tuple, List, Dict, Iterator, Collection, Callable, Optional, FrozenSet, Any,
  5. Pattern as REPattern,
  6. )
  7. from abc import abstractmethod, ABC
  8. _T = TypeVar('_T')
  9. class Pattern(ABC):
  10. value: str
  11. flags: Collection[str]
  12. def __init__(self, value: str, flags: Collection[str] = ...):
  13. ...
  14. @property
  15. @abstractmethod
  16. def type(self) -> str:
  17. ...
  18. @abstractmethod
  19. def to_regexp(self) -> str:
  20. ...
  21. @property
  22. @abstractmethod
  23. def min_width(self) -> int:
  24. ...
  25. @property
  26. @abstractmethod
  27. def max_width(self) -> int:
  28. ...
  29. class PatternStr(Pattern):
  30. type: str = ...
  31. def to_regexp(self) -> str:
  32. ...
  33. @property
  34. def min_width(self) -> int:
  35. ...
  36. @property
  37. def max_width(self) -> int:
  38. ...
  39. class PatternRE(Pattern):
  40. type: str = ...
  41. def to_regexp(self) -> str:
  42. ...
  43. @property
  44. def min_width(self) -> int:
  45. ...
  46. @property
  47. def max_width(self) -> int:
  48. ...
  49. class TerminalDef:
  50. name: str
  51. pattern: Pattern
  52. priority: int
  53. def __init__(self, name: str, pattern: Pattern, priority: int = ...):
  54. ...
  55. class Token(str):
  56. type: str
  57. pos_in_stream: int
  58. value: str
  59. line: int
  60. column: int
  61. end_line: int
  62. end_column: int
  63. end_pos: int
  64. def update(self, type_: Optional[str] = None, value: Optional[str] = None) -> Token:
  65. ...
  66. @classmethod
  67. def new_borrow_pos(cls: Type[_T], type_: str, value: str, borrow_t: Token) -> _T:
  68. ...
  69. _Callback = Callable[[Token], Token]
  70. class Lexer(ABC):
  71. lex: Callable[..., Iterator[Token]]
  72. class LexerConf:
  73. tokens: Collection[TerminalDef]
  74. re_module: ModuleType
  75. ignore: Collection[str] = ()
  76. postlex: Any =None
  77. callbacks: Optional[Dict[str, _Callback]] = None
  78. g_regex_flags: int = 0
  79. skip_validation: bool = False
  80. use_bytes: bool = False
  81. class TraditionalLexer(Lexer):
  82. terminals: Collection[TerminalDef]
  83. ignore_types: FrozenSet[str]
  84. newline_types: FrozenSet[str]
  85. user_callbacks: Dict[str, _Callback]
  86. callback: Dict[str, _Callback]
  87. mres: List[Tuple[REPattern, Dict[int, str]]]
  88. re: ModuleType
  89. def __init__(
  90. self,
  91. conf: LexerConf
  92. ):
  93. ...
  94. def build(self) -> None:
  95. ...
  96. def match(self, stream: str, pos: int) -> Optional[Tuple[str, str]]:
  97. ...
  98. def lex(self, stream: str) -> Iterator[Token]:
  99. ...
  100. def next_token(self, lex_state: Any) -> Token:
  101. ...
  102. class ContextualLexer(Lexer):
  103. lexers: Dict[str, TraditionalLexer]
  104. root_lexer: TraditionalLexer
  105. def __init__(
  106. self,
  107. terminals: Collection[TerminalDef],
  108. states: Dict[str, Collection[str]],
  109. re_: ModuleType,
  110. ignore: Collection[str] = ...,
  111. always_accept: Collection[str] = ...,
  112. user_callbacks: Dict[str, _Callback] = ...,
  113. g_regex_flags: int = ...
  114. ):
  115. ...
  116. def lex(self, stream: str, get_parser_state: Callable[[], str]) -> Iterator[Token]:
  117. ...