Update to the latest ASDL

Update to the latest ASDL
This commit is contained in:
Micha Reiser 2023-07-13 10:41:50 +02:00 committed by GitHub
commit c8092b20a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 1205 additions and 14 deletions

View file

@ -96,6 +96,14 @@ impl<R> PyNode for ast::StmtAssign<R> {
}
}
impl<R> PyNode for ast::StmtTypeAlias<R> {
#[inline]
fn py_type_cache() -> &'static OnceCell<(Py<PyAny>, Py<PyAny>)> {
static PY_TYPE: OnceCell<(Py<PyAny>, Py<PyAny>)> = OnceCell::new();
&PY_TYPE
}
}
impl<R> PyNode for ast::StmtAugAssign<R> {
#[inline]
fn py_type_cache() -> &'static OnceCell<(Py<PyAny>, Py<PyAny>)> {
@ -944,6 +952,38 @@ impl<R> PyNode for ast::TypeIgnoreTypeIgnore<R> {
}
}
impl<R> PyNode for ast::TypeParam<R> {
#[inline]
fn py_type_cache() -> &'static OnceCell<(Py<PyAny>, Py<PyAny>)> {
static PY_TYPE: OnceCell<(Py<PyAny>, Py<PyAny>)> = OnceCell::new();
&PY_TYPE
}
}
impl<R> PyNode for ast::TypeParamTypeVar<R> {
#[inline]
fn py_type_cache() -> &'static OnceCell<(Py<PyAny>, Py<PyAny>)> {
static PY_TYPE: OnceCell<(Py<PyAny>, Py<PyAny>)> = OnceCell::new();
&PY_TYPE
}
}
impl<R> PyNode for ast::TypeParamParamSpec<R> {
#[inline]
fn py_type_cache() -> &'static OnceCell<(Py<PyAny>, Py<PyAny>)> {
static PY_TYPE: OnceCell<(Py<PyAny>, Py<PyAny>)> = OnceCell::new();
&PY_TYPE
}
}
impl<R> PyNode for ast::TypeParamTypeVarTuple<R> {
#[inline]
fn py_type_cache() -> &'static OnceCell<(Py<PyAny>, Py<PyAny>)> {
static PY_TYPE: OnceCell<(Py<PyAny>, Py<PyAny>)> = OnceCell::new();
&PY_TYPE
}
}
impl ToPyAst for ast::ExprContext {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
@ -1112,6 +1152,7 @@ impl ToPyAst for ast::Stmt<TextRange> {
ast::Stmt::Return(cons) => cons.to_py_ast(py)?,
ast::Stmt::Delete(cons) => cons.to_py_ast(py)?,
ast::Stmt::Assign(cons) => cons.to_py_ast(py)?,
ast::Stmt::TypeAlias(cons) => cons.to_py_ast(py)?,
ast::Stmt::AugAssign(cons) => cons.to_py_ast(py)?,
ast::Stmt::AnnAssign(cons) => cons.to_py_ast(py)?,
ast::Stmt::For(cons) => cons.to_py_ast(py)?,
@ -1150,6 +1191,7 @@ impl ToPyAst for ast::StmtFunctionDef<TextRange> {
decorator_list,
returns,
type_comment,
type_params,
range: _range,
} = self;
@ -1160,6 +1202,7 @@ impl ToPyAst for ast::StmtFunctionDef<TextRange> {
decorator_list.to_py_ast(py)?,
returns.to_py_ast(py)?,
type_comment.to_py_ast(py)?,
type_params.to_py_ast(py)?,
))?;
Ok(instance)
@ -1178,6 +1221,7 @@ impl ToPyAst for ast::StmtAsyncFunctionDef<TextRange> {
decorator_list,
returns,
type_comment,
type_params,
range: _range,
} = self;
@ -1188,6 +1232,7 @@ impl ToPyAst for ast::StmtAsyncFunctionDef<TextRange> {
decorator_list.to_py_ast(py)?,
returns.to_py_ast(py)?,
type_comment.to_py_ast(py)?,
type_params.to_py_ast(py)?,
))?;
Ok(instance)
@ -1205,6 +1250,7 @@ impl ToPyAst for ast::StmtClassDef<TextRange> {
keywords,
body,
decorator_list,
type_params,
range: _range,
} = self;
@ -1214,6 +1260,7 @@ impl ToPyAst for ast::StmtClassDef<TextRange> {
keywords.to_py_ast(py)?,
body.to_py_ast(py)?,
decorator_list.to_py_ast(py)?,
type_params.to_py_ast(py)?,
))?;
Ok(instance)
@ -1274,6 +1321,28 @@ impl ToPyAst for ast::StmtAssign<TextRange> {
}
}
impl ToPyAst for ast::StmtTypeAlias<TextRange> {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();
let Self {
name,
type_params,
value,
range: _range,
} = self;
let instance = Py::<PyAny>::as_ref(&cache.0, py).call1((
name.to_py_ast(py)?,
type_params.to_py_ast(py)?,
value.to_py_ast(py)?,
))?;
Ok(instance)
}
}
impl ToPyAst for ast::StmtAugAssign<TextRange> {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
@ -2605,6 +2674,68 @@ impl ToPyAst for ast::TypeIgnoreTypeIgnore<TextRange> {
}
}
impl ToPyAst for ast::TypeParam<TextRange> {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let instance = match &self {
ast::TypeParam::TypeVar(cons) => cons.to_py_ast(py)?,
ast::TypeParam::ParamSpec(cons) => cons.to_py_ast(py)?,
ast::TypeParam::TypeVarTuple(cons) => cons.to_py_ast(py)?,
};
Ok(instance)
}
}
impl ToPyAst for ast::TypeParamTypeVar<TextRange> {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();
let Self {
name,
bound,
range: _range,
} = self;
let instance =
Py::<PyAny>::as_ref(&cache.0, py).call1((name.to_py_ast(py)?, bound.to_py_ast(py)?))?;
Ok(instance)
}
}
impl ToPyAst for ast::TypeParamParamSpec<TextRange> {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();
let Self {
name,
range: _range,
} = self;
let instance = Py::<PyAny>::as_ref(&cache.0, py).call1((name.to_py_ast(py)?,))?;
Ok(instance)
}
}
impl ToPyAst for ast::TypeParamTypeVarTuple<TextRange> {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();
let Self {
name,
range: _range,
} = self;
let instance = Py::<PyAny>::as_ref(&cache.0, py).call1((name.to_py_ast(py)?,))?;
Ok(instance)
}
}
impl ToPyAst for ast::Mod<SourceRange> {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
@ -2696,6 +2827,7 @@ impl ToPyAst for ast::Stmt<SourceRange> {
ast::Stmt::Return(cons) => cons.to_py_ast(py)?,
ast::Stmt::Delete(cons) => cons.to_py_ast(py)?,
ast::Stmt::Assign(cons) => cons.to_py_ast(py)?,
ast::Stmt::TypeAlias(cons) => cons.to_py_ast(py)?,
ast::Stmt::AugAssign(cons) => cons.to_py_ast(py)?,
ast::Stmt::AnnAssign(cons) => cons.to_py_ast(py)?,
ast::Stmt::For(cons) => cons.to_py_ast(py)?,
@ -2734,6 +2866,7 @@ impl ToPyAst for ast::StmtFunctionDef<SourceRange> {
decorator_list,
returns,
type_comment,
type_params,
range: _range,
} = self;
@ -2744,6 +2877,7 @@ impl ToPyAst for ast::StmtFunctionDef<SourceRange> {
decorator_list.to_py_ast(py)?,
returns.to_py_ast(py)?,
type_comment.to_py_ast(py)?,
type_params.to_py_ast(py)?,
))?;
let cache = ast_cache();
@ -2770,6 +2904,7 @@ impl ToPyAst for ast::StmtAsyncFunctionDef<SourceRange> {
decorator_list,
returns,
type_comment,
type_params,
range: _range,
} = self;
@ -2780,6 +2915,7 @@ impl ToPyAst for ast::StmtAsyncFunctionDef<SourceRange> {
decorator_list.to_py_ast(py)?,
returns.to_py_ast(py)?,
type_comment.to_py_ast(py)?,
type_params.to_py_ast(py)?,
))?;
let cache = ast_cache();
@ -2805,6 +2941,7 @@ impl ToPyAst for ast::StmtClassDef<SourceRange> {
keywords,
body,
decorator_list,
type_params,
range: _range,
} = self;
@ -2814,6 +2951,7 @@ impl ToPyAst for ast::StmtClassDef<SourceRange> {
keywords.to_py_ast(py)?,
body.to_py_ast(py)?,
decorator_list.to_py_ast(py)?,
type_params.to_py_ast(py)?,
))?;
let cache = ast_cache();
@ -2906,6 +3044,36 @@ impl ToPyAst for ast::StmtAssign<SourceRange> {
}
}
impl ToPyAst for ast::StmtTypeAlias<SourceRange> {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();
let Self {
name,
type_params,
value,
range: _range,
} = self;
let instance = Py::<PyAny>::as_ref(&cache.0, py).call1((
name.to_py_ast(py)?,
type_params.to_py_ast(py)?,
value.to_py_ast(py)?,
))?;
let cache = ast_cache();
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
instance.setattr(cache.col_offset.as_ref(py), _range.start.column.get())?;
if let Some(end) = _range.end {
instance.setattr(cache.end_lineno.as_ref(py), end.row.get())?;
instance.setattr(cache.end_col_offset.as_ref(py), end.column.get())?;
}
Ok(instance)
}
}
impl ToPyAst for ast::StmtAugAssign<SourceRange> {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
@ -4717,6 +4885,92 @@ impl ToPyAst for ast::TypeIgnoreTypeIgnore<SourceRange> {
}
}
impl ToPyAst for ast::TypeParam<SourceRange> {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let instance = match &self {
ast::TypeParam::TypeVar(cons) => cons.to_py_ast(py)?,
ast::TypeParam::ParamSpec(cons) => cons.to_py_ast(py)?,
ast::TypeParam::TypeVarTuple(cons) => cons.to_py_ast(py)?,
};
Ok(instance)
}
}
impl ToPyAst for ast::TypeParamTypeVar<SourceRange> {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();
let Self {
name,
bound,
range: _range,
} = self;
let instance =
Py::<PyAny>::as_ref(&cache.0, py).call1((name.to_py_ast(py)?, bound.to_py_ast(py)?))?;
let cache = ast_cache();
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
instance.setattr(cache.col_offset.as_ref(py), _range.start.column.get())?;
if let Some(end) = _range.end {
instance.setattr(cache.end_lineno.as_ref(py), end.row.get())?;
instance.setattr(cache.end_col_offset.as_ref(py), end.column.get())?;
}
Ok(instance)
}
}
impl ToPyAst for ast::TypeParamParamSpec<SourceRange> {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();
let Self {
name,
range: _range,
} = self;
let instance = Py::<PyAny>::as_ref(&cache.0, py).call1((name.to_py_ast(py)?,))?;
let cache = ast_cache();
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
instance.setattr(cache.col_offset.as_ref(py), _range.start.column.get())?;
if let Some(end) = _range.end {
instance.setattr(cache.end_lineno.as_ref(py), end.row.get())?;
instance.setattr(cache.end_col_offset.as_ref(py), end.column.get())?;
}
Ok(instance)
}
}
impl ToPyAst for ast::TypeParamTypeVarTuple<SourceRange> {
#[inline]
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
let cache = Self::py_type_cache().get().unwrap();
let Self {
name,
range: _range,
} = self;
let instance = Py::<PyAny>::as_ref(&cache.0, py).call1((name.to_py_ast(py)?,))?;
let cache = ast_cache();
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
instance.setattr(cache.col_offset.as_ref(py), _range.start.column.get())?;
if let Some(end) = _range.end {
instance.setattr(cache.end_lineno.as_ref(py), end.row.get())?;
instance.setattr(cache.end_col_offset.as_ref(py), end.column.get())?;
}
Ok(instance)
}
}
fn init_types(py: Python) -> PyResult<()> {
let ast_module = PyModule::import(py, "_ast")?;
cache_py_type::<ast::Mod>(ast_module)?;
@ -4731,6 +4985,7 @@ fn init_types(py: Python) -> PyResult<()> {
cache_py_type::<ast::StmtReturn>(ast_module)?;
cache_py_type::<ast::StmtDelete>(ast_module)?;
cache_py_type::<ast::StmtAssign>(ast_module)?;
cache_py_type::<ast::StmtTypeAlias>(ast_module)?;
cache_py_type::<ast::StmtAugAssign>(ast_module)?;
cache_py_type::<ast::StmtAnnAssign>(ast_module)?;
cache_py_type::<ast::StmtFor>(ast_module)?;
@ -4837,5 +5092,9 @@ fn init_types(py: Python) -> PyResult<()> {
cache_py_type::<ast::PatternMatchOr>(ast_module)?;
cache_py_type::<ast::TypeIgnore>(ast_module)?;
cache_py_type::<ast::TypeIgnoreTypeIgnore>(ast_module)?;
cache_py_type::<ast::TypeParam>(ast_module)?;
cache_py_type::<ast::TypeParamTypeVar>(ast_module)?;
cache_py_type::<ast::TypeParamParamSpec>(ast_module)?;
cache_py_type::<ast::TypeParamTypeVarTuple>(ast_module)?;
Ok(())
}

View file

@ -222,6 +222,7 @@ impl ToPyWrapper for ast::Stmt<SourceRange> {
Self::Return(cons) => cons.to_py_wrapper(py),
Self::Delete(cons) => cons.to_py_wrapper(py),
Self::Assign(cons) => cons.to_py_wrapper(py),
Self::TypeAlias(cons) => cons.to_py_wrapper(py),
Self::AugAssign(cons) => cons.to_py_wrapper(py),
Self::AnnAssign(cons) => cons.to_py_wrapper(py),
Self::For(cons) => cons.to_py_wrapper(py),
@ -310,6 +311,12 @@ impl StmtFunctionDef {
fn get_type_comment(&self, py: Python) -> PyResult<PyObject> {
self.0.type_comment.to_py_wrapper(py)
}
#[getter]
#[inline]
fn get_type_params(&self, py: Python) -> PyResult<PyObject> {
self.0.type_params.to_py_wrapper(py)
}
}
#[pyclass(module="rustpython_ast.located", name="_AsyncFunctionDef", extends=Stmt, frozen)]
@ -375,6 +382,12 @@ impl StmtAsyncFunctionDef {
fn get_type_comment(&self, py: Python) -> PyResult<PyObject> {
self.0.type_comment.to_py_wrapper(py)
}
#[getter]
#[inline]
fn get_type_params(&self, py: Python) -> PyResult<PyObject> {
self.0.type_params.to_py_wrapper(py)
}
}
#[pyclass(module="rustpython_ast.located", name="_ClassDef", extends=Stmt, frozen)]
@ -434,6 +447,12 @@ impl StmtClassDef {
fn get_decorator_list(&self, py: Python) -> PyResult<PyObject> {
self.0.decorator_list.to_py_wrapper(py)
}
#[getter]
#[inline]
fn get_type_params(&self, py: Python) -> PyResult<PyObject> {
self.0.type_params.to_py_wrapper(py)
}
}
#[pyclass(module="rustpython_ast.located", name="_Return", extends=Stmt, frozen)]
@ -553,6 +572,53 @@ impl StmtAssign {
}
}
#[pyclass(module="rustpython_ast.located", name="_TypeAlias", extends=Stmt, frozen)]
#[derive(Clone, Debug)]
pub struct StmtTypeAlias(pub &'static ast::StmtTypeAlias<SourceRange>);
impl From<&'static ast::StmtTypeAlias<SourceRange>> for StmtTypeAlias {
fn from(node: &'static ast::StmtTypeAlias<SourceRange>) -> Self {
StmtTypeAlias(node)
}
}
impl ToPyObject for StmtTypeAlias {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(Ast)
.add_subclass(Stmt)
.add_subclass(self.clone());
Py::new(py, initializer).unwrap().into_py(py)
}
}
impl ToPyWrapper for ast::StmtTypeAlias<SourceRange> {
#[inline]
fn to_py_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {
Ok(StmtTypeAlias(self).to_object(py))
}
}
#[pymethods]
impl StmtTypeAlias {
#[getter]
#[inline]
fn get_name(&self, py: Python) -> PyResult<PyObject> {
self.0.name.to_py_wrapper(py)
}
#[getter]
#[inline]
fn get_type_params(&self, py: Python) -> PyResult<PyObject> {
self.0.type_params.to_py_wrapper(py)
}
#[getter]
#[inline]
fn get_value(&self, py: Python) -> PyResult<PyObject> {
self.0.value.to_py_wrapper(py)
}
}
#[pyclass(module="rustpython_ast.located", name="_AugAssign", extends=Stmt, frozen)]
#[derive(Clone, Debug)]
pub struct StmtAugAssign(pub &'static ast::StmtAugAssign<SourceRange>);
@ -3993,6 +4059,152 @@ impl TypeIgnoreTypeIgnore {
}
}
#[pyclass(module="rustpython_ast.located", name="_type_param", extends=super::Ast, frozen, subclass)]
#[derive(Clone, Debug)]
pub struct TypeParam;
impl From<&'static ast::TypeParam<SourceRange>> for TypeParam {
fn from(_node: &'static ast::TypeParam<SourceRange>) -> Self {
TypeParam
}
}
#[pymethods]
impl TypeParam {
#[new]
fn new() -> PyClassInitializer<Self> {
PyClassInitializer::from(Ast).add_subclass(Self)
}
}
impl ToPyObject for TypeParam {
fn to_object(&self, py: Python) -> PyObject {
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
impl ToPyWrapper for ast::TypeParam<SourceRange> {
#[inline]
fn to_py_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {
match &self {
Self::TypeVar(cons) => cons.to_py_wrapper(py),
Self::ParamSpec(cons) => cons.to_py_wrapper(py),
Self::TypeVarTuple(cons) => cons.to_py_wrapper(py),
}
}
}
#[pyclass(module="rustpython_ast.located", name="_TypeVar", extends=TypeParam, frozen)]
#[derive(Clone, Debug)]
pub struct TypeParamTypeVar(pub &'static ast::TypeParamTypeVar<SourceRange>);
impl From<&'static ast::TypeParamTypeVar<SourceRange>> for TypeParamTypeVar {
fn from(node: &'static ast::TypeParamTypeVar<SourceRange>) -> Self {
TypeParamTypeVar(node)
}
}
impl ToPyObject for TypeParamTypeVar {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(Ast)
.add_subclass(TypeParam)
.add_subclass(self.clone());
Py::new(py, initializer).unwrap().into_py(py)
}
}
impl ToPyWrapper for ast::TypeParamTypeVar<SourceRange> {
#[inline]
fn to_py_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {
Ok(TypeParamTypeVar(self).to_object(py))
}
}
#[pymethods]
impl TypeParamTypeVar {
#[getter]
#[inline]
fn get_name(&self, py: Python) -> PyResult<PyObject> {
self.0.name.to_py_wrapper(py)
}
#[getter]
#[inline]
fn get_bound(&self, py: Python) -> PyResult<PyObject> {
self.0.bound.to_py_wrapper(py)
}
}
#[pyclass(module="rustpython_ast.located", name="_ParamSpec", extends=TypeParam, frozen)]
#[derive(Clone, Debug)]
pub struct TypeParamParamSpec(pub &'static ast::TypeParamParamSpec<SourceRange>);
impl From<&'static ast::TypeParamParamSpec<SourceRange>> for TypeParamParamSpec {
fn from(node: &'static ast::TypeParamParamSpec<SourceRange>) -> Self {
TypeParamParamSpec(node)
}
}
impl ToPyObject for TypeParamParamSpec {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(Ast)
.add_subclass(TypeParam)
.add_subclass(self.clone());
Py::new(py, initializer).unwrap().into_py(py)
}
}
impl ToPyWrapper for ast::TypeParamParamSpec<SourceRange> {
#[inline]
fn to_py_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {
Ok(TypeParamParamSpec(self).to_object(py))
}
}
#[pymethods]
impl TypeParamParamSpec {
#[getter]
#[inline]
fn get_name(&self, py: Python) -> PyResult<PyObject> {
self.0.name.to_py_wrapper(py)
}
}
#[pyclass(module="rustpython_ast.located", name="_TypeVarTuple", extends=TypeParam, frozen)]
#[derive(Clone, Debug)]
pub struct TypeParamTypeVarTuple(pub &'static ast::TypeParamTypeVarTuple<SourceRange>);
impl From<&'static ast::TypeParamTypeVarTuple<SourceRange>> for TypeParamTypeVarTuple {
fn from(node: &'static ast::TypeParamTypeVarTuple<SourceRange>) -> Self {
TypeParamTypeVarTuple(node)
}
}
impl ToPyObject for TypeParamTypeVarTuple {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(Ast)
.add_subclass(TypeParam)
.add_subclass(self.clone());
Py::new(py, initializer).unwrap().into_py(py)
}
}
impl ToPyWrapper for ast::TypeParamTypeVarTuple<SourceRange> {
#[inline]
fn to_py_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {
Ok(TypeParamTypeVarTuple(self).to_object(py))
}
}
#[pymethods]
impl TypeParamTypeVarTuple {
#[getter]
#[inline]
fn get_name(&self, py: Python) -> PyResult<PyObject> {
self.0.name.to_py_wrapper(py)
}
}
impl ToPyWrapper for ast::ExprContext {
#[inline]
fn to_py_wrapper(&self, py: Python) -> PyResult<Py<PyAny>> {
@ -4303,6 +4515,7 @@ pub fn add_to_module(py: Python, m: &PyModule) -> PyResult<()> {
super::init_type::<StmtReturn, ast::StmtReturn>(py, m)?;
super::init_type::<StmtDelete, ast::StmtDelete>(py, m)?;
super::init_type::<StmtAssign, ast::StmtAssign>(py, m)?;
super::init_type::<StmtTypeAlias, ast::StmtTypeAlias>(py, m)?;
super::init_type::<StmtAugAssign, ast::StmtAugAssign>(py, m)?;
super::init_type::<StmtAnnAssign, ast::StmtAnnAssign>(py, m)?;
super::init_type::<StmtFor, ast::StmtFor>(py, m)?;
@ -4409,5 +4622,9 @@ pub fn add_to_module(py: Python, m: &PyModule) -> PyResult<()> {
super::init_type::<PatternMatchOr, ast::PatternMatchOr>(py, m)?;
super::init_type::<TypeIgnore, ast::TypeIgnore>(py, m)?;
super::init_type::<TypeIgnoreTypeIgnore, ast::TypeIgnoreTypeIgnore>(py, m)?;
super::init_type::<TypeParam, ast::TypeParam>(py, m)?;
super::init_type::<TypeParamTypeVar, ast::TypeParamTypeVar>(py, m)?;
super::init_type::<TypeParamParamSpec, ast::TypeParamParamSpec>(py, m)?;
super::init_type::<TypeParamTypeVarTuple, ast::TypeParamTypeVarTuple>(py, m)?;
Ok(())
}

View file

@ -222,6 +222,7 @@ impl ToPyWrapper for ast::Stmt<TextRange> {
Self::Return(cons) => cons.to_py_wrapper(py),
Self::Delete(cons) => cons.to_py_wrapper(py),
Self::Assign(cons) => cons.to_py_wrapper(py),
Self::TypeAlias(cons) => cons.to_py_wrapper(py),
Self::AugAssign(cons) => cons.to_py_wrapper(py),
Self::AnnAssign(cons) => cons.to_py_wrapper(py),
Self::For(cons) => cons.to_py_wrapper(py),
@ -310,6 +311,12 @@ impl StmtFunctionDef {
fn get_type_comment(&self, py: Python) -> PyResult<PyObject> {
self.0.type_comment.to_py_wrapper(py)
}
#[getter]
#[inline]
fn get_type_params(&self, py: Python) -> PyResult<PyObject> {
self.0.type_params.to_py_wrapper(py)
}
}
#[pyclass(module="rustpython_ast.ranged", name="_AsyncFunctionDef", extends=Stmt, frozen)]
@ -375,6 +382,12 @@ impl StmtAsyncFunctionDef {
fn get_type_comment(&self, py: Python) -> PyResult<PyObject> {
self.0.type_comment.to_py_wrapper(py)
}
#[getter]
#[inline]
fn get_type_params(&self, py: Python) -> PyResult<PyObject> {
self.0.type_params.to_py_wrapper(py)
}
}
#[pyclass(module="rustpython_ast.ranged", name="_ClassDef", extends=Stmt, frozen)]
@ -434,6 +447,12 @@ impl StmtClassDef {
fn get_decorator_list(&self, py: Python) -> PyResult<PyObject> {
self.0.decorator_list.to_py_wrapper(py)
}
#[getter]
#[inline]
fn get_type_params(&self, py: Python) -> PyResult<PyObject> {
self.0.type_params.to_py_wrapper(py)
}
}
#[pyclass(module="rustpython_ast.ranged", name="_Return", extends=Stmt, frozen)]
@ -553,6 +572,53 @@ impl StmtAssign {
}
}
#[pyclass(module="rustpython_ast.ranged", name="_TypeAlias", extends=Stmt, frozen)]
#[derive(Clone, Debug)]
pub struct StmtTypeAlias(pub &'static ast::StmtTypeAlias<TextRange>);
impl From<&'static ast::StmtTypeAlias<TextRange>> for StmtTypeAlias {
fn from(node: &'static ast::StmtTypeAlias<TextRange>) -> Self {
StmtTypeAlias(node)
}
}
impl ToPyObject for StmtTypeAlias {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(Ast)
.add_subclass(Stmt)
.add_subclass(self.clone());
Py::new(py, initializer).unwrap().into_py(py)
}
}
impl ToPyWrapper for ast::StmtTypeAlias<TextRange> {
#[inline]
fn to_py_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {
Ok(StmtTypeAlias(self).to_object(py))
}
}
#[pymethods]
impl StmtTypeAlias {
#[getter]
#[inline]
fn get_name(&self, py: Python) -> PyResult<PyObject> {
self.0.name.to_py_wrapper(py)
}
#[getter]
#[inline]
fn get_type_params(&self, py: Python) -> PyResult<PyObject> {
self.0.type_params.to_py_wrapper(py)
}
#[getter]
#[inline]
fn get_value(&self, py: Python) -> PyResult<PyObject> {
self.0.value.to_py_wrapper(py)
}
}
#[pyclass(module="rustpython_ast.ranged", name="_AugAssign", extends=Stmt, frozen)]
#[derive(Clone, Debug)]
pub struct StmtAugAssign(pub &'static ast::StmtAugAssign<TextRange>);
@ -3993,6 +4059,152 @@ impl TypeIgnoreTypeIgnore {
}
}
#[pyclass(module="rustpython_ast.ranged", name="_type_param", extends=super::Ast, frozen, subclass)]
#[derive(Clone, Debug)]
pub struct TypeParam;
impl From<&'static ast::TypeParam<TextRange>> for TypeParam {
fn from(_node: &'static ast::TypeParam<TextRange>) -> Self {
TypeParam
}
}
#[pymethods]
impl TypeParam {
#[new]
fn new() -> PyClassInitializer<Self> {
PyClassInitializer::from(Ast).add_subclass(Self)
}
}
impl ToPyObject for TypeParam {
fn to_object(&self, py: Python) -> PyObject {
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
impl ToPyWrapper for ast::TypeParam<TextRange> {
#[inline]
fn to_py_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {
match &self {
Self::TypeVar(cons) => cons.to_py_wrapper(py),
Self::ParamSpec(cons) => cons.to_py_wrapper(py),
Self::TypeVarTuple(cons) => cons.to_py_wrapper(py),
}
}
}
#[pyclass(module="rustpython_ast.ranged", name="_TypeVar", extends=TypeParam, frozen)]
#[derive(Clone, Debug)]
pub struct TypeParamTypeVar(pub &'static ast::TypeParamTypeVar<TextRange>);
impl From<&'static ast::TypeParamTypeVar<TextRange>> for TypeParamTypeVar {
fn from(node: &'static ast::TypeParamTypeVar<TextRange>) -> Self {
TypeParamTypeVar(node)
}
}
impl ToPyObject for TypeParamTypeVar {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(Ast)
.add_subclass(TypeParam)
.add_subclass(self.clone());
Py::new(py, initializer).unwrap().into_py(py)
}
}
impl ToPyWrapper for ast::TypeParamTypeVar<TextRange> {
#[inline]
fn to_py_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {
Ok(TypeParamTypeVar(self).to_object(py))
}
}
#[pymethods]
impl TypeParamTypeVar {
#[getter]
#[inline]
fn get_name(&self, py: Python) -> PyResult<PyObject> {
self.0.name.to_py_wrapper(py)
}
#[getter]
#[inline]
fn get_bound(&self, py: Python) -> PyResult<PyObject> {
self.0.bound.to_py_wrapper(py)
}
}
#[pyclass(module="rustpython_ast.ranged", name="_ParamSpec", extends=TypeParam, frozen)]
#[derive(Clone, Debug)]
pub struct TypeParamParamSpec(pub &'static ast::TypeParamParamSpec<TextRange>);
impl From<&'static ast::TypeParamParamSpec<TextRange>> for TypeParamParamSpec {
fn from(node: &'static ast::TypeParamParamSpec<TextRange>) -> Self {
TypeParamParamSpec(node)
}
}
impl ToPyObject for TypeParamParamSpec {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(Ast)
.add_subclass(TypeParam)
.add_subclass(self.clone());
Py::new(py, initializer).unwrap().into_py(py)
}
}
impl ToPyWrapper for ast::TypeParamParamSpec<TextRange> {
#[inline]
fn to_py_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {
Ok(TypeParamParamSpec(self).to_object(py))
}
}
#[pymethods]
impl TypeParamParamSpec {
#[getter]
#[inline]
fn get_name(&self, py: Python) -> PyResult<PyObject> {
self.0.name.to_py_wrapper(py)
}
}
#[pyclass(module="rustpython_ast.ranged", name="_TypeVarTuple", extends=TypeParam, frozen)]
#[derive(Clone, Debug)]
pub struct TypeParamTypeVarTuple(pub &'static ast::TypeParamTypeVarTuple<TextRange>);
impl From<&'static ast::TypeParamTypeVarTuple<TextRange>> for TypeParamTypeVarTuple {
fn from(node: &'static ast::TypeParamTypeVarTuple<TextRange>) -> Self {
TypeParamTypeVarTuple(node)
}
}
impl ToPyObject for TypeParamTypeVarTuple {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(Ast)
.add_subclass(TypeParam)
.add_subclass(self.clone());
Py::new(py, initializer).unwrap().into_py(py)
}
}
impl ToPyWrapper for ast::TypeParamTypeVarTuple<TextRange> {
#[inline]
fn to_py_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {
Ok(TypeParamTypeVarTuple(self).to_object(py))
}
}
#[pymethods]
impl TypeParamTypeVarTuple {
#[getter]
#[inline]
fn get_name(&self, py: Python) -> PyResult<PyObject> {
self.0.name.to_py_wrapper(py)
}
}
pub fn add_to_module(py: Python, m: &PyModule) -> PyResult<()> {
super::init_module(py, m)?;
super::init_type::<Mod, ast::Mod>(py, m)?;
@ -4007,6 +4219,7 @@ pub fn add_to_module(py: Python, m: &PyModule) -> PyResult<()> {
super::init_type::<StmtReturn, ast::StmtReturn>(py, m)?;
super::init_type::<StmtDelete, ast::StmtDelete>(py, m)?;
super::init_type::<StmtAssign, ast::StmtAssign>(py, m)?;
super::init_type::<StmtTypeAlias, ast::StmtTypeAlias>(py, m)?;
super::init_type::<StmtAugAssign, ast::StmtAugAssign>(py, m)?;
super::init_type::<StmtAnnAssign, ast::StmtAnnAssign>(py, m)?;
super::init_type::<StmtFor, ast::StmtFor>(py, m)?;
@ -4113,5 +4326,9 @@ pub fn add_to_module(py: Python, m: &PyModule) -> PyResult<()> {
super::init_type::<PatternMatchOr, ast::PatternMatchOr>(py, m)?;
super::init_type::<TypeIgnore, ast::TypeIgnore>(py, m)?;
super::init_type::<TypeIgnoreTypeIgnore, ast::TypeIgnoreTypeIgnore>(py, m)?;
super::init_type::<TypeParam, ast::TypeParam>(py, m)?;
super::init_type::<TypeParamTypeVar, ast::TypeParamTypeVar>(py, m)?;
super::init_type::<TypeParamParamSpec, ast::TypeParamParamSpec>(py, m)?;
super::init_type::<TypeParamTypeVarTuple, ast::TypeParamTypeVarTuple>(py, m)?;
Ok(())
}

View file

@ -10,20 +10,22 @@ module Python
stmt = FunctionDef(identifier name, arguments args,
stmt* body, expr* decorator_list, expr? returns,
string? type_comment)
string? type_comment, type_param* type_params)
| AsyncFunctionDef(identifier name, arguments args,
stmt* body, expr* decorator_list, expr? returns,
string? type_comment)
string? type_comment, type_param* type_params)
| ClassDef(identifier name,
expr* bases,
keyword* keywords,
stmt* body,
expr* decorator_list)
expr* decorator_list,
type_param* type_params)
| Return(expr? value)
| Delete(expr* targets)
| Assign(expr* targets, expr value, string? type_comment)
| TypeAlias(expr name, type_param* type_params, expr value)
| AugAssign(expr target, operator op, expr value)
-- 'simple' indicates that we annotate simple name without parens
| AnnAssign(expr target, expr annotation, expr? value, int simple)
@ -142,4 +144,9 @@ module Python
attributes (int lineno, int col_offset, int end_lineno, int end_col_offset)
type_ignore = TypeIgnore(int lineno, string tag)
type_param = TypeVar(identifier name, expr? bound)
| ParamSpec(identifier name)
| TypeVarTuple(identifier name)
attributes (int lineno, int col_offset, int end_lineno, int end_col_offset)
}

View file

@ -108,6 +108,12 @@ pub trait Fold<U> {
) -> Result<StmtAssign<Self::TargetU>, Self::Error> {
fold_stmt_assign(self, node)
}
fn fold_stmt_type_alias(
&mut self,
node: StmtTypeAlias<U>,
) -> Result<StmtTypeAlias<Self::TargetU>, Self::Error> {
fold_stmt_type_alias(self, node)
}
fn fold_stmt_aug_assign(
&mut self,
node: StmtAugAssign<U>,
@ -507,6 +513,30 @@ pub trait Fold<U> {
) -> Result<TypeIgnoreTypeIgnore<Self::TargetU>, Self::Error> {
fold_type_ignore_type_ignore(self, node)
}
fn fold_type_param(
&mut self,
node: TypeParam<U>,
) -> Result<TypeParam<Self::TargetU>, Self::Error> {
fold_type_param(self, node)
}
fn fold_type_param_type_var(
&mut self,
node: TypeParamTypeVar<U>,
) -> Result<TypeParamTypeVar<Self::TargetU>, Self::Error> {
fold_type_param_type_var(self, node)
}
fn fold_type_param_param_spec(
&mut self,
node: TypeParamParamSpec<U>,
) -> Result<TypeParamParamSpec<Self::TargetU>, Self::Error> {
fold_type_param_param_spec(self, node)
}
fn fold_type_param_type_var_tuple(
&mut self,
node: TypeParamTypeVarTuple<U>,
) -> Result<TypeParamTypeVarTuple<Self::TargetU>, Self::Error> {
fold_type_param_type_var_tuple(self, node)
}
fn fold_arg_with_default(
&mut self,
node: ArgWithDefault<U>,
@ -653,6 +683,7 @@ pub fn fold_stmt<U, F: Fold<U> + ?Sized>(
Stmt::Return(cons) => Stmt::Return(Foldable::fold(cons, folder)?),
Stmt::Delete(cons) => Stmt::Delete(Foldable::fold(cons, folder)?),
Stmt::Assign(cons) => Stmt::Assign(Foldable::fold(cons, folder)?),
Stmt::TypeAlias(cons) => Stmt::TypeAlias(Foldable::fold(cons, folder)?),
Stmt::AugAssign(cons) => Stmt::AugAssign(Foldable::fold(cons, folder)?),
Stmt::AnnAssign(cons) => Stmt::AnnAssign(Foldable::fold(cons, folder)?),
Stmt::For(cons) => Stmt::For(Foldable::fold(cons, folder)?),
@ -697,6 +728,7 @@ pub fn fold_stmt_function_def<U, F: Fold<U> + ?Sized>(
decorator_list,
returns,
type_comment,
type_params,
range,
} = node;
let context = folder.will_map_user(&range);
@ -707,6 +739,7 @@ pub fn fold_stmt_function_def<U, F: Fold<U> + ?Sized>(
let decorator_list = Foldable::fold(decorator_list, folder)?;
let returns = Foldable::fold(returns, folder)?;
let type_comment = Foldable::fold(type_comment, folder)?;
let type_params = Foldable::fold(type_params, folder)?;
let range = folder.map_user(range, context)?;
Ok(StmtFunctionDef {
name,
@ -715,6 +748,7 @@ pub fn fold_stmt_function_def<U, F: Fold<U> + ?Sized>(
decorator_list,
returns,
type_comment,
type_params,
range,
})
}
@ -738,6 +772,7 @@ pub fn fold_stmt_async_function_def<U, F: Fold<U> + ?Sized>(
decorator_list,
returns,
type_comment,
type_params,
range,
} = node;
let context = folder.will_map_user(&range);
@ -748,6 +783,7 @@ pub fn fold_stmt_async_function_def<U, F: Fold<U> + ?Sized>(
let decorator_list = Foldable::fold(decorator_list, folder)?;
let returns = Foldable::fold(returns, folder)?;
let type_comment = Foldable::fold(type_comment, folder)?;
let type_params = Foldable::fold(type_params, folder)?;
let range = folder.map_user(range, context)?;
Ok(StmtAsyncFunctionDef {
name,
@ -756,6 +792,7 @@ pub fn fold_stmt_async_function_def<U, F: Fold<U> + ?Sized>(
decorator_list,
returns,
type_comment,
type_params,
range,
})
}
@ -778,6 +815,7 @@ pub fn fold_stmt_class_def<U, F: Fold<U> + ?Sized>(
keywords,
body,
decorator_list,
type_params,
range,
} = node;
let context = folder.will_map_user(&range);
@ -787,6 +825,7 @@ pub fn fold_stmt_class_def<U, F: Fold<U> + ?Sized>(
let keywords = Foldable::fold(keywords, folder)?;
let body = Foldable::fold(body, folder)?;
let decorator_list = Foldable::fold(decorator_list, folder)?;
let type_params = Foldable::fold(type_params, folder)?;
let range = folder.map_user(range, context)?;
Ok(StmtClassDef {
name,
@ -794,6 +833,7 @@ pub fn fold_stmt_class_def<U, F: Fold<U> + ?Sized>(
keywords,
body,
decorator_list,
type_params,
range,
})
}
@ -869,6 +909,38 @@ pub fn fold_stmt_assign<U, F: Fold<U> + ?Sized>(
range,
})
}
impl<T, U> Foldable<T, U> for StmtTypeAlias<T> {
type Mapped = StmtTypeAlias<U>;
fn fold<F: Fold<T, TargetU = U> + ?Sized>(
self,
folder: &mut F,
) -> Result<Self::Mapped, F::Error> {
folder.fold_stmt_type_alias(self)
}
}
pub fn fold_stmt_type_alias<U, F: Fold<U> + ?Sized>(
#[allow(unused)] folder: &mut F,
node: StmtTypeAlias<U>,
) -> Result<StmtTypeAlias<F::TargetU>, F::Error> {
let StmtTypeAlias {
name,
type_params,
value,
range,
} = node;
let context = folder.will_map_user(&range);
let name = Foldable::fold(name, folder)?;
let type_params = Foldable::fold(type_params, folder)?;
let value = Foldable::fold(value, folder)?;
let range = folder.map_user(range, context)?;
Ok(StmtTypeAlias {
name,
type_params,
value,
range,
})
}
impl<T, U> Foldable<T, U> for StmtAugAssign<T> {
type Mapped = StmtAugAssign<U>;
fn fold<F: Fold<T, TargetU = U> + ?Sized>(
@ -2791,6 +2863,87 @@ pub fn fold_type_ignore_type_ignore<U, F: Fold<U> + ?Sized>(
let range = folder.map_user_cfg(range, context)?;
Ok(TypeIgnoreTypeIgnore { lineno, tag, range })
}
impl<T, U> Foldable<T, U> for TypeParam<T> {
type Mapped = TypeParam<U>;
fn fold<F: Fold<T, TargetU = U> + ?Sized>(
self,
folder: &mut F,
) -> Result<Self::Mapped, F::Error> {
folder.fold_type_param(self)
}
}
pub fn fold_type_param<U, F: Fold<U> + ?Sized>(
#[allow(unused)] folder: &mut F,
node: TypeParam<U>,
) -> Result<TypeParam<F::TargetU>, F::Error> {
let folded = match node {
TypeParam::TypeVar(cons) => TypeParam::TypeVar(Foldable::fold(cons, folder)?),
TypeParam::ParamSpec(cons) => TypeParam::ParamSpec(Foldable::fold(cons, folder)?),
TypeParam::TypeVarTuple(cons) => TypeParam::TypeVarTuple(Foldable::fold(cons, folder)?),
};
Ok(folded)
}
impl<T, U> Foldable<T, U> for TypeParamTypeVar<T> {
type Mapped = TypeParamTypeVar<U>;
fn fold<F: Fold<T, TargetU = U> + ?Sized>(
self,
folder: &mut F,
) -> Result<Self::Mapped, F::Error> {
folder.fold_type_param_type_var(self)
}
}
pub fn fold_type_param_type_var<U, F: Fold<U> + ?Sized>(
#[allow(unused)] folder: &mut F,
node: TypeParamTypeVar<U>,
) -> Result<TypeParamTypeVar<F::TargetU>, F::Error> {
let TypeParamTypeVar { name, bound, range } = node;
let context = folder.will_map_user(&range);
let name = Foldable::fold(name, folder)?;
let bound = Foldable::fold(bound, folder)?;
let range = folder.map_user(range, context)?;
Ok(TypeParamTypeVar { name, bound, range })
}
impl<T, U> Foldable<T, U> for TypeParamParamSpec<T> {
type Mapped = TypeParamParamSpec<U>;
fn fold<F: Fold<T, TargetU = U> + ?Sized>(
self,
folder: &mut F,
) -> Result<Self::Mapped, F::Error> {
folder.fold_type_param_param_spec(self)
}
}
pub fn fold_type_param_param_spec<U, F: Fold<U> + ?Sized>(
#[allow(unused)] folder: &mut F,
node: TypeParamParamSpec<U>,
) -> Result<TypeParamParamSpec<F::TargetU>, F::Error> {
let TypeParamParamSpec { name, range } = node;
let context = folder.will_map_user(&range);
let name = Foldable::fold(name, folder)?;
let range = folder.map_user(range, context)?;
Ok(TypeParamParamSpec { name, range })
}
impl<T, U> Foldable<T, U> for TypeParamTypeVarTuple<T> {
type Mapped = TypeParamTypeVarTuple<U>;
fn fold<F: Fold<T, TargetU = U> + ?Sized>(
self,
folder: &mut F,
) -> Result<Self::Mapped, F::Error> {
folder.fold_type_param_type_var_tuple(self)
}
}
pub fn fold_type_param_type_var_tuple<U, F: Fold<U> + ?Sized>(
#[allow(unused)] folder: &mut F,
node: TypeParamTypeVarTuple<U>,
) -> Result<TypeParamTypeVarTuple<F::TargetU>, F::Error> {
let TypeParamTypeVarTuple { name, range } = node;
let context = folder.will_map_user(&range);
let name = Foldable::fold(name, folder)?;
let range = folder.map_user(range, context)?;
Ok(TypeParamTypeVarTuple { name, range })
}
impl<T, U> Foldable<T, U> for ArgWithDefault<T> {
type Mapped = ArgWithDefault<U>;
fn fold<F: Fold<T, TargetU = U> + ?Sized>(

View file

@ -1,8 +1,7 @@
// File automatically generated by ast/asdl_rs.py.
use crate::text_size::TextRange;
#[derive(Clone, Debug, PartialEq)]
#[derive(is_macro::Is)]
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Ast<R = TextRange> {
#[is(name = "module")]
Mod(Mod<R>),
@ -23,6 +22,7 @@ pub enum Ast<R = TextRange> {
MatchCase(MatchCase<R>),
Pattern(Pattern<R>),
TypeIgnore(TypeIgnore<R>),
TypeParam(TypeParam<R>),
}
impl<R> Node for Ast<R> {
const NAME: &'static str = "AST";
@ -137,6 +137,12 @@ impl<R> From<TypeIgnore<R>> for Ast<R> {
}
}
impl<R> From<TypeParam<R>> for Ast<R> {
fn from(node: TypeParam<R>) -> Self {
Ast::TypeParam(node)
}
}
/// See also [mod](https://docs.python.org/3/library/ast.html#ast.mod)
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Mod<R = TextRange> {
@ -256,6 +262,8 @@ pub enum Stmt<R = TextRange> {
Delete(StmtDelete<R>),
#[is(name = "assign_stmt")]
Assign(StmtAssign<R>),
#[is(name = "type_alias_stmt")]
TypeAlias(StmtTypeAlias<R>),
#[is(name = "aug_assign_stmt")]
AugAssign(StmtAugAssign<R>),
#[is(name = "ann_assign_stmt")]
@ -310,6 +318,7 @@ pub struct StmtFunctionDef<R = TextRange> {
pub decorator_list: Vec<Expr<R>>,
pub returns: Option<Box<Expr<R>>>,
pub type_comment: Option<String>,
pub type_params: Vec<TypeParam<R>>,
}
impl<R> Node for StmtFunctionDef<R> {
@ -321,6 +330,7 @@ impl<R> Node for StmtFunctionDef<R> {
"decorator_list",
"returns",
"type_comment",
"type_params",
];
}
impl<R> From<StmtFunctionDef<R>> for Stmt<R> {
@ -344,6 +354,7 @@ pub struct StmtAsyncFunctionDef<R = TextRange> {
pub decorator_list: Vec<Expr<R>>,
pub returns: Option<Box<Expr<R>>>,
pub type_comment: Option<String>,
pub type_params: Vec<TypeParam<R>>,
}
impl<R> Node for StmtAsyncFunctionDef<R> {
@ -355,6 +366,7 @@ impl<R> Node for StmtAsyncFunctionDef<R> {
"decorator_list",
"returns",
"type_comment",
"type_params",
];
}
impl<R> From<StmtAsyncFunctionDef<R>> for Stmt<R> {
@ -377,12 +389,19 @@ pub struct StmtClassDef<R = TextRange> {
pub keywords: Vec<Keyword<R>>,
pub body: Vec<Stmt<R>>,
pub decorator_list: Vec<Expr<R>>,
pub type_params: Vec<TypeParam<R>>,
}
impl<R> Node for StmtClassDef<R> {
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<R> From<StmtClassDef<R>> for Stmt<R> {
fn from(payload: StmtClassDef<R>) -> Self {
@ -463,6 +482,30 @@ impl<R> From<StmtAssign<R>> for Ast<R> {
}
}
/// See also [TypeAlias](https://docs.python.org/3/library/ast.html#ast.TypeAlias)
#[derive(Clone, Debug, PartialEq)]
pub struct StmtTypeAlias<R = TextRange> {
pub range: R,
pub name: Box<Expr<R>>,
pub type_params: Vec<TypeParam<R>>,
pub value: Box<Expr<R>>,
}
impl<R> Node for StmtTypeAlias<R> {
const NAME: &'static str = "TypeAlias";
const FIELD_NAMES: &'static [&'static str] = &["name", "type_params", "value"];
}
impl<R> From<StmtTypeAlias<R>> for Stmt<R> {
fn from(payload: StmtTypeAlias<R>) -> Self {
Stmt::TypeAlias(payload)
}
}
impl<R> From<StmtTypeAlias<R>> for Ast<R> {
fn from(payload: StmtTypeAlias<R>) -> Self {
Stmt::from(payload).into()
}
}
/// See also [AugAssign](https://docs.python.org/3/library/ast.html#ast.AugAssign)
#[derive(Clone, Debug, PartialEq)]
pub struct StmtAugAssign<R = TextRange> {
@ -3074,6 +3117,86 @@ impl<R> Node for TypeIgnore<R> {
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<R = TextRange> {
TypeVar(TypeParamTypeVar<R>),
ParamSpec(TypeParamParamSpec<R>),
TypeVarTuple(TypeParamTypeVarTuple<R>),
}
/// See also [TypeVar](https://docs.python.org/3/library/ast.html#ast.TypeVar)
#[derive(Clone, Debug, PartialEq)]
pub struct TypeParamTypeVar<R = TextRange> {
pub range: R,
pub name: Identifier,
pub bound: Option<Box<Expr<R>>>,
}
impl<R> Node for TypeParamTypeVar<R> {
const NAME: &'static str = "TypeVar";
const FIELD_NAMES: &'static [&'static str] = &["name", "bound"];
}
impl<R> From<TypeParamTypeVar<R>> for TypeParam<R> {
fn from(payload: TypeParamTypeVar<R>) -> Self {
TypeParam::TypeVar(payload)
}
}
impl<R> From<TypeParamTypeVar<R>> for Ast<R> {
fn from(payload: TypeParamTypeVar<R>) -> 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<R = TextRange> {
pub range: R,
pub name: Identifier,
}
impl<R> Node for TypeParamParamSpec<R> {
const NAME: &'static str = "ParamSpec";
const FIELD_NAMES: &'static [&'static str] = &["name"];
}
impl<R> From<TypeParamParamSpec<R>> for TypeParam<R> {
fn from(payload: TypeParamParamSpec<R>) -> Self {
TypeParam::ParamSpec(payload)
}
}
impl<R> From<TypeParamParamSpec<R>> for Ast<R> {
fn from(payload: TypeParamParamSpec<R>) -> 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<R = TextRange> {
pub range: R,
pub name: Identifier,
}
impl<R> Node for TypeParamTypeVarTuple<R> {
const NAME: &'static str = "TypeVarTuple";
const FIELD_NAMES: &'static [&'static str] = &["name"];
}
impl<R> From<TypeParamTypeVarTuple<R>> for TypeParam<R> {
fn from(payload: TypeParamTypeVarTuple<R>) -> Self {
TypeParam::TypeVarTuple(payload)
}
}
impl<R> From<TypeParamTypeVarTuple<R>> for Ast<R> {
fn from(payload: TypeParamTypeVarTuple<R>) -> Self {
TypeParam::from(payload).into()
}
}
impl<R> Node for TypeParam<R> {
const NAME: &'static str = "type_param";
const FIELD_NAMES: &'static [&'static str] = &[];
}
/// An alternative type of AST `arguments`. This is parser-friendly and human-friendly definition of function arguments.
/// This form also has advantage to implement pre-order traverse.
/// `defaults` and `kw_defaults` fields are removed and the default values are placed under each `arg_with_default` typed argument.

View file

@ -171,6 +171,20 @@ impl LocatedMut for StmtAssign {
}
}
pub type StmtTypeAlias = crate::generic::StmtTypeAlias<SourceRange>;
impl Located for StmtTypeAlias {
fn range(&self) -> SourceRange {
self.range
}
}
impl LocatedMut for StmtTypeAlias {
fn range_mut(&mut self) -> &mut SourceRange {
&mut self.range
}
}
pub type StmtAugAssign = crate::generic::StmtAugAssign<SourceRange>;
impl Located for StmtAugAssign {
@ -474,6 +488,7 @@ impl Located for Stmt {
Self::Return(node) => node.range(),
Self::Delete(node) => node.range(),
Self::Assign(node) => node.range(),
Self::TypeAlias(node) => node.range(),
Self::AugAssign(node) => node.range(),
Self::AnnAssign(node) => node.range(),
Self::For(node) => node.range(),
@ -508,6 +523,7 @@ impl LocatedMut for Stmt {
Self::Return(node) => node.range_mut(),
Self::Delete(node) => node.range_mut(),
Self::Assign(node) => node.range_mut(),
Self::TypeAlias(node) => node.range_mut(),
Self::AugAssign(node) => node.range_mut(),
Self::AnnAssign(node) => node.range_mut(),
Self::For(node) => node.range_mut(),
@ -1367,6 +1383,70 @@ impl LocatedMut for TypeIgnore {
}
}
pub type TypeParam = crate::generic::TypeParam<SourceRange>;
pub type TypeParamTypeVar = crate::generic::TypeParamTypeVar<SourceRange>;
impl Located for TypeParamTypeVar {
fn range(&self) -> SourceRange {
self.range
}
}
impl LocatedMut for TypeParamTypeVar {
fn range_mut(&mut self) -> &mut SourceRange {
&mut self.range
}
}
pub type TypeParamParamSpec = crate::generic::TypeParamParamSpec<SourceRange>;
impl Located for TypeParamParamSpec {
fn range(&self) -> SourceRange {
self.range
}
}
impl LocatedMut for TypeParamParamSpec {
fn range_mut(&mut self) -> &mut SourceRange {
&mut self.range
}
}
pub type TypeParamTypeVarTuple = crate::generic::TypeParamTypeVarTuple<SourceRange>;
impl Located for TypeParamTypeVarTuple {
fn range(&self) -> SourceRange {
self.range
}
}
impl LocatedMut for TypeParamTypeVarTuple {
fn range_mut(&mut self) -> &mut SourceRange {
&mut self.range
}
}
impl Located for TypeParam {
fn range(&self) -> SourceRange {
match self {
Self::TypeVar(node) => node.range(),
Self::ParamSpec(node) => node.range(),
Self::TypeVarTuple(node) => node.range(),
}
}
}
impl LocatedMut for TypeParam {
fn range_mut(&mut self) -> &mut SourceRange {
match self {
Self::TypeVar(node) => node.range_mut(),
Self::ParamSpec(node) => node.range_mut(),
Self::TypeVarTuple(node) => node.range_mut(),
}
}
}
pub type Arguments = crate::generic::Arguments<SourceRange>;
#[cfg(feature = "all-nodes-with-ranges")]

View file

@ -66,6 +66,11 @@ impl Ranged for crate::generic::StmtAssign<TextRange> {
self.range
}
}
impl Ranged for crate::generic::StmtTypeAlias<TextRange> {
fn range(&self) -> TextRange {
self.range
}
}
impl Ranged for crate::generic::StmtAugAssign<TextRange> {
fn range(&self) -> TextRange {
self.range
@ -180,6 +185,7 @@ impl Ranged for crate::Stmt {
Self::Return(node) => node.range(),
Self::Delete(node) => node.range(),
Self::Assign(node) => node.range(),
Self::TypeAlias(node) => node.range(),
Self::AugAssign(node) => node.range(),
Self::AnnAssign(node) => node.range(),
Self::For(node) => node.range(),
@ -496,6 +502,31 @@ impl Ranged for crate::TypeIgnore {
}
}
impl Ranged for crate::generic::TypeParamTypeVar<TextRange> {
fn range(&self) -> TextRange {
self.range
}
}
impl Ranged for crate::generic::TypeParamParamSpec<TextRange> {
fn range(&self) -> TextRange {
self.range
}
}
impl Ranged for crate::generic::TypeParamTypeVarTuple<TextRange> {
fn range(&self) -> TextRange {
self.range
}
}
impl Ranged for crate::TypeParam {
fn range(&self) -> TextRange {
match self {
Self::TypeVar(node) => node.range(),
Self::ParamSpec(node) => node.range(),
Self::TypeVarTuple(node) => node.range(),
}
}
}
#[cfg(feature = "all-nodes-with-ranges")]
impl Ranged for crate::generic::Arguments<TextRange> {
fn range(&self) -> TextRange {

View file

@ -13,6 +13,7 @@ pub trait Visitor<R = crate::text_size::TextRange> {
Stmt::Return(data) => self.visit_stmt_return(data),
Stmt::Delete(data) => self.visit_stmt_delete(data),
Stmt::Assign(data) => self.visit_stmt_assign(data),
Stmt::TypeAlias(data) => self.visit_stmt_type_alias(data),
Stmt::AugAssign(data) => self.visit_stmt_aug_assign(data),
Stmt::AnnAssign(data) => self.visit_stmt_ann_assign(data),
Stmt::For(data) => self.visit_stmt_for(data),
@ -53,6 +54,9 @@ pub trait Visitor<R = crate::text_size::TextRange> {
if let Some(value) = node.returns {
self.visit_expr(*value);
}
for value in node.type_params {
self.visit_type_param(value);
}
}
fn visit_stmt_async_function_def(&mut self, node: StmtAsyncFunctionDef<R>) {
self.generic_visit_stmt_async_function_def(node)
@ -71,6 +75,9 @@ pub trait Visitor<R = crate::text_size::TextRange> {
if let Some(value) = node.returns {
self.visit_expr(*value);
}
for value in node.type_params {
self.visit_type_param(value);
}
}
fn visit_stmt_class_def(&mut self, node: StmtClassDef<R>) {
self.generic_visit_stmt_class_def(node)
@ -88,6 +95,9 @@ pub trait Visitor<R = crate::text_size::TextRange> {
for value in node.decorator_list {
self.visit_expr(value);
}
for value in node.type_params {
self.visit_type_param(value);
}
}
fn visit_stmt_return(&mut self, node: StmtReturn<R>) {
self.generic_visit_stmt_return(node)
@ -117,6 +127,22 @@ pub trait Visitor<R = crate::text_size::TextRange> {
self.visit_expr(*value);
}
}
fn visit_stmt_type_alias(&mut self, node: StmtTypeAlias<R>) {
self.generic_visit_stmt_type_alias(node)
}
fn generic_visit_stmt_type_alias(&mut self, node: StmtTypeAlias<R>) {
{
let value = node.name;
self.visit_expr(*value);
}
for value in node.type_params {
self.visit_type_param(value);
}
{
let value = node.value;
self.visit_expr(*value);
}
}
fn visit_stmt_aug_assign(&mut self, node: StmtAugAssign<R>) {
self.generic_visit_stmt_aug_assign(node)
}
@ -810,4 +836,30 @@ pub trait Visitor<R = crate::text_size::TextRange> {
self.visit_pattern(value);
}
}
fn visit_type_param(&mut self, node: TypeParam<R>) {
self.generic_visit_type_param(node)
}
fn generic_visit_type_param(&mut self, node: TypeParam<R>) {
match node {
TypeParam::TypeVar(data) => self.visit_type_param_type_var(data),
TypeParam::ParamSpec(data) => self.visit_type_param_param_spec(data),
TypeParam::TypeVarTuple(data) => self.visit_type_param_type_var_tuple(data),
}
}
fn visit_type_param_type_var(&mut self, node: TypeParamTypeVar<R>) {
self.generic_visit_type_param_type_var(node)
}
fn generic_visit_type_param_type_var(&mut self, node: TypeParamTypeVar<R>) {
if let Some(value) = node.bound {
self.visit_expr(*value);
}
}
fn visit_type_param_param_spec(&mut self, node: TypeParamParamSpec<R>) {
self.generic_visit_type_param_param_spec(node)
}
fn generic_visit_type_param_param_spec(&mut self, node: TypeParamParamSpec<R>) {}
fn visit_type_param_type_var_tuple(&mut self, node: TypeParamTypeVarTuple<R>) {
self.generic_visit_type_param_type_var_tuple(node)
}
fn generic_visit_type_param_type_var_tuple(&mut self, node: TypeParamTypeVarTuple<R>) {}
}

View file

@ -57,7 +57,7 @@ impl<R> Expr<R> {
#[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; 136]);
static_assertions::assert_eq_size!(crate::Stmt, [u8; 160]);
#[cfg(target_arch = "x86_64")]
static_assertions::assert_eq_size!(crate::Pattern, [u8; 96]);
#[cfg(target_arch = "x86_64")]

View file

@ -149,6 +149,7 @@ impl crate::fold::Fold<TextRange> for LinearLocator<'_> {
keywords,
body,
decorator_list,
type_params,
range,
} = node;
let decorator_list = self.fold(decorator_list)?;
@ -159,12 +160,15 @@ impl crate::fold::Fold<TextRange> for LinearLocator<'_> {
let keywords = self.fold(keywords)?;
let body = self.fold(body)?;
let range = self.map_user(range, context)?;
let type_params = self.fold(type_params)?;
Ok(crate::StmtClassDef {
name,
bases,
keywords,
body,
decorator_list,
type_params,
range,
})
}
@ -180,6 +184,7 @@ impl crate::fold::Fold<TextRange> for LinearLocator<'_> {
returns,
type_comment,
range,
type_params,
} = node;
let decorator_list = self.fold(decorator_list)?;
let context = self.will_map_user(&range);
@ -189,6 +194,7 @@ impl crate::fold::Fold<TextRange> for LinearLocator<'_> {
let returns = self.fold(returns)?;
let body = self.fold(body)?;
let type_comment = self.fold(type_comment)?;
let type_params = self.fold(type_params)?;
let range = self.map_user(range, context)?;
Ok(crate::StmtFunctionDef {
name,
@ -196,6 +202,7 @@ impl crate::fold::Fold<TextRange> for LinearLocator<'_> {
body,
decorator_list,
returns,
type_params,
type_comment,
range,
})
@ -211,6 +218,7 @@ impl crate::fold::Fold<TextRange> for LinearLocator<'_> {
decorator_list,
returns,
type_comment,
type_params,
range,
} = node;
let decorator_list = self.fold(decorator_list)?;
@ -221,6 +229,7 @@ impl crate::fold::Fold<TextRange> for LinearLocator<'_> {
let returns = self.fold(returns)?;
let body = self.fold(body)?;
let type_comment = self.fold(type_comment)?;
let type_params = self.fold(type_params)?;
let range = self.map_user(range, context)?;
Ok(crate::StmtAsyncFunctionDef {
name,
@ -229,6 +238,7 @@ impl crate::fold::Fold<TextRange> for LinearLocator<'_> {
decorator_list,
returns,
type_comment,
type_params,
range,
})
}

View file

@ -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,

View file

@ -970,10 +970,11 @@ FuncDef: ast::Stmt = {
let returns = r.map(|x| Box::new(x));
let end_location = body.last().unwrap().end();
let type_comment = None;
let type_params = Vec::new();
if is_async.is_some() {
ast::StmtAsyncFunctionDef { name, args, body, decorator_list, returns, type_comment, range: (location..end_location).into() }.into()
ast::StmtAsyncFunctionDef { name, args, body, decorator_list, returns, type_comment, type_params, range: (location..end_location).into() }.into()
} else {
ast::StmtFunctionDef { name, args, body, decorator_list, returns, type_comment, range: (location..end_location).into() }.into()
ast::StmtFunctionDef { name, args, body, decorator_list, returns, type_comment, type_params, range: (location..end_location).into() }.into()
}
},
};
@ -1131,6 +1132,7 @@ ClassDef: ast::Stmt = {
None => (vec![], vec![]),
};
let end_location = body.last().unwrap().end();
let type_params = Vec::new();
ast::Stmt::ClassDef(
ast::StmtClassDef {
name,
@ -1138,6 +1140,7 @@ ClassDef: ast::Stmt = {
keywords,
body,
decorator_list,
type_params,
range: (location..end_location).into()
},
)

9
parser/src/python.rs generated
View file

@ -1,5 +1,5 @@
// auto-generated: "lalrpop 0.20.0"
// sha3: c39f9711066c6f94aaf93d62d86b41efb4242ddcdcbe5b9d35e5a77a14ff22d6
// sha3: 63c75be3af99ad823887ab5407feb1d091c0c150fc7ec64c257b95becfbbe6be
use crate::{
ast::{self as ast, Ranged, bigint::BigInt},
lexer::{LexicalError, LexicalErrorType},
@ -31068,10 +31068,11 @@ fn __action157<
let returns = r.map(|x| Box::new(x));
let end_location = body.last().unwrap().end();
let type_comment = None;
let type_params = Vec::new();
if is_async.is_some() {
ast::StmtAsyncFunctionDef { name, args, body, decorator_list, returns, type_comment, range: (location..end_location).into() }.into()
ast::StmtAsyncFunctionDef { name, args, body, decorator_list, returns, type_comment, type_params, range: (location..end_location).into() }.into()
} else {
ast::StmtFunctionDef { name, args, body, decorator_list, returns, type_comment, range: (location..end_location).into() }.into()
ast::StmtFunctionDef { name, args, body, decorator_list, returns, type_comment, type_params, range: (location..end_location).into() }.into()
}
}
}
@ -31184,6 +31185,7 @@ fn __action164<
None => (vec![], vec![]),
};
let end_location = body.last().unwrap().end();
let type_params = Vec::new();
ast::Stmt::ClassDef(
ast::StmtClassDef {
name,
@ -31191,6 +31193,7 @@ fn __action164<
keywords,
body,
decorator_list,
type_params,
range: (location..end_location).into()
},
)

View file

@ -65,6 +65,7 @@ Ok(
decorator_list: [],
returns: None,
type_comment: None,
type_params: [],
},
),
],

View file

@ -85,6 +85,7 @@ Ok(
decorator_list: [],
returns: None,
type_comment: None,
type_params: [],
},
),
],

View file

@ -28,6 +28,7 @@ Ok(
decorator_list: [],
returns: None,
type_comment: None,
type_params: [],
},
),
],

View file

@ -102,6 +102,7 @@ Ok(
decorator_list: [],
returns: None,
type_comment: None,
type_params: [],
},
),
],

View file

@ -122,6 +122,7 @@ Ok(
decorator_list: [],
returns: None,
type_comment: None,
type_params: [],
},
),
],

View file

@ -131,6 +131,7 @@ Ok(
decorator_list: [],
returns: None,
type_comment: None,
type_params: [],
},
),
],

View file

@ -140,6 +140,7 @@ Ok(
decorator_list: [],
returns: None,
type_comment: None,
type_params: [],
},
),
],

View file

@ -65,6 +65,7 @@ Ok(
decorator_list: [],
returns: None,
type_comment: None,
type_params: [],
},
),
],

View file

@ -85,6 +85,7 @@ Ok(
decorator_list: [],
returns: None,
type_comment: None,
type_params: [],
},
),
],

View file

@ -1,6 +1,6 @@
---
source: parser/src/parser.rs
expression: "parse_program(source, \"<test>\").unwrap()"
expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
---
[
ClassDef(
@ -68,6 +68,7 @@ expression: "parse_program(source, \"<test>\").unwrap()"
decorator_list: [],
returns: None,
type_comment: None,
type_params: [],
},
),
FunctionDef(
@ -129,10 +130,12 @@ expression: "parse_program(source, \"<test>\").unwrap()"
decorator_list: [],
returns: None,
type_comment: None,
type_params: [],
},
),
],
decorator_list: [],
type_params: [],
},
),
]

View file

@ -90,6 +90,7 @@ expression: parse_ast
),
),
type_comment: None,
type_params: [],
},
),
]