99how this parsing engine works.
1010
1111"""
12- import copy
1312from contextlib import contextmanager
1413
1514# Local imports
1817 cast ,
1918 Any ,
2019 Optional ,
21- Text ,
2220 Union ,
2321 Tuple ,
2422 Dict ,
3533 from blib2to3 .pgen2 .driver import TokenProxy
3634
3735
38- Results = Dict [Text , NL ]
36+ Results = Dict [str , NL ]
3937Convert = Callable [[Grammar , RawNode ], Union [Node , Leaf ]]
4038DFA = List [List [Tuple [int , int ]]]
4139DFAS = Tuple [DFA , Dict [int , int ]]
@@ -100,7 +98,7 @@ def backtrack(self) -> Iterator[None]:
10098 finally :
10199 self .parser .is_backtracking = is_backtracking
102100
103- def add_token (self , tok_type : int , tok_val : Text , raw : bool = False ) -> None :
101+ def add_token (self , tok_type : int , tok_val : str , raw : bool = False ) -> None :
104102 func : Callable [..., Any ]
105103 if raw :
106104 func = self .parser ._addtoken
@@ -114,7 +112,7 @@ def add_token(self, tok_type: int, tok_val: Text, raw: bool = False) -> None:
114112 args .insert (0 , ilabel )
115113 func (* args )
116114
117- def determine_route (self , value : Optional [Text ] = None , force : bool = False ) -> Optional [int ]:
115+ def determine_route (self , value : Optional [str ] = None , force : bool = False ) -> Optional [int ]:
118116 alive_ilabels = self .ilabels
119117 if len (alive_ilabels ) == 0 :
120118 * _ , most_successful_ilabel = self ._dead_ilabels
@@ -131,18 +129,18 @@ class ParseError(Exception):
131129 """Exception to signal the parser is stuck."""
132130
133131 def __init__ (
134- self , msg : Text , type : Optional [int ], value : Optional [Text ], context : Context
132+ self , msg : str , type : Optional [int ], value : Optional [str ], context : Context
135133 ) -> None :
136134 Exception .__init__ (
137- self , "%s : type=%r , value=%r , context=%r" % ( msg , type , value , context )
135+ self , f" { msg } : type={ type !r } , value={ value !r } , context={ context !r } "
138136 )
139137 self .msg = msg
140138 self .type = type
141139 self .value = value
142140 self .context = context
143141
144142
145- class Parser ( object ) :
143+ class Parser :
146144 """Parser engine.
147145
148146 The proper usage sequence is:
@@ -236,7 +234,7 @@ def setup(self, proxy: "TokenProxy", start: Optional[int] = None) -> None:
236234 self .used_names : Set [str ] = set ()
237235 self .proxy = proxy
238236
239- def addtoken (self , type : int , value : Text , context : Context ) -> bool :
237+ def addtoken (self , type : int , value : str , context : Context ) -> bool :
240238 """Add a token; return True iff this is the end of the program."""
241239 # Map from token to label
242240 ilabels = self .classify (type , value , context )
@@ -284,7 +282,7 @@ def addtoken(self, type: int, value: Text, context: Context) -> bool:
284282
285283 return self ._addtoken (ilabel , type , value , context )
286284
287- def _addtoken (self , ilabel : int , type : int , value : Text , context : Context ) -> bool :
285+ def _addtoken (self , ilabel : int , type : int , value : str , context : Context ) -> bool :
288286 # Loop until the token is shifted; may raise exceptions
289287 while True :
290288 dfa , state , node = self .stack [- 1 ]
@@ -329,7 +327,7 @@ def _addtoken(self, ilabel: int, type: int, value: Text, context: Context) -> bo
329327 # No success finding a transition
330328 raise ParseError ("bad input" , type , value , context )
331329
332- def classify (self , type : int , value : Text , context : Context ) -> List [int ]:
330+ def classify (self , type : int , value : str , context : Context ) -> List [int ]:
333331 """Turn a token into a label. (Internal)
334332
335333 Depending on whether the value is a soft-keyword or not,
@@ -352,7 +350,7 @@ def classify(self, type: int, value: Text, context: Context) -> List[int]:
352350 raise ParseError ("bad token" , type , value , context )
353351 return [ilabel ]
354352
355- def shift (self , type : int , value : Text , newstate : int , context : Context ) -> None :
353+ def shift (self , type : int , value : str , newstate : int , context : Context ) -> None :
356354 """Shift a token. (Internal)"""
357355 if self .is_backtracking :
358356 dfa , state , _ = self .stack [- 1 ]
0 commit comments