11from typing import Any , Callable , Dict , Optional , Collection , Union , TYPE_CHECKING
22
33from .exceptions import ConfigurationError , GrammarError , assert_config
4- from .utils import get_regexp_width , Serialize , TextOrSlice , TextSlice
4+ from .utils import get_regexp_width , Serialize , TextOrSlice , TextSlice , LarkInput
55from .lexer import LexerThread , BasicLexer , ContextualLexer , Lexer
66from .parsers import earley , xearley , cyk
77from .parsers .lalr_parser import LALR_Parser
@@ -23,7 +23,7 @@ class CustomLexerWrapper1(Lexer):
2323 def __init__ (self , lexer_conf ):
2424 self .lexer = lexer_class (lexer_conf )
2525 def lex (self , lexer_state , parser_state ):
26- if not lexer_state .text .is_complete_text ():
26+ if isinstance ( lexer_state . text , TextSlice ) and not lexer_state .text .is_complete_text ():
2727 raise TypeError ("Interface=1 Custom Lexer don't support TextSlice" )
2828 lexer_state .text = lexer_state .text
2929 return self .lexer .lex (lexer_state , parser_state )
@@ -34,9 +34,11 @@ def __init__(self, lexer_conf):
3434 self .lexer = lexer_class (lexer_conf )
3535
3636 def lex (self , lexer_state , parser_state ):
37- if not lexer_state .text .is_complete_text ():
38- raise TypeError ("Interface=0 Custom Lexer don't support TextSlice" )
39- return self .lexer .lex (lexer_state .text .text )
37+ if isinstance (lexer_state .text , TextSlice ):
38+ if not lexer_state .text .is_complete_text ():
39+ raise TypeError ("Interface=0 Custom Lexer don't support TextSlice" )
40+ return self .lexer .lex (lexer_state .text .text )
41+ return self .lexer .lex (lexer_state .text )
4042 return CustomLexerWrapper0
4143 else :
4244 raise ValueError (f"Unknown __future_interface__ value { future_interface } , integer 0-2 expected" )
@@ -108,11 +110,17 @@ def _verify_start(self, start=None):
108110 raise ConfigurationError ("Unknown start rule %s. Must be one of %r" % (start , self .parser_conf .start ))
109111 return start
110112
111- def _make_lexer_thread (self , text : Optional [TextOrSlice ]) -> Union [TextOrSlice , LexerThread , None ]:
113+ def _make_lexer_thread (self , text : Optional [LarkInput ]) -> Union [LarkInput , LexerThread , None ]:
112114 cls = (self .options and self .options ._plugins .get ('LexerThread' )) or LexerThread
113- return text if self .skip_lexer else cls (self .lexer , None ) if text is None else cls .from_text (self .lexer , text )
114-
115- def parse (self , text : Optional [TextOrSlice ], start = None , on_error = None ):
115+ if self .skip_lexer :
116+ return text
117+ if text is None :
118+ return cls (self .lexer , None )
119+ if isinstance (text , (str , bytes , TextSlice )):
120+ return cls .from_text (self .lexer , text )
121+ return cls .from_custom_input (self .lexer , text )
122+
123+ def parse (self , text : Optional [LarkInput ], start = None , on_error = None ):
116124 if self .lexer_conf .lexer_type in ("dynamic" , "dynamic_complete" ):
117125 if isinstance (text , TextSlice ) and not text .is_complete_text ():
118126 raise TypeError (f"Lexer { self .lexer_conf .lexer_type } does not support text slices." )
0 commit comments