Skip to content
This repository was archived by the owner on Jul 27, 2023. It is now read-only.

Merge upstream changes for PEP695 support #27

Merged
merged 13 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 126 additions & 2 deletions ast/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub enum Ast {
MatchCase(MatchCase),
Pattern(Pattern),
TypeIgnore(TypeIgnore),
TypeParam(TypeParam),
Decorator(Decorator),
}
impl Node for Ast {
Expand Down Expand Up @@ -141,6 +142,12 @@ impl From<TypeIgnore> for Ast {
}
}

impl From<TypeParam> for Ast {
fn from(node: TypeParam) -> Self {
Ast::TypeParam(node)
}
}

impl From<Decorator> for Ast {
fn from(node: Decorator) -> Self {
Ast::Decorator(node)
Expand Down Expand Up @@ -270,6 +277,8 @@ pub enum Stmt {
AugAssign(StmtAugAssign),
#[is(name = "ann_assign_stmt")]
AnnAssign(StmtAnnAssign),
#[is(name = "type_alias_stmt")]
TypeAlias(StmtTypeAlias),
#[is(name = "for_stmt")]
For(StmtFor),
#[is(name = "async_for_stmt")]
Expand Down Expand Up @@ -319,6 +328,7 @@ pub struct StmtFunctionDef {
pub body: Vec<Stmt>,
pub decorator_list: Vec<Decorator>,
pub returns: Option<Box<Expr>>,
pub type_params: Vec<TypeParam>,
pub type_comment: Option<String>,
}

Expand All @@ -330,6 +340,7 @@ impl Node for StmtFunctionDef {
"body",
"decorator_list",
"returns",
"type_params",
"type_comment",
];
}
Expand All @@ -353,6 +364,7 @@ pub struct StmtAsyncFunctionDef {
pub body: Vec<Stmt>,
pub decorator_list: Vec<Decorator>,
pub returns: Option<Box<Expr>>,
pub type_params: Vec<TypeParam>,
pub type_comment: Option<String>,
}

Expand All @@ -364,6 +376,7 @@ impl Node for StmtAsyncFunctionDef {
"body",
"decorator_list",
"returns",
"type_params",
"type_comment",
];
}
Expand All @@ -386,13 +399,20 @@ pub struct StmtClassDef {
pub bases: Vec<Expr>,
pub keywords: Vec<Keyword>,
pub body: Vec<Stmt>,
pub type_params: Vec<TypeParam>,
pub decorator_list: Vec<Decorator>,
}

impl Node for StmtClassDef {
const NAME: &'static str = "ClassDef";
const FIELD_NAMES: &'static [&'static str] =
&["name", "bases", "keywords", "body", "decorator_list"];
const FIELD_NAMES: &'static [&'static str] = &[
"name",
"bases",
"keywords",
"body",
"decorator_list",
"type_params",
];
}
impl From<StmtClassDef> for Stmt {
fn from(payload: StmtClassDef) -> Self {
Expand Down Expand Up @@ -449,6 +469,30 @@ impl From<StmtDelete> for Ast {
}
}

/// See also [TypeAlias](https://docs.python.org/3/library/ast.html#ast.TypeAlias)
#[derive(Clone, Debug, PartialEq)]
pub struct StmtTypeAlias {
pub range: TextRange,
pub name: Box<Expr>,
pub type_params: Vec<TypeParam>,
pub value: Box<Expr>,
}

impl Node for StmtTypeAlias {
const NAME: &'static str = "TypeAlias";
const FIELD_NAMES: &'static [&'static str] = &["name", "type_params", "value"];
}
impl From<StmtTypeAlias> for Stmt {
fn from(payload: StmtTypeAlias) -> Self {
Stmt::TypeAlias(payload)
}
}
impl From<StmtTypeAlias> for Ast {
fn from(payload: StmtTypeAlias) -> Self {
Stmt::from(payload).into()
}
}

/// See also [Assign](https://docs.python.org/3/library/ast.html#ast.Assign)
#[derive(Clone, Debug, PartialEq)]
pub struct StmtAssign {
Expand Down Expand Up @@ -3084,6 +3128,86 @@ impl Node for TypeIgnore {
const FIELD_NAMES: &'static [&'static str] = &[];
}

/// See also [type_param](https://docs.python.org/3/library/ast.html#ast.type_param)
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum TypeParam {
TypeVar(TypeParamTypeVar),
ParamSpec(TypeParamParamSpec),
TypeVarTuple(TypeParamTypeVarTuple),
}

/// See also [TypeVar](https://docs.python.org/3/library/ast.html#ast.TypeVar)
#[derive(Clone, Debug, PartialEq)]
pub struct TypeParamTypeVar {
pub range: TextRange,
pub name: Identifier,
pub bound: Option<Box<Expr>>,
}

impl Node for TypeParamTypeVar {
const NAME: &'static str = "TypeVar";
const FIELD_NAMES: &'static [&'static str] = &["name", "bound"];
}
impl From<TypeParamTypeVar> for TypeParam {
fn from(payload: TypeParamTypeVar) -> Self {
TypeParam::TypeVar(payload)
}
}
impl From<TypeParamTypeVar> for Ast {
fn from(payload: TypeParamTypeVar) -> Self {
TypeParam::from(payload).into()
}
}

/// See also [ParamSpec](https://docs.python.org/3/library/ast.html#ast.ParamSpec)
#[derive(Clone, Debug, PartialEq)]
pub struct TypeParamParamSpec {
pub range: TextRange,
pub name: Identifier,
}

impl Node for TypeParamParamSpec {
const NAME: &'static str = "ParamSpec";
const FIELD_NAMES: &'static [&'static str] = &["name"];
}
impl From<TypeParamParamSpec> for TypeParam {
fn from(payload: TypeParamParamSpec) -> Self {
TypeParam::ParamSpec(payload)
}
}
impl From<TypeParamParamSpec> for Ast {
fn from(payload: TypeParamParamSpec) -> Self {
TypeParam::from(payload).into()
}
}

/// See also [TypeVarTuple](https://docs.python.org/3/library/ast.html#ast.TypeVarTuple)
#[derive(Clone, Debug, PartialEq)]
pub struct TypeParamTypeVarTuple {
pub range: TextRange,
pub name: Identifier,
}

impl Node for TypeParamTypeVarTuple {
const NAME: &'static str = "TypeVarTuple";
const FIELD_NAMES: &'static [&'static str] = &["name"];
}
impl From<TypeParamTypeVarTuple> for TypeParam {
fn from(payload: TypeParamTypeVarTuple) -> Self {
TypeParam::TypeVarTuple(payload)
}
}
impl From<TypeParamTypeVarTuple> for Ast {
fn from(payload: TypeParamTypeVarTuple) -> Self {
TypeParam::from(payload).into()
}
}

impl Node for TypeParam {
const NAME: &'static str = "type_param";
const FIELD_NAMES: &'static [&'static str] = &[];
}

/// See also [decorator](https://docs.python.org/3/library/ast.html#ast.decorator)
#[derive(Clone, Debug, PartialEq)]
pub struct Decorator {
Expand Down
2 changes: 1 addition & 1 deletion ast/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Expr {
#[cfg(target_arch = "x86_64")]
static_assertions::assert_eq_size!(crate::Expr, [u8; 72]);
#[cfg(target_arch = "x86_64")]
static_assertions::assert_eq_size!(crate::Stmt, [u8; 144]);
static_assertions::assert_eq_size!(crate::Stmt, [u8; 168]);
#[cfg(target_arch = "x86_64")]
static_assertions::assert_eq_size!(crate::Pattern, [u8; 96]);
#[cfg(target_arch = "x86_64")]
Expand Down
6 changes: 6 additions & 0 deletions ast/src/ranged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ impl Ranged for crate::generic::StmtDelete {
self.range
}
}
impl Ranged for crate::generic::StmtTypeAlias {
fn range(&self) -> TextRange {
self.range
}
}
impl Ranged for crate::generic::StmtAssign {
fn range(&self) -> TextRange {
self.range
Expand Down Expand Up @@ -205,6 +210,7 @@ impl Ranged for crate::Stmt {
Self::ClassDef(node) => node.range(),
Self::Return(node) => node.range(),
Self::Delete(node) => node.range(),
Self::TypeAlias(node) => node.range(),
Self::Assign(node) => node.range(),
Self::AugAssign(node) => node.range(),
Self::AnnAssign(node) => node.range(),
Expand Down
1 change: 1 addition & 0 deletions parser/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
23 changes: 23 additions & 0 deletions parser/src/gen/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ impl Parse for ast::StmtAssign {
}
}

impl Parse for ast::StmtTypeAlias {
fn lex_starts_at(
source: &str,
offset: TextSize,
) -> SoftKeywordTransformer<Lexer<std::str::Chars>> {
ast::Stmt::lex_starts_at(source, offset)
}
fn parse_tokens(
lxr: impl IntoIterator<Item = LexResult>,
source_path: &str,
) -> Result<Self, ParseError> {
let node = ast::Stmt::parse_tokens(lxr, source_path)?;
match node {
ast::Stmt::TypeAlias(node) => Ok(node),
node => Err(ParseError {
error: ParseErrorType::InvalidToken,
offset: node.range().start(),
source_path: source_path.to_owned(),
}),
}
}
}

impl Parse for ast::StmtAugAssign {
fn lex_starts_at(
source: &str,
Expand Down
Loading