Parse type parameters in class definitions

This commit is contained in:
Zanie
2023-07-12 10:24:43 -05:00
committed by Zanie Blue
parent c31b58eb39
commit ed7acfe477
10 changed files with 24406 additions and 22686 deletions

View File

@@ -1133,13 +1133,12 @@ KwargParameter<ArgType>: Option<Box<ast::Arg>> = {
};
ClassDef: ast::Stmt = {
<location:@L> <decorator_list:Decorator*> "class" <name:Identifier> <a:("(" ArgumentList ")")?> ":" <body:Suite> => {
<decorator_list:Decorator*> <location:@L> "class" <name:Identifier> <type_params:TypeParamList?> <a:("(" ArgumentList ")")?> ":" <body:Suite> => {
let (bases, keywords) = match a {
Some((_, arg, _)) => (arg.args, arg.keywords),
None => (vec![], vec![]),
};
let end_location = body.last().unwrap().end();
let type_params = Vec::new();
ast::Stmt::ClassDef(
ast::StmtClassDef {
name,
@@ -1147,13 +1146,38 @@ ClassDef: ast::Stmt = {
keywords,
body,
decorator_list,
type_params,
type_params: type_params.unwrap_or_default(),
range: (location..end_location).into()
},
)
},
};
TypeParamList: Vec<ast::TypeParam> = {
<location:@L> "[" <vars:OneOrMore<TypeParam>> ","? "]" <end_location:@R> => {
vars
}
};
TypeParam: ast::TypeParam = {
<location:@L> <name:Identifier> <bound:(":" <Test<"all">>)?> <end_location:@R> => {
ast::TypeParam::TypeVar(
ast::TypeParamTypeVar { name, bound: bound.map(Box::new), range: (location..end_location).into() }
)
},
<location:@L> "*" <name:Identifier> <end_location:@R> => {
ast::TypeParam::TypeVarTuple(
ast::TypeParamTypeVarTuple { name, range: (location..end_location).into() }
)
},
<location:@L> "**" <name:Identifier> <end_location:@R> => {
ast::TypeParam::ParamSpec(
ast::TypeParamParamSpec { name, range: (location..end_location).into() }
)
}
};
// Decorators:
Decorator: ast::Decorator = {
<location:@L> "@" <p:NamedExpressionTest> <end_location:@R> "\n" => {