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.

147 lines
2.8 KiB

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