diff --git a/parser/src/parser.rs b/parser/src/parser.rs index c440272..21822fa 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -931,16 +931,12 @@ type()[a: b] # (type())[a: b] if type := 1: pass type = lambda query: query == event +type type = int | str print(type(12)) +type(type) +type match = int # other soft keyword +type case = int "#; - - use crate::lexer::lex; - let lexer = lex(source, Mode::Module); - println!( - "tokens {:#?}", - lexer.map(|x| x.unwrap().0).collect::>() - ); - insta::assert_debug_snapshot!(ast::Suite::parse(source, "").unwrap()); } diff --git a/parser/src/snapshots/rustpython_parser__parser__tests__type_as_identifier.snap b/parser/src/snapshots/rustpython_parser__parser__tests__type_as_identifier.snap index f7ca880..b9ddd03 100644 --- a/parser/src/snapshots/rustpython_parser__parser__tests__type_as_identifier.snap +++ b/parser/src/snapshots/rustpython_parser__parser__tests__type_as_identifier.snap @@ -769,15 +769,54 @@ expression: "ast::Suite::parse(source, \"\").unwrap()" type_comment: None, }, ), + TypeAlias( + StmtTypeAlias { + range: 536..557, + name: Name( + ExprName { + range: 541..545, + id: Identifier( + "type", + ), + ctx: Load, + }, + ), + type_params: [], + value: BinOp( + ExprBinOp { + range: 548..557, + left: Name( + ExprName { + range: 548..551, + id: Identifier( + "int", + ), + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 554..557, + id: Identifier( + "str", + ), + ctx: Load, + }, + ), + }, + ), + }, + ), Expr( StmtExpr { - range: 536..551, + range: 558..573, value: Call( ExprCall { - range: 536..551, + range: 558..573, func: Name( ExprName { - range: 536..541, + range: 558..563, id: Identifier( "print", ), @@ -787,10 +826,10 @@ expression: "ast::Suite::parse(source, \"\").unwrap()" args: [ Call( ExprCall { - range: 542..550, + range: 564..572, func: Name( ExprName { - range: 542..546, + range: 564..568, id: Identifier( "type", ), @@ -800,7 +839,7 @@ expression: "ast::Suite::parse(source, \"\").unwrap()" args: [ Constant( ExprConstant { - range: 547..549, + range: 569..571, value: Int( 12, ), @@ -817,4 +856,83 @@ expression: "ast::Suite::parse(source, \"\").unwrap()" ), }, ), + Expr( + StmtExpr { + range: 574..584, + value: Call( + ExprCall { + range: 574..584, + func: Name( + ExprName { + range: 574..578, + id: Identifier( + "type", + ), + ctx: Load, + }, + ), + args: [ + Name( + ExprName { + range: 579..583, + id: Identifier( + "type", + ), + ctx: Load, + }, + ), + ], + keywords: [], + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 585..601, + name: Name( + ExprName { + range: 590..595, + id: Identifier( + "match", + ), + ctx: Load, + }, + ), + type_params: [], + value: Name( + ExprName { + range: 598..601, + id: Identifier( + "int", + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 624..639, + name: Name( + ExprName { + range: 629..633, + id: Identifier( + "case", + ), + ctx: Load, + }, + ), + type_params: [], + value: Name( + ExprName { + range: 636..639, + id: Identifier( + "int", + ), + ctx: Load, + }, + ), + }, + ), ] diff --git a/parser/src/soft_keywords.rs b/parser/src/soft_keywords.rs index 7507700..bcb8680 100644 --- a/parser/src/soft_keywords.rs +++ b/parser/src/soft_keywords.rs @@ -102,6 +102,9 @@ where match tok { Tok::Newline => break, Tok::Name { .. } if nesting == 0 => seen_name = true, + // We treat a soft keyword token following a type token as a + // name to support cases like `type type = int` or `type match = int` + Tok::Type | Tok::Match | Tok::Case if nesting == 0 => seen_name = true, Tok::Equal if nesting == 0 && seen_name => seen_equal = true, Tok::Lpar | Tok::Lsqb | Tok::Lbrace => nesting += 1, Tok::Rpar | Tok::Rsqb | Tok::Rbrace => nesting -= 1,