@@ -138,20 +138,13 @@ def transform_whitespace(
138138
139139
140140def tokenize (source : str , grammar : Optional [Grammar ] = None ) -> Iterator [TokenInfo ]:
141- async_keywords = False if grammar is None else grammar .async_keywords
142-
143141 lines = source .split ("\n " )
144142 lines += ["" ] # For newline tokens in files that don't end in a newline
145143 line , column = 1 , 0
146144
147- token_iterator = pytokens .tokenize (source )
148- is_async = False
149- current_indent = 0
150- async_indent = 0
151-
152145 prev_token : Optional [pytokens .Token ] = None
153146 try :
154- for token in token_iterator :
147+ for token in pytokens . tokenize ( source ) :
155148 token = transform_whitespace (token , source , prev_token )
156149
157150 line , column = token .start_line , token .start_col
@@ -166,58 +159,18 @@ def tokenize(source: str, grammar: Optional[Grammar] = None) -> Iterator[TokenIn
166159 prev_token = token
167160 continue
168161
169- if token .type == TokenType .indent :
170- current_indent += 1
171- if token .type == TokenType .dedent :
172- current_indent -= 1
173- if is_async and current_indent < async_indent :
174- is_async = False
175-
176162 source_line = lines [token .start_line - 1 ]
177163
178164 if token .type == TokenType .identifier and token_str in ("async" , "await" ):
179165 # Black uses `async` and `await` token types just for those two keywords
180- while True :
181- next_token = next (token_iterator )
182- next_str = source [next_token .start_index : next_token .end_index ]
183- next_token = transform_whitespace (next_token , next_str , token )
184- if next_token .type == TokenType .whitespace :
185- continue
186- break
187-
188- next_token_type = TOKEN_TYPE_MAP [next_token .type ]
189- next_line = lines [next_token .start_line - 1 ]
190-
191- if token_str == "async" and (
192- async_keywords
193- or (next_token_type == NAME and next_str in ("def" , "for" ))
194- ):
195- is_async = True
196- async_indent = current_indent + 1
197- current_token_type = ASYNC
198- elif token_str == "await" and (async_keywords or is_async ):
199- current_token_type = AWAIT
200- else :
201- current_token_type = TOKEN_TYPE_MAP [token .type ]
202-
203166 yield (
204- current_token_type ,
167+ ASYNC if token_str == "async" else AWAIT ,
205168 token_str ,
206169 (token .start_line , token .start_col ),
207170 (token .end_line , token .end_col ),
208171 source_line ,
209172 )
210- yield (
211- next_token_type ,
212- next_str ,
213- (next_token .start_line , next_token .start_col ),
214- (next_token .end_line , next_token .end_col ),
215- next_line ,
216- )
217- prev_token = token
218- continue
219-
220- if token .type == TokenType .op and token_str == "..." :
173+ elif token .type == TokenType .op and token_str == "..." :
221174 # Black doesn't have an ellipsis token yet, yield 3 DOTs instead
222175 assert token .start_line == token .end_line
223176 assert token .end_col == token .start_col + 3
@@ -232,16 +185,14 @@ def tokenize(source: str, grammar: Optional[Grammar] = None) -> Iterator[TokenIn
232185 (token .end_line , end_col ),
233186 source_line ,
234187 )
235- prev_token = token
236- continue
237-
238- yield (
239- TOKEN_TYPE_MAP [token .type ],
240- token_str ,
241- (token .start_line , token .start_col ),
242- (token .end_line , token .end_col ),
243- source_line ,
244- )
188+ else :
189+ yield (
190+ TOKEN_TYPE_MAP [token .type ],
191+ token_str ,
192+ (token .start_line , token .start_col ),
193+ (token .end_line , token .end_col ),
194+ source_line ,
195+ )
245196 prev_token = token
246197
247198 except pytokens .UnexpectedEOF :
0 commit comments