mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-31 07:38:04 +00:00
Add parsing of type alias statements
This commit is contained in:
parent
6980037ad9
commit
f96642aac4
9 changed files with 18961 additions and 18164 deletions
|
@ -154,6 +154,7 @@ fn gen_phf(out_dir: &Path) {
|
|||
.entry("raise", "Tok::Raise")
|
||||
.entry("return", "Tok::Return")
|
||||
.entry("try", "Tok::Try")
|
||||
.entry("type", "Tok::Type")
|
||||
.entry("while", "Tok::While")
|
||||
.entry("with", "Tok::With")
|
||||
.entry("yield", "Tok::Yield")
|
||||
|
|
|
@ -636,6 +636,7 @@ class Foo(A, B):
|
|||
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "all-nodes-with-ranges")]
|
||||
fn test_parse_class_generic_types() {
|
||||
|
@ -894,11 +895,45 @@ except* OSError as e:
|
|||
assert!(parse(source, Mode::Interactive, "<embedded>").is_ok());
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "all-nodes-with-ranges")]
|
||||
fn test_parse_type_declaration() {
|
||||
let source = "\
|
||||
# A non-generic type alias
|
||||
type IntOrStr = int | str
|
||||
|
||||
# A generic type alias
|
||||
type ListOrSet[T] = list[T] | set[T]
|
||||
|
||||
# A type alias that includes a forward reference
|
||||
type AnimalOrVegetable = Animal | \"Vegetable\"
|
||||
|
||||
# A generic self-referential type alias
|
||||
type RecursiveList[T] = T | list[RecursiveList[T]]
|
||||
";
|
||||
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "all-nodes-with-ranges")]
|
||||
fn test_type_as_identifier() {
|
||||
let source = r#"\
|
||||
type = lambda query: query == event
|
||||
print(type(12))
|
||||
"#;
|
||||
|
||||
use crate::lexer::lex;
|
||||
let lexer = lex(source, Mode::Module);
|
||||
println!("tokens {:#?}", lexer.map(|x| x.unwrap().0).collect::<Vec<_>>());
|
||||
|
||||
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "all-nodes-with-ranges")]
|
||||
fn test_match_as_identifier() {
|
||||
let parse_ast = ast::Suite::parse(
|
||||
r#"
|
||||
let source = r#"\
|
||||
match *a + b, c # ((match * a) + b), c
|
||||
match *(a + b), c # (match * (a + b)), c
|
||||
match (*a + b, c) # match ((*(a + b)), c)
|
||||
|
@ -920,11 +955,8 @@ match match:
|
|||
pass
|
||||
match = lambda query: query == event
|
||||
print(match(12))
|
||||
"#,
|
||||
"<test>",
|
||||
)
|
||||
.unwrap();
|
||||
insta::assert_debug_snapshot!(parse_ast);
|
||||
"#;
|
||||
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -86,6 +86,7 @@ SmallStatement: ast::Stmt = {
|
|||
GlobalStatement,
|
||||
NonlocalStatement,
|
||||
AssertStatement,
|
||||
TypeAliasStatement,
|
||||
};
|
||||
|
||||
PassStatement: ast::Stmt = {
|
||||
|
@ -978,6 +979,25 @@ FuncDef: ast::Stmt = {
|
|||
},
|
||||
};
|
||||
|
||||
TypeAliasName: ast::Expr = {
|
||||
<location:@L> <name:Identifier> <end_location:@R> => ast::Expr::Name(
|
||||
ast::ExprName { id: name, ctx: ast::ExprContext::Load, range: (location..end_location).into() },
|
||||
),
|
||||
}
|
||||
|
||||
TypeAliasStatement: ast::Stmt = {
|
||||
<location:@L> "type" <name:TypeAliasName> <type_params:TypeParamList?> "=" <value:Test<"all">> <end_location:@R> => {
|
||||
ast::Stmt::TypeAlias(
|
||||
ast::StmtTypeAlias {
|
||||
name: Box::new(name),
|
||||
value: Box::new(value),
|
||||
type_params: type_params.unwrap_or_default(),
|
||||
range: (location..end_location).into()
|
||||
},
|
||||
)
|
||||
},
|
||||
};
|
||||
|
||||
Parameters: ast::Arguments = {
|
||||
<location:@L> "(" <a: (ParameterList<TypedParameter, StarTypedParameter, DoubleStarTypedParameter>)?> ")" <end_location:@R> =>? {
|
||||
a.as_ref().map(validate_arguments).transpose()?;
|
||||
|
@ -1750,6 +1770,7 @@ extern {
|
|||
"raise" => token::Tok::Raise,
|
||||
"return" => token::Tok::Return,
|
||||
"try" => token::Tok::Try,
|
||||
"type" => token::Tok::Type,
|
||||
"while" => token::Tok::While,
|
||||
"match" => token::Tok::Match,
|
||||
"case" => token::Tok::Case,
|
||||
|
|
36405
parser/src/python.rs
generated
36405
parser/src/python.rs
generated
File diff suppressed because it is too large
Load diff
|
@ -1,24 +1,24 @@
|
|||
---
|
||||
source: parser/src/parser.rs
|
||||
expression: parse_ast
|
||||
expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||
---
|
||||
[
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 1..16,
|
||||
range: 2..17,
|
||||
value: Tuple(
|
||||
ExprTuple {
|
||||
range: 1..16,
|
||||
range: 2..17,
|
||||
elts: [
|
||||
BinOp(
|
||||
ExprBinOp {
|
||||
range: 1..13,
|
||||
range: 2..14,
|
||||
left: BinOp(
|
||||
ExprBinOp {
|
||||
range: 1..9,
|
||||
range: 2..10,
|
||||
left: Name(
|
||||
ExprName {
|
||||
range: 1..6,
|
||||
range: 2..7,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -28,7 +28,7 @@ expression: parse_ast
|
|||
op: Mult,
|
||||
right: Name(
|
||||
ExprName {
|
||||
range: 8..9,
|
||||
range: 9..10,
|
||||
id: Identifier(
|
||||
"a",
|
||||
),
|
||||
|
@ -40,7 +40,7 @@ expression: parse_ast
|
|||
op: Add,
|
||||
right: Name(
|
||||
ExprName {
|
||||
range: 12..13,
|
||||
range: 13..14,
|
||||
id: Identifier(
|
||||
"b",
|
||||
),
|
||||
|
@ -51,7 +51,7 @@ expression: parse_ast
|
|||
),
|
||||
Name(
|
||||
ExprName {
|
||||
range: 15..16,
|
||||
range: 16..17,
|
||||
id: Identifier(
|
||||
"c",
|
||||
),
|
||||
|
@ -66,17 +66,17 @@ expression: parse_ast
|
|||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 42..59,
|
||||
range: 43..60,
|
||||
value: Tuple(
|
||||
ExprTuple {
|
||||
range: 42..59,
|
||||
range: 43..60,
|
||||
elts: [
|
||||
BinOp(
|
||||
ExprBinOp {
|
||||
range: 42..56,
|
||||
range: 43..57,
|
||||
left: Name(
|
||||
ExprName {
|
||||
range: 42..47,
|
||||
range: 43..48,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -86,10 +86,10 @@ expression: parse_ast
|
|||
op: Mult,
|
||||
right: BinOp(
|
||||
ExprBinOp {
|
||||
range: 50..55,
|
||||
range: 51..56,
|
||||
left: Name(
|
||||
ExprName {
|
||||
range: 50..51,
|
||||
range: 51..52,
|
||||
id: Identifier(
|
||||
"a",
|
||||
),
|
||||
|
@ -99,7 +99,7 @@ expression: parse_ast
|
|||
op: Add,
|
||||
right: Name(
|
||||
ExprName {
|
||||
range: 54..55,
|
||||
range: 55..56,
|
||||
id: Identifier(
|
||||
"b",
|
||||
),
|
||||
|
@ -112,7 +112,7 @@ expression: parse_ast
|
|||
),
|
||||
Name(
|
||||
ExprName {
|
||||
range: 58..59,
|
||||
range: 59..60,
|
||||
id: Identifier(
|
||||
"c",
|
||||
),
|
||||
|
@ -127,13 +127,13 @@ expression: parse_ast
|
|||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 85..102,
|
||||
range: 86..103,
|
||||
value: Call(
|
||||
ExprCall {
|
||||
range: 85..102,
|
||||
range: 86..103,
|
||||
func: Name(
|
||||
ExprName {
|
||||
range: 85..90,
|
||||
range: 86..91,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -143,13 +143,13 @@ expression: parse_ast
|
|||
args: [
|
||||
Starred(
|
||||
ExprStarred {
|
||||
range: 92..98,
|
||||
range: 93..99,
|
||||
value: BinOp(
|
||||
ExprBinOp {
|
||||
range: 93..98,
|
||||
range: 94..99,
|
||||
left: Name(
|
||||
ExprName {
|
||||
range: 93..94,
|
||||
range: 94..95,
|
||||
id: Identifier(
|
||||
"a",
|
||||
),
|
||||
|
@ -159,7 +159,7 @@ expression: parse_ast
|
|||
op: Add,
|
||||
right: Name(
|
||||
ExprName {
|
||||
range: 97..98,
|
||||
range: 98..99,
|
||||
id: Identifier(
|
||||
"b",
|
||||
),
|
||||
|
@ -173,7 +173,7 @@ expression: parse_ast
|
|||
),
|
||||
Name(
|
||||
ExprName {
|
||||
range: 100..101,
|
||||
range: 101..102,
|
||||
id: Identifier(
|
||||
"c",
|
||||
),
|
||||
|
@ -188,16 +188,16 @@ expression: parse_ast
|
|||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 129..145,
|
||||
range: 130..146,
|
||||
value: BinOp(
|
||||
ExprBinOp {
|
||||
range: 129..145,
|
||||
range: 130..146,
|
||||
left: BinOp(
|
||||
ExprBinOp {
|
||||
range: 129..141,
|
||||
range: 130..142,
|
||||
left: Name(
|
||||
ExprName {
|
||||
range: 129..134,
|
||||
range: 130..135,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -207,10 +207,10 @@ expression: parse_ast
|
|||
op: Sub,
|
||||
right: BinOp(
|
||||
ExprBinOp {
|
||||
range: 136..141,
|
||||
range: 137..142,
|
||||
left: Name(
|
||||
ExprName {
|
||||
range: 136..137,
|
||||
range: 137..138,
|
||||
id: Identifier(
|
||||
"a",
|
||||
),
|
||||
|
@ -220,7 +220,7 @@ expression: parse_ast
|
|||
op: Mult,
|
||||
right: Name(
|
||||
ExprName {
|
||||
range: 140..141,
|
||||
range: 141..142,
|
||||
id: Identifier(
|
||||
"b",
|
||||
),
|
||||
|
@ -234,7 +234,7 @@ expression: parse_ast
|
|||
op: Add,
|
||||
right: Name(
|
||||
ExprName {
|
||||
range: 144..145,
|
||||
range: 145..146,
|
||||
id: Identifier(
|
||||
"c",
|
||||
),
|
||||
|
@ -247,16 +247,16 @@ expression: parse_ast
|
|||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 172..190,
|
||||
range: 173..191,
|
||||
value: BinOp(
|
||||
ExprBinOp {
|
||||
range: 172..190,
|
||||
range: 173..191,
|
||||
left: BinOp(
|
||||
ExprBinOp {
|
||||
range: 172..186,
|
||||
range: 173..187,
|
||||
left: Name(
|
||||
ExprName {
|
||||
range: 172..177,
|
||||
range: 173..178,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -266,10 +266,10 @@ expression: parse_ast
|
|||
op: Sub,
|
||||
right: BinOp(
|
||||
ExprBinOp {
|
||||
range: 180..185,
|
||||
range: 181..186,
|
||||
left: Name(
|
||||
ExprName {
|
||||
range: 180..181,
|
||||
range: 181..182,
|
||||
id: Identifier(
|
||||
"a",
|
||||
),
|
||||
|
@ -279,7 +279,7 @@ expression: parse_ast
|
|||
op: Mult,
|
||||
right: Name(
|
||||
ExprName {
|
||||
range: 184..185,
|
||||
range: 185..186,
|
||||
id: Identifier(
|
||||
"b",
|
||||
),
|
||||
|
@ -293,7 +293,7 @@ expression: parse_ast
|
|||
op: Add,
|
||||
right: Name(
|
||||
ExprName {
|
||||
range: 189..190,
|
||||
range: 190..191,
|
||||
id: Identifier(
|
||||
"c",
|
||||
),
|
||||
|
@ -306,19 +306,19 @@ expression: parse_ast
|
|||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 217..235,
|
||||
range: 218..236,
|
||||
value: BinOp(
|
||||
ExprBinOp {
|
||||
range: 217..235,
|
||||
range: 218..236,
|
||||
left: BinOp(
|
||||
ExprBinOp {
|
||||
range: 217..231,
|
||||
range: 218..232,
|
||||
left: Call(
|
||||
ExprCall {
|
||||
range: 217..227,
|
||||
range: 218..228,
|
||||
func: Name(
|
||||
ExprName {
|
||||
range: 217..222,
|
||||
range: 218..223,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -328,11 +328,11 @@ expression: parse_ast
|
|||
args: [
|
||||
UnaryOp(
|
||||
ExprUnaryOp {
|
||||
range: 224..226,
|
||||
range: 225..227,
|
||||
op: USub,
|
||||
operand: Name(
|
||||
ExprName {
|
||||
range: 225..226,
|
||||
range: 226..227,
|
||||
id: Identifier(
|
||||
"a",
|
||||
),
|
||||
|
@ -348,7 +348,7 @@ expression: parse_ast
|
|||
op: Mult,
|
||||
right: Name(
|
||||
ExprName {
|
||||
range: 230..231,
|
||||
range: 231..232,
|
||||
id: Identifier(
|
||||
"b",
|
||||
),
|
||||
|
@ -360,7 +360,7 @@ expression: parse_ast
|
|||
op: Add,
|
||||
right: Name(
|
||||
ExprName {
|
||||
range: 234..235,
|
||||
range: 235..236,
|
||||
id: Identifier(
|
||||
"c",
|
||||
),
|
||||
|
@ -373,16 +373,16 @@ expression: parse_ast
|
|||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 263..273,
|
||||
range: 264..274,
|
||||
value: Attribute(
|
||||
ExprAttribute {
|
||||
range: 263..273,
|
||||
range: 264..274,
|
||||
value: Call(
|
||||
ExprCall {
|
||||
range: 263..271,
|
||||
range: 264..272,
|
||||
func: Name(
|
||||
ExprName {
|
||||
range: 263..268,
|
||||
range: 264..269,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -403,16 +403,16 @@ expression: parse_ast
|
|||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 290..302,
|
||||
range: 291..303,
|
||||
value: Attribute(
|
||||
ExprAttribute {
|
||||
range: 290..302,
|
||||
range: 291..303,
|
||||
value: Call(
|
||||
ExprCall {
|
||||
range: 290..300,
|
||||
range: 291..301,
|
||||
func: Name(
|
||||
ExprName {
|
||||
range: 290..295,
|
||||
range: 291..296,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -422,7 +422,7 @@ expression: parse_ast
|
|||
args: [
|
||||
Tuple(
|
||||
ExprTuple {
|
||||
range: 297..299,
|
||||
range: 298..300,
|
||||
elts: [],
|
||||
ctx: Load,
|
||||
},
|
||||
|
@ -441,16 +441,16 @@ expression: parse_ast
|
|||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 321..334,
|
||||
range: 322..335,
|
||||
value: Attribute(
|
||||
ExprAttribute {
|
||||
range: 321..334,
|
||||
range: 322..335,
|
||||
value: Call(
|
||||
ExprCall {
|
||||
range: 321..332,
|
||||
range: 322..333,
|
||||
func: Name(
|
||||
ExprName {
|
||||
range: 321..326,
|
||||
range: 322..327,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -460,7 +460,7 @@ expression: parse_ast
|
|||
args: [
|
||||
Tuple(
|
||||
ExprTuple {
|
||||
range: 328..330,
|
||||
range: 329..331,
|
||||
elts: [],
|
||||
ctx: Load,
|
||||
},
|
||||
|
@ -479,16 +479,16 @@ expression: parse_ast
|
|||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 353..364,
|
||||
range: 354..365,
|
||||
value: Attribute(
|
||||
ExprAttribute {
|
||||
range: 353..364,
|
||||
range: 354..365,
|
||||
value: Subscript(
|
||||
ExprSubscript {
|
||||
range: 353..362,
|
||||
range: 354..363,
|
||||
value: Name(
|
||||
ExprName {
|
||||
range: 353..358,
|
||||
range: 354..359,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -497,7 +497,7 @@ expression: parse_ast
|
|||
),
|
||||
slice: Name(
|
||||
ExprName {
|
||||
range: 360..361,
|
||||
range: 361..362,
|
||||
id: Identifier(
|
||||
"a",
|
||||
),
|
||||
|
@ -517,16 +517,16 @@ expression: parse_ast
|
|||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 382..394,
|
||||
range: 383..395,
|
||||
value: Attribute(
|
||||
ExprAttribute {
|
||||
range: 382..394,
|
||||
range: 383..395,
|
||||
value: Subscript(
|
||||
ExprSubscript {
|
||||
range: 382..392,
|
||||
range: 383..393,
|
||||
value: Name(
|
||||
ExprName {
|
||||
range: 382..387,
|
||||
range: 383..388,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -535,11 +535,11 @@ expression: parse_ast
|
|||
),
|
||||
slice: Tuple(
|
||||
ExprTuple {
|
||||
range: 389..391,
|
||||
range: 390..392,
|
||||
elts: [
|
||||
Name(
|
||||
ExprName {
|
||||
range: 389..390,
|
||||
range: 390..391,
|
||||
id: Identifier(
|
||||
"a",
|
||||
),
|
||||
|
@ -563,16 +563,16 @@ expression: parse_ast
|
|||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 435..449,
|
||||
range: 436..450,
|
||||
value: Attribute(
|
||||
ExprAttribute {
|
||||
range: 435..449,
|
||||
range: 436..450,
|
||||
value: Subscript(
|
||||
ExprSubscript {
|
||||
range: 435..447,
|
||||
range: 436..448,
|
||||
value: Name(
|
||||
ExprName {
|
||||
range: 435..440,
|
||||
range: 436..441,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -581,11 +581,11 @@ expression: parse_ast
|
|||
),
|
||||
slice: Tuple(
|
||||
ExprTuple {
|
||||
range: 442..446,
|
||||
range: 443..447,
|
||||
elts: [
|
||||
Name(
|
||||
ExprName {
|
||||
range: 443..444,
|
||||
range: 444..445,
|
||||
id: Identifier(
|
||||
"a",
|
||||
),
|
||||
|
@ -609,16 +609,16 @@ expression: parse_ast
|
|||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 470..487,
|
||||
range: 471..488,
|
||||
value: Subscript(
|
||||
ExprSubscript {
|
||||
range: 470..487,
|
||||
range: 471..488,
|
||||
value: Call(
|
||||
ExprCall {
|
||||
range: 470..477,
|
||||
range: 471..478,
|
||||
func: Name(
|
||||
ExprName {
|
||||
range: 470..475,
|
||||
range: 471..476,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -631,11 +631,11 @@ expression: parse_ast
|
|||
),
|
||||
slice: Slice(
|
||||
ExprSlice {
|
||||
range: 478..486,
|
||||
range: 479..487,
|
||||
lower: Some(
|
||||
Name(
|
||||
ExprName {
|
||||
range: 478..479,
|
||||
range: 479..480,
|
||||
id: Identifier(
|
||||
"a",
|
||||
),
|
||||
|
@ -646,7 +646,7 @@ expression: parse_ast
|
|||
upper: Some(
|
||||
Name(
|
||||
ExprName {
|
||||
range: 485..486,
|
||||
range: 486..487,
|
||||
id: Identifier(
|
||||
"b",
|
||||
),
|
||||
|
@ -664,13 +664,13 @@ expression: parse_ast
|
|||
),
|
||||
If(
|
||||
StmtIf {
|
||||
range: 507..526,
|
||||
range: 508..527,
|
||||
test: NamedExpr(
|
||||
ExprNamedExpr {
|
||||
range: 510..520,
|
||||
range: 511..521,
|
||||
target: Name(
|
||||
ExprName {
|
||||
range: 510..515,
|
||||
range: 511..516,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -679,7 +679,7 @@ expression: parse_ast
|
|||
),
|
||||
value: Constant(
|
||||
ExprConstant {
|
||||
range: 519..520,
|
||||
range: 520..521,
|
||||
value: Int(
|
||||
1,
|
||||
),
|
||||
|
@ -691,7 +691,7 @@ expression: parse_ast
|
|||
body: [
|
||||
Pass(
|
||||
StmtPass {
|
||||
range: 522..526,
|
||||
range: 523..527,
|
||||
},
|
||||
),
|
||||
],
|
||||
|
@ -700,10 +700,10 @@ expression: parse_ast
|
|||
),
|
||||
Match(
|
||||
StmtMatch {
|
||||
range: 527..581,
|
||||
range: 528..582,
|
||||
subject: Name(
|
||||
ExprName {
|
||||
range: 533..538,
|
||||
range: 534..539,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -712,13 +712,13 @@ expression: parse_ast
|
|||
),
|
||||
cases: [
|
||||
MatchCase {
|
||||
range: 544..556,
|
||||
range: 545..557,
|
||||
pattern: MatchValue(
|
||||
PatternMatchValue {
|
||||
range: 549..550,
|
||||
range: 550..551,
|
||||
value: Constant(
|
||||
ExprConstant {
|
||||
range: 549..550,
|
||||
range: 550..551,
|
||||
value: Int(
|
||||
1,
|
||||
),
|
||||
|
@ -731,19 +731,19 @@ expression: parse_ast
|
|||
body: [
|
||||
Pass(
|
||||
StmtPass {
|
||||
range: 552..556,
|
||||
range: 553..557,
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
MatchCase {
|
||||
range: 561..581,
|
||||
range: 562..582,
|
||||
pattern: MatchValue(
|
||||
PatternMatchValue {
|
||||
range: 566..567,
|
||||
range: 567..568,
|
||||
value: Constant(
|
||||
ExprConstant {
|
||||
range: 566..567,
|
||||
range: 567..568,
|
||||
value: Int(
|
||||
2,
|
||||
),
|
||||
|
@ -756,7 +756,7 @@ expression: parse_ast
|
|||
body: [
|
||||
Pass(
|
||||
StmtPass {
|
||||
range: 577..581,
|
||||
range: 578..582,
|
||||
},
|
||||
),
|
||||
],
|
||||
|
@ -766,11 +766,11 @@ expression: parse_ast
|
|||
),
|
||||
Assign(
|
||||
StmtAssign {
|
||||
range: 582..618,
|
||||
range: 583..619,
|
||||
targets: [
|
||||
Name(
|
||||
ExprName {
|
||||
range: 582..587,
|
||||
range: 583..588,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -780,15 +780,15 @@ expression: parse_ast
|
|||
],
|
||||
value: Lambda(
|
||||
ExprLambda {
|
||||
range: 590..618,
|
||||
range: 591..619,
|
||||
args: Arguments {
|
||||
range: 597..602,
|
||||
range: 598..603,
|
||||
posonlyargs: [],
|
||||
args: [
|
||||
ArgWithDefault {
|
||||
range: 597..602,
|
||||
range: 598..603,
|
||||
def: Arg {
|
||||
range: 597..602,
|
||||
range: 598..603,
|
||||
arg: Identifier(
|
||||
"query",
|
||||
),
|
||||
|
@ -804,10 +804,10 @@ expression: parse_ast
|
|||
},
|
||||
body: Compare(
|
||||
ExprCompare {
|
||||
range: 604..618,
|
||||
range: 605..619,
|
||||
left: Name(
|
||||
ExprName {
|
||||
range: 604..609,
|
||||
range: 605..610,
|
||||
id: Identifier(
|
||||
"query",
|
||||
),
|
||||
|
@ -820,7 +820,7 @@ expression: parse_ast
|
|||
comparators: [
|
||||
Name(
|
||||
ExprName {
|
||||
range: 613..618,
|
||||
range: 614..619,
|
||||
id: Identifier(
|
||||
"event",
|
||||
),
|
||||
|
@ -837,13 +837,13 @@ expression: parse_ast
|
|||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 619..635,
|
||||
range: 620..636,
|
||||
value: Call(
|
||||
ExprCall {
|
||||
range: 619..635,
|
||||
range: 620..636,
|
||||
func: Name(
|
||||
ExprName {
|
||||
range: 619..624,
|
||||
range: 620..625,
|
||||
id: Identifier(
|
||||
"print",
|
||||
),
|
||||
|
@ -853,10 +853,10 @@ expression: parse_ast
|
|||
args: [
|
||||
Call(
|
||||
ExprCall {
|
||||
range: 625..634,
|
||||
range: 626..635,
|
||||
func: Name(
|
||||
ExprName {
|
||||
range: 625..630,
|
||||
range: 626..631,
|
||||
id: Identifier(
|
||||
"match",
|
||||
),
|
||||
|
@ -866,7 +866,7 @@ expression: parse_ast
|
|||
args: [
|
||||
Constant(
|
||||
ExprConstant {
|
||||
range: 631..633,
|
||||
range: 632..634,
|
||||
value: Int(
|
||||
12,
|
||||
),
|
||||
|
|
242
parser/src/snapshots/rustpython_parser__parser__tests__parse_type_declaration.snap
generated
Normal file
242
parser/src/snapshots/rustpython_parser__parser__tests__parse_type_declaration.snap
generated
Normal file
|
@ -0,0 +1,242 @@
|
|||
---
|
||||
source: parser/src/parser.rs
|
||||
expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||
---
|
||||
[
|
||||
TypeAlias(
|
||||
StmtTypeAlias {
|
||||
range: 27..52,
|
||||
name: Name(
|
||||
ExprName {
|
||||
range: 32..40,
|
||||
id: Identifier(
|
||||
"IntOrStr",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
type_params: [],
|
||||
value: BinOp(
|
||||
ExprBinOp {
|
||||
range: 43..52,
|
||||
left: Name(
|
||||
ExprName {
|
||||
range: 43..46,
|
||||
id: Identifier(
|
||||
"int",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
op: BitOr,
|
||||
right: Name(
|
||||
ExprName {
|
||||
range: 49..52,
|
||||
id: Identifier(
|
||||
"str",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
TypeAlias(
|
||||
StmtTypeAlias {
|
||||
range: 77..113,
|
||||
name: Name(
|
||||
ExprName {
|
||||
range: 82..91,
|
||||
id: Identifier(
|
||||
"ListOrSet",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
type_params: [
|
||||
TypeVar(
|
||||
TypeParamTypeVar {
|
||||
range: 92..93,
|
||||
name: Identifier(
|
||||
"T",
|
||||
),
|
||||
bound: None,
|
||||
},
|
||||
),
|
||||
],
|
||||
value: BinOp(
|
||||
ExprBinOp {
|
||||
range: 97..113,
|
||||
left: Subscript(
|
||||
ExprSubscript {
|
||||
range: 97..104,
|
||||
value: Name(
|
||||
ExprName {
|
||||
range: 97..101,
|
||||
id: Identifier(
|
||||
"list",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
slice: Name(
|
||||
ExprName {
|
||||
range: 102..103,
|
||||
id: Identifier(
|
||||
"T",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
op: BitOr,
|
||||
right: Subscript(
|
||||
ExprSubscript {
|
||||
range: 107..113,
|
||||
value: Name(
|
||||
ExprName {
|
||||
range: 107..110,
|
||||
id: Identifier(
|
||||
"set",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
slice: Name(
|
||||
ExprName {
|
||||
range: 111..112,
|
||||
id: Identifier(
|
||||
"T",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
TypeAlias(
|
||||
StmtTypeAlias {
|
||||
range: 164..209,
|
||||
name: Name(
|
||||
ExprName {
|
||||
range: 169..186,
|
||||
id: Identifier(
|
||||
"AnimalOrVegetable",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
type_params: [],
|
||||
value: BinOp(
|
||||
ExprBinOp {
|
||||
range: 189..209,
|
||||
left: Name(
|
||||
ExprName {
|
||||
range: 189..195,
|
||||
id: Identifier(
|
||||
"Animal",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
op: BitOr,
|
||||
right: Constant(
|
||||
ExprConstant {
|
||||
range: 198..209,
|
||||
value: Str(
|
||||
"Vegetable",
|
||||
),
|
||||
kind: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
TypeAlias(
|
||||
StmtTypeAlias {
|
||||
range: 251..301,
|
||||
name: Name(
|
||||
ExprName {
|
||||
range: 256..269,
|
||||
id: Identifier(
|
||||
"RecursiveList",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
type_params: [
|
||||
TypeVar(
|
||||
TypeParamTypeVar {
|
||||
range: 270..271,
|
||||
name: Identifier(
|
||||
"T",
|
||||
),
|
||||
bound: None,
|
||||
},
|
||||
),
|
||||
],
|
||||
value: BinOp(
|
||||
ExprBinOp {
|
||||
range: 275..301,
|
||||
left: Name(
|
||||
ExprName {
|
||||
range: 275..276,
|
||||
id: Identifier(
|
||||
"T",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
op: BitOr,
|
||||
right: Subscript(
|
||||
ExprSubscript {
|
||||
range: 279..301,
|
||||
value: Name(
|
||||
ExprName {
|
||||
range: 279..283,
|
||||
id: Identifier(
|
||||
"list",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
slice: Subscript(
|
||||
ExprSubscript {
|
||||
range: 284..300,
|
||||
value: Name(
|
||||
ExprName {
|
||||
range: 284..297,
|
||||
id: Identifier(
|
||||
"RecursiveList",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
slice: Name(
|
||||
ExprName {
|
||||
range: 298..299,
|
||||
id: Identifier(
|
||||
"T",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
]
|
125
parser/src/snapshots/rustpython_parser__parser__tests__type_as_identifier.snap
generated
Normal file
125
parser/src/snapshots/rustpython_parser__parser__tests__type_as_identifier.snap
generated
Normal file
|
@ -0,0 +1,125 @@
|
|||
---
|
||||
source: parser/src/parser.rs
|
||||
expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||
---
|
||||
[
|
||||
Assign(
|
||||
StmtAssign {
|
||||
range: 2..37,
|
||||
targets: [
|
||||
Name(
|
||||
ExprName {
|
||||
range: 2..6,
|
||||
id: Identifier(
|
||||
"type",
|
||||
),
|
||||
ctx: Store,
|
||||
},
|
||||
),
|
||||
],
|
||||
value: Lambda(
|
||||
ExprLambda {
|
||||
range: 9..37,
|
||||
args: Arguments {
|
||||
range: 16..21,
|
||||
posonlyargs: [],
|
||||
args: [
|
||||
ArgWithDefault {
|
||||
range: 16..21,
|
||||
def: Arg {
|
||||
range: 16..21,
|
||||
arg: Identifier(
|
||||
"query",
|
||||
),
|
||||
annotation: None,
|
||||
type_comment: None,
|
||||
},
|
||||
default: None,
|
||||
},
|
||||
],
|
||||
vararg: None,
|
||||
kwonlyargs: [],
|
||||
kwarg: None,
|
||||
},
|
||||
body: Compare(
|
||||
ExprCompare {
|
||||
range: 23..37,
|
||||
left: Name(
|
||||
ExprName {
|
||||
range: 23..28,
|
||||
id: Identifier(
|
||||
"query",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
ops: [
|
||||
Eq,
|
||||
],
|
||||
comparators: [
|
||||
Name(
|
||||
ExprName {
|
||||
range: 32..37,
|
||||
id: Identifier(
|
||||
"event",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
type_comment: None,
|
||||
},
|
||||
),
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 38..53,
|
||||
value: Call(
|
||||
ExprCall {
|
||||
range: 38..53,
|
||||
func: Name(
|
||||
ExprName {
|
||||
range: 38..43,
|
||||
id: Identifier(
|
||||
"print",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
args: [
|
||||
Call(
|
||||
ExprCall {
|
||||
range: 44..52,
|
||||
func: Name(
|
||||
ExprName {
|
||||
range: 44..48,
|
||||
id: Identifier(
|
||||
"type",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
args: [
|
||||
Constant(
|
||||
ExprConstant {
|
||||
range: 49..51,
|
||||
value: Int(
|
||||
12,
|
||||
),
|
||||
kind: None,
|
||||
},
|
||||
),
|
||||
],
|
||||
keywords: [],
|
||||
},
|
||||
),
|
||||
],
|
||||
keywords: [],
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
]
|
|
@ -2,14 +2,17 @@ use crate::{lexer::LexResult, token::Tok, Mode};
|
|||
use itertools::{Itertools, MultiPeek};
|
||||
|
||||
/// An [`Iterator`] that transforms a token stream to accommodate soft keywords (namely, `match`
|
||||
/// and `case`).
|
||||
/// `case`, and `type`).
|
||||
///
|
||||
/// [PEP 634](https://www.python.org/dev/peps/pep-0634/) introduced the `match` and `case` keywords
|
||||
/// as soft keywords, meaning that they can be used as identifiers (e.g., variable names) in certain
|
||||
/// contexts.
|
||||
///
|
||||
/// Later, [PEP 695](https://peps.python.org/pep-0695/#generic-type-alias) introduced the `type`
|
||||
/// soft keyword.
|
||||
///
|
||||
/// This function modifies a token stream to accommodate this change. In particular, it replaces
|
||||
/// `match` and `case` tokens with `identifier` tokens if they are used as identifiers.
|
||||
/// soft keyword tokens with `identifier` tokens if they are used as identifiers.
|
||||
///
|
||||
/// Handling soft keywords in this intermediary pass allows us to simplify both the lexer and
|
||||
/// parser, as neither of them need to be aware of soft keywords.
|
||||
|
@ -43,14 +46,16 @@ where
|
|||
fn next(&mut self) -> Option<LexResult> {
|
||||
let mut next = self.underlying.next();
|
||||
if let Some(Ok((tok, range))) = next.as_ref() {
|
||||
// If the token is a `match` or `case` token, check if it's used as an identifier.
|
||||
// We assume every `match` or `case` is an identifier unless both of the following
|
||||
// conditions are met:
|
||||
// If the token is a soft keyword e.g. `type`, `match`, or `case`, check if it's
|
||||
// used as an identifier. We assume every soft keyword use is an identifier unless
|
||||
// a heuristic is met.
|
||||
|
||||
// For `match` and `case`, all of the following conditions must be met:
|
||||
// 1. The token is at the start of a logical line.
|
||||
// 2. The logical line contains a top-level colon (that is, a colon that is not nested
|
||||
// inside a parenthesized expression, list, or dictionary).
|
||||
// 3. The top-level colon is not the immediate sibling of a `match` or `case` token.
|
||||
// (This is to avoid treating `match` and `case` as identifiers when annotated with
|
||||
// 3. The top-level colon is not the immediate sibling of a soft keyword token.
|
||||
// (This is to avoid treating soft keywords as identifiers when annotated with
|
||||
// type hints.)
|
||||
if matches!(tok, Tok::Match | Tok::Case) {
|
||||
if !self.start_of_line {
|
||||
|
@ -82,6 +87,33 @@ where
|
|||
}
|
||||
}
|
||||
}
|
||||
// For `type` all of the following conditions must be met:
|
||||
// 1. The token is at the start of a logical line.
|
||||
// 2. The type token is followed by a name token.
|
||||
// 3. The name token is followed by an equality token.
|
||||
else if matches!(tok, Tok::Type) {
|
||||
if !self.start_of_line {
|
||||
next = Some(Ok((soft_to_name(tok), *range)));
|
||||
} else {
|
||||
let mut nesting = 0;
|
||||
let mut seen_name = false;
|
||||
let mut seen_equal = false;
|
||||
while let Some(Ok((tok, _))) = self.underlying.peek() {
|
||||
match tok {
|
||||
Tok::Newline => break,
|
||||
Tok::Name { .. } 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,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if !(seen_name && seen_equal) {
|
||||
next = Some(Ok((soft_to_name(tok), *range)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
self.start_of_line = next.as_ref().map_or(false, |lex_result| {
|
||||
|
@ -111,6 +143,7 @@ fn soft_to_name(tok: &Tok) -> Tok {
|
|||
let name = match tok {
|
||||
Tok::Match => "match",
|
||||
Tok::Case => "case",
|
||||
Tok::Type => "type",
|
||||
_ => unreachable!("other tokens never reach here"),
|
||||
};
|
||||
Tok::Name {
|
||||
|
|
|
@ -188,6 +188,7 @@ pub enum Tok {
|
|||
Try,
|
||||
While,
|
||||
Match,
|
||||
Type,
|
||||
Case,
|
||||
With,
|
||||
Yield,
|
||||
|
@ -315,6 +316,7 @@ impl fmt::Display for Tok {
|
|||
Try => f.write_str("'try'"),
|
||||
While => f.write_str("'while'"),
|
||||
Match => f.write_str("'match'"),
|
||||
Type => f.write_str("'type'"),
|
||||
Case => f.write_str("'case'"),
|
||||
With => f.write_str("'with'"),
|
||||
Yield => f.write_str("'yield'"),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue