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.

163 lines
3.3 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 __init__(self, type_: str, value: Any, pos_in_stream: int = None, line: int = None, column: int = None, end_line: int = None, end_column: int = None, end_pos: int = None):
  65. ...
  66. def update(self, type_: Optional[str] = None, value: Optional[str] = None) -> Token:
  67. ...
  68. @classmethod
  69. def new_borrow_pos(cls: Type[_T], type_: str, value: str, borrow_t: Token) -> _T:
  70. ...
  71. _Callback = Callable[[Token], Token]
  72. class Lexer(ABC):
  73. lex: Callable[..., Iterator[Token]]
  74. class LexerConf:
  75. tokens: Collection[TerminalDef]
  76. re_module: ModuleType
  77. ignore: Collection[str] = ()
  78. postlex: Any =None
  79. callbacks: Optional[Dict[str, _Callback]] = None
  80. g_regex_flags: int = 0
  81. skip_validation: bool = False
  82. use_bytes: bool = False
  83. class TraditionalLexer(Lexer):
  84. terminals: Collection[TerminalDef]
  85. ignore_types: FrozenSet[str]
  86. newline_types: FrozenSet[str]
  87. user_callbacks: Dict[str, _Callback]
  88. callback: Dict[str, _Callback]
  89. mres: List[Tuple[REPattern, Dict[int, str]]]
  90. re: ModuleType
  91. def __init__(
  92. self,
  93. conf: LexerConf
  94. ):
  95. ...
  96. def build(self) -> None:
  97. ...
  98. def match(self, stream: str, pos: int) -> Optional[Tuple[str, str]]:
  99. ...
  100. def lex(self, stream: str) -> Iterator[Token]:
  101. ...
  102. def next_token(self, lex_state: Any, parser_state: Any = None) -> Token:
  103. ...
  104. class ContextualLexer(Lexer):
  105. lexers: Dict[str, TraditionalLexer]
  106. root_lexer: TraditionalLexer
  107. def __init__(
  108. self,
  109. terminals: Collection[TerminalDef],
  110. states: Dict[str, Collection[str]],
  111. re_: ModuleType,
  112. ignore: Collection[str] = ...,
  113. always_accept: Collection[str] = ...,
  114. user_callbacks: Dict[str, _Callback] = ...,
  115. g_regex_flags: int = ...
  116. ):
  117. ...
  118. def lex(self, stream: str, get_parser_state: Callable[[], str]) -> Iterator[Token]:
  119. ...