Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/lark/common.py: 83%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

60 statements  

1from copy import deepcopy 

2import sys 

3from types import ModuleType 

4from typing import Callable, Collection, Dict, Optional, TYPE_CHECKING, List 

5 

6if TYPE_CHECKING: 

7 from .lark import PostLex 

8 from .lexer import Lexer 

9 from .grammar import Rule 

10 from typing import Union, Type 

11 from typing import Literal 

12 if sys.version_info >= (3, 10): 

13 from typing import TypeAlias 

14 else: 

15 from typing_extensions import TypeAlias 

16 

17from .utils import Serialize 

18from .lexer import TerminalDef, Token 

19 

20###{standalone 

21 

22_ParserArgType: 'TypeAlias' = 'Literal["earley", "lalr", "cyk", "auto"]' 

23_LexerArgType: 'TypeAlias' = 'Union[Literal["auto", "basic", "contextual", "dynamic", "dynamic_complete"], Type[Lexer]]' 

24_LexerCallback = Callable[[Token], Token] 

25ParserCallbacks = Dict[str, Callable] 

26 

27class LexerConf(Serialize): 

28 __serialize_fields__ = 'terminals', 'ignore', 'g_regex_flags', 'use_bytes', 'lexer_type' 

29 __serialize_namespace__ = TerminalDef, 

30 

31 terminals: Collection[TerminalDef] 

32 re_module: ModuleType 

33 ignore: Collection[str] 

34 postlex: 'Optional[PostLex]' 

35 callbacks: Dict[str, _LexerCallback] 

36 g_regex_flags: int 

37 skip_validation: bool 

38 use_bytes: bool 

39 lexer_type: Optional[_LexerArgType] 

40 strict: bool 

41 

42 def __init__(self, terminals: Collection[TerminalDef], re_module: ModuleType, ignore: Collection[str]=(), postlex: 'Optional[PostLex]'=None, 

43 callbacks: Optional[Dict[str, _LexerCallback]]=None, g_regex_flags: int=0, skip_validation: bool=False, use_bytes: bool=False, strict: bool=False): 

44 self.terminals = terminals 

45 self.terminals_by_name = {t.name: t for t in self.terminals} 

46 assert len(self.terminals) == len(self.terminals_by_name) 

47 self.ignore = ignore 

48 self.postlex = postlex 

49 self.callbacks = callbacks or {} 

50 self.g_regex_flags = g_regex_flags 

51 self.re_module = re_module 

52 self.skip_validation = skip_validation 

53 self.use_bytes = use_bytes 

54 self.strict = strict 

55 self.lexer_type = None 

56 

57 def _deserialize(self): 

58 self.terminals_by_name = {t.name: t for t in self.terminals} 

59 

60 def __deepcopy__(self, memo=None): 

61 return type(self)( 

62 deepcopy(self.terminals, memo), 

63 self.re_module, 

64 deepcopy(self.ignore, memo), 

65 deepcopy(self.postlex, memo), 

66 deepcopy(self.callbacks, memo), 

67 deepcopy(self.g_regex_flags, memo), 

68 deepcopy(self.skip_validation, memo), 

69 deepcopy(self.use_bytes, memo), 

70 ) 

71 

72class ParserConf(Serialize): 

73 __serialize_fields__ = 'rules', 'start', 'parser_type' 

74 

75 rules: List['Rule'] 

76 callbacks: ParserCallbacks 

77 start: List[str] 

78 parser_type: _ParserArgType 

79 

80 def __init__(self, rules: List['Rule'], callbacks: ParserCallbacks, start: List[str]): 

81 assert isinstance(start, list) 

82 self.rules = rules 

83 self.callbacks = callbacks 

84 self.start = start 

85 

86###}