From 8e3633f55ac8e3768df7d2e51619f37e6a8580c8 Mon Sep 17 00:00:00 2001 From: Douglas Creager Date: Fri, 17 Jan 2025 14:23:02 -0500 Subject: [PATCH] Auto-generate AST boilerplate (#15544) This PR replaces most of the hard-coded AST definitions with a generation script, similar to what happens in `rust_python_formatter`. I've replaced every "rote" definition that I could find, where the content is entirely boilerplate and only depends on what syntax nodes there are and which groups they belong to. This is a pretty massive diff, but it's entirely a refactoring. It should make absolutely no changes to the API or implementation. In particular, this required adding some configuration knobs that let us override default auto-generated names where they don't line up with types that we created previously by hand. ## Test plan There should be no changes outside of the `rust_python_ast` crate, which verifies that there were no API changes as a result of the auto-generation. Aggressive `cargo clippy` and `uvx pre-commit` runs after each commit in the branch. --------- Co-authored-by: Micha Reiser Co-authored-by: Alex Waygood --- .gitattributes | 4 +- .github/workflows/ci.yaml | 6 + .../knot_extensions/knot_extensions.pyi | 2 +- ...ow_settings__display_default_settings.snap | 6 +- crates/ruff_python_ast/ast.toml | 174 + crates/ruff_python_ast/generate.py | 752 ++ crates/ruff_python_ast/src/expression.rs | 321 +- crates/ruff_python_ast/src/generated.rs | 8790 +++++++++++++++++ crates/ruff_python_ast/src/lib.rs | 4 +- crates/ruff_python_ast/src/node.rs | 7446 +------------- crates/ruff_python_ast/src/nodes.rs | 1191 +-- .../src/visitor/source_order.rs | 36 +- crates/ruff_python_formatter/generate.py | 2 +- crates/ruff_python_formatter/src/generated.rs | 216 +- pyproject.toml | 3 + 15 files changed, 10257 insertions(+), 8696 deletions(-) create mode 100644 crates/ruff_python_ast/ast.toml create mode 100644 crates/ruff_python_ast/generate.py create mode 100644 crates/ruff_python_ast/src/generated.rs diff --git a/.gitattributes b/.gitattributes index 9ae06f93d11a9..ebc35c6745458 100644 --- a/.gitattributes +++ b/.gitattributes @@ -14,5 +14,7 @@ crates/ruff_python_parser/resources/invalid/re_lex_logical_token_mac_eol.py text crates/ruff_python_parser/resources/inline linguist-generated=true -ruff.schema.json linguist-generated=true text=auto eol=lf +ruff.schema.json -diff linguist-generated=true text=auto eol=lf +crates/ruff_python_ast/src/generated.rs -diff linguist-generated=true text=auto eol=lf +crates/ruff_python_formatter/src/generated.rs -diff linguist-generated=true text=auto eol=lf *.md.snap linguist-language=Markdown diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 83502cfc55dff..d3d435a45290e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -386,6 +386,12 @@ jobs: - name: "Install Rust toolchain" run: rustup component add rustfmt - uses: Swatinem/rust-cache@v2 + # Run all code generation scripts, and verify that the current output is + # already checked into git. + - run: python crates/ruff_python_ast/generate.py + - run: python crates/ruff_python_formatter/generate.py + - run: test -z "$(git status --porcelain)" + # Verify that adding a plugin or rule produces clean code. - run: ./scripts/add_rule.py --name DoTheThing --prefix F --code 999 --linter pyflakes - run: cargo check - run: cargo fmt --all --check diff --git a/crates/red_knot_vendored/knot_extensions/knot_extensions.pyi b/crates/red_knot_vendored/knot_extensions/knot_extensions.pyi index 3b0f01eb65894..80d565479b6df 100644 --- a/crates/red_knot_vendored/knot_extensions/knot_extensions.pyi +++ b/crates/red_knot_vendored/knot_extensions/knot_extensions.pyi @@ -1,4 +1,4 @@ -from typing import _SpecialForm, Any, LiteralString +from typing import Any, LiteralString, _SpecialForm # Special operations def static_assert(condition: object, msg: LiteralString | None = None) -> None: ... diff --git a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap index 46cd4b0dddbae..2e1465fb034c6 100644 --- a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap +++ b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap @@ -1,12 +1,12 @@ --- source: crates/ruff/tests/show_settings.rs +assertion_line: 30 info: program: ruff args: - check - "--show-settings" - unformatted.py -snapshot_kind: text --- success: true exit_code: 0 @@ -72,6 +72,8 @@ file_resolver.project_root = "[BASEPATH]" linter.exclude = [] linter.project_root = "[BASEPATH]" linter.rules.enabled = [ + unsorted-imports (I001), + missing-required-import (I002), multiple-imports-on-one-line (E401), module-import-not-at-top-of-file (E402), multiple-statements-on-one-line-colon (E701), @@ -133,6 +135,8 @@ linter.rules.enabled = [ raise-not-implemented (F901), ] linter.rules.should_fix = [ + unsorted-imports (I001), + missing-required-import (I002), multiple-imports-on-one-line (E401), module-import-not-at-top-of-file (E402), multiple-statements-on-one-line-colon (E701), diff --git a/crates/ruff_python_ast/ast.toml b/crates/ruff_python_ast/ast.toml new file mode 100644 index 0000000000000..60b9cddd00610 --- /dev/null +++ b/crates/ruff_python_ast/ast.toml @@ -0,0 +1,174 @@ +# This file is used by generate.py to autogenerate our Python AST data model. +# +# We have defined a Rust struct for each syntax node in `src/nodes.rs`. Many of +# these nodes belong to groups. For instance, there is a `Stmt` group +# consisting of all of the syntax nodes that represent valid Python statements. +# +# There is a special group named `ungrouped` that contains syntax nodes that do +# not belong to any group. +# +# Each group is defined by two sections below. The `[GROUP]` section defines +# options that control the auto-generation for that group. The `[GROUP.nodes]` +# section defines which syntax nodes belong to that group. The name of each +# entry in the nodes section must match the name of the corresponding Rust +# struct. The value of each entry defines options that control the +# auto-generation for that syntax node. +# +# The following group options are available: +# +# add_suffix_to_is_methods: [true/false] +# Controls the name of the is_foo methods of the group's enums. If false (the +# default), these methods will use the variant name in snake_case. If true, +# then the group prefix will be moved to the end before snake_casing. (That +# is, `StmtIf` will become `if_stmt`.) +# +# anynode_is_label: foo_bar +# Controls the name of the AnyNode::foo_bar, AnyNode::is_foo_bar, and +# AnyNodeRef::is_foo_bar methods. The default is the group name in +# snake_case. +# +# ref_enum_ty: +# The name of the reference enum that we create for this group. The default +# is the group name with `Ref` added to the end. +# +# rustdoc: +# A rustdoc comment that is added to the group's enums. +# +# The following syntax node options are available: +# +# variant: +# The name of the enum variant for this syntax node. Defaults to the node +# name with the group prefix removed. (That is, `StmtIf` becomes `If`.) + +[Mod] +anynode_is_label = "module" +rustdoc = "/// See also [mod](https://docs.python.org/3/library/ast.html#ast.mod)" + +[Mod.nodes] +ModModule = {} +ModExpression = {} + +[Stmt] +add_suffix_to_is_methods = true +anynode_is_label = "statement" +rustdoc = "/// See also [stmt](https://docs.python.org/3/library/ast.html#ast.stmt)" +ref_enum_ty = "StatementRef" + +[Stmt.nodes] +StmtFunctionDef = {} +StmtClassDef = {} +StmtReturn = {} +StmtDelete = {} +StmtTypeAlias = {} +StmtAssign = {} +StmtAugAssign = {} +StmtAnnAssign = {} +StmtFor = {} +StmtWhile = {} +StmtIf = {} +StmtWith = {} +StmtMatch = {} +StmtRaise = {} +StmtTry = {} +StmtAssert = {} +StmtImport = {} +StmtImportFrom = {} +StmtGlobal = {} +StmtNonlocal = {} +StmtExpr = {} +StmtPass = {} +StmtBreak = {} +StmtContinue = {} +StmtIpyEscapeCommand = {} + +[Expr] +add_suffix_to_is_methods = true +anynode_is_label = "expression" +rustdoc = "/// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr)" +ref_enum_ty = "ExpressionRef" + +[Expr.nodes] +ExprBoolOp = {} +ExprNamed = {} +ExprBinOp = {} +ExprUnaryOp = {} +ExprLambda = {} +ExprIf = {} +ExprDict = {} +ExprSet = {} +ExprListComp = {} +ExprSetComp = {} +ExprDictComp = {} +ExprGenerator = {} +ExprAwait = {} +ExprYield = {} +ExprYieldFrom = {} +ExprCompare = {} +ExprCall = {} +ExprFString = {} +ExprStringLiteral = {} +ExprBytesLiteral = {} +ExprNumberLiteral = {} +ExprBooleanLiteral = {} +ExprNoneLiteral = {} +ExprEllipsisLiteral = {} +ExprAttribute = {} +ExprSubscript = {} +ExprStarred = {} +ExprName = {} +ExprList = {} +ExprTuple = {} +ExprSlice = {} +ExprIpyEscapeCommand = {} + +[ExceptHandler] +rustdoc = "/// See also [excepthandler](https://docs.python.org/3/library/ast.html#ast.excepthandler)" + +[ExceptHandler.nodes] +ExceptHandlerExceptHandler = {} + +[FStringElement.nodes] +FStringExpressionElement = {variant = "Expression"} +FStringLiteralElement = {variant = "Literal"} + +[Pattern] +rustdoc = "/// See also [pattern](https://docs.python.org/3/library/ast.html#ast.pattern)" + +[Pattern.nodes] +PatternMatchValue = {} +PatternMatchSingleton = {} +PatternMatchSequence = {} +PatternMatchMapping = {} +PatternMatchClass = {} +PatternMatchStar = {} +PatternMatchAs = {} +PatternMatchOr = {} + +[TypeParam] +rustdoc = "/// See also [type_param](https://docs.python.org/3/library/ast.html#ast.type_param)" + +[TypeParam.nodes] +TypeParamTypeVar = {} +TypeParamTypeVarTuple = {} +TypeParamParamSpec = {} + +[ungrouped.nodes] +FStringFormatSpec = {} +PatternArguments = {} +PatternKeyword = {} +Comprehension = {} +Arguments = {} +Parameters = {} +Parameter = {} +ParameterWithDefault = {} +Keyword = {} +Alias = {} +WithItem = {} +MatchCase = {} +Decorator = {} +ElifElseClause = {} +TypeParams = {} +FString = {} +StringLiteral = {} +BytesLiteral = {} +Identifier = {} diff --git a/crates/ruff_python_ast/generate.py b/crates/ruff_python_ast/generate.py new file mode 100644 index 0000000000000..e294314dbf6b6 --- /dev/null +++ b/crates/ruff_python_ast/generate.py @@ -0,0 +1,752 @@ +#!/usr/bin/python +# /// script +# requires-python = ">=3.11" +# dependencies = [] +# /// + +from __future__ import annotations + +import re +from dataclasses import dataclass +from pathlib import Path +from subprocess import check_output +from typing import Any + +import tomllib + + +def rustfmt(code: str) -> str: + return check_output(["rustfmt", "--emit=stdout"], input=code, text=True) + + +def to_snake_case(node: str) -> str: + """Converts CamelCase to snake_case""" + return re.sub("([A-Z])", r"_\1", node).lower().lstrip("_") + + +# ------------------------------------------------------------------------------ +# Read AST description + + +def load_ast(root: Path) -> list[Group]: + ast_path = root.joinpath("crates", "ruff_python_ast", "ast.toml") + with ast_path.open("rb") as ast_file: + ast = tomllib.load(ast_file) + return [Group(group_name, group) for group_name, group in ast.items()] + + +# ------------------------------------------------------------------------------ +# Preprocess + + +@dataclass +class Group: + name: str + nodes: list[Node] + owned_enum_ty: str + ref_enum_ty: str + + add_suffix_to_is_methods: bool + anynode_is_label: str + rustdoc: str | None + + def __init__(self, group_name: str, group: dict[str, Any]) -> None: + self.name = group_name + self.owned_enum_ty = group_name + self.ref_enum_ty = group.get("ref_enum_ty", group_name + "Ref") + self.add_suffix_to_is_methods = group.get("add_suffix_to_is_methods", False) + self.anynode_is_label = group.get("anynode_is_label", to_snake_case(group_name)) + self.rustdoc = group.get("rustdoc") + self.nodes = [ + Node(self, node_name, node) for node_name, node in group["nodes"].items() + ] + + +@dataclass +class Node: + name: str + variant: str + ty: str + + def __init__(self, group: Group, node_name: str, node: dict[str, Any]) -> None: + self.name = node_name + self.variant = node.get("variant", node_name.removeprefix(group.name)) + self.ty = f"crate::{node_name}" + + +# ------------------------------------------------------------------------------ +# Preamble + + +def write_preamble(out: list[str]) -> None: + out.append(""" + // This is a generated file. Don't modify it by hand! + // Run `crates/ruff_python_ast/generate.py` to re-generate the file. + """) + + +# ------------------------------------------------------------------------------ +# Owned enum + + +def write_owned_enum(out: list[str], groups: list[Group]) -> None: + """ + Create an enum for each group that contains an owned copy of a syntax node. + + ```rust + pub enum TypeParam { + TypeVar(TypeParamTypeVar), + TypeVarTuple(TypeParamTypeVarTuple), + ... + } + ``` + + Also creates: + - `impl Ranged for TypeParam` + - `TypeParam::visit_source_order` + - `impl From for TypeParam` + - `impl Ranged for TypeParamTypeVar` + - `fn TypeParam::is_type_var() -> bool` + + If the `add_suffix_to_is_methods` group option is true, then the + `is_type_var` method will be named `is_type_var_type_param`. + """ + + for group in groups: + if group.name == "ungrouped": + continue + + out.append("") + if group.rustdoc is not None: + out.append(group.rustdoc) + out.append("#[derive(Clone, Debug, PartialEq, is_macro::Is)]") + out.append(f"pub enum {group.owned_enum_ty} {{") + for node in group.nodes: + if group.add_suffix_to_is_methods: + is_name = to_snake_case(node.variant + group.name) + out.append(f'#[is(name = "{is_name}")]') + out.append(f"{node.variant}({node.ty}),") + out.append("}") + + for node in group.nodes: + out.append(f""" + impl From<{node.ty}> for {group.owned_enum_ty} {{ + fn from(node: {node.ty}) -> Self {{ + Self::{node.variant}(node) + }} + }} + """) + + out.append(f""" + impl ruff_text_size::Ranged for {group.owned_enum_ty} {{ + fn range(&self) -> ruff_text_size::TextRange {{ + match self {{ + """) + for node in group.nodes: + out.append(f"Self::{node.variant}(node) => node.range(),") + out.append(""" + } + } + } + """) + + for group in groups: + for node in group.nodes: + out.append(f""" + impl ruff_text_size::Ranged for {node.ty} {{ + fn range(&self) -> ruff_text_size::TextRange {{ + self.range + }} + }} + """) + + for group in groups: + if group.name == "ungrouped": + continue + out.append(f""" + impl {group.owned_enum_ty} {{ + #[allow(unused)] + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) + where + V: crate::visitor::source_order::SourceOrderVisitor<'a> + ?Sized, + {{ + match self {{ + """) + for node in group.nodes: + out.append( + f"""{group.owned_enum_ty}::{node.variant}(node) => node.visit_source_order(visitor),""" + ) + out.append(""" + } + } + } + """) + + +# ------------------------------------------------------------------------------ +# Ref enum + + +def write_ref_enum(out: list[str], groups: list[Group]) -> None: + """ + Create an enum for each group that contains a reference to a syntax node. + + ```rust + pub enum TypeParamRef<'a> { + TypeVar(&'a TypeParamTypeVar), + TypeVarTuple(&'a TypeParamTypeVarTuple), + ... + } + ``` + + Also creates: + - `impl<'a> From<&'a TypeParam> for TypeParamRef<'a>` + - `impl<'a> From<&'a TypeParamTypeVar> for TypeParamRef<'a>` + - `impl Ranged for TypeParamRef<'_>` + - `fn TypeParamRef::is_type_var() -> bool` + + The name of the enum can be customized via the `ref_enum_ty` group option. + The name of each variant can be customized via the `variant` node option. If + the `add_suffix_to_is_methods` group option is true, then the `is_type_var` + method will be named `is_type_var_type_param`. + """ + + for group in groups: + if group.name == "ungrouped": + continue + + out.append("") + if group.rustdoc is not None: + out.append(group.rustdoc) + out.append("""#[derive(Clone, Copy, Debug, PartialEq, is_macro::Is)]""") + out.append(f"""pub enum {group.ref_enum_ty}<'a> {{""") + for node in group.nodes: + if group.add_suffix_to_is_methods: + is_name = to_snake_case(node.variant + group.name) + out.append(f'#[is(name = "{is_name}")]') + out.append(f"""{node.variant}(&'a {node.ty}),""") + out.append("}") + + out.append(f""" + impl<'a> From<&'a {group.owned_enum_ty}> for {group.ref_enum_ty}<'a> {{ + fn from(node: &'a {group.owned_enum_ty}) -> Self {{ + match node {{ + """) + for node in group.nodes: + out.append( + f"""{group.owned_enum_ty}::{node.variant}(node) => {group.ref_enum_ty}::{node.variant}(node),""" + ) + out.append(""" + } + } + } + """) + + for node in group.nodes: + out.append(f""" + impl<'a> From<&'a {node.ty}> for {group.ref_enum_ty}<'a> {{ + fn from(node: &'a {node.ty}) -> Self {{ + Self::{node.variant}(node) + }} + }} + """) + + out.append(f""" + impl ruff_text_size::Ranged for {group.ref_enum_ty}<'_> {{ + fn range(&self) -> ruff_text_size::TextRange {{ + match self {{ + """) + for node in group.nodes: + out.append(f"Self::{node.variant}(node) => node.range(),") + out.append(""" + } + } + } + """) + + +# ------------------------------------------------------------------------------ +# AnyNode + + +def write_anynode(out: list[str], groups: list[Group]) -> None: + """ + Create the AnyNode type. + + ```rust + pub enum AnyNode { + ... + TypeParamTypeVar(TypeParamTypeVar), + TypeParamTypeVarTuple(TypeParamTypeVarTuple), + ... + } + ``` + + Also creates: + - `impl From for AnyNode` + - `impl From for AnyNode` + - `impl Ranged for AnyNode` + - `fn AnyNode::type_param(self) -> Option` + - `fn AnyNode::is_type_param(&self) -> bool` + - `fn AnyNode::is_type_param_type_var(&self) -> bool` + - `fn AnyNode::as_ref(&self) -> AnyNodeRef` + + The name of the `type_param` and `is_type_param` methods can be customized + via the `anynode_is_label` group option. + """ + + out.append(""" + #[derive(Clone, Debug, is_macro::Is, PartialEq)] + pub enum AnyNode { + """) + for group in groups: + for node in group.nodes: + out.append(f"{node.name}({node.ty}),") + out.append(""" + } + """) + + for group in groups: + if group.name != "ungrouped": + out.append(f""" + impl From<{group.owned_enum_ty}> for AnyNode {{ + fn from(node: {group.owned_enum_ty}) -> AnyNode {{ + match node {{ + """) + for node in group.nodes: + out.append( + f"""{group.owned_enum_ty}::{node.variant}(node) => AnyNode::{node.name}(node),""" + ) + out.append(""" + } + } + } + """) + + for node in group.nodes: + out.append(f""" + impl From<{node.ty}> for AnyNode {{ + fn from(node: {node.ty}) -> AnyNode {{ + AnyNode::{node.name}(node) + }} + }} + """) + + out.append(""" + impl ruff_text_size::Ranged for AnyNode { + fn range(&self) -> ruff_text_size::TextRange { + match self { + """) + for group in groups: + for node in group.nodes: + out.append(f"""AnyNode::{node.name}(node) => node.range(),""") + out.append(""" + } + } + } + """) + + for group in groups: + if group.name == "ungrouped": + continue + out.append(f""" + impl AnyNode {{ + pub fn {group.anynode_is_label}(self) -> Option<{group.owned_enum_ty}> {{ + match self {{ + """) + for node in group.nodes: + out.append( + f"""AnyNode::{node.name}(node) => Some({group.owned_enum_ty}::{node.variant}(node)),""" + ) + out.append(""" + _ => None, + } + } + } + """) + + for group in groups: + if group.name == "ungrouped": + continue + out.append(f""" + impl AnyNode {{ + pub const fn is_{group.anynode_is_label}(&self) -> bool {{ + matches!(self, + """) + for i, node in enumerate(group.nodes): + if i > 0: + out.append("|") + out.append(f"""AnyNode::{node.name}(_)""") + out.append(""" + ) + } + } + """) + + out.append(""" + impl AnyNode { + pub const fn as_ref(&self) -> AnyNodeRef { + match self { + """) + for group in groups: + for node in group.nodes: + out.append( + f"""AnyNode::{node.name}(node) => AnyNodeRef::{node.name}(node),""" + ) + out.append(""" + } + } + } + """) + + +# ------------------------------------------------------------------------------ +# AnyNodeRef + + +def write_anynoderef(out: list[str], groups: list[Group]) -> None: + """ + Create the AnyNodeRef type. + + ```rust + pub enum AnyNodeRef<'a> { + ... + TypeParamTypeVar(&'a TypeParamTypeVar), + TypeParamTypeVarTuple(&'a TypeParamTypeVarTuple), + ... + } + ``` + + Also creates: + - `impl<'a> From<&'a TypeParam> for AnyNodeRef<'a>` + - `impl<'a> From> for AnyNodeRef<'a>` + - `impl<'a> From<&'a TypeParamTypeVarTuple> for AnyNodeRef<'a>` + - `impl Ranged for AnyNodeRef<'_>` + - `fn AnyNodeRef::as_ptr(&self) -> std::ptr::NonNull<()>` + - `fn AnyNodeRef::visit_preorder(self, visitor &mut impl SourceOrderVisitor)` + - `fn AnyNode::is_type_param(&self) -> bool` + """ + + out.append(""" + #[derive(Copy, Clone, Debug, is_macro::Is, PartialEq)] + pub enum AnyNodeRef<'a> { + """) + for group in groups: + for node in group.nodes: + out.append(f"""{node.name}(&'a {node.ty}),""") + out.append(""" + } + """) + + for group in groups: + if group.name != "ungrouped": + out.append(f""" + impl<'a> From<&'a {group.owned_enum_ty}> for AnyNodeRef<'a> {{ + fn from(node: &'a {group.owned_enum_ty}) -> AnyNodeRef<'a> {{ + match node {{ + """) + for node in group.nodes: + out.append( + f"""{group.owned_enum_ty}::{node.variant}(node) => AnyNodeRef::{node.name}(node),""" + ) + out.append(""" + } + } + } + """) + + out.append(f""" + impl<'a> From<{group.ref_enum_ty}<'a>> for AnyNodeRef<'a> {{ + fn from(node: {group.ref_enum_ty}<'a>) -> AnyNodeRef<'a> {{ + match node {{ + """) + for node in group.nodes: + out.append( + f"""{group.ref_enum_ty}::{node.variant}(node) => AnyNodeRef::{node.name}(node),""" + ) + out.append(""" + } + } + } + """) + + for node in group.nodes: + out.append(f""" + impl<'a> From<&'a {node.ty}> for AnyNodeRef<'a> {{ + fn from(node: &'a {node.ty}) -> AnyNodeRef<'a> {{ + AnyNodeRef::{node.name}(node) + }} + }} + """) + + out.append(""" + impl ruff_text_size::Ranged for AnyNodeRef<'_> { + fn range(&self) -> ruff_text_size::TextRange { + match self { + """) + for group in groups: + for node in group.nodes: + out.append(f"""AnyNodeRef::{node.name}(node) => node.range(),""") + out.append(""" + } + } + } + """) + + out.append(""" + impl AnyNodeRef<'_> { + pub fn as_ptr(&self) -> std::ptr::NonNull<()> { + match self { + """) + for group in groups: + for node in group.nodes: + out.append( + f"""AnyNodeRef::{node.name}(node) => std::ptr::NonNull::from(*node).cast(),""" + ) + out.append(""" + } + } + } + """) + + out.append(""" + impl<'a> AnyNodeRef<'a> { + pub fn visit_preorder<'b, V>(self, visitor: &mut V) + where + V: crate::visitor::source_order::SourceOrderVisitor<'b> + ?Sized, + 'a: 'b, + { + match self { + """) + for group in groups: + for node in group.nodes: + out.append( + f"""AnyNodeRef::{node.name}(node) => node.visit_source_order(visitor),""" + ) + out.append(""" + } + } + } + """) + + for group in groups: + if group.name == "ungrouped": + continue + out.append(f""" + impl AnyNodeRef<'_> {{ + pub const fn is_{group.anynode_is_label}(self) -> bool {{ + matches!(self, + """) + for i, node in enumerate(group.nodes): + if i > 0: + out.append("|") + out.append(f"""AnyNodeRef::{node.name}(_)""") + out.append(""" + ) + } + } + """) + + +# ------------------------------------------------------------------------------ +# NodeKind + + +def write_nodekind(out: list[str], groups: list[Group]) -> None: + """ + Create the NodeKind type. + + ```rust + pub enum NodeKind { + ... + TypeParamTypeVar, + TypeParamTypeVarTuple, + ... + } + + Also creates: + - `fn AnyNode::kind(&self) -> NodeKind` + - `fn AnyNodeRef::kind(self) -> NodeKind` + ``` + """ + + out.append(""" + #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] + pub enum NodeKind { + """) + for group in groups: + for node in group.nodes: + out.append(f"""{node.name},""") + out.append(""" + } + """) + + out.append(""" + impl AnyNode { + pub const fn kind(&self) -> NodeKind { + match self { + """) + for group in groups: + for node in group.nodes: + out.append(f"""AnyNode::{node.name}(_) => NodeKind::{node.name},""") + out.append(""" + } + } + } + """) + + out.append(""" + impl AnyNodeRef<'_> { + pub const fn kind(self) -> NodeKind { + match self { + """) + for group in groups: + for node in group.nodes: + out.append(f"""AnyNodeRef::{node.name}(_) => NodeKind::{node.name},""") + out.append(""" + } + } + } + """) + + +# ------------------------------------------------------------------------------ +# AstNode + + +def write_astnode(out: list[str], groups: list[Group]) -> None: + """ + Creates AstNode trait impls: + - `impl AstNode for TypeParam` + - `impl AstNode for TypeParamTypeVar` + """ + + for group in groups: + if group.name != "ungrouped": + out.append(f""" + impl crate::AstNode for {group.owned_enum_ty} {{ + type Ref<'a> = {group.ref_enum_ty}<'a>; + + fn cast(node: AnyNode) -> Option + where + Self: Sized, + {{ + match node {{ + """) + for node in group.nodes: + out.append( + f"""AnyNode::{node.name}(node) => Some({group.owned_enum_ty}::{node.variant}(node)),""" + ) + out.append(""" + _ => None, + } + } + + fn cast_ref(node: AnyNodeRef) -> Option> { + match node { + """) + for node in group.nodes: + out.append( + f"""AnyNodeRef::{node.name}(node) => Some({group.ref_enum_ty}::{node.variant}(node)),""" + ) + out.append(""" + _ => None, + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, + """) + for i, node in enumerate(group.nodes): + if i > 0: + out.append("|") + out.append(f"""NodeKind::{node.name}""") + out.append(""" + ) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } + } + """) + + for node in group.nodes: + out.append(f""" + impl crate::AstNode for {node.ty} {{ + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + {{ + if let AnyNode::{node.name}(node) = kind {{ + Some(node) + }} else {{ + None + }} + }} + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {{ + if let AnyNodeRef::{node.name}(node) = kind {{ + Some(node) + }} else {{ + None + }} + }} + + fn can_cast(kind: NodeKind) -> bool {{ + matches!(kind, NodeKind::{node.name}) + }} + + fn as_any_node_ref(&self) -> AnyNodeRef {{ + AnyNodeRef::from(self) + }} + + fn into_any_node(self) -> AnyNode {{ + AnyNode::from(self) + }} + }} + """) + + +# ------------------------------------------------------------------------------ +# Format and write output + + +def generate(groups: list[Group]) -> list[str]: + out = [] + write_preamble(out) + write_owned_enum(out, groups) + write_ref_enum(out, groups) + write_anynode(out, groups) + write_anynoderef(out, groups) + write_nodekind(out, groups) + write_astnode(out, groups) + return out + + +def write_output(root: Path, out: list[str]) -> None: + out_path = root.joinpath("crates", "ruff_python_ast", "src", "generated.rs") + out_path.write_text(rustfmt("\n".join(out))) + + +# ------------------------------------------------------------------------------ +# Main + + +def main() -> None: + root = Path( + check_output(["git", "rev-parse", "--show-toplevel"], text=True).strip() + ) + groups = load_ast(root) + out = generate(groups) + write_output(root, out) + + +if __name__ == "__main__": + main() diff --git a/crates/ruff_python_ast/src/expression.rs b/crates/ruff_python_ast/src/expression.rs index 40bc5ea64f67b..9a17815858a46 100644 --- a/crates/ruff_python_ast/src/expression.rs +++ b/crates/ruff_python_ast/src/expression.rs @@ -4,334 +4,15 @@ use ruff_text_size::{Ranged, TextRange}; use crate::{ self as ast, AnyNodeRef, AnyStringFlags, Expr, ExprBytesLiteral, ExprFString, - ExprStringLiteral, StringFlags, + ExprStringLiteral, ExpressionRef, StringFlags, }; -/// Unowned pendant to [`ast::Expr`] that stores a reference instead of a owned value. -#[derive(Copy, Clone, Debug, PartialEq)] -pub enum ExpressionRef<'a> { - BoolOp(&'a ast::ExprBoolOp), - Named(&'a ast::ExprNamed), - BinOp(&'a ast::ExprBinOp), - UnaryOp(&'a ast::ExprUnaryOp), - Lambda(&'a ast::ExprLambda), - If(&'a ast::ExprIf), - Dict(&'a ast::ExprDict), - Set(&'a ast::ExprSet), - ListComp(&'a ast::ExprListComp), - SetComp(&'a ast::ExprSetComp), - DictComp(&'a ast::ExprDictComp), - Generator(&'a ast::ExprGenerator), - Await(&'a ast::ExprAwait), - Yield(&'a ast::ExprYield), - YieldFrom(&'a ast::ExprYieldFrom), - Compare(&'a ast::ExprCompare), - Call(&'a ast::ExprCall), - FString(&'a ast::ExprFString), - StringLiteral(&'a ast::ExprStringLiteral), - BytesLiteral(&'a ast::ExprBytesLiteral), - NumberLiteral(&'a ast::ExprNumberLiteral), - BooleanLiteral(&'a ast::ExprBooleanLiteral), - NoneLiteral(&'a ast::ExprNoneLiteral), - EllipsisLiteral(&'a ast::ExprEllipsisLiteral), - Attribute(&'a ast::ExprAttribute), - Subscript(&'a ast::ExprSubscript), - Starred(&'a ast::ExprStarred), - Name(&'a ast::ExprName), - List(&'a ast::ExprList), - Tuple(&'a ast::ExprTuple), - Slice(&'a ast::ExprSlice), - IpyEscapeCommand(&'a ast::ExprIpyEscapeCommand), -} - impl<'a> From<&'a Box> for ExpressionRef<'a> { fn from(value: &'a Box) -> Self { ExpressionRef::from(value.as_ref()) } } -impl<'a> From<&'a Expr> for ExpressionRef<'a> { - fn from(value: &'a Expr) -> Self { - match value { - Expr::BoolOp(value) => ExpressionRef::BoolOp(value), - Expr::Named(value) => ExpressionRef::Named(value), - Expr::BinOp(value) => ExpressionRef::BinOp(value), - Expr::UnaryOp(value) => ExpressionRef::UnaryOp(value), - Expr::Lambda(value) => ExpressionRef::Lambda(value), - Expr::If(value) => ExpressionRef::If(value), - Expr::Dict(value) => ExpressionRef::Dict(value), - Expr::Set(value) => ExpressionRef::Set(value), - Expr::ListComp(value) => ExpressionRef::ListComp(value), - Expr::SetComp(value) => ExpressionRef::SetComp(value), - Expr::DictComp(value) => ExpressionRef::DictComp(value), - Expr::Generator(value) => ExpressionRef::Generator(value), - Expr::Await(value) => ExpressionRef::Await(value), - Expr::Yield(value) => ExpressionRef::Yield(value), - Expr::YieldFrom(value) => ExpressionRef::YieldFrom(value), - Expr::Compare(value) => ExpressionRef::Compare(value), - Expr::Call(value) => ExpressionRef::Call(value), - Expr::FString(value) => ExpressionRef::FString(value), - Expr::StringLiteral(value) => ExpressionRef::StringLiteral(value), - Expr::BytesLiteral(value) => ExpressionRef::BytesLiteral(value), - Expr::NumberLiteral(value) => ExpressionRef::NumberLiteral(value), - Expr::BooleanLiteral(value) => ExpressionRef::BooleanLiteral(value), - Expr::NoneLiteral(value) => ExpressionRef::NoneLiteral(value), - Expr::EllipsisLiteral(value) => ExpressionRef::EllipsisLiteral(value), - Expr::Attribute(value) => ExpressionRef::Attribute(value), - Expr::Subscript(value) => ExpressionRef::Subscript(value), - Expr::Starred(value) => ExpressionRef::Starred(value), - Expr::Name(value) => ExpressionRef::Name(value), - Expr::List(value) => ExpressionRef::List(value), - Expr::Tuple(value) => ExpressionRef::Tuple(value), - Expr::Slice(value) => ExpressionRef::Slice(value), - Expr::IpyEscapeCommand(value) => ExpressionRef::IpyEscapeCommand(value), - } - } -} - -impl<'a> From<&'a ast::ExprBoolOp> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprBoolOp) -> Self { - Self::BoolOp(value) - } -} -impl<'a> From<&'a ast::ExprNamed> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprNamed) -> Self { - Self::Named(value) - } -} -impl<'a> From<&'a ast::ExprBinOp> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprBinOp) -> Self { - Self::BinOp(value) - } -} -impl<'a> From<&'a ast::ExprUnaryOp> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprUnaryOp) -> Self { - Self::UnaryOp(value) - } -} -impl<'a> From<&'a ast::ExprLambda> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprLambda) -> Self { - Self::Lambda(value) - } -} -impl<'a> From<&'a ast::ExprIf> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprIf) -> Self { - Self::If(value) - } -} -impl<'a> From<&'a ast::ExprDict> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprDict) -> Self { - Self::Dict(value) - } -} -impl<'a> From<&'a ast::ExprSet> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprSet) -> Self { - Self::Set(value) - } -} -impl<'a> From<&'a ast::ExprListComp> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprListComp) -> Self { - Self::ListComp(value) - } -} -impl<'a> From<&'a ast::ExprSetComp> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprSetComp) -> Self { - Self::SetComp(value) - } -} -impl<'a> From<&'a ast::ExprDictComp> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprDictComp) -> Self { - Self::DictComp(value) - } -} -impl<'a> From<&'a ast::ExprGenerator> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprGenerator) -> Self { - Self::Generator(value) - } -} -impl<'a> From<&'a ast::ExprAwait> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprAwait) -> Self { - Self::Await(value) - } -} -impl<'a> From<&'a ast::ExprYield> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprYield) -> Self { - Self::Yield(value) - } -} -impl<'a> From<&'a ast::ExprYieldFrom> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprYieldFrom) -> Self { - Self::YieldFrom(value) - } -} -impl<'a> From<&'a ast::ExprCompare> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprCompare) -> Self { - Self::Compare(value) - } -} -impl<'a> From<&'a ast::ExprCall> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprCall) -> Self { - Self::Call(value) - } -} -impl<'a> From<&'a ast::ExprFString> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprFString) -> Self { - Self::FString(value) - } -} -impl<'a> From<&'a ast::ExprStringLiteral> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprStringLiteral) -> Self { - Self::StringLiteral(value) - } -} -impl<'a> From<&'a ast::ExprBytesLiteral> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprBytesLiteral) -> Self { - Self::BytesLiteral(value) - } -} -impl<'a> From<&'a ast::ExprNumberLiteral> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprNumberLiteral) -> Self { - Self::NumberLiteral(value) - } -} -impl<'a> From<&'a ast::ExprBooleanLiteral> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprBooleanLiteral) -> Self { - Self::BooleanLiteral(value) - } -} -impl<'a> From<&'a ast::ExprNoneLiteral> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprNoneLiteral) -> Self { - Self::NoneLiteral(value) - } -} -impl<'a> From<&'a ast::ExprEllipsisLiteral> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprEllipsisLiteral) -> Self { - Self::EllipsisLiteral(value) - } -} -impl<'a> From<&'a ast::ExprAttribute> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprAttribute) -> Self { - Self::Attribute(value) - } -} -impl<'a> From<&'a ast::ExprSubscript> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprSubscript) -> Self { - Self::Subscript(value) - } -} -impl<'a> From<&'a ast::ExprStarred> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprStarred) -> Self { - Self::Starred(value) - } -} -impl<'a> From<&'a ast::ExprName> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprName) -> Self { - Self::Name(value) - } -} -impl<'a> From<&'a ast::ExprList> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprList) -> Self { - Self::List(value) - } -} -impl<'a> From<&'a ast::ExprTuple> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprTuple) -> Self { - Self::Tuple(value) - } -} -impl<'a> From<&'a ast::ExprSlice> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprSlice) -> Self { - Self::Slice(value) - } -} -impl<'a> From<&'a ast::ExprIpyEscapeCommand> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprIpyEscapeCommand) -> Self { - Self::IpyEscapeCommand(value) - } -} - -impl<'a> From> for AnyNodeRef<'a> { - fn from(value: ExpressionRef<'a>) -> Self { - match value { - ExpressionRef::BoolOp(expression) => AnyNodeRef::ExprBoolOp(expression), - ExpressionRef::Named(expression) => AnyNodeRef::ExprNamed(expression), - ExpressionRef::BinOp(expression) => AnyNodeRef::ExprBinOp(expression), - ExpressionRef::UnaryOp(expression) => AnyNodeRef::ExprUnaryOp(expression), - ExpressionRef::Lambda(expression) => AnyNodeRef::ExprLambda(expression), - ExpressionRef::If(expression) => AnyNodeRef::ExprIf(expression), - ExpressionRef::Dict(expression) => AnyNodeRef::ExprDict(expression), - ExpressionRef::Set(expression) => AnyNodeRef::ExprSet(expression), - ExpressionRef::ListComp(expression) => AnyNodeRef::ExprListComp(expression), - ExpressionRef::SetComp(expression) => AnyNodeRef::ExprSetComp(expression), - ExpressionRef::DictComp(expression) => AnyNodeRef::ExprDictComp(expression), - ExpressionRef::Generator(expression) => AnyNodeRef::ExprGenerator(expression), - ExpressionRef::Await(expression) => AnyNodeRef::ExprAwait(expression), - ExpressionRef::Yield(expression) => AnyNodeRef::ExprYield(expression), - ExpressionRef::YieldFrom(expression) => AnyNodeRef::ExprYieldFrom(expression), - ExpressionRef::Compare(expression) => AnyNodeRef::ExprCompare(expression), - ExpressionRef::Call(expression) => AnyNodeRef::ExprCall(expression), - ExpressionRef::FString(expression) => AnyNodeRef::ExprFString(expression), - ExpressionRef::StringLiteral(expression) => AnyNodeRef::ExprStringLiteral(expression), - ExpressionRef::BytesLiteral(expression) => AnyNodeRef::ExprBytesLiteral(expression), - ExpressionRef::NumberLiteral(expression) => AnyNodeRef::ExprNumberLiteral(expression), - ExpressionRef::BooleanLiteral(expression) => AnyNodeRef::ExprBooleanLiteral(expression), - ExpressionRef::NoneLiteral(expression) => AnyNodeRef::ExprNoneLiteral(expression), - ExpressionRef::EllipsisLiteral(expression) => { - AnyNodeRef::ExprEllipsisLiteral(expression) - } - ExpressionRef::Attribute(expression) => AnyNodeRef::ExprAttribute(expression), - ExpressionRef::Subscript(expression) => AnyNodeRef::ExprSubscript(expression), - ExpressionRef::Starred(expression) => AnyNodeRef::ExprStarred(expression), - ExpressionRef::Name(expression) => AnyNodeRef::ExprName(expression), - ExpressionRef::List(expression) => AnyNodeRef::ExprList(expression), - ExpressionRef::Tuple(expression) => AnyNodeRef::ExprTuple(expression), - ExpressionRef::Slice(expression) => AnyNodeRef::ExprSlice(expression), - ExpressionRef::IpyEscapeCommand(expression) => { - AnyNodeRef::ExprIpyEscapeCommand(expression) - } - } - } -} - -impl Ranged for ExpressionRef<'_> { - fn range(&self) -> TextRange { - match self { - ExpressionRef::BoolOp(expression) => expression.range(), - ExpressionRef::Named(expression) => expression.range(), - ExpressionRef::BinOp(expression) => expression.range(), - ExpressionRef::UnaryOp(expression) => expression.range(), - ExpressionRef::Lambda(expression) => expression.range(), - ExpressionRef::If(expression) => expression.range(), - ExpressionRef::Dict(expression) => expression.range(), - ExpressionRef::Set(expression) => expression.range(), - ExpressionRef::ListComp(expression) => expression.range(), - ExpressionRef::SetComp(expression) => expression.range(), - ExpressionRef::DictComp(expression) => expression.range(), - ExpressionRef::Generator(expression) => expression.range(), - ExpressionRef::Await(expression) => expression.range(), - ExpressionRef::Yield(expression) => expression.range(), - ExpressionRef::YieldFrom(expression) => expression.range(), - ExpressionRef::Compare(expression) => expression.range(), - ExpressionRef::Call(expression) => expression.range(), - ExpressionRef::FString(expression) => expression.range(), - ExpressionRef::StringLiteral(expression) => expression.range(), - ExpressionRef::BytesLiteral(expression) => expression.range(), - ExpressionRef::NumberLiteral(expression) => expression.range(), - ExpressionRef::BooleanLiteral(expression) => expression.range(), - ExpressionRef::NoneLiteral(expression) => expression.range(), - ExpressionRef::EllipsisLiteral(expression) => expression.range(), - ExpressionRef::Attribute(expression) => expression.range(), - ExpressionRef::Subscript(expression) => expression.range(), - ExpressionRef::Starred(expression) => expression.range(), - ExpressionRef::Name(expression) => expression.range(), - ExpressionRef::List(expression) => expression.range(), - ExpressionRef::Tuple(expression) => expression.range(), - ExpressionRef::Slice(expression) => expression.range(), - ExpressionRef::IpyEscapeCommand(expression) => expression.range(), - } - } -} - /// Unowned pendant to all the literal variants of [`ast::Expr`] that stores a /// reference instead of an owned value. #[derive(Copy, Clone, Debug, PartialEq, is_macro::Is)] diff --git a/crates/ruff_python_ast/src/generated.rs b/crates/ruff_python_ast/src/generated.rs new file mode 100644 index 0000000000000..ac2ef3611cffc --- /dev/null +++ b/crates/ruff_python_ast/src/generated.rs @@ -0,0 +1,8790 @@ +// This is a generated file. Don't modify it by hand! +// Run `crates/ruff_python_ast/generate.py` to re-generate the file. + +/// See also [mod](https://docs.python.org/3/library/ast.html#ast.mod) +#[derive(Clone, Debug, PartialEq, is_macro::Is)] +pub enum Mod { + Module(crate::ModModule), + Expression(crate::ModExpression), +} + +impl From for Mod { + fn from(node: crate::ModModule) -> Self { + Self::Module(node) + } +} + +impl From for Mod { + fn from(node: crate::ModExpression) -> Self { + Self::Expression(node) + } +} + +impl ruff_text_size::Ranged for Mod { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::Module(node) => node.range(), + Self::Expression(node) => node.range(), + } + } +} + +/// See also [stmt](https://docs.python.org/3/library/ast.html#ast.stmt) +#[derive(Clone, Debug, PartialEq, is_macro::Is)] +pub enum Stmt { + #[is(name = "function_def_stmt")] + FunctionDef(crate::StmtFunctionDef), + #[is(name = "class_def_stmt")] + ClassDef(crate::StmtClassDef), + #[is(name = "return_stmt")] + Return(crate::StmtReturn), + #[is(name = "delete_stmt")] + Delete(crate::StmtDelete), + #[is(name = "type_alias_stmt")] + TypeAlias(crate::StmtTypeAlias), + #[is(name = "assign_stmt")] + Assign(crate::StmtAssign), + #[is(name = "aug_assign_stmt")] + AugAssign(crate::StmtAugAssign), + #[is(name = "ann_assign_stmt")] + AnnAssign(crate::StmtAnnAssign), + #[is(name = "for_stmt")] + For(crate::StmtFor), + #[is(name = "while_stmt")] + While(crate::StmtWhile), + #[is(name = "if_stmt")] + If(crate::StmtIf), + #[is(name = "with_stmt")] + With(crate::StmtWith), + #[is(name = "match_stmt")] + Match(crate::StmtMatch), + #[is(name = "raise_stmt")] + Raise(crate::StmtRaise), + #[is(name = "try_stmt")] + Try(crate::StmtTry), + #[is(name = "assert_stmt")] + Assert(crate::StmtAssert), + #[is(name = "import_stmt")] + Import(crate::StmtImport), + #[is(name = "import_from_stmt")] + ImportFrom(crate::StmtImportFrom), + #[is(name = "global_stmt")] + Global(crate::StmtGlobal), + #[is(name = "nonlocal_stmt")] + Nonlocal(crate::StmtNonlocal), + #[is(name = "expr_stmt")] + Expr(crate::StmtExpr), + #[is(name = "pass_stmt")] + Pass(crate::StmtPass), + #[is(name = "break_stmt")] + Break(crate::StmtBreak), + #[is(name = "continue_stmt")] + Continue(crate::StmtContinue), + #[is(name = "ipy_escape_command_stmt")] + IpyEscapeCommand(crate::StmtIpyEscapeCommand), +} + +impl From for Stmt { + fn from(node: crate::StmtFunctionDef) -> Self { + Self::FunctionDef(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtClassDef) -> Self { + Self::ClassDef(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtReturn) -> Self { + Self::Return(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtDelete) -> Self { + Self::Delete(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtTypeAlias) -> Self { + Self::TypeAlias(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtAssign) -> Self { + Self::Assign(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtAugAssign) -> Self { + Self::AugAssign(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtAnnAssign) -> Self { + Self::AnnAssign(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtFor) -> Self { + Self::For(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtWhile) -> Self { + Self::While(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtIf) -> Self { + Self::If(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtWith) -> Self { + Self::With(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtMatch) -> Self { + Self::Match(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtRaise) -> Self { + Self::Raise(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtTry) -> Self { + Self::Try(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtAssert) -> Self { + Self::Assert(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtImport) -> Self { + Self::Import(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtImportFrom) -> Self { + Self::ImportFrom(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtGlobal) -> Self { + Self::Global(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtNonlocal) -> Self { + Self::Nonlocal(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtExpr) -> Self { + Self::Expr(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtPass) -> Self { + Self::Pass(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtBreak) -> Self { + Self::Break(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtContinue) -> Self { + Self::Continue(node) + } +} + +impl From for Stmt { + fn from(node: crate::StmtIpyEscapeCommand) -> Self { + Self::IpyEscapeCommand(node) + } +} + +impl ruff_text_size::Ranged for Stmt { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::FunctionDef(node) => node.range(), + 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(), + Self::For(node) => node.range(), + Self::While(node) => node.range(), + Self::If(node) => node.range(), + Self::With(node) => node.range(), + Self::Match(node) => node.range(), + Self::Raise(node) => node.range(), + Self::Try(node) => node.range(), + Self::Assert(node) => node.range(), + Self::Import(node) => node.range(), + Self::ImportFrom(node) => node.range(), + Self::Global(node) => node.range(), + Self::Nonlocal(node) => node.range(), + Self::Expr(node) => node.range(), + Self::Pass(node) => node.range(), + Self::Break(node) => node.range(), + Self::Continue(node) => node.range(), + Self::IpyEscapeCommand(node) => node.range(), + } + } +} + +/// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr) +#[derive(Clone, Debug, PartialEq, is_macro::Is)] +pub enum Expr { + #[is(name = "bool_op_expr")] + BoolOp(crate::ExprBoolOp), + #[is(name = "named_expr")] + Named(crate::ExprNamed), + #[is(name = "bin_op_expr")] + BinOp(crate::ExprBinOp), + #[is(name = "unary_op_expr")] + UnaryOp(crate::ExprUnaryOp), + #[is(name = "lambda_expr")] + Lambda(crate::ExprLambda), + #[is(name = "if_expr")] + If(crate::ExprIf), + #[is(name = "dict_expr")] + Dict(crate::ExprDict), + #[is(name = "set_expr")] + Set(crate::ExprSet), + #[is(name = "list_comp_expr")] + ListComp(crate::ExprListComp), + #[is(name = "set_comp_expr")] + SetComp(crate::ExprSetComp), + #[is(name = "dict_comp_expr")] + DictComp(crate::ExprDictComp), + #[is(name = "generator_expr")] + Generator(crate::ExprGenerator), + #[is(name = "await_expr")] + Await(crate::ExprAwait), + #[is(name = "yield_expr")] + Yield(crate::ExprYield), + #[is(name = "yield_from_expr")] + YieldFrom(crate::ExprYieldFrom), + #[is(name = "compare_expr")] + Compare(crate::ExprCompare), + #[is(name = "call_expr")] + Call(crate::ExprCall), + #[is(name = "f_string_expr")] + FString(crate::ExprFString), + #[is(name = "string_literal_expr")] + StringLiteral(crate::ExprStringLiteral), + #[is(name = "bytes_literal_expr")] + BytesLiteral(crate::ExprBytesLiteral), + #[is(name = "number_literal_expr")] + NumberLiteral(crate::ExprNumberLiteral), + #[is(name = "boolean_literal_expr")] + BooleanLiteral(crate::ExprBooleanLiteral), + #[is(name = "none_literal_expr")] + NoneLiteral(crate::ExprNoneLiteral), + #[is(name = "ellipsis_literal_expr")] + EllipsisLiteral(crate::ExprEllipsisLiteral), + #[is(name = "attribute_expr")] + Attribute(crate::ExprAttribute), + #[is(name = "subscript_expr")] + Subscript(crate::ExprSubscript), + #[is(name = "starred_expr")] + Starred(crate::ExprStarred), + #[is(name = "name_expr")] + Name(crate::ExprName), + #[is(name = "list_expr")] + List(crate::ExprList), + #[is(name = "tuple_expr")] + Tuple(crate::ExprTuple), + #[is(name = "slice_expr")] + Slice(crate::ExprSlice), + #[is(name = "ipy_escape_command_expr")] + IpyEscapeCommand(crate::ExprIpyEscapeCommand), +} + +impl From for Expr { + fn from(node: crate::ExprBoolOp) -> Self { + Self::BoolOp(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprNamed) -> Self { + Self::Named(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprBinOp) -> Self { + Self::BinOp(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprUnaryOp) -> Self { + Self::UnaryOp(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprLambda) -> Self { + Self::Lambda(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprIf) -> Self { + Self::If(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprDict) -> Self { + Self::Dict(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprSet) -> Self { + Self::Set(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprListComp) -> Self { + Self::ListComp(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprSetComp) -> Self { + Self::SetComp(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprDictComp) -> Self { + Self::DictComp(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprGenerator) -> Self { + Self::Generator(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprAwait) -> Self { + Self::Await(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprYield) -> Self { + Self::Yield(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprYieldFrom) -> Self { + Self::YieldFrom(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprCompare) -> Self { + Self::Compare(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprCall) -> Self { + Self::Call(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprFString) -> Self { + Self::FString(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprStringLiteral) -> Self { + Self::StringLiteral(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprBytesLiteral) -> Self { + Self::BytesLiteral(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprNumberLiteral) -> Self { + Self::NumberLiteral(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprBooleanLiteral) -> Self { + Self::BooleanLiteral(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprNoneLiteral) -> Self { + Self::NoneLiteral(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprEllipsisLiteral) -> Self { + Self::EllipsisLiteral(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprAttribute) -> Self { + Self::Attribute(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprSubscript) -> Self { + Self::Subscript(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprStarred) -> Self { + Self::Starred(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprName) -> Self { + Self::Name(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprList) -> Self { + Self::List(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprTuple) -> Self { + Self::Tuple(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprSlice) -> Self { + Self::Slice(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprIpyEscapeCommand) -> Self { + Self::IpyEscapeCommand(node) + } +} + +impl ruff_text_size::Ranged for Expr { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::BoolOp(node) => node.range(), + Self::Named(node) => node.range(), + Self::BinOp(node) => node.range(), + Self::UnaryOp(node) => node.range(), + Self::Lambda(node) => node.range(), + Self::If(node) => node.range(), + Self::Dict(node) => node.range(), + Self::Set(node) => node.range(), + Self::ListComp(node) => node.range(), + Self::SetComp(node) => node.range(), + Self::DictComp(node) => node.range(), + Self::Generator(node) => node.range(), + Self::Await(node) => node.range(), + Self::Yield(node) => node.range(), + Self::YieldFrom(node) => node.range(), + Self::Compare(node) => node.range(), + Self::Call(node) => node.range(), + Self::FString(node) => node.range(), + Self::StringLiteral(node) => node.range(), + Self::BytesLiteral(node) => node.range(), + Self::NumberLiteral(node) => node.range(), + Self::BooleanLiteral(node) => node.range(), + Self::NoneLiteral(node) => node.range(), + Self::EllipsisLiteral(node) => node.range(), + Self::Attribute(node) => node.range(), + Self::Subscript(node) => node.range(), + Self::Starred(node) => node.range(), + Self::Name(node) => node.range(), + Self::List(node) => node.range(), + Self::Tuple(node) => node.range(), + Self::Slice(node) => node.range(), + Self::IpyEscapeCommand(node) => node.range(), + } + } +} + +/// See also [excepthandler](https://docs.python.org/3/library/ast.html#ast.excepthandler) +#[derive(Clone, Debug, PartialEq, is_macro::Is)] +pub enum ExceptHandler { + ExceptHandler(crate::ExceptHandlerExceptHandler), +} + +impl From for ExceptHandler { + fn from(node: crate::ExceptHandlerExceptHandler) -> Self { + Self::ExceptHandler(node) + } +} + +impl ruff_text_size::Ranged for ExceptHandler { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::ExceptHandler(node) => node.range(), + } + } +} + +#[derive(Clone, Debug, PartialEq, is_macro::Is)] +pub enum FStringElement { + Expression(crate::FStringExpressionElement), + Literal(crate::FStringLiteralElement), +} + +impl From for FStringElement { + fn from(node: crate::FStringExpressionElement) -> Self { + Self::Expression(node) + } +} + +impl From for FStringElement { + fn from(node: crate::FStringLiteralElement) -> Self { + Self::Literal(node) + } +} + +impl ruff_text_size::Ranged for FStringElement { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::Expression(node) => node.range(), + Self::Literal(node) => node.range(), + } + } +} + +/// See also [pattern](https://docs.python.org/3/library/ast.html#ast.pattern) +#[derive(Clone, Debug, PartialEq, is_macro::Is)] +pub enum Pattern { + MatchValue(crate::PatternMatchValue), + MatchSingleton(crate::PatternMatchSingleton), + MatchSequence(crate::PatternMatchSequence), + MatchMapping(crate::PatternMatchMapping), + MatchClass(crate::PatternMatchClass), + MatchStar(crate::PatternMatchStar), + MatchAs(crate::PatternMatchAs), + MatchOr(crate::PatternMatchOr), +} + +impl From for Pattern { + fn from(node: crate::PatternMatchValue) -> Self { + Self::MatchValue(node) + } +} + +impl From for Pattern { + fn from(node: crate::PatternMatchSingleton) -> Self { + Self::MatchSingleton(node) + } +} + +impl From for Pattern { + fn from(node: crate::PatternMatchSequence) -> Self { + Self::MatchSequence(node) + } +} + +impl From for Pattern { + fn from(node: crate::PatternMatchMapping) -> Self { + Self::MatchMapping(node) + } +} + +impl From for Pattern { + fn from(node: crate::PatternMatchClass) -> Self { + Self::MatchClass(node) + } +} + +impl From for Pattern { + fn from(node: crate::PatternMatchStar) -> Self { + Self::MatchStar(node) + } +} + +impl From for Pattern { + fn from(node: crate::PatternMatchAs) -> Self { + Self::MatchAs(node) + } +} + +impl From for Pattern { + fn from(node: crate::PatternMatchOr) -> Self { + Self::MatchOr(node) + } +} + +impl ruff_text_size::Ranged for Pattern { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::MatchValue(node) => node.range(), + Self::MatchSingleton(node) => node.range(), + Self::MatchSequence(node) => node.range(), + Self::MatchMapping(node) => node.range(), + Self::MatchClass(node) => node.range(), + Self::MatchStar(node) => node.range(), + Self::MatchAs(node) => node.range(), + Self::MatchOr(node) => node.range(), + } + } +} + +/// 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(crate::TypeParamTypeVar), + TypeVarTuple(crate::TypeParamTypeVarTuple), + ParamSpec(crate::TypeParamParamSpec), +} + +impl From for TypeParam { + fn from(node: crate::TypeParamTypeVar) -> Self { + Self::TypeVar(node) + } +} + +impl From for TypeParam { + fn from(node: crate::TypeParamTypeVarTuple) -> Self { + Self::TypeVarTuple(node) + } +} + +impl From for TypeParam { + fn from(node: crate::TypeParamParamSpec) -> Self { + Self::ParamSpec(node) + } +} + +impl ruff_text_size::Ranged for TypeParam { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::TypeVar(node) => node.range(), + Self::TypeVarTuple(node) => node.range(), + Self::ParamSpec(node) => node.range(), + } + } +} + +impl ruff_text_size::Ranged for crate::ModModule { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ModExpression { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtFunctionDef { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtClassDef { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtReturn { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtDelete { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtTypeAlias { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtAssign { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtAugAssign { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtAnnAssign { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtFor { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtWhile { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtIf { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtWith { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtMatch { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtRaise { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtTry { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtAssert { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtImport { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtImportFrom { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtGlobal { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtNonlocal { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtExpr { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtPass { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtBreak { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtContinue { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StmtIpyEscapeCommand { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprBoolOp { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprNamed { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprBinOp { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprUnaryOp { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprLambda { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprIf { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprDict { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprSet { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprListComp { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprSetComp { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprDictComp { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprGenerator { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprAwait { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprYield { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprYieldFrom { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprCompare { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprCall { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprFString { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprStringLiteral { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprBytesLiteral { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprNumberLiteral { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprBooleanLiteral { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprNoneLiteral { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprEllipsisLiteral { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprAttribute { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprSubscript { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprStarred { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprName { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprList { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprTuple { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprSlice { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExprIpyEscapeCommand { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ExceptHandlerExceptHandler { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::FStringExpressionElement { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::FStringLiteralElement { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::PatternMatchValue { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::PatternMatchSingleton { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::PatternMatchSequence { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::PatternMatchMapping { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::PatternMatchClass { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::PatternMatchStar { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::PatternMatchAs { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::PatternMatchOr { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::TypeParamTypeVar { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::TypeParamTypeVarTuple { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::TypeParamParamSpec { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::FStringFormatSpec { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::PatternArguments { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::PatternKeyword { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::Comprehension { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::Arguments { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::Parameters { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::Parameter { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ParameterWithDefault { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::Keyword { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::Alias { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::WithItem { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::MatchCase { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::Decorator { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::ElifElseClause { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::TypeParams { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::FString { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::StringLiteral { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::BytesLiteral { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl ruff_text_size::Ranged for crate::Identifier { + fn range(&self) -> ruff_text_size::TextRange { + self.range + } +} + +impl Mod { + #[allow(unused)] + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) + where + V: crate::visitor::source_order::SourceOrderVisitor<'a> + ?Sized, + { + match self { + Mod::Module(node) => node.visit_source_order(visitor), + Mod::Expression(node) => node.visit_source_order(visitor), + } + } +} + +impl Stmt { + #[allow(unused)] + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) + where + V: crate::visitor::source_order::SourceOrderVisitor<'a> + ?Sized, + { + match self { + Stmt::FunctionDef(node) => node.visit_source_order(visitor), + Stmt::ClassDef(node) => node.visit_source_order(visitor), + Stmt::Return(node) => node.visit_source_order(visitor), + Stmt::Delete(node) => node.visit_source_order(visitor), + Stmt::TypeAlias(node) => node.visit_source_order(visitor), + Stmt::Assign(node) => node.visit_source_order(visitor), + Stmt::AugAssign(node) => node.visit_source_order(visitor), + Stmt::AnnAssign(node) => node.visit_source_order(visitor), + Stmt::For(node) => node.visit_source_order(visitor), + Stmt::While(node) => node.visit_source_order(visitor), + Stmt::If(node) => node.visit_source_order(visitor), + Stmt::With(node) => node.visit_source_order(visitor), + Stmt::Match(node) => node.visit_source_order(visitor), + Stmt::Raise(node) => node.visit_source_order(visitor), + Stmt::Try(node) => node.visit_source_order(visitor), + Stmt::Assert(node) => node.visit_source_order(visitor), + Stmt::Import(node) => node.visit_source_order(visitor), + Stmt::ImportFrom(node) => node.visit_source_order(visitor), + Stmt::Global(node) => node.visit_source_order(visitor), + Stmt::Nonlocal(node) => node.visit_source_order(visitor), + Stmt::Expr(node) => node.visit_source_order(visitor), + Stmt::Pass(node) => node.visit_source_order(visitor), + Stmt::Break(node) => node.visit_source_order(visitor), + Stmt::Continue(node) => node.visit_source_order(visitor), + Stmt::IpyEscapeCommand(node) => node.visit_source_order(visitor), + } + } +} + +impl Expr { + #[allow(unused)] + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) + where + V: crate::visitor::source_order::SourceOrderVisitor<'a> + ?Sized, + { + match self { + Expr::BoolOp(node) => node.visit_source_order(visitor), + Expr::Named(node) => node.visit_source_order(visitor), + Expr::BinOp(node) => node.visit_source_order(visitor), + Expr::UnaryOp(node) => node.visit_source_order(visitor), + Expr::Lambda(node) => node.visit_source_order(visitor), + Expr::If(node) => node.visit_source_order(visitor), + Expr::Dict(node) => node.visit_source_order(visitor), + Expr::Set(node) => node.visit_source_order(visitor), + Expr::ListComp(node) => node.visit_source_order(visitor), + Expr::SetComp(node) => node.visit_source_order(visitor), + Expr::DictComp(node) => node.visit_source_order(visitor), + Expr::Generator(node) => node.visit_source_order(visitor), + Expr::Await(node) => node.visit_source_order(visitor), + Expr::Yield(node) => node.visit_source_order(visitor), + Expr::YieldFrom(node) => node.visit_source_order(visitor), + Expr::Compare(node) => node.visit_source_order(visitor), + Expr::Call(node) => node.visit_source_order(visitor), + Expr::FString(node) => node.visit_source_order(visitor), + Expr::StringLiteral(node) => node.visit_source_order(visitor), + Expr::BytesLiteral(node) => node.visit_source_order(visitor), + Expr::NumberLiteral(node) => node.visit_source_order(visitor), + Expr::BooleanLiteral(node) => node.visit_source_order(visitor), + Expr::NoneLiteral(node) => node.visit_source_order(visitor), + Expr::EllipsisLiteral(node) => node.visit_source_order(visitor), + Expr::Attribute(node) => node.visit_source_order(visitor), + Expr::Subscript(node) => node.visit_source_order(visitor), + Expr::Starred(node) => node.visit_source_order(visitor), + Expr::Name(node) => node.visit_source_order(visitor), + Expr::List(node) => node.visit_source_order(visitor), + Expr::Tuple(node) => node.visit_source_order(visitor), + Expr::Slice(node) => node.visit_source_order(visitor), + Expr::IpyEscapeCommand(node) => node.visit_source_order(visitor), + } + } +} + +impl ExceptHandler { + #[allow(unused)] + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) + where + V: crate::visitor::source_order::SourceOrderVisitor<'a> + ?Sized, + { + match self { + ExceptHandler::ExceptHandler(node) => node.visit_source_order(visitor), + } + } +} + +impl FStringElement { + #[allow(unused)] + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) + where + V: crate::visitor::source_order::SourceOrderVisitor<'a> + ?Sized, + { + match self { + FStringElement::Expression(node) => node.visit_source_order(visitor), + FStringElement::Literal(node) => node.visit_source_order(visitor), + } + } +} + +impl Pattern { + #[allow(unused)] + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) + where + V: crate::visitor::source_order::SourceOrderVisitor<'a> + ?Sized, + { + match self { + Pattern::MatchValue(node) => node.visit_source_order(visitor), + Pattern::MatchSingleton(node) => node.visit_source_order(visitor), + Pattern::MatchSequence(node) => node.visit_source_order(visitor), + Pattern::MatchMapping(node) => node.visit_source_order(visitor), + Pattern::MatchClass(node) => node.visit_source_order(visitor), + Pattern::MatchStar(node) => node.visit_source_order(visitor), + Pattern::MatchAs(node) => node.visit_source_order(visitor), + Pattern::MatchOr(node) => node.visit_source_order(visitor), + } + } +} + +impl TypeParam { + #[allow(unused)] + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) + where + V: crate::visitor::source_order::SourceOrderVisitor<'a> + ?Sized, + { + match self { + TypeParam::TypeVar(node) => node.visit_source_order(visitor), + TypeParam::TypeVarTuple(node) => node.visit_source_order(visitor), + TypeParam::ParamSpec(node) => node.visit_source_order(visitor), + } + } +} + +/// See also [mod](https://docs.python.org/3/library/ast.html#ast.mod) +#[derive(Clone, Copy, Debug, PartialEq, is_macro::Is)] +pub enum ModRef<'a> { + Module(&'a crate::ModModule), + Expression(&'a crate::ModExpression), +} + +impl<'a> From<&'a Mod> for ModRef<'a> { + fn from(node: &'a Mod) -> Self { + match node { + Mod::Module(node) => ModRef::Module(node), + Mod::Expression(node) => ModRef::Expression(node), + } + } +} + +impl<'a> From<&'a crate::ModModule> for ModRef<'a> { + fn from(node: &'a crate::ModModule) -> Self { + Self::Module(node) + } +} + +impl<'a> From<&'a crate::ModExpression> for ModRef<'a> { + fn from(node: &'a crate::ModExpression) -> Self { + Self::Expression(node) + } +} + +impl ruff_text_size::Ranged for ModRef<'_> { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::Module(node) => node.range(), + Self::Expression(node) => node.range(), + } + } +} + +/// See also [stmt](https://docs.python.org/3/library/ast.html#ast.stmt) +#[derive(Clone, Copy, Debug, PartialEq, is_macro::Is)] +pub enum StatementRef<'a> { + #[is(name = "function_def_stmt")] + FunctionDef(&'a crate::StmtFunctionDef), + #[is(name = "class_def_stmt")] + ClassDef(&'a crate::StmtClassDef), + #[is(name = "return_stmt")] + Return(&'a crate::StmtReturn), + #[is(name = "delete_stmt")] + Delete(&'a crate::StmtDelete), + #[is(name = "type_alias_stmt")] + TypeAlias(&'a crate::StmtTypeAlias), + #[is(name = "assign_stmt")] + Assign(&'a crate::StmtAssign), + #[is(name = "aug_assign_stmt")] + AugAssign(&'a crate::StmtAugAssign), + #[is(name = "ann_assign_stmt")] + AnnAssign(&'a crate::StmtAnnAssign), + #[is(name = "for_stmt")] + For(&'a crate::StmtFor), + #[is(name = "while_stmt")] + While(&'a crate::StmtWhile), + #[is(name = "if_stmt")] + If(&'a crate::StmtIf), + #[is(name = "with_stmt")] + With(&'a crate::StmtWith), + #[is(name = "match_stmt")] + Match(&'a crate::StmtMatch), + #[is(name = "raise_stmt")] + Raise(&'a crate::StmtRaise), + #[is(name = "try_stmt")] + Try(&'a crate::StmtTry), + #[is(name = "assert_stmt")] + Assert(&'a crate::StmtAssert), + #[is(name = "import_stmt")] + Import(&'a crate::StmtImport), + #[is(name = "import_from_stmt")] + ImportFrom(&'a crate::StmtImportFrom), + #[is(name = "global_stmt")] + Global(&'a crate::StmtGlobal), + #[is(name = "nonlocal_stmt")] + Nonlocal(&'a crate::StmtNonlocal), + #[is(name = "expr_stmt")] + Expr(&'a crate::StmtExpr), + #[is(name = "pass_stmt")] + Pass(&'a crate::StmtPass), + #[is(name = "break_stmt")] + Break(&'a crate::StmtBreak), + #[is(name = "continue_stmt")] + Continue(&'a crate::StmtContinue), + #[is(name = "ipy_escape_command_stmt")] + IpyEscapeCommand(&'a crate::StmtIpyEscapeCommand), +} + +impl<'a> From<&'a Stmt> for StatementRef<'a> { + fn from(node: &'a Stmt) -> Self { + match node { + Stmt::FunctionDef(node) => StatementRef::FunctionDef(node), + Stmt::ClassDef(node) => StatementRef::ClassDef(node), + Stmt::Return(node) => StatementRef::Return(node), + Stmt::Delete(node) => StatementRef::Delete(node), + Stmt::TypeAlias(node) => StatementRef::TypeAlias(node), + Stmt::Assign(node) => StatementRef::Assign(node), + Stmt::AugAssign(node) => StatementRef::AugAssign(node), + Stmt::AnnAssign(node) => StatementRef::AnnAssign(node), + Stmt::For(node) => StatementRef::For(node), + Stmt::While(node) => StatementRef::While(node), + Stmt::If(node) => StatementRef::If(node), + Stmt::With(node) => StatementRef::With(node), + Stmt::Match(node) => StatementRef::Match(node), + Stmt::Raise(node) => StatementRef::Raise(node), + Stmt::Try(node) => StatementRef::Try(node), + Stmt::Assert(node) => StatementRef::Assert(node), + Stmt::Import(node) => StatementRef::Import(node), + Stmt::ImportFrom(node) => StatementRef::ImportFrom(node), + Stmt::Global(node) => StatementRef::Global(node), + Stmt::Nonlocal(node) => StatementRef::Nonlocal(node), + Stmt::Expr(node) => StatementRef::Expr(node), + Stmt::Pass(node) => StatementRef::Pass(node), + Stmt::Break(node) => StatementRef::Break(node), + Stmt::Continue(node) => StatementRef::Continue(node), + Stmt::IpyEscapeCommand(node) => StatementRef::IpyEscapeCommand(node), + } + } +} + +impl<'a> From<&'a crate::StmtFunctionDef> for StatementRef<'a> { + fn from(node: &'a crate::StmtFunctionDef) -> Self { + Self::FunctionDef(node) + } +} + +impl<'a> From<&'a crate::StmtClassDef> for StatementRef<'a> { + fn from(node: &'a crate::StmtClassDef) -> Self { + Self::ClassDef(node) + } +} + +impl<'a> From<&'a crate::StmtReturn> for StatementRef<'a> { + fn from(node: &'a crate::StmtReturn) -> Self { + Self::Return(node) + } +} + +impl<'a> From<&'a crate::StmtDelete> for StatementRef<'a> { + fn from(node: &'a crate::StmtDelete) -> Self { + Self::Delete(node) + } +} + +impl<'a> From<&'a crate::StmtTypeAlias> for StatementRef<'a> { + fn from(node: &'a crate::StmtTypeAlias) -> Self { + Self::TypeAlias(node) + } +} + +impl<'a> From<&'a crate::StmtAssign> for StatementRef<'a> { + fn from(node: &'a crate::StmtAssign) -> Self { + Self::Assign(node) + } +} + +impl<'a> From<&'a crate::StmtAugAssign> for StatementRef<'a> { + fn from(node: &'a crate::StmtAugAssign) -> Self { + Self::AugAssign(node) + } +} + +impl<'a> From<&'a crate::StmtAnnAssign> for StatementRef<'a> { + fn from(node: &'a crate::StmtAnnAssign) -> Self { + Self::AnnAssign(node) + } +} + +impl<'a> From<&'a crate::StmtFor> for StatementRef<'a> { + fn from(node: &'a crate::StmtFor) -> Self { + Self::For(node) + } +} + +impl<'a> From<&'a crate::StmtWhile> for StatementRef<'a> { + fn from(node: &'a crate::StmtWhile) -> Self { + Self::While(node) + } +} + +impl<'a> From<&'a crate::StmtIf> for StatementRef<'a> { + fn from(node: &'a crate::StmtIf) -> Self { + Self::If(node) + } +} + +impl<'a> From<&'a crate::StmtWith> for StatementRef<'a> { + fn from(node: &'a crate::StmtWith) -> Self { + Self::With(node) + } +} + +impl<'a> From<&'a crate::StmtMatch> for StatementRef<'a> { + fn from(node: &'a crate::StmtMatch) -> Self { + Self::Match(node) + } +} + +impl<'a> From<&'a crate::StmtRaise> for StatementRef<'a> { + fn from(node: &'a crate::StmtRaise) -> Self { + Self::Raise(node) + } +} + +impl<'a> From<&'a crate::StmtTry> for StatementRef<'a> { + fn from(node: &'a crate::StmtTry) -> Self { + Self::Try(node) + } +} + +impl<'a> From<&'a crate::StmtAssert> for StatementRef<'a> { + fn from(node: &'a crate::StmtAssert) -> Self { + Self::Assert(node) + } +} + +impl<'a> From<&'a crate::StmtImport> for StatementRef<'a> { + fn from(node: &'a crate::StmtImport) -> Self { + Self::Import(node) + } +} + +impl<'a> From<&'a crate::StmtImportFrom> for StatementRef<'a> { + fn from(node: &'a crate::StmtImportFrom) -> Self { + Self::ImportFrom(node) + } +} + +impl<'a> From<&'a crate::StmtGlobal> for StatementRef<'a> { + fn from(node: &'a crate::StmtGlobal) -> Self { + Self::Global(node) + } +} + +impl<'a> From<&'a crate::StmtNonlocal> for StatementRef<'a> { + fn from(node: &'a crate::StmtNonlocal) -> Self { + Self::Nonlocal(node) + } +} + +impl<'a> From<&'a crate::StmtExpr> for StatementRef<'a> { + fn from(node: &'a crate::StmtExpr) -> Self { + Self::Expr(node) + } +} + +impl<'a> From<&'a crate::StmtPass> for StatementRef<'a> { + fn from(node: &'a crate::StmtPass) -> Self { + Self::Pass(node) + } +} + +impl<'a> From<&'a crate::StmtBreak> for StatementRef<'a> { + fn from(node: &'a crate::StmtBreak) -> Self { + Self::Break(node) + } +} + +impl<'a> From<&'a crate::StmtContinue> for StatementRef<'a> { + fn from(node: &'a crate::StmtContinue) -> Self { + Self::Continue(node) + } +} + +impl<'a> From<&'a crate::StmtIpyEscapeCommand> for StatementRef<'a> { + fn from(node: &'a crate::StmtIpyEscapeCommand) -> Self { + Self::IpyEscapeCommand(node) + } +} + +impl ruff_text_size::Ranged for StatementRef<'_> { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::FunctionDef(node) => node.range(), + 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(), + Self::For(node) => node.range(), + Self::While(node) => node.range(), + Self::If(node) => node.range(), + Self::With(node) => node.range(), + Self::Match(node) => node.range(), + Self::Raise(node) => node.range(), + Self::Try(node) => node.range(), + Self::Assert(node) => node.range(), + Self::Import(node) => node.range(), + Self::ImportFrom(node) => node.range(), + Self::Global(node) => node.range(), + Self::Nonlocal(node) => node.range(), + Self::Expr(node) => node.range(), + Self::Pass(node) => node.range(), + Self::Break(node) => node.range(), + Self::Continue(node) => node.range(), + Self::IpyEscapeCommand(node) => node.range(), + } + } +} + +/// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr) +#[derive(Clone, Copy, Debug, PartialEq, is_macro::Is)] +pub enum ExpressionRef<'a> { + #[is(name = "bool_op_expr")] + BoolOp(&'a crate::ExprBoolOp), + #[is(name = "named_expr")] + Named(&'a crate::ExprNamed), + #[is(name = "bin_op_expr")] + BinOp(&'a crate::ExprBinOp), + #[is(name = "unary_op_expr")] + UnaryOp(&'a crate::ExprUnaryOp), + #[is(name = "lambda_expr")] + Lambda(&'a crate::ExprLambda), + #[is(name = "if_expr")] + If(&'a crate::ExprIf), + #[is(name = "dict_expr")] + Dict(&'a crate::ExprDict), + #[is(name = "set_expr")] + Set(&'a crate::ExprSet), + #[is(name = "list_comp_expr")] + ListComp(&'a crate::ExprListComp), + #[is(name = "set_comp_expr")] + SetComp(&'a crate::ExprSetComp), + #[is(name = "dict_comp_expr")] + DictComp(&'a crate::ExprDictComp), + #[is(name = "generator_expr")] + Generator(&'a crate::ExprGenerator), + #[is(name = "await_expr")] + Await(&'a crate::ExprAwait), + #[is(name = "yield_expr")] + Yield(&'a crate::ExprYield), + #[is(name = "yield_from_expr")] + YieldFrom(&'a crate::ExprYieldFrom), + #[is(name = "compare_expr")] + Compare(&'a crate::ExprCompare), + #[is(name = "call_expr")] + Call(&'a crate::ExprCall), + #[is(name = "f_string_expr")] + FString(&'a crate::ExprFString), + #[is(name = "string_literal_expr")] + StringLiteral(&'a crate::ExprStringLiteral), + #[is(name = "bytes_literal_expr")] + BytesLiteral(&'a crate::ExprBytesLiteral), + #[is(name = "number_literal_expr")] + NumberLiteral(&'a crate::ExprNumberLiteral), + #[is(name = "boolean_literal_expr")] + BooleanLiteral(&'a crate::ExprBooleanLiteral), + #[is(name = "none_literal_expr")] + NoneLiteral(&'a crate::ExprNoneLiteral), + #[is(name = "ellipsis_literal_expr")] + EllipsisLiteral(&'a crate::ExprEllipsisLiteral), + #[is(name = "attribute_expr")] + Attribute(&'a crate::ExprAttribute), + #[is(name = "subscript_expr")] + Subscript(&'a crate::ExprSubscript), + #[is(name = "starred_expr")] + Starred(&'a crate::ExprStarred), + #[is(name = "name_expr")] + Name(&'a crate::ExprName), + #[is(name = "list_expr")] + List(&'a crate::ExprList), + #[is(name = "tuple_expr")] + Tuple(&'a crate::ExprTuple), + #[is(name = "slice_expr")] + Slice(&'a crate::ExprSlice), + #[is(name = "ipy_escape_command_expr")] + IpyEscapeCommand(&'a crate::ExprIpyEscapeCommand), +} + +impl<'a> From<&'a Expr> for ExpressionRef<'a> { + fn from(node: &'a Expr) -> Self { + match node { + Expr::BoolOp(node) => ExpressionRef::BoolOp(node), + Expr::Named(node) => ExpressionRef::Named(node), + Expr::BinOp(node) => ExpressionRef::BinOp(node), + Expr::UnaryOp(node) => ExpressionRef::UnaryOp(node), + Expr::Lambda(node) => ExpressionRef::Lambda(node), + Expr::If(node) => ExpressionRef::If(node), + Expr::Dict(node) => ExpressionRef::Dict(node), + Expr::Set(node) => ExpressionRef::Set(node), + Expr::ListComp(node) => ExpressionRef::ListComp(node), + Expr::SetComp(node) => ExpressionRef::SetComp(node), + Expr::DictComp(node) => ExpressionRef::DictComp(node), + Expr::Generator(node) => ExpressionRef::Generator(node), + Expr::Await(node) => ExpressionRef::Await(node), + Expr::Yield(node) => ExpressionRef::Yield(node), + Expr::YieldFrom(node) => ExpressionRef::YieldFrom(node), + Expr::Compare(node) => ExpressionRef::Compare(node), + Expr::Call(node) => ExpressionRef::Call(node), + Expr::FString(node) => ExpressionRef::FString(node), + Expr::StringLiteral(node) => ExpressionRef::StringLiteral(node), + Expr::BytesLiteral(node) => ExpressionRef::BytesLiteral(node), + Expr::NumberLiteral(node) => ExpressionRef::NumberLiteral(node), + Expr::BooleanLiteral(node) => ExpressionRef::BooleanLiteral(node), + Expr::NoneLiteral(node) => ExpressionRef::NoneLiteral(node), + Expr::EllipsisLiteral(node) => ExpressionRef::EllipsisLiteral(node), + Expr::Attribute(node) => ExpressionRef::Attribute(node), + Expr::Subscript(node) => ExpressionRef::Subscript(node), + Expr::Starred(node) => ExpressionRef::Starred(node), + Expr::Name(node) => ExpressionRef::Name(node), + Expr::List(node) => ExpressionRef::List(node), + Expr::Tuple(node) => ExpressionRef::Tuple(node), + Expr::Slice(node) => ExpressionRef::Slice(node), + Expr::IpyEscapeCommand(node) => ExpressionRef::IpyEscapeCommand(node), + } + } +} + +impl<'a> From<&'a crate::ExprBoolOp> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprBoolOp) -> Self { + Self::BoolOp(node) + } +} + +impl<'a> From<&'a crate::ExprNamed> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprNamed) -> Self { + Self::Named(node) + } +} + +impl<'a> From<&'a crate::ExprBinOp> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprBinOp) -> Self { + Self::BinOp(node) + } +} + +impl<'a> From<&'a crate::ExprUnaryOp> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprUnaryOp) -> Self { + Self::UnaryOp(node) + } +} + +impl<'a> From<&'a crate::ExprLambda> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprLambda) -> Self { + Self::Lambda(node) + } +} + +impl<'a> From<&'a crate::ExprIf> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprIf) -> Self { + Self::If(node) + } +} + +impl<'a> From<&'a crate::ExprDict> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprDict) -> Self { + Self::Dict(node) + } +} + +impl<'a> From<&'a crate::ExprSet> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprSet) -> Self { + Self::Set(node) + } +} + +impl<'a> From<&'a crate::ExprListComp> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprListComp) -> Self { + Self::ListComp(node) + } +} + +impl<'a> From<&'a crate::ExprSetComp> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprSetComp) -> Self { + Self::SetComp(node) + } +} + +impl<'a> From<&'a crate::ExprDictComp> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprDictComp) -> Self { + Self::DictComp(node) + } +} + +impl<'a> From<&'a crate::ExprGenerator> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprGenerator) -> Self { + Self::Generator(node) + } +} + +impl<'a> From<&'a crate::ExprAwait> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprAwait) -> Self { + Self::Await(node) + } +} + +impl<'a> From<&'a crate::ExprYield> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprYield) -> Self { + Self::Yield(node) + } +} + +impl<'a> From<&'a crate::ExprYieldFrom> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprYieldFrom) -> Self { + Self::YieldFrom(node) + } +} + +impl<'a> From<&'a crate::ExprCompare> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprCompare) -> Self { + Self::Compare(node) + } +} + +impl<'a> From<&'a crate::ExprCall> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprCall) -> Self { + Self::Call(node) + } +} + +impl<'a> From<&'a crate::ExprFString> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprFString) -> Self { + Self::FString(node) + } +} + +impl<'a> From<&'a crate::ExprStringLiteral> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprStringLiteral) -> Self { + Self::StringLiteral(node) + } +} + +impl<'a> From<&'a crate::ExprBytesLiteral> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprBytesLiteral) -> Self { + Self::BytesLiteral(node) + } +} + +impl<'a> From<&'a crate::ExprNumberLiteral> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprNumberLiteral) -> Self { + Self::NumberLiteral(node) + } +} + +impl<'a> From<&'a crate::ExprBooleanLiteral> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprBooleanLiteral) -> Self { + Self::BooleanLiteral(node) + } +} + +impl<'a> From<&'a crate::ExprNoneLiteral> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprNoneLiteral) -> Self { + Self::NoneLiteral(node) + } +} + +impl<'a> From<&'a crate::ExprEllipsisLiteral> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprEllipsisLiteral) -> Self { + Self::EllipsisLiteral(node) + } +} + +impl<'a> From<&'a crate::ExprAttribute> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprAttribute) -> Self { + Self::Attribute(node) + } +} + +impl<'a> From<&'a crate::ExprSubscript> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprSubscript) -> Self { + Self::Subscript(node) + } +} + +impl<'a> From<&'a crate::ExprStarred> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprStarred) -> Self { + Self::Starred(node) + } +} + +impl<'a> From<&'a crate::ExprName> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprName) -> Self { + Self::Name(node) + } +} + +impl<'a> From<&'a crate::ExprList> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprList) -> Self { + Self::List(node) + } +} + +impl<'a> From<&'a crate::ExprTuple> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprTuple) -> Self { + Self::Tuple(node) + } +} + +impl<'a> From<&'a crate::ExprSlice> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprSlice) -> Self { + Self::Slice(node) + } +} + +impl<'a> From<&'a crate::ExprIpyEscapeCommand> for ExpressionRef<'a> { + fn from(node: &'a crate::ExprIpyEscapeCommand) -> Self { + Self::IpyEscapeCommand(node) + } +} + +impl ruff_text_size::Ranged for ExpressionRef<'_> { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::BoolOp(node) => node.range(), + Self::Named(node) => node.range(), + Self::BinOp(node) => node.range(), + Self::UnaryOp(node) => node.range(), + Self::Lambda(node) => node.range(), + Self::If(node) => node.range(), + Self::Dict(node) => node.range(), + Self::Set(node) => node.range(), + Self::ListComp(node) => node.range(), + Self::SetComp(node) => node.range(), + Self::DictComp(node) => node.range(), + Self::Generator(node) => node.range(), + Self::Await(node) => node.range(), + Self::Yield(node) => node.range(), + Self::YieldFrom(node) => node.range(), + Self::Compare(node) => node.range(), + Self::Call(node) => node.range(), + Self::FString(node) => node.range(), + Self::StringLiteral(node) => node.range(), + Self::BytesLiteral(node) => node.range(), + Self::NumberLiteral(node) => node.range(), + Self::BooleanLiteral(node) => node.range(), + Self::NoneLiteral(node) => node.range(), + Self::EllipsisLiteral(node) => node.range(), + Self::Attribute(node) => node.range(), + Self::Subscript(node) => node.range(), + Self::Starred(node) => node.range(), + Self::Name(node) => node.range(), + Self::List(node) => node.range(), + Self::Tuple(node) => node.range(), + Self::Slice(node) => node.range(), + Self::IpyEscapeCommand(node) => node.range(), + } + } +} + +/// See also [excepthandler](https://docs.python.org/3/library/ast.html#ast.excepthandler) +#[derive(Clone, Copy, Debug, PartialEq, is_macro::Is)] +pub enum ExceptHandlerRef<'a> { + ExceptHandler(&'a crate::ExceptHandlerExceptHandler), +} + +impl<'a> From<&'a ExceptHandler> for ExceptHandlerRef<'a> { + fn from(node: &'a ExceptHandler) -> Self { + match node { + ExceptHandler::ExceptHandler(node) => ExceptHandlerRef::ExceptHandler(node), + } + } +} + +impl<'a> From<&'a crate::ExceptHandlerExceptHandler> for ExceptHandlerRef<'a> { + fn from(node: &'a crate::ExceptHandlerExceptHandler) -> Self { + Self::ExceptHandler(node) + } +} + +impl ruff_text_size::Ranged for ExceptHandlerRef<'_> { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::ExceptHandler(node) => node.range(), + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, is_macro::Is)] +pub enum FStringElementRef<'a> { + Expression(&'a crate::FStringExpressionElement), + Literal(&'a crate::FStringLiteralElement), +} + +impl<'a> From<&'a FStringElement> for FStringElementRef<'a> { + fn from(node: &'a FStringElement) -> Self { + match node { + FStringElement::Expression(node) => FStringElementRef::Expression(node), + FStringElement::Literal(node) => FStringElementRef::Literal(node), + } + } +} + +impl<'a> From<&'a crate::FStringExpressionElement> for FStringElementRef<'a> { + fn from(node: &'a crate::FStringExpressionElement) -> Self { + Self::Expression(node) + } +} + +impl<'a> From<&'a crate::FStringLiteralElement> for FStringElementRef<'a> { + fn from(node: &'a crate::FStringLiteralElement) -> Self { + Self::Literal(node) + } +} + +impl ruff_text_size::Ranged for FStringElementRef<'_> { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::Expression(node) => node.range(), + Self::Literal(node) => node.range(), + } + } +} + +/// See also [pattern](https://docs.python.org/3/library/ast.html#ast.pattern) +#[derive(Clone, Copy, Debug, PartialEq, is_macro::Is)] +pub enum PatternRef<'a> { + MatchValue(&'a crate::PatternMatchValue), + MatchSingleton(&'a crate::PatternMatchSingleton), + MatchSequence(&'a crate::PatternMatchSequence), + MatchMapping(&'a crate::PatternMatchMapping), + MatchClass(&'a crate::PatternMatchClass), + MatchStar(&'a crate::PatternMatchStar), + MatchAs(&'a crate::PatternMatchAs), + MatchOr(&'a crate::PatternMatchOr), +} + +impl<'a> From<&'a Pattern> for PatternRef<'a> { + fn from(node: &'a Pattern) -> Self { + match node { + Pattern::MatchValue(node) => PatternRef::MatchValue(node), + Pattern::MatchSingleton(node) => PatternRef::MatchSingleton(node), + Pattern::MatchSequence(node) => PatternRef::MatchSequence(node), + Pattern::MatchMapping(node) => PatternRef::MatchMapping(node), + Pattern::MatchClass(node) => PatternRef::MatchClass(node), + Pattern::MatchStar(node) => PatternRef::MatchStar(node), + Pattern::MatchAs(node) => PatternRef::MatchAs(node), + Pattern::MatchOr(node) => PatternRef::MatchOr(node), + } + } +} + +impl<'a> From<&'a crate::PatternMatchValue> for PatternRef<'a> { + fn from(node: &'a crate::PatternMatchValue) -> Self { + Self::MatchValue(node) + } +} + +impl<'a> From<&'a crate::PatternMatchSingleton> for PatternRef<'a> { + fn from(node: &'a crate::PatternMatchSingleton) -> Self { + Self::MatchSingleton(node) + } +} + +impl<'a> From<&'a crate::PatternMatchSequence> for PatternRef<'a> { + fn from(node: &'a crate::PatternMatchSequence) -> Self { + Self::MatchSequence(node) + } +} + +impl<'a> From<&'a crate::PatternMatchMapping> for PatternRef<'a> { + fn from(node: &'a crate::PatternMatchMapping) -> Self { + Self::MatchMapping(node) + } +} + +impl<'a> From<&'a crate::PatternMatchClass> for PatternRef<'a> { + fn from(node: &'a crate::PatternMatchClass) -> Self { + Self::MatchClass(node) + } +} + +impl<'a> From<&'a crate::PatternMatchStar> for PatternRef<'a> { + fn from(node: &'a crate::PatternMatchStar) -> Self { + Self::MatchStar(node) + } +} + +impl<'a> From<&'a crate::PatternMatchAs> for PatternRef<'a> { + fn from(node: &'a crate::PatternMatchAs) -> Self { + Self::MatchAs(node) + } +} + +impl<'a> From<&'a crate::PatternMatchOr> for PatternRef<'a> { + fn from(node: &'a crate::PatternMatchOr) -> Self { + Self::MatchOr(node) + } +} + +impl ruff_text_size::Ranged for PatternRef<'_> { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::MatchValue(node) => node.range(), + Self::MatchSingleton(node) => node.range(), + Self::MatchSequence(node) => node.range(), + Self::MatchMapping(node) => node.range(), + Self::MatchClass(node) => node.range(), + Self::MatchStar(node) => node.range(), + Self::MatchAs(node) => node.range(), + Self::MatchOr(node) => node.range(), + } + } +} + +/// See also [type_param](https://docs.python.org/3/library/ast.html#ast.type_param) +#[derive(Clone, Copy, Debug, PartialEq, is_macro::Is)] +pub enum TypeParamRef<'a> { + TypeVar(&'a crate::TypeParamTypeVar), + TypeVarTuple(&'a crate::TypeParamTypeVarTuple), + ParamSpec(&'a crate::TypeParamParamSpec), +} + +impl<'a> From<&'a TypeParam> for TypeParamRef<'a> { + fn from(node: &'a TypeParam) -> Self { + match node { + TypeParam::TypeVar(node) => TypeParamRef::TypeVar(node), + TypeParam::TypeVarTuple(node) => TypeParamRef::TypeVarTuple(node), + TypeParam::ParamSpec(node) => TypeParamRef::ParamSpec(node), + } + } +} + +impl<'a> From<&'a crate::TypeParamTypeVar> for TypeParamRef<'a> { + fn from(node: &'a crate::TypeParamTypeVar) -> Self { + Self::TypeVar(node) + } +} + +impl<'a> From<&'a crate::TypeParamTypeVarTuple> for TypeParamRef<'a> { + fn from(node: &'a crate::TypeParamTypeVarTuple) -> Self { + Self::TypeVarTuple(node) + } +} + +impl<'a> From<&'a crate::TypeParamParamSpec> for TypeParamRef<'a> { + fn from(node: &'a crate::TypeParamParamSpec) -> Self { + Self::ParamSpec(node) + } +} + +impl ruff_text_size::Ranged for TypeParamRef<'_> { + fn range(&self) -> ruff_text_size::TextRange { + match self { + Self::TypeVar(node) => node.range(), + Self::TypeVarTuple(node) => node.range(), + Self::ParamSpec(node) => node.range(), + } + } +} + +#[derive(Clone, Debug, is_macro::Is, PartialEq)] +pub enum AnyNode { + ModModule(crate::ModModule), + ModExpression(crate::ModExpression), + StmtFunctionDef(crate::StmtFunctionDef), + StmtClassDef(crate::StmtClassDef), + StmtReturn(crate::StmtReturn), + StmtDelete(crate::StmtDelete), + StmtTypeAlias(crate::StmtTypeAlias), + StmtAssign(crate::StmtAssign), + StmtAugAssign(crate::StmtAugAssign), + StmtAnnAssign(crate::StmtAnnAssign), + StmtFor(crate::StmtFor), + StmtWhile(crate::StmtWhile), + StmtIf(crate::StmtIf), + StmtWith(crate::StmtWith), + StmtMatch(crate::StmtMatch), + StmtRaise(crate::StmtRaise), + StmtTry(crate::StmtTry), + StmtAssert(crate::StmtAssert), + StmtImport(crate::StmtImport), + StmtImportFrom(crate::StmtImportFrom), + StmtGlobal(crate::StmtGlobal), + StmtNonlocal(crate::StmtNonlocal), + StmtExpr(crate::StmtExpr), + StmtPass(crate::StmtPass), + StmtBreak(crate::StmtBreak), + StmtContinue(crate::StmtContinue), + StmtIpyEscapeCommand(crate::StmtIpyEscapeCommand), + ExprBoolOp(crate::ExprBoolOp), + ExprNamed(crate::ExprNamed), + ExprBinOp(crate::ExprBinOp), + ExprUnaryOp(crate::ExprUnaryOp), + ExprLambda(crate::ExprLambda), + ExprIf(crate::ExprIf), + ExprDict(crate::ExprDict), + ExprSet(crate::ExprSet), + ExprListComp(crate::ExprListComp), + ExprSetComp(crate::ExprSetComp), + ExprDictComp(crate::ExprDictComp), + ExprGenerator(crate::ExprGenerator), + ExprAwait(crate::ExprAwait), + ExprYield(crate::ExprYield), + ExprYieldFrom(crate::ExprYieldFrom), + ExprCompare(crate::ExprCompare), + ExprCall(crate::ExprCall), + ExprFString(crate::ExprFString), + ExprStringLiteral(crate::ExprStringLiteral), + ExprBytesLiteral(crate::ExprBytesLiteral), + ExprNumberLiteral(crate::ExprNumberLiteral), + ExprBooleanLiteral(crate::ExprBooleanLiteral), + ExprNoneLiteral(crate::ExprNoneLiteral), + ExprEllipsisLiteral(crate::ExprEllipsisLiteral), + ExprAttribute(crate::ExprAttribute), + ExprSubscript(crate::ExprSubscript), + ExprStarred(crate::ExprStarred), + ExprName(crate::ExprName), + ExprList(crate::ExprList), + ExprTuple(crate::ExprTuple), + ExprSlice(crate::ExprSlice), + ExprIpyEscapeCommand(crate::ExprIpyEscapeCommand), + ExceptHandlerExceptHandler(crate::ExceptHandlerExceptHandler), + FStringExpressionElement(crate::FStringExpressionElement), + FStringLiteralElement(crate::FStringLiteralElement), + PatternMatchValue(crate::PatternMatchValue), + PatternMatchSingleton(crate::PatternMatchSingleton), + PatternMatchSequence(crate::PatternMatchSequence), + PatternMatchMapping(crate::PatternMatchMapping), + PatternMatchClass(crate::PatternMatchClass), + PatternMatchStar(crate::PatternMatchStar), + PatternMatchAs(crate::PatternMatchAs), + PatternMatchOr(crate::PatternMatchOr), + TypeParamTypeVar(crate::TypeParamTypeVar), + TypeParamTypeVarTuple(crate::TypeParamTypeVarTuple), + TypeParamParamSpec(crate::TypeParamParamSpec), + FStringFormatSpec(crate::FStringFormatSpec), + PatternArguments(crate::PatternArguments), + PatternKeyword(crate::PatternKeyword), + Comprehension(crate::Comprehension), + Arguments(crate::Arguments), + Parameters(crate::Parameters), + Parameter(crate::Parameter), + ParameterWithDefault(crate::ParameterWithDefault), + Keyword(crate::Keyword), + Alias(crate::Alias), + WithItem(crate::WithItem), + MatchCase(crate::MatchCase), + Decorator(crate::Decorator), + ElifElseClause(crate::ElifElseClause), + TypeParams(crate::TypeParams), + FString(crate::FString), + StringLiteral(crate::StringLiteral), + BytesLiteral(crate::BytesLiteral), + Identifier(crate::Identifier), +} + +impl From for AnyNode { + fn from(node: Mod) -> AnyNode { + match node { + Mod::Module(node) => AnyNode::ModModule(node), + Mod::Expression(node) => AnyNode::ModExpression(node), + } + } +} + +impl From for AnyNode { + fn from(node: crate::ModModule) -> AnyNode { + AnyNode::ModModule(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ModExpression) -> AnyNode { + AnyNode::ModExpression(node) + } +} + +impl From for AnyNode { + fn from(node: Stmt) -> AnyNode { + match node { + Stmt::FunctionDef(node) => AnyNode::StmtFunctionDef(node), + Stmt::ClassDef(node) => AnyNode::StmtClassDef(node), + Stmt::Return(node) => AnyNode::StmtReturn(node), + Stmt::Delete(node) => AnyNode::StmtDelete(node), + Stmt::TypeAlias(node) => AnyNode::StmtTypeAlias(node), + Stmt::Assign(node) => AnyNode::StmtAssign(node), + Stmt::AugAssign(node) => AnyNode::StmtAugAssign(node), + Stmt::AnnAssign(node) => AnyNode::StmtAnnAssign(node), + Stmt::For(node) => AnyNode::StmtFor(node), + Stmt::While(node) => AnyNode::StmtWhile(node), + Stmt::If(node) => AnyNode::StmtIf(node), + Stmt::With(node) => AnyNode::StmtWith(node), + Stmt::Match(node) => AnyNode::StmtMatch(node), + Stmt::Raise(node) => AnyNode::StmtRaise(node), + Stmt::Try(node) => AnyNode::StmtTry(node), + Stmt::Assert(node) => AnyNode::StmtAssert(node), + Stmt::Import(node) => AnyNode::StmtImport(node), + Stmt::ImportFrom(node) => AnyNode::StmtImportFrom(node), + Stmt::Global(node) => AnyNode::StmtGlobal(node), + Stmt::Nonlocal(node) => AnyNode::StmtNonlocal(node), + Stmt::Expr(node) => AnyNode::StmtExpr(node), + Stmt::Pass(node) => AnyNode::StmtPass(node), + Stmt::Break(node) => AnyNode::StmtBreak(node), + Stmt::Continue(node) => AnyNode::StmtContinue(node), + Stmt::IpyEscapeCommand(node) => AnyNode::StmtIpyEscapeCommand(node), + } + } +} + +impl From for AnyNode { + fn from(node: crate::StmtFunctionDef) -> AnyNode { + AnyNode::StmtFunctionDef(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtClassDef) -> AnyNode { + AnyNode::StmtClassDef(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtReturn) -> AnyNode { + AnyNode::StmtReturn(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtDelete) -> AnyNode { + AnyNode::StmtDelete(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtTypeAlias) -> AnyNode { + AnyNode::StmtTypeAlias(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtAssign) -> AnyNode { + AnyNode::StmtAssign(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtAugAssign) -> AnyNode { + AnyNode::StmtAugAssign(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtAnnAssign) -> AnyNode { + AnyNode::StmtAnnAssign(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtFor) -> AnyNode { + AnyNode::StmtFor(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtWhile) -> AnyNode { + AnyNode::StmtWhile(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtIf) -> AnyNode { + AnyNode::StmtIf(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtWith) -> AnyNode { + AnyNode::StmtWith(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtMatch) -> AnyNode { + AnyNode::StmtMatch(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtRaise) -> AnyNode { + AnyNode::StmtRaise(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtTry) -> AnyNode { + AnyNode::StmtTry(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtAssert) -> AnyNode { + AnyNode::StmtAssert(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtImport) -> AnyNode { + AnyNode::StmtImport(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtImportFrom) -> AnyNode { + AnyNode::StmtImportFrom(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtGlobal) -> AnyNode { + AnyNode::StmtGlobal(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtNonlocal) -> AnyNode { + AnyNode::StmtNonlocal(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtExpr) -> AnyNode { + AnyNode::StmtExpr(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtPass) -> AnyNode { + AnyNode::StmtPass(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtBreak) -> AnyNode { + AnyNode::StmtBreak(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtContinue) -> AnyNode { + AnyNode::StmtContinue(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StmtIpyEscapeCommand) -> AnyNode { + AnyNode::StmtIpyEscapeCommand(node) + } +} + +impl From for AnyNode { + fn from(node: Expr) -> AnyNode { + match node { + Expr::BoolOp(node) => AnyNode::ExprBoolOp(node), + Expr::Named(node) => AnyNode::ExprNamed(node), + Expr::BinOp(node) => AnyNode::ExprBinOp(node), + Expr::UnaryOp(node) => AnyNode::ExprUnaryOp(node), + Expr::Lambda(node) => AnyNode::ExprLambda(node), + Expr::If(node) => AnyNode::ExprIf(node), + Expr::Dict(node) => AnyNode::ExprDict(node), + Expr::Set(node) => AnyNode::ExprSet(node), + Expr::ListComp(node) => AnyNode::ExprListComp(node), + Expr::SetComp(node) => AnyNode::ExprSetComp(node), + Expr::DictComp(node) => AnyNode::ExprDictComp(node), + Expr::Generator(node) => AnyNode::ExprGenerator(node), + Expr::Await(node) => AnyNode::ExprAwait(node), + Expr::Yield(node) => AnyNode::ExprYield(node), + Expr::YieldFrom(node) => AnyNode::ExprYieldFrom(node), + Expr::Compare(node) => AnyNode::ExprCompare(node), + Expr::Call(node) => AnyNode::ExprCall(node), + Expr::FString(node) => AnyNode::ExprFString(node), + Expr::StringLiteral(node) => AnyNode::ExprStringLiteral(node), + Expr::BytesLiteral(node) => AnyNode::ExprBytesLiteral(node), + Expr::NumberLiteral(node) => AnyNode::ExprNumberLiteral(node), + Expr::BooleanLiteral(node) => AnyNode::ExprBooleanLiteral(node), + Expr::NoneLiteral(node) => AnyNode::ExprNoneLiteral(node), + Expr::EllipsisLiteral(node) => AnyNode::ExprEllipsisLiteral(node), + Expr::Attribute(node) => AnyNode::ExprAttribute(node), + Expr::Subscript(node) => AnyNode::ExprSubscript(node), + Expr::Starred(node) => AnyNode::ExprStarred(node), + Expr::Name(node) => AnyNode::ExprName(node), + Expr::List(node) => AnyNode::ExprList(node), + Expr::Tuple(node) => AnyNode::ExprTuple(node), + Expr::Slice(node) => AnyNode::ExprSlice(node), + Expr::IpyEscapeCommand(node) => AnyNode::ExprIpyEscapeCommand(node), + } + } +} + +impl From for AnyNode { + fn from(node: crate::ExprBoolOp) -> AnyNode { + AnyNode::ExprBoolOp(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprNamed) -> AnyNode { + AnyNode::ExprNamed(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprBinOp) -> AnyNode { + AnyNode::ExprBinOp(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprUnaryOp) -> AnyNode { + AnyNode::ExprUnaryOp(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprLambda) -> AnyNode { + AnyNode::ExprLambda(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprIf) -> AnyNode { + AnyNode::ExprIf(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprDict) -> AnyNode { + AnyNode::ExprDict(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprSet) -> AnyNode { + AnyNode::ExprSet(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprListComp) -> AnyNode { + AnyNode::ExprListComp(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprSetComp) -> AnyNode { + AnyNode::ExprSetComp(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprDictComp) -> AnyNode { + AnyNode::ExprDictComp(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprGenerator) -> AnyNode { + AnyNode::ExprGenerator(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprAwait) -> AnyNode { + AnyNode::ExprAwait(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprYield) -> AnyNode { + AnyNode::ExprYield(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprYieldFrom) -> AnyNode { + AnyNode::ExprYieldFrom(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprCompare) -> AnyNode { + AnyNode::ExprCompare(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprCall) -> AnyNode { + AnyNode::ExprCall(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprFString) -> AnyNode { + AnyNode::ExprFString(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprStringLiteral) -> AnyNode { + AnyNode::ExprStringLiteral(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprBytesLiteral) -> AnyNode { + AnyNode::ExprBytesLiteral(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprNumberLiteral) -> AnyNode { + AnyNode::ExprNumberLiteral(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprBooleanLiteral) -> AnyNode { + AnyNode::ExprBooleanLiteral(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprNoneLiteral) -> AnyNode { + AnyNode::ExprNoneLiteral(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprEllipsisLiteral) -> AnyNode { + AnyNode::ExprEllipsisLiteral(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprAttribute) -> AnyNode { + AnyNode::ExprAttribute(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprSubscript) -> AnyNode { + AnyNode::ExprSubscript(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprStarred) -> AnyNode { + AnyNode::ExprStarred(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprName) -> AnyNode { + AnyNode::ExprName(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprList) -> AnyNode { + AnyNode::ExprList(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprTuple) -> AnyNode { + AnyNode::ExprTuple(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprSlice) -> AnyNode { + AnyNode::ExprSlice(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ExprIpyEscapeCommand) -> AnyNode { + AnyNode::ExprIpyEscapeCommand(node) + } +} + +impl From for AnyNode { + fn from(node: ExceptHandler) -> AnyNode { + match node { + ExceptHandler::ExceptHandler(node) => AnyNode::ExceptHandlerExceptHandler(node), + } + } +} + +impl From for AnyNode { + fn from(node: crate::ExceptHandlerExceptHandler) -> AnyNode { + AnyNode::ExceptHandlerExceptHandler(node) + } +} + +impl From for AnyNode { + fn from(node: FStringElement) -> AnyNode { + match node { + FStringElement::Expression(node) => AnyNode::FStringExpressionElement(node), + FStringElement::Literal(node) => AnyNode::FStringLiteralElement(node), + } + } +} + +impl From for AnyNode { + fn from(node: crate::FStringExpressionElement) -> AnyNode { + AnyNode::FStringExpressionElement(node) + } +} + +impl From for AnyNode { + fn from(node: crate::FStringLiteralElement) -> AnyNode { + AnyNode::FStringLiteralElement(node) + } +} + +impl From for AnyNode { + fn from(node: Pattern) -> AnyNode { + match node { + Pattern::MatchValue(node) => AnyNode::PatternMatchValue(node), + Pattern::MatchSingleton(node) => AnyNode::PatternMatchSingleton(node), + Pattern::MatchSequence(node) => AnyNode::PatternMatchSequence(node), + Pattern::MatchMapping(node) => AnyNode::PatternMatchMapping(node), + Pattern::MatchClass(node) => AnyNode::PatternMatchClass(node), + Pattern::MatchStar(node) => AnyNode::PatternMatchStar(node), + Pattern::MatchAs(node) => AnyNode::PatternMatchAs(node), + Pattern::MatchOr(node) => AnyNode::PatternMatchOr(node), + } + } +} + +impl From for AnyNode { + fn from(node: crate::PatternMatchValue) -> AnyNode { + AnyNode::PatternMatchValue(node) + } +} + +impl From for AnyNode { + fn from(node: crate::PatternMatchSingleton) -> AnyNode { + AnyNode::PatternMatchSingleton(node) + } +} + +impl From for AnyNode { + fn from(node: crate::PatternMatchSequence) -> AnyNode { + AnyNode::PatternMatchSequence(node) + } +} + +impl From for AnyNode { + fn from(node: crate::PatternMatchMapping) -> AnyNode { + AnyNode::PatternMatchMapping(node) + } +} + +impl From for AnyNode { + fn from(node: crate::PatternMatchClass) -> AnyNode { + AnyNode::PatternMatchClass(node) + } +} + +impl From for AnyNode { + fn from(node: crate::PatternMatchStar) -> AnyNode { + AnyNode::PatternMatchStar(node) + } +} + +impl From for AnyNode { + fn from(node: crate::PatternMatchAs) -> AnyNode { + AnyNode::PatternMatchAs(node) + } +} + +impl From for AnyNode { + fn from(node: crate::PatternMatchOr) -> AnyNode { + AnyNode::PatternMatchOr(node) + } +} + +impl From for AnyNode { + fn from(node: TypeParam) -> AnyNode { + match node { + TypeParam::TypeVar(node) => AnyNode::TypeParamTypeVar(node), + TypeParam::TypeVarTuple(node) => AnyNode::TypeParamTypeVarTuple(node), + TypeParam::ParamSpec(node) => AnyNode::TypeParamParamSpec(node), + } + } +} + +impl From for AnyNode { + fn from(node: crate::TypeParamTypeVar) -> AnyNode { + AnyNode::TypeParamTypeVar(node) + } +} + +impl From for AnyNode { + fn from(node: crate::TypeParamTypeVarTuple) -> AnyNode { + AnyNode::TypeParamTypeVarTuple(node) + } +} + +impl From for AnyNode { + fn from(node: crate::TypeParamParamSpec) -> AnyNode { + AnyNode::TypeParamParamSpec(node) + } +} + +impl From for AnyNode { + fn from(node: crate::FStringFormatSpec) -> AnyNode { + AnyNode::FStringFormatSpec(node) + } +} + +impl From for AnyNode { + fn from(node: crate::PatternArguments) -> AnyNode { + AnyNode::PatternArguments(node) + } +} + +impl From for AnyNode { + fn from(node: crate::PatternKeyword) -> AnyNode { + AnyNode::PatternKeyword(node) + } +} + +impl From for AnyNode { + fn from(node: crate::Comprehension) -> AnyNode { + AnyNode::Comprehension(node) + } +} + +impl From for AnyNode { + fn from(node: crate::Arguments) -> AnyNode { + AnyNode::Arguments(node) + } +} + +impl From for AnyNode { + fn from(node: crate::Parameters) -> AnyNode { + AnyNode::Parameters(node) + } +} + +impl From for AnyNode { + fn from(node: crate::Parameter) -> AnyNode { + AnyNode::Parameter(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ParameterWithDefault) -> AnyNode { + AnyNode::ParameterWithDefault(node) + } +} + +impl From for AnyNode { + fn from(node: crate::Keyword) -> AnyNode { + AnyNode::Keyword(node) + } +} + +impl From for AnyNode { + fn from(node: crate::Alias) -> AnyNode { + AnyNode::Alias(node) + } +} + +impl From for AnyNode { + fn from(node: crate::WithItem) -> AnyNode { + AnyNode::WithItem(node) + } +} + +impl From for AnyNode { + fn from(node: crate::MatchCase) -> AnyNode { + AnyNode::MatchCase(node) + } +} + +impl From for AnyNode { + fn from(node: crate::Decorator) -> AnyNode { + AnyNode::Decorator(node) + } +} + +impl From for AnyNode { + fn from(node: crate::ElifElseClause) -> AnyNode { + AnyNode::ElifElseClause(node) + } +} + +impl From for AnyNode { + fn from(node: crate::TypeParams) -> AnyNode { + AnyNode::TypeParams(node) + } +} + +impl From for AnyNode { + fn from(node: crate::FString) -> AnyNode { + AnyNode::FString(node) + } +} + +impl From for AnyNode { + fn from(node: crate::StringLiteral) -> AnyNode { + AnyNode::StringLiteral(node) + } +} + +impl From for AnyNode { + fn from(node: crate::BytesLiteral) -> AnyNode { + AnyNode::BytesLiteral(node) + } +} + +impl From for AnyNode { + fn from(node: crate::Identifier) -> AnyNode { + AnyNode::Identifier(node) + } +} + +impl ruff_text_size::Ranged for AnyNode { + fn range(&self) -> ruff_text_size::TextRange { + match self { + AnyNode::ModModule(node) => node.range(), + AnyNode::ModExpression(node) => node.range(), + AnyNode::StmtFunctionDef(node) => node.range(), + AnyNode::StmtClassDef(node) => node.range(), + AnyNode::StmtReturn(node) => node.range(), + AnyNode::StmtDelete(node) => node.range(), + AnyNode::StmtTypeAlias(node) => node.range(), + AnyNode::StmtAssign(node) => node.range(), + AnyNode::StmtAugAssign(node) => node.range(), + AnyNode::StmtAnnAssign(node) => node.range(), + AnyNode::StmtFor(node) => node.range(), + AnyNode::StmtWhile(node) => node.range(), + AnyNode::StmtIf(node) => node.range(), + AnyNode::StmtWith(node) => node.range(), + AnyNode::StmtMatch(node) => node.range(), + AnyNode::StmtRaise(node) => node.range(), + AnyNode::StmtTry(node) => node.range(), + AnyNode::StmtAssert(node) => node.range(), + AnyNode::StmtImport(node) => node.range(), + AnyNode::StmtImportFrom(node) => node.range(), + AnyNode::StmtGlobal(node) => node.range(), + AnyNode::StmtNonlocal(node) => node.range(), + AnyNode::StmtExpr(node) => node.range(), + AnyNode::StmtPass(node) => node.range(), + AnyNode::StmtBreak(node) => node.range(), + AnyNode::StmtContinue(node) => node.range(), + AnyNode::StmtIpyEscapeCommand(node) => node.range(), + AnyNode::ExprBoolOp(node) => node.range(), + AnyNode::ExprNamed(node) => node.range(), + AnyNode::ExprBinOp(node) => node.range(), + AnyNode::ExprUnaryOp(node) => node.range(), + AnyNode::ExprLambda(node) => node.range(), + AnyNode::ExprIf(node) => node.range(), + AnyNode::ExprDict(node) => node.range(), + AnyNode::ExprSet(node) => node.range(), + AnyNode::ExprListComp(node) => node.range(), + AnyNode::ExprSetComp(node) => node.range(), + AnyNode::ExprDictComp(node) => node.range(), + AnyNode::ExprGenerator(node) => node.range(), + AnyNode::ExprAwait(node) => node.range(), + AnyNode::ExprYield(node) => node.range(), + AnyNode::ExprYieldFrom(node) => node.range(), + AnyNode::ExprCompare(node) => node.range(), + AnyNode::ExprCall(node) => node.range(), + AnyNode::ExprFString(node) => node.range(), + AnyNode::ExprStringLiteral(node) => node.range(), + AnyNode::ExprBytesLiteral(node) => node.range(), + AnyNode::ExprNumberLiteral(node) => node.range(), + AnyNode::ExprBooleanLiteral(node) => node.range(), + AnyNode::ExprNoneLiteral(node) => node.range(), + AnyNode::ExprEllipsisLiteral(node) => node.range(), + AnyNode::ExprAttribute(node) => node.range(), + AnyNode::ExprSubscript(node) => node.range(), + AnyNode::ExprStarred(node) => node.range(), + AnyNode::ExprName(node) => node.range(), + AnyNode::ExprList(node) => node.range(), + AnyNode::ExprTuple(node) => node.range(), + AnyNode::ExprSlice(node) => node.range(), + AnyNode::ExprIpyEscapeCommand(node) => node.range(), + AnyNode::ExceptHandlerExceptHandler(node) => node.range(), + AnyNode::FStringExpressionElement(node) => node.range(), + AnyNode::FStringLiteralElement(node) => node.range(), + AnyNode::PatternMatchValue(node) => node.range(), + AnyNode::PatternMatchSingleton(node) => node.range(), + AnyNode::PatternMatchSequence(node) => node.range(), + AnyNode::PatternMatchMapping(node) => node.range(), + AnyNode::PatternMatchClass(node) => node.range(), + AnyNode::PatternMatchStar(node) => node.range(), + AnyNode::PatternMatchAs(node) => node.range(), + AnyNode::PatternMatchOr(node) => node.range(), + AnyNode::TypeParamTypeVar(node) => node.range(), + AnyNode::TypeParamTypeVarTuple(node) => node.range(), + AnyNode::TypeParamParamSpec(node) => node.range(), + AnyNode::FStringFormatSpec(node) => node.range(), + AnyNode::PatternArguments(node) => node.range(), + AnyNode::PatternKeyword(node) => node.range(), + AnyNode::Comprehension(node) => node.range(), + AnyNode::Arguments(node) => node.range(), + AnyNode::Parameters(node) => node.range(), + AnyNode::Parameter(node) => node.range(), + AnyNode::ParameterWithDefault(node) => node.range(), + AnyNode::Keyword(node) => node.range(), + AnyNode::Alias(node) => node.range(), + AnyNode::WithItem(node) => node.range(), + AnyNode::MatchCase(node) => node.range(), + AnyNode::Decorator(node) => node.range(), + AnyNode::ElifElseClause(node) => node.range(), + AnyNode::TypeParams(node) => node.range(), + AnyNode::FString(node) => node.range(), + AnyNode::StringLiteral(node) => node.range(), + AnyNode::BytesLiteral(node) => node.range(), + AnyNode::Identifier(node) => node.range(), + } + } +} + +impl AnyNode { + pub fn module(self) -> Option { + match self { + AnyNode::ModModule(node) => Some(Mod::Module(node)), + AnyNode::ModExpression(node) => Some(Mod::Expression(node)), + + _ => None, + } + } +} + +impl AnyNode { + pub fn statement(self) -> Option { + match self { + AnyNode::StmtFunctionDef(node) => Some(Stmt::FunctionDef(node)), + AnyNode::StmtClassDef(node) => Some(Stmt::ClassDef(node)), + AnyNode::StmtReturn(node) => Some(Stmt::Return(node)), + AnyNode::StmtDelete(node) => Some(Stmt::Delete(node)), + AnyNode::StmtTypeAlias(node) => Some(Stmt::TypeAlias(node)), + AnyNode::StmtAssign(node) => Some(Stmt::Assign(node)), + AnyNode::StmtAugAssign(node) => Some(Stmt::AugAssign(node)), + AnyNode::StmtAnnAssign(node) => Some(Stmt::AnnAssign(node)), + AnyNode::StmtFor(node) => Some(Stmt::For(node)), + AnyNode::StmtWhile(node) => Some(Stmt::While(node)), + AnyNode::StmtIf(node) => Some(Stmt::If(node)), + AnyNode::StmtWith(node) => Some(Stmt::With(node)), + AnyNode::StmtMatch(node) => Some(Stmt::Match(node)), + AnyNode::StmtRaise(node) => Some(Stmt::Raise(node)), + AnyNode::StmtTry(node) => Some(Stmt::Try(node)), + AnyNode::StmtAssert(node) => Some(Stmt::Assert(node)), + AnyNode::StmtImport(node) => Some(Stmt::Import(node)), + AnyNode::StmtImportFrom(node) => Some(Stmt::ImportFrom(node)), + AnyNode::StmtGlobal(node) => Some(Stmt::Global(node)), + AnyNode::StmtNonlocal(node) => Some(Stmt::Nonlocal(node)), + AnyNode::StmtExpr(node) => Some(Stmt::Expr(node)), + AnyNode::StmtPass(node) => Some(Stmt::Pass(node)), + AnyNode::StmtBreak(node) => Some(Stmt::Break(node)), + AnyNode::StmtContinue(node) => Some(Stmt::Continue(node)), + AnyNode::StmtIpyEscapeCommand(node) => Some(Stmt::IpyEscapeCommand(node)), + + _ => None, + } + } +} + +impl AnyNode { + pub fn expression(self) -> Option { + match self { + AnyNode::ExprBoolOp(node) => Some(Expr::BoolOp(node)), + AnyNode::ExprNamed(node) => Some(Expr::Named(node)), + AnyNode::ExprBinOp(node) => Some(Expr::BinOp(node)), + AnyNode::ExprUnaryOp(node) => Some(Expr::UnaryOp(node)), + AnyNode::ExprLambda(node) => Some(Expr::Lambda(node)), + AnyNode::ExprIf(node) => Some(Expr::If(node)), + AnyNode::ExprDict(node) => Some(Expr::Dict(node)), + AnyNode::ExprSet(node) => Some(Expr::Set(node)), + AnyNode::ExprListComp(node) => Some(Expr::ListComp(node)), + AnyNode::ExprSetComp(node) => Some(Expr::SetComp(node)), + AnyNode::ExprDictComp(node) => Some(Expr::DictComp(node)), + AnyNode::ExprGenerator(node) => Some(Expr::Generator(node)), + AnyNode::ExprAwait(node) => Some(Expr::Await(node)), + AnyNode::ExprYield(node) => Some(Expr::Yield(node)), + AnyNode::ExprYieldFrom(node) => Some(Expr::YieldFrom(node)), + AnyNode::ExprCompare(node) => Some(Expr::Compare(node)), + AnyNode::ExprCall(node) => Some(Expr::Call(node)), + AnyNode::ExprFString(node) => Some(Expr::FString(node)), + AnyNode::ExprStringLiteral(node) => Some(Expr::StringLiteral(node)), + AnyNode::ExprBytesLiteral(node) => Some(Expr::BytesLiteral(node)), + AnyNode::ExprNumberLiteral(node) => Some(Expr::NumberLiteral(node)), + AnyNode::ExprBooleanLiteral(node) => Some(Expr::BooleanLiteral(node)), + AnyNode::ExprNoneLiteral(node) => Some(Expr::NoneLiteral(node)), + AnyNode::ExprEllipsisLiteral(node) => Some(Expr::EllipsisLiteral(node)), + AnyNode::ExprAttribute(node) => Some(Expr::Attribute(node)), + AnyNode::ExprSubscript(node) => Some(Expr::Subscript(node)), + AnyNode::ExprStarred(node) => Some(Expr::Starred(node)), + AnyNode::ExprName(node) => Some(Expr::Name(node)), + AnyNode::ExprList(node) => Some(Expr::List(node)), + AnyNode::ExprTuple(node) => Some(Expr::Tuple(node)), + AnyNode::ExprSlice(node) => Some(Expr::Slice(node)), + AnyNode::ExprIpyEscapeCommand(node) => Some(Expr::IpyEscapeCommand(node)), + + _ => None, + } + } +} + +impl AnyNode { + pub fn except_handler(self) -> Option { + match self { + AnyNode::ExceptHandlerExceptHandler(node) => Some(ExceptHandler::ExceptHandler(node)), + + _ => None, + } + } +} + +impl AnyNode { + pub fn f_string_element(self) -> Option { + match self { + AnyNode::FStringExpressionElement(node) => Some(FStringElement::Expression(node)), + AnyNode::FStringLiteralElement(node) => Some(FStringElement::Literal(node)), + + _ => None, + } + } +} + +impl AnyNode { + pub fn pattern(self) -> Option { + match self { + AnyNode::PatternMatchValue(node) => Some(Pattern::MatchValue(node)), + AnyNode::PatternMatchSingleton(node) => Some(Pattern::MatchSingleton(node)), + AnyNode::PatternMatchSequence(node) => Some(Pattern::MatchSequence(node)), + AnyNode::PatternMatchMapping(node) => Some(Pattern::MatchMapping(node)), + AnyNode::PatternMatchClass(node) => Some(Pattern::MatchClass(node)), + AnyNode::PatternMatchStar(node) => Some(Pattern::MatchStar(node)), + AnyNode::PatternMatchAs(node) => Some(Pattern::MatchAs(node)), + AnyNode::PatternMatchOr(node) => Some(Pattern::MatchOr(node)), + + _ => None, + } + } +} + +impl AnyNode { + pub fn type_param(self) -> Option { + match self { + AnyNode::TypeParamTypeVar(node) => Some(TypeParam::TypeVar(node)), + AnyNode::TypeParamTypeVarTuple(node) => Some(TypeParam::TypeVarTuple(node)), + AnyNode::TypeParamParamSpec(node) => Some(TypeParam::ParamSpec(node)), + + _ => None, + } + } +} + +impl AnyNode { + pub const fn is_module(&self) -> bool { + matches!(self, AnyNode::ModModule(_) | AnyNode::ModExpression(_)) + } +} + +impl AnyNode { + pub const fn is_statement(&self) -> bool { + matches!( + self, + AnyNode::StmtFunctionDef(_) + | AnyNode::StmtClassDef(_) + | AnyNode::StmtReturn(_) + | AnyNode::StmtDelete(_) + | AnyNode::StmtTypeAlias(_) + | AnyNode::StmtAssign(_) + | AnyNode::StmtAugAssign(_) + | AnyNode::StmtAnnAssign(_) + | AnyNode::StmtFor(_) + | AnyNode::StmtWhile(_) + | AnyNode::StmtIf(_) + | AnyNode::StmtWith(_) + | AnyNode::StmtMatch(_) + | AnyNode::StmtRaise(_) + | AnyNode::StmtTry(_) + | AnyNode::StmtAssert(_) + | AnyNode::StmtImport(_) + | AnyNode::StmtImportFrom(_) + | AnyNode::StmtGlobal(_) + | AnyNode::StmtNonlocal(_) + | AnyNode::StmtExpr(_) + | AnyNode::StmtPass(_) + | AnyNode::StmtBreak(_) + | AnyNode::StmtContinue(_) + | AnyNode::StmtIpyEscapeCommand(_) + ) + } +} + +impl AnyNode { + pub const fn is_expression(&self) -> bool { + matches!( + self, + AnyNode::ExprBoolOp(_) + | AnyNode::ExprNamed(_) + | AnyNode::ExprBinOp(_) + | AnyNode::ExprUnaryOp(_) + | AnyNode::ExprLambda(_) + | AnyNode::ExprIf(_) + | AnyNode::ExprDict(_) + | AnyNode::ExprSet(_) + | AnyNode::ExprListComp(_) + | AnyNode::ExprSetComp(_) + | AnyNode::ExprDictComp(_) + | AnyNode::ExprGenerator(_) + | AnyNode::ExprAwait(_) + | AnyNode::ExprYield(_) + | AnyNode::ExprYieldFrom(_) + | AnyNode::ExprCompare(_) + | AnyNode::ExprCall(_) + | AnyNode::ExprFString(_) + | AnyNode::ExprStringLiteral(_) + | AnyNode::ExprBytesLiteral(_) + | AnyNode::ExprNumberLiteral(_) + | AnyNode::ExprBooleanLiteral(_) + | AnyNode::ExprNoneLiteral(_) + | AnyNode::ExprEllipsisLiteral(_) + | AnyNode::ExprAttribute(_) + | AnyNode::ExprSubscript(_) + | AnyNode::ExprStarred(_) + | AnyNode::ExprName(_) + | AnyNode::ExprList(_) + | AnyNode::ExprTuple(_) + | AnyNode::ExprSlice(_) + | AnyNode::ExprIpyEscapeCommand(_) + ) + } +} + +impl AnyNode { + pub const fn is_except_handler(&self) -> bool { + matches!(self, AnyNode::ExceptHandlerExceptHandler(_)) + } +} + +impl AnyNode { + pub const fn is_f_string_element(&self) -> bool { + matches!( + self, + AnyNode::FStringExpressionElement(_) | AnyNode::FStringLiteralElement(_) + ) + } +} + +impl AnyNode { + pub const fn is_pattern(&self) -> bool { + matches!( + self, + AnyNode::PatternMatchValue(_) + | AnyNode::PatternMatchSingleton(_) + | AnyNode::PatternMatchSequence(_) + | AnyNode::PatternMatchMapping(_) + | AnyNode::PatternMatchClass(_) + | AnyNode::PatternMatchStar(_) + | AnyNode::PatternMatchAs(_) + | AnyNode::PatternMatchOr(_) + ) + } +} + +impl AnyNode { + pub const fn is_type_param(&self) -> bool { + matches!( + self, + AnyNode::TypeParamTypeVar(_) + | AnyNode::TypeParamTypeVarTuple(_) + | AnyNode::TypeParamParamSpec(_) + ) + } +} + +impl AnyNode { + pub const fn as_ref(&self) -> AnyNodeRef { + match self { + AnyNode::ModModule(node) => AnyNodeRef::ModModule(node), + AnyNode::ModExpression(node) => AnyNodeRef::ModExpression(node), + AnyNode::StmtFunctionDef(node) => AnyNodeRef::StmtFunctionDef(node), + AnyNode::StmtClassDef(node) => AnyNodeRef::StmtClassDef(node), + AnyNode::StmtReturn(node) => AnyNodeRef::StmtReturn(node), + AnyNode::StmtDelete(node) => AnyNodeRef::StmtDelete(node), + AnyNode::StmtTypeAlias(node) => AnyNodeRef::StmtTypeAlias(node), + AnyNode::StmtAssign(node) => AnyNodeRef::StmtAssign(node), + AnyNode::StmtAugAssign(node) => AnyNodeRef::StmtAugAssign(node), + AnyNode::StmtAnnAssign(node) => AnyNodeRef::StmtAnnAssign(node), + AnyNode::StmtFor(node) => AnyNodeRef::StmtFor(node), + AnyNode::StmtWhile(node) => AnyNodeRef::StmtWhile(node), + AnyNode::StmtIf(node) => AnyNodeRef::StmtIf(node), + AnyNode::StmtWith(node) => AnyNodeRef::StmtWith(node), + AnyNode::StmtMatch(node) => AnyNodeRef::StmtMatch(node), + AnyNode::StmtRaise(node) => AnyNodeRef::StmtRaise(node), + AnyNode::StmtTry(node) => AnyNodeRef::StmtTry(node), + AnyNode::StmtAssert(node) => AnyNodeRef::StmtAssert(node), + AnyNode::StmtImport(node) => AnyNodeRef::StmtImport(node), + AnyNode::StmtImportFrom(node) => AnyNodeRef::StmtImportFrom(node), + AnyNode::StmtGlobal(node) => AnyNodeRef::StmtGlobal(node), + AnyNode::StmtNonlocal(node) => AnyNodeRef::StmtNonlocal(node), + AnyNode::StmtExpr(node) => AnyNodeRef::StmtExpr(node), + AnyNode::StmtPass(node) => AnyNodeRef::StmtPass(node), + AnyNode::StmtBreak(node) => AnyNodeRef::StmtBreak(node), + AnyNode::StmtContinue(node) => AnyNodeRef::StmtContinue(node), + AnyNode::StmtIpyEscapeCommand(node) => AnyNodeRef::StmtIpyEscapeCommand(node), + AnyNode::ExprBoolOp(node) => AnyNodeRef::ExprBoolOp(node), + AnyNode::ExprNamed(node) => AnyNodeRef::ExprNamed(node), + AnyNode::ExprBinOp(node) => AnyNodeRef::ExprBinOp(node), + AnyNode::ExprUnaryOp(node) => AnyNodeRef::ExprUnaryOp(node), + AnyNode::ExprLambda(node) => AnyNodeRef::ExprLambda(node), + AnyNode::ExprIf(node) => AnyNodeRef::ExprIf(node), + AnyNode::ExprDict(node) => AnyNodeRef::ExprDict(node), + AnyNode::ExprSet(node) => AnyNodeRef::ExprSet(node), + AnyNode::ExprListComp(node) => AnyNodeRef::ExprListComp(node), + AnyNode::ExprSetComp(node) => AnyNodeRef::ExprSetComp(node), + AnyNode::ExprDictComp(node) => AnyNodeRef::ExprDictComp(node), + AnyNode::ExprGenerator(node) => AnyNodeRef::ExprGenerator(node), + AnyNode::ExprAwait(node) => AnyNodeRef::ExprAwait(node), + AnyNode::ExprYield(node) => AnyNodeRef::ExprYield(node), + AnyNode::ExprYieldFrom(node) => AnyNodeRef::ExprYieldFrom(node), + AnyNode::ExprCompare(node) => AnyNodeRef::ExprCompare(node), + AnyNode::ExprCall(node) => AnyNodeRef::ExprCall(node), + AnyNode::ExprFString(node) => AnyNodeRef::ExprFString(node), + AnyNode::ExprStringLiteral(node) => AnyNodeRef::ExprStringLiteral(node), + AnyNode::ExprBytesLiteral(node) => AnyNodeRef::ExprBytesLiteral(node), + AnyNode::ExprNumberLiteral(node) => AnyNodeRef::ExprNumberLiteral(node), + AnyNode::ExprBooleanLiteral(node) => AnyNodeRef::ExprBooleanLiteral(node), + AnyNode::ExprNoneLiteral(node) => AnyNodeRef::ExprNoneLiteral(node), + AnyNode::ExprEllipsisLiteral(node) => AnyNodeRef::ExprEllipsisLiteral(node), + AnyNode::ExprAttribute(node) => AnyNodeRef::ExprAttribute(node), + AnyNode::ExprSubscript(node) => AnyNodeRef::ExprSubscript(node), + AnyNode::ExprStarred(node) => AnyNodeRef::ExprStarred(node), + AnyNode::ExprName(node) => AnyNodeRef::ExprName(node), + AnyNode::ExprList(node) => AnyNodeRef::ExprList(node), + AnyNode::ExprTuple(node) => AnyNodeRef::ExprTuple(node), + AnyNode::ExprSlice(node) => AnyNodeRef::ExprSlice(node), + AnyNode::ExprIpyEscapeCommand(node) => AnyNodeRef::ExprIpyEscapeCommand(node), + AnyNode::ExceptHandlerExceptHandler(node) => { + AnyNodeRef::ExceptHandlerExceptHandler(node) + } + AnyNode::FStringExpressionElement(node) => AnyNodeRef::FStringExpressionElement(node), + AnyNode::FStringLiteralElement(node) => AnyNodeRef::FStringLiteralElement(node), + AnyNode::PatternMatchValue(node) => AnyNodeRef::PatternMatchValue(node), + AnyNode::PatternMatchSingleton(node) => AnyNodeRef::PatternMatchSingleton(node), + AnyNode::PatternMatchSequence(node) => AnyNodeRef::PatternMatchSequence(node), + AnyNode::PatternMatchMapping(node) => AnyNodeRef::PatternMatchMapping(node), + AnyNode::PatternMatchClass(node) => AnyNodeRef::PatternMatchClass(node), + AnyNode::PatternMatchStar(node) => AnyNodeRef::PatternMatchStar(node), + AnyNode::PatternMatchAs(node) => AnyNodeRef::PatternMatchAs(node), + AnyNode::PatternMatchOr(node) => AnyNodeRef::PatternMatchOr(node), + AnyNode::TypeParamTypeVar(node) => AnyNodeRef::TypeParamTypeVar(node), + AnyNode::TypeParamTypeVarTuple(node) => AnyNodeRef::TypeParamTypeVarTuple(node), + AnyNode::TypeParamParamSpec(node) => AnyNodeRef::TypeParamParamSpec(node), + AnyNode::FStringFormatSpec(node) => AnyNodeRef::FStringFormatSpec(node), + AnyNode::PatternArguments(node) => AnyNodeRef::PatternArguments(node), + AnyNode::PatternKeyword(node) => AnyNodeRef::PatternKeyword(node), + AnyNode::Comprehension(node) => AnyNodeRef::Comprehension(node), + AnyNode::Arguments(node) => AnyNodeRef::Arguments(node), + AnyNode::Parameters(node) => AnyNodeRef::Parameters(node), + AnyNode::Parameter(node) => AnyNodeRef::Parameter(node), + AnyNode::ParameterWithDefault(node) => AnyNodeRef::ParameterWithDefault(node), + AnyNode::Keyword(node) => AnyNodeRef::Keyword(node), + AnyNode::Alias(node) => AnyNodeRef::Alias(node), + AnyNode::WithItem(node) => AnyNodeRef::WithItem(node), + AnyNode::MatchCase(node) => AnyNodeRef::MatchCase(node), + AnyNode::Decorator(node) => AnyNodeRef::Decorator(node), + AnyNode::ElifElseClause(node) => AnyNodeRef::ElifElseClause(node), + AnyNode::TypeParams(node) => AnyNodeRef::TypeParams(node), + AnyNode::FString(node) => AnyNodeRef::FString(node), + AnyNode::StringLiteral(node) => AnyNodeRef::StringLiteral(node), + AnyNode::BytesLiteral(node) => AnyNodeRef::BytesLiteral(node), + AnyNode::Identifier(node) => AnyNodeRef::Identifier(node), + } + } +} + +#[derive(Copy, Clone, Debug, is_macro::Is, PartialEq)] +pub enum AnyNodeRef<'a> { + ModModule(&'a crate::ModModule), + ModExpression(&'a crate::ModExpression), + StmtFunctionDef(&'a crate::StmtFunctionDef), + StmtClassDef(&'a crate::StmtClassDef), + StmtReturn(&'a crate::StmtReturn), + StmtDelete(&'a crate::StmtDelete), + StmtTypeAlias(&'a crate::StmtTypeAlias), + StmtAssign(&'a crate::StmtAssign), + StmtAugAssign(&'a crate::StmtAugAssign), + StmtAnnAssign(&'a crate::StmtAnnAssign), + StmtFor(&'a crate::StmtFor), + StmtWhile(&'a crate::StmtWhile), + StmtIf(&'a crate::StmtIf), + StmtWith(&'a crate::StmtWith), + StmtMatch(&'a crate::StmtMatch), + StmtRaise(&'a crate::StmtRaise), + StmtTry(&'a crate::StmtTry), + StmtAssert(&'a crate::StmtAssert), + StmtImport(&'a crate::StmtImport), + StmtImportFrom(&'a crate::StmtImportFrom), + StmtGlobal(&'a crate::StmtGlobal), + StmtNonlocal(&'a crate::StmtNonlocal), + StmtExpr(&'a crate::StmtExpr), + StmtPass(&'a crate::StmtPass), + StmtBreak(&'a crate::StmtBreak), + StmtContinue(&'a crate::StmtContinue), + StmtIpyEscapeCommand(&'a crate::StmtIpyEscapeCommand), + ExprBoolOp(&'a crate::ExprBoolOp), + ExprNamed(&'a crate::ExprNamed), + ExprBinOp(&'a crate::ExprBinOp), + ExprUnaryOp(&'a crate::ExprUnaryOp), + ExprLambda(&'a crate::ExprLambda), + ExprIf(&'a crate::ExprIf), + ExprDict(&'a crate::ExprDict), + ExprSet(&'a crate::ExprSet), + ExprListComp(&'a crate::ExprListComp), + ExprSetComp(&'a crate::ExprSetComp), + ExprDictComp(&'a crate::ExprDictComp), + ExprGenerator(&'a crate::ExprGenerator), + ExprAwait(&'a crate::ExprAwait), + ExprYield(&'a crate::ExprYield), + ExprYieldFrom(&'a crate::ExprYieldFrom), + ExprCompare(&'a crate::ExprCompare), + ExprCall(&'a crate::ExprCall), + ExprFString(&'a crate::ExprFString), + ExprStringLiteral(&'a crate::ExprStringLiteral), + ExprBytesLiteral(&'a crate::ExprBytesLiteral), + ExprNumberLiteral(&'a crate::ExprNumberLiteral), + ExprBooleanLiteral(&'a crate::ExprBooleanLiteral), + ExprNoneLiteral(&'a crate::ExprNoneLiteral), + ExprEllipsisLiteral(&'a crate::ExprEllipsisLiteral), + ExprAttribute(&'a crate::ExprAttribute), + ExprSubscript(&'a crate::ExprSubscript), + ExprStarred(&'a crate::ExprStarred), + ExprName(&'a crate::ExprName), + ExprList(&'a crate::ExprList), + ExprTuple(&'a crate::ExprTuple), + ExprSlice(&'a crate::ExprSlice), + ExprIpyEscapeCommand(&'a crate::ExprIpyEscapeCommand), + ExceptHandlerExceptHandler(&'a crate::ExceptHandlerExceptHandler), + FStringExpressionElement(&'a crate::FStringExpressionElement), + FStringLiteralElement(&'a crate::FStringLiteralElement), + PatternMatchValue(&'a crate::PatternMatchValue), + PatternMatchSingleton(&'a crate::PatternMatchSingleton), + PatternMatchSequence(&'a crate::PatternMatchSequence), + PatternMatchMapping(&'a crate::PatternMatchMapping), + PatternMatchClass(&'a crate::PatternMatchClass), + PatternMatchStar(&'a crate::PatternMatchStar), + PatternMatchAs(&'a crate::PatternMatchAs), + PatternMatchOr(&'a crate::PatternMatchOr), + TypeParamTypeVar(&'a crate::TypeParamTypeVar), + TypeParamTypeVarTuple(&'a crate::TypeParamTypeVarTuple), + TypeParamParamSpec(&'a crate::TypeParamParamSpec), + FStringFormatSpec(&'a crate::FStringFormatSpec), + PatternArguments(&'a crate::PatternArguments), + PatternKeyword(&'a crate::PatternKeyword), + Comprehension(&'a crate::Comprehension), + Arguments(&'a crate::Arguments), + Parameters(&'a crate::Parameters), + Parameter(&'a crate::Parameter), + ParameterWithDefault(&'a crate::ParameterWithDefault), + Keyword(&'a crate::Keyword), + Alias(&'a crate::Alias), + WithItem(&'a crate::WithItem), + MatchCase(&'a crate::MatchCase), + Decorator(&'a crate::Decorator), + ElifElseClause(&'a crate::ElifElseClause), + TypeParams(&'a crate::TypeParams), + FString(&'a crate::FString), + StringLiteral(&'a crate::StringLiteral), + BytesLiteral(&'a crate::BytesLiteral), + Identifier(&'a crate::Identifier), +} + +impl<'a> From<&'a Mod> for AnyNodeRef<'a> { + fn from(node: &'a Mod) -> AnyNodeRef<'a> { + match node { + Mod::Module(node) => AnyNodeRef::ModModule(node), + Mod::Expression(node) => AnyNodeRef::ModExpression(node), + } + } +} + +impl<'a> From> for AnyNodeRef<'a> { + fn from(node: ModRef<'a>) -> AnyNodeRef<'a> { + match node { + ModRef::Module(node) => AnyNodeRef::ModModule(node), + ModRef::Expression(node) => AnyNodeRef::ModExpression(node), + } + } +} + +impl<'a> From<&'a crate::ModModule> for AnyNodeRef<'a> { + fn from(node: &'a crate::ModModule) -> AnyNodeRef<'a> { + AnyNodeRef::ModModule(node) + } +} + +impl<'a> From<&'a crate::ModExpression> for AnyNodeRef<'a> { + fn from(node: &'a crate::ModExpression) -> AnyNodeRef<'a> { + AnyNodeRef::ModExpression(node) + } +} + +impl<'a> From<&'a Stmt> for AnyNodeRef<'a> { + fn from(node: &'a Stmt) -> AnyNodeRef<'a> { + match node { + Stmt::FunctionDef(node) => AnyNodeRef::StmtFunctionDef(node), + Stmt::ClassDef(node) => AnyNodeRef::StmtClassDef(node), + Stmt::Return(node) => AnyNodeRef::StmtReturn(node), + Stmt::Delete(node) => AnyNodeRef::StmtDelete(node), + Stmt::TypeAlias(node) => AnyNodeRef::StmtTypeAlias(node), + Stmt::Assign(node) => AnyNodeRef::StmtAssign(node), + Stmt::AugAssign(node) => AnyNodeRef::StmtAugAssign(node), + Stmt::AnnAssign(node) => AnyNodeRef::StmtAnnAssign(node), + Stmt::For(node) => AnyNodeRef::StmtFor(node), + Stmt::While(node) => AnyNodeRef::StmtWhile(node), + Stmt::If(node) => AnyNodeRef::StmtIf(node), + Stmt::With(node) => AnyNodeRef::StmtWith(node), + Stmt::Match(node) => AnyNodeRef::StmtMatch(node), + Stmt::Raise(node) => AnyNodeRef::StmtRaise(node), + Stmt::Try(node) => AnyNodeRef::StmtTry(node), + Stmt::Assert(node) => AnyNodeRef::StmtAssert(node), + Stmt::Import(node) => AnyNodeRef::StmtImport(node), + Stmt::ImportFrom(node) => AnyNodeRef::StmtImportFrom(node), + Stmt::Global(node) => AnyNodeRef::StmtGlobal(node), + Stmt::Nonlocal(node) => AnyNodeRef::StmtNonlocal(node), + Stmt::Expr(node) => AnyNodeRef::StmtExpr(node), + Stmt::Pass(node) => AnyNodeRef::StmtPass(node), + Stmt::Break(node) => AnyNodeRef::StmtBreak(node), + Stmt::Continue(node) => AnyNodeRef::StmtContinue(node), + Stmt::IpyEscapeCommand(node) => AnyNodeRef::StmtIpyEscapeCommand(node), + } + } +} + +impl<'a> From> for AnyNodeRef<'a> { + fn from(node: StatementRef<'a>) -> AnyNodeRef<'a> { + match node { + StatementRef::FunctionDef(node) => AnyNodeRef::StmtFunctionDef(node), + StatementRef::ClassDef(node) => AnyNodeRef::StmtClassDef(node), + StatementRef::Return(node) => AnyNodeRef::StmtReturn(node), + StatementRef::Delete(node) => AnyNodeRef::StmtDelete(node), + StatementRef::TypeAlias(node) => AnyNodeRef::StmtTypeAlias(node), + StatementRef::Assign(node) => AnyNodeRef::StmtAssign(node), + StatementRef::AugAssign(node) => AnyNodeRef::StmtAugAssign(node), + StatementRef::AnnAssign(node) => AnyNodeRef::StmtAnnAssign(node), + StatementRef::For(node) => AnyNodeRef::StmtFor(node), + StatementRef::While(node) => AnyNodeRef::StmtWhile(node), + StatementRef::If(node) => AnyNodeRef::StmtIf(node), + StatementRef::With(node) => AnyNodeRef::StmtWith(node), + StatementRef::Match(node) => AnyNodeRef::StmtMatch(node), + StatementRef::Raise(node) => AnyNodeRef::StmtRaise(node), + StatementRef::Try(node) => AnyNodeRef::StmtTry(node), + StatementRef::Assert(node) => AnyNodeRef::StmtAssert(node), + StatementRef::Import(node) => AnyNodeRef::StmtImport(node), + StatementRef::ImportFrom(node) => AnyNodeRef::StmtImportFrom(node), + StatementRef::Global(node) => AnyNodeRef::StmtGlobal(node), + StatementRef::Nonlocal(node) => AnyNodeRef::StmtNonlocal(node), + StatementRef::Expr(node) => AnyNodeRef::StmtExpr(node), + StatementRef::Pass(node) => AnyNodeRef::StmtPass(node), + StatementRef::Break(node) => AnyNodeRef::StmtBreak(node), + StatementRef::Continue(node) => AnyNodeRef::StmtContinue(node), + StatementRef::IpyEscapeCommand(node) => AnyNodeRef::StmtIpyEscapeCommand(node), + } + } +} + +impl<'a> From<&'a crate::StmtFunctionDef> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtFunctionDef) -> AnyNodeRef<'a> { + AnyNodeRef::StmtFunctionDef(node) + } +} + +impl<'a> From<&'a crate::StmtClassDef> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtClassDef) -> AnyNodeRef<'a> { + AnyNodeRef::StmtClassDef(node) + } +} + +impl<'a> From<&'a crate::StmtReturn> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtReturn) -> AnyNodeRef<'a> { + AnyNodeRef::StmtReturn(node) + } +} + +impl<'a> From<&'a crate::StmtDelete> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtDelete) -> AnyNodeRef<'a> { + AnyNodeRef::StmtDelete(node) + } +} + +impl<'a> From<&'a crate::StmtTypeAlias> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtTypeAlias) -> AnyNodeRef<'a> { + AnyNodeRef::StmtTypeAlias(node) + } +} + +impl<'a> From<&'a crate::StmtAssign> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtAssign) -> AnyNodeRef<'a> { + AnyNodeRef::StmtAssign(node) + } +} + +impl<'a> From<&'a crate::StmtAugAssign> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtAugAssign) -> AnyNodeRef<'a> { + AnyNodeRef::StmtAugAssign(node) + } +} + +impl<'a> From<&'a crate::StmtAnnAssign> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtAnnAssign) -> AnyNodeRef<'a> { + AnyNodeRef::StmtAnnAssign(node) + } +} + +impl<'a> From<&'a crate::StmtFor> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtFor) -> AnyNodeRef<'a> { + AnyNodeRef::StmtFor(node) + } +} + +impl<'a> From<&'a crate::StmtWhile> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtWhile) -> AnyNodeRef<'a> { + AnyNodeRef::StmtWhile(node) + } +} + +impl<'a> From<&'a crate::StmtIf> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtIf) -> AnyNodeRef<'a> { + AnyNodeRef::StmtIf(node) + } +} + +impl<'a> From<&'a crate::StmtWith> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtWith) -> AnyNodeRef<'a> { + AnyNodeRef::StmtWith(node) + } +} + +impl<'a> From<&'a crate::StmtMatch> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtMatch) -> AnyNodeRef<'a> { + AnyNodeRef::StmtMatch(node) + } +} + +impl<'a> From<&'a crate::StmtRaise> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtRaise) -> AnyNodeRef<'a> { + AnyNodeRef::StmtRaise(node) + } +} + +impl<'a> From<&'a crate::StmtTry> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtTry) -> AnyNodeRef<'a> { + AnyNodeRef::StmtTry(node) + } +} + +impl<'a> From<&'a crate::StmtAssert> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtAssert) -> AnyNodeRef<'a> { + AnyNodeRef::StmtAssert(node) + } +} + +impl<'a> From<&'a crate::StmtImport> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtImport) -> AnyNodeRef<'a> { + AnyNodeRef::StmtImport(node) + } +} + +impl<'a> From<&'a crate::StmtImportFrom> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtImportFrom) -> AnyNodeRef<'a> { + AnyNodeRef::StmtImportFrom(node) + } +} + +impl<'a> From<&'a crate::StmtGlobal> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtGlobal) -> AnyNodeRef<'a> { + AnyNodeRef::StmtGlobal(node) + } +} + +impl<'a> From<&'a crate::StmtNonlocal> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtNonlocal) -> AnyNodeRef<'a> { + AnyNodeRef::StmtNonlocal(node) + } +} + +impl<'a> From<&'a crate::StmtExpr> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtExpr) -> AnyNodeRef<'a> { + AnyNodeRef::StmtExpr(node) + } +} + +impl<'a> From<&'a crate::StmtPass> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtPass) -> AnyNodeRef<'a> { + AnyNodeRef::StmtPass(node) + } +} + +impl<'a> From<&'a crate::StmtBreak> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtBreak) -> AnyNodeRef<'a> { + AnyNodeRef::StmtBreak(node) + } +} + +impl<'a> From<&'a crate::StmtContinue> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtContinue) -> AnyNodeRef<'a> { + AnyNodeRef::StmtContinue(node) + } +} + +impl<'a> From<&'a crate::StmtIpyEscapeCommand> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtIpyEscapeCommand) -> AnyNodeRef<'a> { + AnyNodeRef::StmtIpyEscapeCommand(node) + } +} + +impl<'a> From<&'a Expr> for AnyNodeRef<'a> { + fn from(node: &'a Expr) -> AnyNodeRef<'a> { + match node { + Expr::BoolOp(node) => AnyNodeRef::ExprBoolOp(node), + Expr::Named(node) => AnyNodeRef::ExprNamed(node), + Expr::BinOp(node) => AnyNodeRef::ExprBinOp(node), + Expr::UnaryOp(node) => AnyNodeRef::ExprUnaryOp(node), + Expr::Lambda(node) => AnyNodeRef::ExprLambda(node), + Expr::If(node) => AnyNodeRef::ExprIf(node), + Expr::Dict(node) => AnyNodeRef::ExprDict(node), + Expr::Set(node) => AnyNodeRef::ExprSet(node), + Expr::ListComp(node) => AnyNodeRef::ExprListComp(node), + Expr::SetComp(node) => AnyNodeRef::ExprSetComp(node), + Expr::DictComp(node) => AnyNodeRef::ExprDictComp(node), + Expr::Generator(node) => AnyNodeRef::ExprGenerator(node), + Expr::Await(node) => AnyNodeRef::ExprAwait(node), + Expr::Yield(node) => AnyNodeRef::ExprYield(node), + Expr::YieldFrom(node) => AnyNodeRef::ExprYieldFrom(node), + Expr::Compare(node) => AnyNodeRef::ExprCompare(node), + Expr::Call(node) => AnyNodeRef::ExprCall(node), + Expr::FString(node) => AnyNodeRef::ExprFString(node), + Expr::StringLiteral(node) => AnyNodeRef::ExprStringLiteral(node), + Expr::BytesLiteral(node) => AnyNodeRef::ExprBytesLiteral(node), + Expr::NumberLiteral(node) => AnyNodeRef::ExprNumberLiteral(node), + Expr::BooleanLiteral(node) => AnyNodeRef::ExprBooleanLiteral(node), + Expr::NoneLiteral(node) => AnyNodeRef::ExprNoneLiteral(node), + Expr::EllipsisLiteral(node) => AnyNodeRef::ExprEllipsisLiteral(node), + Expr::Attribute(node) => AnyNodeRef::ExprAttribute(node), + Expr::Subscript(node) => AnyNodeRef::ExprSubscript(node), + Expr::Starred(node) => AnyNodeRef::ExprStarred(node), + Expr::Name(node) => AnyNodeRef::ExprName(node), + Expr::List(node) => AnyNodeRef::ExprList(node), + Expr::Tuple(node) => AnyNodeRef::ExprTuple(node), + Expr::Slice(node) => AnyNodeRef::ExprSlice(node), + Expr::IpyEscapeCommand(node) => AnyNodeRef::ExprIpyEscapeCommand(node), + } + } +} + +impl<'a> From> for AnyNodeRef<'a> { + fn from(node: ExpressionRef<'a>) -> AnyNodeRef<'a> { + match node { + ExpressionRef::BoolOp(node) => AnyNodeRef::ExprBoolOp(node), + ExpressionRef::Named(node) => AnyNodeRef::ExprNamed(node), + ExpressionRef::BinOp(node) => AnyNodeRef::ExprBinOp(node), + ExpressionRef::UnaryOp(node) => AnyNodeRef::ExprUnaryOp(node), + ExpressionRef::Lambda(node) => AnyNodeRef::ExprLambda(node), + ExpressionRef::If(node) => AnyNodeRef::ExprIf(node), + ExpressionRef::Dict(node) => AnyNodeRef::ExprDict(node), + ExpressionRef::Set(node) => AnyNodeRef::ExprSet(node), + ExpressionRef::ListComp(node) => AnyNodeRef::ExprListComp(node), + ExpressionRef::SetComp(node) => AnyNodeRef::ExprSetComp(node), + ExpressionRef::DictComp(node) => AnyNodeRef::ExprDictComp(node), + ExpressionRef::Generator(node) => AnyNodeRef::ExprGenerator(node), + ExpressionRef::Await(node) => AnyNodeRef::ExprAwait(node), + ExpressionRef::Yield(node) => AnyNodeRef::ExprYield(node), + ExpressionRef::YieldFrom(node) => AnyNodeRef::ExprYieldFrom(node), + ExpressionRef::Compare(node) => AnyNodeRef::ExprCompare(node), + ExpressionRef::Call(node) => AnyNodeRef::ExprCall(node), + ExpressionRef::FString(node) => AnyNodeRef::ExprFString(node), + ExpressionRef::StringLiteral(node) => AnyNodeRef::ExprStringLiteral(node), + ExpressionRef::BytesLiteral(node) => AnyNodeRef::ExprBytesLiteral(node), + ExpressionRef::NumberLiteral(node) => AnyNodeRef::ExprNumberLiteral(node), + ExpressionRef::BooleanLiteral(node) => AnyNodeRef::ExprBooleanLiteral(node), + ExpressionRef::NoneLiteral(node) => AnyNodeRef::ExprNoneLiteral(node), + ExpressionRef::EllipsisLiteral(node) => AnyNodeRef::ExprEllipsisLiteral(node), + ExpressionRef::Attribute(node) => AnyNodeRef::ExprAttribute(node), + ExpressionRef::Subscript(node) => AnyNodeRef::ExprSubscript(node), + ExpressionRef::Starred(node) => AnyNodeRef::ExprStarred(node), + ExpressionRef::Name(node) => AnyNodeRef::ExprName(node), + ExpressionRef::List(node) => AnyNodeRef::ExprList(node), + ExpressionRef::Tuple(node) => AnyNodeRef::ExprTuple(node), + ExpressionRef::Slice(node) => AnyNodeRef::ExprSlice(node), + ExpressionRef::IpyEscapeCommand(node) => AnyNodeRef::ExprIpyEscapeCommand(node), + } + } +} + +impl<'a> From<&'a crate::ExprBoolOp> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprBoolOp) -> AnyNodeRef<'a> { + AnyNodeRef::ExprBoolOp(node) + } +} + +impl<'a> From<&'a crate::ExprNamed> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprNamed) -> AnyNodeRef<'a> { + AnyNodeRef::ExprNamed(node) + } +} + +impl<'a> From<&'a crate::ExprBinOp> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprBinOp) -> AnyNodeRef<'a> { + AnyNodeRef::ExprBinOp(node) + } +} + +impl<'a> From<&'a crate::ExprUnaryOp> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprUnaryOp) -> AnyNodeRef<'a> { + AnyNodeRef::ExprUnaryOp(node) + } +} + +impl<'a> From<&'a crate::ExprLambda> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprLambda) -> AnyNodeRef<'a> { + AnyNodeRef::ExprLambda(node) + } +} + +impl<'a> From<&'a crate::ExprIf> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprIf) -> AnyNodeRef<'a> { + AnyNodeRef::ExprIf(node) + } +} + +impl<'a> From<&'a crate::ExprDict> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprDict) -> AnyNodeRef<'a> { + AnyNodeRef::ExprDict(node) + } +} + +impl<'a> From<&'a crate::ExprSet> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprSet) -> AnyNodeRef<'a> { + AnyNodeRef::ExprSet(node) + } +} + +impl<'a> From<&'a crate::ExprListComp> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprListComp) -> AnyNodeRef<'a> { + AnyNodeRef::ExprListComp(node) + } +} + +impl<'a> From<&'a crate::ExprSetComp> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprSetComp) -> AnyNodeRef<'a> { + AnyNodeRef::ExprSetComp(node) + } +} + +impl<'a> From<&'a crate::ExprDictComp> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprDictComp) -> AnyNodeRef<'a> { + AnyNodeRef::ExprDictComp(node) + } +} + +impl<'a> From<&'a crate::ExprGenerator> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprGenerator) -> AnyNodeRef<'a> { + AnyNodeRef::ExprGenerator(node) + } +} + +impl<'a> From<&'a crate::ExprAwait> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprAwait) -> AnyNodeRef<'a> { + AnyNodeRef::ExprAwait(node) + } +} + +impl<'a> From<&'a crate::ExprYield> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprYield) -> AnyNodeRef<'a> { + AnyNodeRef::ExprYield(node) + } +} + +impl<'a> From<&'a crate::ExprYieldFrom> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprYieldFrom) -> AnyNodeRef<'a> { + AnyNodeRef::ExprYieldFrom(node) + } +} + +impl<'a> From<&'a crate::ExprCompare> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprCompare) -> AnyNodeRef<'a> { + AnyNodeRef::ExprCompare(node) + } +} + +impl<'a> From<&'a crate::ExprCall> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprCall) -> AnyNodeRef<'a> { + AnyNodeRef::ExprCall(node) + } +} + +impl<'a> From<&'a crate::ExprFString> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprFString) -> AnyNodeRef<'a> { + AnyNodeRef::ExprFString(node) + } +} + +impl<'a> From<&'a crate::ExprStringLiteral> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprStringLiteral) -> AnyNodeRef<'a> { + AnyNodeRef::ExprStringLiteral(node) + } +} + +impl<'a> From<&'a crate::ExprBytesLiteral> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprBytesLiteral) -> AnyNodeRef<'a> { + AnyNodeRef::ExprBytesLiteral(node) + } +} + +impl<'a> From<&'a crate::ExprNumberLiteral> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprNumberLiteral) -> AnyNodeRef<'a> { + AnyNodeRef::ExprNumberLiteral(node) + } +} + +impl<'a> From<&'a crate::ExprBooleanLiteral> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprBooleanLiteral) -> AnyNodeRef<'a> { + AnyNodeRef::ExprBooleanLiteral(node) + } +} + +impl<'a> From<&'a crate::ExprNoneLiteral> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprNoneLiteral) -> AnyNodeRef<'a> { + AnyNodeRef::ExprNoneLiteral(node) + } +} + +impl<'a> From<&'a crate::ExprEllipsisLiteral> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprEllipsisLiteral) -> AnyNodeRef<'a> { + AnyNodeRef::ExprEllipsisLiteral(node) + } +} + +impl<'a> From<&'a crate::ExprAttribute> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprAttribute) -> AnyNodeRef<'a> { + AnyNodeRef::ExprAttribute(node) + } +} + +impl<'a> From<&'a crate::ExprSubscript> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprSubscript) -> AnyNodeRef<'a> { + AnyNodeRef::ExprSubscript(node) + } +} + +impl<'a> From<&'a crate::ExprStarred> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprStarred) -> AnyNodeRef<'a> { + AnyNodeRef::ExprStarred(node) + } +} + +impl<'a> From<&'a crate::ExprName> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprName) -> AnyNodeRef<'a> { + AnyNodeRef::ExprName(node) + } +} + +impl<'a> From<&'a crate::ExprList> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprList) -> AnyNodeRef<'a> { + AnyNodeRef::ExprList(node) + } +} + +impl<'a> From<&'a crate::ExprTuple> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprTuple) -> AnyNodeRef<'a> { + AnyNodeRef::ExprTuple(node) + } +} + +impl<'a> From<&'a crate::ExprSlice> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprSlice) -> AnyNodeRef<'a> { + AnyNodeRef::ExprSlice(node) + } +} + +impl<'a> From<&'a crate::ExprIpyEscapeCommand> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprIpyEscapeCommand) -> AnyNodeRef<'a> { + AnyNodeRef::ExprIpyEscapeCommand(node) + } +} + +impl<'a> From<&'a ExceptHandler> for AnyNodeRef<'a> { + fn from(node: &'a ExceptHandler) -> AnyNodeRef<'a> { + match node { + ExceptHandler::ExceptHandler(node) => AnyNodeRef::ExceptHandlerExceptHandler(node), + } + } +} + +impl<'a> From> for AnyNodeRef<'a> { + fn from(node: ExceptHandlerRef<'a>) -> AnyNodeRef<'a> { + match node { + ExceptHandlerRef::ExceptHandler(node) => AnyNodeRef::ExceptHandlerExceptHandler(node), + } + } +} + +impl<'a> From<&'a crate::ExceptHandlerExceptHandler> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExceptHandlerExceptHandler) -> AnyNodeRef<'a> { + AnyNodeRef::ExceptHandlerExceptHandler(node) + } +} + +impl<'a> From<&'a FStringElement> for AnyNodeRef<'a> { + fn from(node: &'a FStringElement) -> AnyNodeRef<'a> { + match node { + FStringElement::Expression(node) => AnyNodeRef::FStringExpressionElement(node), + FStringElement::Literal(node) => AnyNodeRef::FStringLiteralElement(node), + } + } +} + +impl<'a> From> for AnyNodeRef<'a> { + fn from(node: FStringElementRef<'a>) -> AnyNodeRef<'a> { + match node { + FStringElementRef::Expression(node) => AnyNodeRef::FStringExpressionElement(node), + FStringElementRef::Literal(node) => AnyNodeRef::FStringLiteralElement(node), + } + } +} + +impl<'a> From<&'a crate::FStringExpressionElement> for AnyNodeRef<'a> { + fn from(node: &'a crate::FStringExpressionElement) -> AnyNodeRef<'a> { + AnyNodeRef::FStringExpressionElement(node) + } +} + +impl<'a> From<&'a crate::FStringLiteralElement> for AnyNodeRef<'a> { + fn from(node: &'a crate::FStringLiteralElement) -> AnyNodeRef<'a> { + AnyNodeRef::FStringLiteralElement(node) + } +} + +impl<'a> From<&'a Pattern> for AnyNodeRef<'a> { + fn from(node: &'a Pattern) -> AnyNodeRef<'a> { + match node { + Pattern::MatchValue(node) => AnyNodeRef::PatternMatchValue(node), + Pattern::MatchSingleton(node) => AnyNodeRef::PatternMatchSingleton(node), + Pattern::MatchSequence(node) => AnyNodeRef::PatternMatchSequence(node), + Pattern::MatchMapping(node) => AnyNodeRef::PatternMatchMapping(node), + Pattern::MatchClass(node) => AnyNodeRef::PatternMatchClass(node), + Pattern::MatchStar(node) => AnyNodeRef::PatternMatchStar(node), + Pattern::MatchAs(node) => AnyNodeRef::PatternMatchAs(node), + Pattern::MatchOr(node) => AnyNodeRef::PatternMatchOr(node), + } + } +} + +impl<'a> From> for AnyNodeRef<'a> { + fn from(node: PatternRef<'a>) -> AnyNodeRef<'a> { + match node { + PatternRef::MatchValue(node) => AnyNodeRef::PatternMatchValue(node), + PatternRef::MatchSingleton(node) => AnyNodeRef::PatternMatchSingleton(node), + PatternRef::MatchSequence(node) => AnyNodeRef::PatternMatchSequence(node), + PatternRef::MatchMapping(node) => AnyNodeRef::PatternMatchMapping(node), + PatternRef::MatchClass(node) => AnyNodeRef::PatternMatchClass(node), + PatternRef::MatchStar(node) => AnyNodeRef::PatternMatchStar(node), + PatternRef::MatchAs(node) => AnyNodeRef::PatternMatchAs(node), + PatternRef::MatchOr(node) => AnyNodeRef::PatternMatchOr(node), + } + } +} + +impl<'a> From<&'a crate::PatternMatchValue> for AnyNodeRef<'a> { + fn from(node: &'a crate::PatternMatchValue) -> AnyNodeRef<'a> { + AnyNodeRef::PatternMatchValue(node) + } +} + +impl<'a> From<&'a crate::PatternMatchSingleton> for AnyNodeRef<'a> { + fn from(node: &'a crate::PatternMatchSingleton) -> AnyNodeRef<'a> { + AnyNodeRef::PatternMatchSingleton(node) + } +} + +impl<'a> From<&'a crate::PatternMatchSequence> for AnyNodeRef<'a> { + fn from(node: &'a crate::PatternMatchSequence) -> AnyNodeRef<'a> { + AnyNodeRef::PatternMatchSequence(node) + } +} + +impl<'a> From<&'a crate::PatternMatchMapping> for AnyNodeRef<'a> { + fn from(node: &'a crate::PatternMatchMapping) -> AnyNodeRef<'a> { + AnyNodeRef::PatternMatchMapping(node) + } +} + +impl<'a> From<&'a crate::PatternMatchClass> for AnyNodeRef<'a> { + fn from(node: &'a crate::PatternMatchClass) -> AnyNodeRef<'a> { + AnyNodeRef::PatternMatchClass(node) + } +} + +impl<'a> From<&'a crate::PatternMatchStar> for AnyNodeRef<'a> { + fn from(node: &'a crate::PatternMatchStar) -> AnyNodeRef<'a> { + AnyNodeRef::PatternMatchStar(node) + } +} + +impl<'a> From<&'a crate::PatternMatchAs> for AnyNodeRef<'a> { + fn from(node: &'a crate::PatternMatchAs) -> AnyNodeRef<'a> { + AnyNodeRef::PatternMatchAs(node) + } +} + +impl<'a> From<&'a crate::PatternMatchOr> for AnyNodeRef<'a> { + fn from(node: &'a crate::PatternMatchOr) -> AnyNodeRef<'a> { + AnyNodeRef::PatternMatchOr(node) + } +} + +impl<'a> From<&'a TypeParam> for AnyNodeRef<'a> { + fn from(node: &'a TypeParam) -> AnyNodeRef<'a> { + match node { + TypeParam::TypeVar(node) => AnyNodeRef::TypeParamTypeVar(node), + TypeParam::TypeVarTuple(node) => AnyNodeRef::TypeParamTypeVarTuple(node), + TypeParam::ParamSpec(node) => AnyNodeRef::TypeParamParamSpec(node), + } + } +} + +impl<'a> From> for AnyNodeRef<'a> { + fn from(node: TypeParamRef<'a>) -> AnyNodeRef<'a> { + match node { + TypeParamRef::TypeVar(node) => AnyNodeRef::TypeParamTypeVar(node), + TypeParamRef::TypeVarTuple(node) => AnyNodeRef::TypeParamTypeVarTuple(node), + TypeParamRef::ParamSpec(node) => AnyNodeRef::TypeParamParamSpec(node), + } + } +} + +impl<'a> From<&'a crate::TypeParamTypeVar> for AnyNodeRef<'a> { + fn from(node: &'a crate::TypeParamTypeVar) -> AnyNodeRef<'a> { + AnyNodeRef::TypeParamTypeVar(node) + } +} + +impl<'a> From<&'a crate::TypeParamTypeVarTuple> for AnyNodeRef<'a> { + fn from(node: &'a crate::TypeParamTypeVarTuple) -> AnyNodeRef<'a> { + AnyNodeRef::TypeParamTypeVarTuple(node) + } +} + +impl<'a> From<&'a crate::TypeParamParamSpec> for AnyNodeRef<'a> { + fn from(node: &'a crate::TypeParamParamSpec) -> AnyNodeRef<'a> { + AnyNodeRef::TypeParamParamSpec(node) + } +} + +impl<'a> From<&'a crate::FStringFormatSpec> for AnyNodeRef<'a> { + fn from(node: &'a crate::FStringFormatSpec) -> AnyNodeRef<'a> { + AnyNodeRef::FStringFormatSpec(node) + } +} + +impl<'a> From<&'a crate::PatternArguments> for AnyNodeRef<'a> { + fn from(node: &'a crate::PatternArguments) -> AnyNodeRef<'a> { + AnyNodeRef::PatternArguments(node) + } +} + +impl<'a> From<&'a crate::PatternKeyword> for AnyNodeRef<'a> { + fn from(node: &'a crate::PatternKeyword) -> AnyNodeRef<'a> { + AnyNodeRef::PatternKeyword(node) + } +} + +impl<'a> From<&'a crate::Comprehension> for AnyNodeRef<'a> { + fn from(node: &'a crate::Comprehension) -> AnyNodeRef<'a> { + AnyNodeRef::Comprehension(node) + } +} + +impl<'a> From<&'a crate::Arguments> for AnyNodeRef<'a> { + fn from(node: &'a crate::Arguments) -> AnyNodeRef<'a> { + AnyNodeRef::Arguments(node) + } +} + +impl<'a> From<&'a crate::Parameters> for AnyNodeRef<'a> { + fn from(node: &'a crate::Parameters) -> AnyNodeRef<'a> { + AnyNodeRef::Parameters(node) + } +} + +impl<'a> From<&'a crate::Parameter> for AnyNodeRef<'a> { + fn from(node: &'a crate::Parameter) -> AnyNodeRef<'a> { + AnyNodeRef::Parameter(node) + } +} + +impl<'a> From<&'a crate::ParameterWithDefault> for AnyNodeRef<'a> { + fn from(node: &'a crate::ParameterWithDefault) -> AnyNodeRef<'a> { + AnyNodeRef::ParameterWithDefault(node) + } +} + +impl<'a> From<&'a crate::Keyword> for AnyNodeRef<'a> { + fn from(node: &'a crate::Keyword) -> AnyNodeRef<'a> { + AnyNodeRef::Keyword(node) + } +} + +impl<'a> From<&'a crate::Alias> for AnyNodeRef<'a> { + fn from(node: &'a crate::Alias) -> AnyNodeRef<'a> { + AnyNodeRef::Alias(node) + } +} + +impl<'a> From<&'a crate::WithItem> for AnyNodeRef<'a> { + fn from(node: &'a crate::WithItem) -> AnyNodeRef<'a> { + AnyNodeRef::WithItem(node) + } +} + +impl<'a> From<&'a crate::MatchCase> for AnyNodeRef<'a> { + fn from(node: &'a crate::MatchCase) -> AnyNodeRef<'a> { + AnyNodeRef::MatchCase(node) + } +} + +impl<'a> From<&'a crate::Decorator> for AnyNodeRef<'a> { + fn from(node: &'a crate::Decorator) -> AnyNodeRef<'a> { + AnyNodeRef::Decorator(node) + } +} + +impl<'a> From<&'a crate::ElifElseClause> for AnyNodeRef<'a> { + fn from(node: &'a crate::ElifElseClause) -> AnyNodeRef<'a> { + AnyNodeRef::ElifElseClause(node) + } +} + +impl<'a> From<&'a crate::TypeParams> for AnyNodeRef<'a> { + fn from(node: &'a crate::TypeParams) -> AnyNodeRef<'a> { + AnyNodeRef::TypeParams(node) + } +} + +impl<'a> From<&'a crate::FString> for AnyNodeRef<'a> { + fn from(node: &'a crate::FString) -> AnyNodeRef<'a> { + AnyNodeRef::FString(node) + } +} + +impl<'a> From<&'a crate::StringLiteral> for AnyNodeRef<'a> { + fn from(node: &'a crate::StringLiteral) -> AnyNodeRef<'a> { + AnyNodeRef::StringLiteral(node) + } +} + +impl<'a> From<&'a crate::BytesLiteral> for AnyNodeRef<'a> { + fn from(node: &'a crate::BytesLiteral) -> AnyNodeRef<'a> { + AnyNodeRef::BytesLiteral(node) + } +} + +impl<'a> From<&'a crate::Identifier> for AnyNodeRef<'a> { + fn from(node: &'a crate::Identifier) -> AnyNodeRef<'a> { + AnyNodeRef::Identifier(node) + } +} + +impl ruff_text_size::Ranged for AnyNodeRef<'_> { + fn range(&self) -> ruff_text_size::TextRange { + match self { + AnyNodeRef::ModModule(node) => node.range(), + AnyNodeRef::ModExpression(node) => node.range(), + AnyNodeRef::StmtFunctionDef(node) => node.range(), + AnyNodeRef::StmtClassDef(node) => node.range(), + AnyNodeRef::StmtReturn(node) => node.range(), + AnyNodeRef::StmtDelete(node) => node.range(), + AnyNodeRef::StmtTypeAlias(node) => node.range(), + AnyNodeRef::StmtAssign(node) => node.range(), + AnyNodeRef::StmtAugAssign(node) => node.range(), + AnyNodeRef::StmtAnnAssign(node) => node.range(), + AnyNodeRef::StmtFor(node) => node.range(), + AnyNodeRef::StmtWhile(node) => node.range(), + AnyNodeRef::StmtIf(node) => node.range(), + AnyNodeRef::StmtWith(node) => node.range(), + AnyNodeRef::StmtMatch(node) => node.range(), + AnyNodeRef::StmtRaise(node) => node.range(), + AnyNodeRef::StmtTry(node) => node.range(), + AnyNodeRef::StmtAssert(node) => node.range(), + AnyNodeRef::StmtImport(node) => node.range(), + AnyNodeRef::StmtImportFrom(node) => node.range(), + AnyNodeRef::StmtGlobal(node) => node.range(), + AnyNodeRef::StmtNonlocal(node) => node.range(), + AnyNodeRef::StmtExpr(node) => node.range(), + AnyNodeRef::StmtPass(node) => node.range(), + AnyNodeRef::StmtBreak(node) => node.range(), + AnyNodeRef::StmtContinue(node) => node.range(), + AnyNodeRef::StmtIpyEscapeCommand(node) => node.range(), + AnyNodeRef::ExprBoolOp(node) => node.range(), + AnyNodeRef::ExprNamed(node) => node.range(), + AnyNodeRef::ExprBinOp(node) => node.range(), + AnyNodeRef::ExprUnaryOp(node) => node.range(), + AnyNodeRef::ExprLambda(node) => node.range(), + AnyNodeRef::ExprIf(node) => node.range(), + AnyNodeRef::ExprDict(node) => node.range(), + AnyNodeRef::ExprSet(node) => node.range(), + AnyNodeRef::ExprListComp(node) => node.range(), + AnyNodeRef::ExprSetComp(node) => node.range(), + AnyNodeRef::ExprDictComp(node) => node.range(), + AnyNodeRef::ExprGenerator(node) => node.range(), + AnyNodeRef::ExprAwait(node) => node.range(), + AnyNodeRef::ExprYield(node) => node.range(), + AnyNodeRef::ExprYieldFrom(node) => node.range(), + AnyNodeRef::ExprCompare(node) => node.range(), + AnyNodeRef::ExprCall(node) => node.range(), + AnyNodeRef::ExprFString(node) => node.range(), + AnyNodeRef::ExprStringLiteral(node) => node.range(), + AnyNodeRef::ExprBytesLiteral(node) => node.range(), + AnyNodeRef::ExprNumberLiteral(node) => node.range(), + AnyNodeRef::ExprBooleanLiteral(node) => node.range(), + AnyNodeRef::ExprNoneLiteral(node) => node.range(), + AnyNodeRef::ExprEllipsisLiteral(node) => node.range(), + AnyNodeRef::ExprAttribute(node) => node.range(), + AnyNodeRef::ExprSubscript(node) => node.range(), + AnyNodeRef::ExprStarred(node) => node.range(), + AnyNodeRef::ExprName(node) => node.range(), + AnyNodeRef::ExprList(node) => node.range(), + AnyNodeRef::ExprTuple(node) => node.range(), + AnyNodeRef::ExprSlice(node) => node.range(), + AnyNodeRef::ExprIpyEscapeCommand(node) => node.range(), + AnyNodeRef::ExceptHandlerExceptHandler(node) => node.range(), + AnyNodeRef::FStringExpressionElement(node) => node.range(), + AnyNodeRef::FStringLiteralElement(node) => node.range(), + AnyNodeRef::PatternMatchValue(node) => node.range(), + AnyNodeRef::PatternMatchSingleton(node) => node.range(), + AnyNodeRef::PatternMatchSequence(node) => node.range(), + AnyNodeRef::PatternMatchMapping(node) => node.range(), + AnyNodeRef::PatternMatchClass(node) => node.range(), + AnyNodeRef::PatternMatchStar(node) => node.range(), + AnyNodeRef::PatternMatchAs(node) => node.range(), + AnyNodeRef::PatternMatchOr(node) => node.range(), + AnyNodeRef::TypeParamTypeVar(node) => node.range(), + AnyNodeRef::TypeParamTypeVarTuple(node) => node.range(), + AnyNodeRef::TypeParamParamSpec(node) => node.range(), + AnyNodeRef::FStringFormatSpec(node) => node.range(), + AnyNodeRef::PatternArguments(node) => node.range(), + AnyNodeRef::PatternKeyword(node) => node.range(), + AnyNodeRef::Comprehension(node) => node.range(), + AnyNodeRef::Arguments(node) => node.range(), + AnyNodeRef::Parameters(node) => node.range(), + AnyNodeRef::Parameter(node) => node.range(), + AnyNodeRef::ParameterWithDefault(node) => node.range(), + AnyNodeRef::Keyword(node) => node.range(), + AnyNodeRef::Alias(node) => node.range(), + AnyNodeRef::WithItem(node) => node.range(), + AnyNodeRef::MatchCase(node) => node.range(), + AnyNodeRef::Decorator(node) => node.range(), + AnyNodeRef::ElifElseClause(node) => node.range(), + AnyNodeRef::TypeParams(node) => node.range(), + AnyNodeRef::FString(node) => node.range(), + AnyNodeRef::StringLiteral(node) => node.range(), + AnyNodeRef::BytesLiteral(node) => node.range(), + AnyNodeRef::Identifier(node) => node.range(), + } + } +} + +impl AnyNodeRef<'_> { + pub fn as_ptr(&self) -> std::ptr::NonNull<()> { + match self { + AnyNodeRef::ModModule(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ModExpression(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtFunctionDef(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtClassDef(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtReturn(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtDelete(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtTypeAlias(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtAssign(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtAugAssign(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtAnnAssign(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtFor(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtWhile(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtIf(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtWith(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtMatch(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtRaise(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtTry(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtAssert(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtImport(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtImportFrom(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtGlobal(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtNonlocal(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtExpr(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtPass(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtBreak(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtContinue(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StmtIpyEscapeCommand(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprBoolOp(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprNamed(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprBinOp(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprUnaryOp(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprLambda(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprIf(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprDict(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprSet(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprListComp(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprSetComp(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprDictComp(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprGenerator(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprAwait(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprYield(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprYieldFrom(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprCompare(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprCall(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprFString(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprStringLiteral(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprBytesLiteral(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprNumberLiteral(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprBooleanLiteral(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprNoneLiteral(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprEllipsisLiteral(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprAttribute(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprSubscript(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprStarred(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprName(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprList(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprTuple(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprSlice(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprIpyEscapeCommand(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExceptHandlerExceptHandler(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::FStringExpressionElement(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::FStringLiteralElement(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::PatternMatchValue(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::PatternMatchSingleton(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::PatternMatchSequence(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::PatternMatchMapping(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::PatternMatchClass(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::PatternMatchStar(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::PatternMatchAs(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::PatternMatchOr(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::TypeParamTypeVar(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::TypeParamTypeVarTuple(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::TypeParamParamSpec(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::FStringFormatSpec(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::PatternArguments(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::PatternKeyword(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::Comprehension(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::Arguments(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::Parameters(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::Parameter(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ParameterWithDefault(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::Keyword(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::Alias(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::WithItem(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::MatchCase(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::Decorator(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ElifElseClause(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::TypeParams(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::FString(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::StringLiteral(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::BytesLiteral(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::Identifier(node) => std::ptr::NonNull::from(*node).cast(), + } + } +} + +impl<'a> AnyNodeRef<'a> { + pub fn visit_preorder<'b, V>(self, visitor: &mut V) + where + V: crate::visitor::source_order::SourceOrderVisitor<'b> + ?Sized, + 'a: 'b, + { + match self { + AnyNodeRef::ModModule(node) => node.visit_source_order(visitor), + AnyNodeRef::ModExpression(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtFunctionDef(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtClassDef(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtReturn(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtDelete(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtTypeAlias(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtAssign(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtAugAssign(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtAnnAssign(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtFor(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtWhile(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtIf(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtWith(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtMatch(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtRaise(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtTry(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtAssert(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtImport(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtImportFrom(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtGlobal(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtNonlocal(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtExpr(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtPass(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtBreak(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtContinue(node) => node.visit_source_order(visitor), + AnyNodeRef::StmtIpyEscapeCommand(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprBoolOp(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprNamed(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprBinOp(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprUnaryOp(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprLambda(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprIf(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprDict(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprSet(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprListComp(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprSetComp(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprDictComp(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprGenerator(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprAwait(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprYield(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprYieldFrom(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprCompare(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprCall(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprFString(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprStringLiteral(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprBytesLiteral(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprNumberLiteral(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprBooleanLiteral(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprNoneLiteral(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprEllipsisLiteral(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprAttribute(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprSubscript(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprStarred(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprName(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprList(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprTuple(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprSlice(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprIpyEscapeCommand(node) => node.visit_source_order(visitor), + AnyNodeRef::ExceptHandlerExceptHandler(node) => node.visit_source_order(visitor), + AnyNodeRef::FStringExpressionElement(node) => node.visit_source_order(visitor), + AnyNodeRef::FStringLiteralElement(node) => node.visit_source_order(visitor), + AnyNodeRef::PatternMatchValue(node) => node.visit_source_order(visitor), + AnyNodeRef::PatternMatchSingleton(node) => node.visit_source_order(visitor), + AnyNodeRef::PatternMatchSequence(node) => node.visit_source_order(visitor), + AnyNodeRef::PatternMatchMapping(node) => node.visit_source_order(visitor), + AnyNodeRef::PatternMatchClass(node) => node.visit_source_order(visitor), + AnyNodeRef::PatternMatchStar(node) => node.visit_source_order(visitor), + AnyNodeRef::PatternMatchAs(node) => node.visit_source_order(visitor), + AnyNodeRef::PatternMatchOr(node) => node.visit_source_order(visitor), + AnyNodeRef::TypeParamTypeVar(node) => node.visit_source_order(visitor), + AnyNodeRef::TypeParamTypeVarTuple(node) => node.visit_source_order(visitor), + AnyNodeRef::TypeParamParamSpec(node) => node.visit_source_order(visitor), + AnyNodeRef::FStringFormatSpec(node) => node.visit_source_order(visitor), + AnyNodeRef::PatternArguments(node) => node.visit_source_order(visitor), + AnyNodeRef::PatternKeyword(node) => node.visit_source_order(visitor), + AnyNodeRef::Comprehension(node) => node.visit_source_order(visitor), + AnyNodeRef::Arguments(node) => node.visit_source_order(visitor), + AnyNodeRef::Parameters(node) => node.visit_source_order(visitor), + AnyNodeRef::Parameter(node) => node.visit_source_order(visitor), + AnyNodeRef::ParameterWithDefault(node) => node.visit_source_order(visitor), + AnyNodeRef::Keyword(node) => node.visit_source_order(visitor), + AnyNodeRef::Alias(node) => node.visit_source_order(visitor), + AnyNodeRef::WithItem(node) => node.visit_source_order(visitor), + AnyNodeRef::MatchCase(node) => node.visit_source_order(visitor), + AnyNodeRef::Decorator(node) => node.visit_source_order(visitor), + AnyNodeRef::ElifElseClause(node) => node.visit_source_order(visitor), + AnyNodeRef::TypeParams(node) => node.visit_source_order(visitor), + AnyNodeRef::FString(node) => node.visit_source_order(visitor), + AnyNodeRef::StringLiteral(node) => node.visit_source_order(visitor), + AnyNodeRef::BytesLiteral(node) => node.visit_source_order(visitor), + AnyNodeRef::Identifier(node) => node.visit_source_order(visitor), + } + } +} + +impl AnyNodeRef<'_> { + pub const fn is_module(self) -> bool { + matches!( + self, + AnyNodeRef::ModModule(_) | AnyNodeRef::ModExpression(_) + ) + } +} + +impl AnyNodeRef<'_> { + pub const fn is_statement(self) -> bool { + matches!( + self, + AnyNodeRef::StmtFunctionDef(_) + | AnyNodeRef::StmtClassDef(_) + | AnyNodeRef::StmtReturn(_) + | AnyNodeRef::StmtDelete(_) + | AnyNodeRef::StmtTypeAlias(_) + | AnyNodeRef::StmtAssign(_) + | AnyNodeRef::StmtAugAssign(_) + | AnyNodeRef::StmtAnnAssign(_) + | AnyNodeRef::StmtFor(_) + | AnyNodeRef::StmtWhile(_) + | AnyNodeRef::StmtIf(_) + | AnyNodeRef::StmtWith(_) + | AnyNodeRef::StmtMatch(_) + | AnyNodeRef::StmtRaise(_) + | AnyNodeRef::StmtTry(_) + | AnyNodeRef::StmtAssert(_) + | AnyNodeRef::StmtImport(_) + | AnyNodeRef::StmtImportFrom(_) + | AnyNodeRef::StmtGlobal(_) + | AnyNodeRef::StmtNonlocal(_) + | AnyNodeRef::StmtExpr(_) + | AnyNodeRef::StmtPass(_) + | AnyNodeRef::StmtBreak(_) + | AnyNodeRef::StmtContinue(_) + | AnyNodeRef::StmtIpyEscapeCommand(_) + ) + } +} + +impl AnyNodeRef<'_> { + pub const fn is_expression(self) -> bool { + matches!( + self, + AnyNodeRef::ExprBoolOp(_) + | AnyNodeRef::ExprNamed(_) + | AnyNodeRef::ExprBinOp(_) + | AnyNodeRef::ExprUnaryOp(_) + | AnyNodeRef::ExprLambda(_) + | AnyNodeRef::ExprIf(_) + | AnyNodeRef::ExprDict(_) + | AnyNodeRef::ExprSet(_) + | AnyNodeRef::ExprListComp(_) + | AnyNodeRef::ExprSetComp(_) + | AnyNodeRef::ExprDictComp(_) + | AnyNodeRef::ExprGenerator(_) + | AnyNodeRef::ExprAwait(_) + | AnyNodeRef::ExprYield(_) + | AnyNodeRef::ExprYieldFrom(_) + | AnyNodeRef::ExprCompare(_) + | AnyNodeRef::ExprCall(_) + | AnyNodeRef::ExprFString(_) + | AnyNodeRef::ExprStringLiteral(_) + | AnyNodeRef::ExprBytesLiteral(_) + | AnyNodeRef::ExprNumberLiteral(_) + | AnyNodeRef::ExprBooleanLiteral(_) + | AnyNodeRef::ExprNoneLiteral(_) + | AnyNodeRef::ExprEllipsisLiteral(_) + | AnyNodeRef::ExprAttribute(_) + | AnyNodeRef::ExprSubscript(_) + | AnyNodeRef::ExprStarred(_) + | AnyNodeRef::ExprName(_) + | AnyNodeRef::ExprList(_) + | AnyNodeRef::ExprTuple(_) + | AnyNodeRef::ExprSlice(_) + | AnyNodeRef::ExprIpyEscapeCommand(_) + ) + } +} + +impl AnyNodeRef<'_> { + pub const fn is_except_handler(self) -> bool { + matches!(self, AnyNodeRef::ExceptHandlerExceptHandler(_)) + } +} + +impl AnyNodeRef<'_> { + pub const fn is_f_string_element(self) -> bool { + matches!( + self, + AnyNodeRef::FStringExpressionElement(_) | AnyNodeRef::FStringLiteralElement(_) + ) + } +} + +impl AnyNodeRef<'_> { + pub const fn is_pattern(self) -> bool { + matches!( + self, + AnyNodeRef::PatternMatchValue(_) + | AnyNodeRef::PatternMatchSingleton(_) + | AnyNodeRef::PatternMatchSequence(_) + | AnyNodeRef::PatternMatchMapping(_) + | AnyNodeRef::PatternMatchClass(_) + | AnyNodeRef::PatternMatchStar(_) + | AnyNodeRef::PatternMatchAs(_) + | AnyNodeRef::PatternMatchOr(_) + ) + } +} + +impl AnyNodeRef<'_> { + pub const fn is_type_param(self) -> bool { + matches!( + self, + AnyNodeRef::TypeParamTypeVar(_) + | AnyNodeRef::TypeParamTypeVarTuple(_) + | AnyNodeRef::TypeParamParamSpec(_) + ) + } +} + +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] +pub enum NodeKind { + ModModule, + ModExpression, + StmtFunctionDef, + StmtClassDef, + StmtReturn, + StmtDelete, + StmtTypeAlias, + StmtAssign, + StmtAugAssign, + StmtAnnAssign, + StmtFor, + StmtWhile, + StmtIf, + StmtWith, + StmtMatch, + StmtRaise, + StmtTry, + StmtAssert, + StmtImport, + StmtImportFrom, + StmtGlobal, + StmtNonlocal, + StmtExpr, + StmtPass, + StmtBreak, + StmtContinue, + StmtIpyEscapeCommand, + ExprBoolOp, + ExprNamed, + ExprBinOp, + ExprUnaryOp, + ExprLambda, + ExprIf, + ExprDict, + ExprSet, + ExprListComp, + ExprSetComp, + ExprDictComp, + ExprGenerator, + ExprAwait, + ExprYield, + ExprYieldFrom, + ExprCompare, + ExprCall, + ExprFString, + ExprStringLiteral, + ExprBytesLiteral, + ExprNumberLiteral, + ExprBooleanLiteral, + ExprNoneLiteral, + ExprEllipsisLiteral, + ExprAttribute, + ExprSubscript, + ExprStarred, + ExprName, + ExprList, + ExprTuple, + ExprSlice, + ExprIpyEscapeCommand, + ExceptHandlerExceptHandler, + FStringExpressionElement, + FStringLiteralElement, + PatternMatchValue, + PatternMatchSingleton, + PatternMatchSequence, + PatternMatchMapping, + PatternMatchClass, + PatternMatchStar, + PatternMatchAs, + PatternMatchOr, + TypeParamTypeVar, + TypeParamTypeVarTuple, + TypeParamParamSpec, + FStringFormatSpec, + PatternArguments, + PatternKeyword, + Comprehension, + Arguments, + Parameters, + Parameter, + ParameterWithDefault, + Keyword, + Alias, + WithItem, + MatchCase, + Decorator, + ElifElseClause, + TypeParams, + FString, + StringLiteral, + BytesLiteral, + Identifier, +} + +impl AnyNode { + pub const fn kind(&self) -> NodeKind { + match self { + AnyNode::ModModule(_) => NodeKind::ModModule, + AnyNode::ModExpression(_) => NodeKind::ModExpression, + AnyNode::StmtFunctionDef(_) => NodeKind::StmtFunctionDef, + AnyNode::StmtClassDef(_) => NodeKind::StmtClassDef, + AnyNode::StmtReturn(_) => NodeKind::StmtReturn, + AnyNode::StmtDelete(_) => NodeKind::StmtDelete, + AnyNode::StmtTypeAlias(_) => NodeKind::StmtTypeAlias, + AnyNode::StmtAssign(_) => NodeKind::StmtAssign, + AnyNode::StmtAugAssign(_) => NodeKind::StmtAugAssign, + AnyNode::StmtAnnAssign(_) => NodeKind::StmtAnnAssign, + AnyNode::StmtFor(_) => NodeKind::StmtFor, + AnyNode::StmtWhile(_) => NodeKind::StmtWhile, + AnyNode::StmtIf(_) => NodeKind::StmtIf, + AnyNode::StmtWith(_) => NodeKind::StmtWith, + AnyNode::StmtMatch(_) => NodeKind::StmtMatch, + AnyNode::StmtRaise(_) => NodeKind::StmtRaise, + AnyNode::StmtTry(_) => NodeKind::StmtTry, + AnyNode::StmtAssert(_) => NodeKind::StmtAssert, + AnyNode::StmtImport(_) => NodeKind::StmtImport, + AnyNode::StmtImportFrom(_) => NodeKind::StmtImportFrom, + AnyNode::StmtGlobal(_) => NodeKind::StmtGlobal, + AnyNode::StmtNonlocal(_) => NodeKind::StmtNonlocal, + AnyNode::StmtExpr(_) => NodeKind::StmtExpr, + AnyNode::StmtPass(_) => NodeKind::StmtPass, + AnyNode::StmtBreak(_) => NodeKind::StmtBreak, + AnyNode::StmtContinue(_) => NodeKind::StmtContinue, + AnyNode::StmtIpyEscapeCommand(_) => NodeKind::StmtIpyEscapeCommand, + AnyNode::ExprBoolOp(_) => NodeKind::ExprBoolOp, + AnyNode::ExprNamed(_) => NodeKind::ExprNamed, + AnyNode::ExprBinOp(_) => NodeKind::ExprBinOp, + AnyNode::ExprUnaryOp(_) => NodeKind::ExprUnaryOp, + AnyNode::ExprLambda(_) => NodeKind::ExprLambda, + AnyNode::ExprIf(_) => NodeKind::ExprIf, + AnyNode::ExprDict(_) => NodeKind::ExprDict, + AnyNode::ExprSet(_) => NodeKind::ExprSet, + AnyNode::ExprListComp(_) => NodeKind::ExprListComp, + AnyNode::ExprSetComp(_) => NodeKind::ExprSetComp, + AnyNode::ExprDictComp(_) => NodeKind::ExprDictComp, + AnyNode::ExprGenerator(_) => NodeKind::ExprGenerator, + AnyNode::ExprAwait(_) => NodeKind::ExprAwait, + AnyNode::ExprYield(_) => NodeKind::ExprYield, + AnyNode::ExprYieldFrom(_) => NodeKind::ExprYieldFrom, + AnyNode::ExprCompare(_) => NodeKind::ExprCompare, + AnyNode::ExprCall(_) => NodeKind::ExprCall, + AnyNode::ExprFString(_) => NodeKind::ExprFString, + AnyNode::ExprStringLiteral(_) => NodeKind::ExprStringLiteral, + AnyNode::ExprBytesLiteral(_) => NodeKind::ExprBytesLiteral, + AnyNode::ExprNumberLiteral(_) => NodeKind::ExprNumberLiteral, + AnyNode::ExprBooleanLiteral(_) => NodeKind::ExprBooleanLiteral, + AnyNode::ExprNoneLiteral(_) => NodeKind::ExprNoneLiteral, + AnyNode::ExprEllipsisLiteral(_) => NodeKind::ExprEllipsisLiteral, + AnyNode::ExprAttribute(_) => NodeKind::ExprAttribute, + AnyNode::ExprSubscript(_) => NodeKind::ExprSubscript, + AnyNode::ExprStarred(_) => NodeKind::ExprStarred, + AnyNode::ExprName(_) => NodeKind::ExprName, + AnyNode::ExprList(_) => NodeKind::ExprList, + AnyNode::ExprTuple(_) => NodeKind::ExprTuple, + AnyNode::ExprSlice(_) => NodeKind::ExprSlice, + AnyNode::ExprIpyEscapeCommand(_) => NodeKind::ExprIpyEscapeCommand, + AnyNode::ExceptHandlerExceptHandler(_) => NodeKind::ExceptHandlerExceptHandler, + AnyNode::FStringExpressionElement(_) => NodeKind::FStringExpressionElement, + AnyNode::FStringLiteralElement(_) => NodeKind::FStringLiteralElement, + AnyNode::PatternMatchValue(_) => NodeKind::PatternMatchValue, + AnyNode::PatternMatchSingleton(_) => NodeKind::PatternMatchSingleton, + AnyNode::PatternMatchSequence(_) => NodeKind::PatternMatchSequence, + AnyNode::PatternMatchMapping(_) => NodeKind::PatternMatchMapping, + AnyNode::PatternMatchClass(_) => NodeKind::PatternMatchClass, + AnyNode::PatternMatchStar(_) => NodeKind::PatternMatchStar, + AnyNode::PatternMatchAs(_) => NodeKind::PatternMatchAs, + AnyNode::PatternMatchOr(_) => NodeKind::PatternMatchOr, + AnyNode::TypeParamTypeVar(_) => NodeKind::TypeParamTypeVar, + AnyNode::TypeParamTypeVarTuple(_) => NodeKind::TypeParamTypeVarTuple, + AnyNode::TypeParamParamSpec(_) => NodeKind::TypeParamParamSpec, + AnyNode::FStringFormatSpec(_) => NodeKind::FStringFormatSpec, + AnyNode::PatternArguments(_) => NodeKind::PatternArguments, + AnyNode::PatternKeyword(_) => NodeKind::PatternKeyword, + AnyNode::Comprehension(_) => NodeKind::Comprehension, + AnyNode::Arguments(_) => NodeKind::Arguments, + AnyNode::Parameters(_) => NodeKind::Parameters, + AnyNode::Parameter(_) => NodeKind::Parameter, + AnyNode::ParameterWithDefault(_) => NodeKind::ParameterWithDefault, + AnyNode::Keyword(_) => NodeKind::Keyword, + AnyNode::Alias(_) => NodeKind::Alias, + AnyNode::WithItem(_) => NodeKind::WithItem, + AnyNode::MatchCase(_) => NodeKind::MatchCase, + AnyNode::Decorator(_) => NodeKind::Decorator, + AnyNode::ElifElseClause(_) => NodeKind::ElifElseClause, + AnyNode::TypeParams(_) => NodeKind::TypeParams, + AnyNode::FString(_) => NodeKind::FString, + AnyNode::StringLiteral(_) => NodeKind::StringLiteral, + AnyNode::BytesLiteral(_) => NodeKind::BytesLiteral, + AnyNode::Identifier(_) => NodeKind::Identifier, + } + } +} + +impl AnyNodeRef<'_> { + pub const fn kind(self) -> NodeKind { + match self { + AnyNodeRef::ModModule(_) => NodeKind::ModModule, + AnyNodeRef::ModExpression(_) => NodeKind::ModExpression, + AnyNodeRef::StmtFunctionDef(_) => NodeKind::StmtFunctionDef, + AnyNodeRef::StmtClassDef(_) => NodeKind::StmtClassDef, + AnyNodeRef::StmtReturn(_) => NodeKind::StmtReturn, + AnyNodeRef::StmtDelete(_) => NodeKind::StmtDelete, + AnyNodeRef::StmtTypeAlias(_) => NodeKind::StmtTypeAlias, + AnyNodeRef::StmtAssign(_) => NodeKind::StmtAssign, + AnyNodeRef::StmtAugAssign(_) => NodeKind::StmtAugAssign, + AnyNodeRef::StmtAnnAssign(_) => NodeKind::StmtAnnAssign, + AnyNodeRef::StmtFor(_) => NodeKind::StmtFor, + AnyNodeRef::StmtWhile(_) => NodeKind::StmtWhile, + AnyNodeRef::StmtIf(_) => NodeKind::StmtIf, + AnyNodeRef::StmtWith(_) => NodeKind::StmtWith, + AnyNodeRef::StmtMatch(_) => NodeKind::StmtMatch, + AnyNodeRef::StmtRaise(_) => NodeKind::StmtRaise, + AnyNodeRef::StmtTry(_) => NodeKind::StmtTry, + AnyNodeRef::StmtAssert(_) => NodeKind::StmtAssert, + AnyNodeRef::StmtImport(_) => NodeKind::StmtImport, + AnyNodeRef::StmtImportFrom(_) => NodeKind::StmtImportFrom, + AnyNodeRef::StmtGlobal(_) => NodeKind::StmtGlobal, + AnyNodeRef::StmtNonlocal(_) => NodeKind::StmtNonlocal, + AnyNodeRef::StmtExpr(_) => NodeKind::StmtExpr, + AnyNodeRef::StmtPass(_) => NodeKind::StmtPass, + AnyNodeRef::StmtBreak(_) => NodeKind::StmtBreak, + AnyNodeRef::StmtContinue(_) => NodeKind::StmtContinue, + AnyNodeRef::StmtIpyEscapeCommand(_) => NodeKind::StmtIpyEscapeCommand, + AnyNodeRef::ExprBoolOp(_) => NodeKind::ExprBoolOp, + AnyNodeRef::ExprNamed(_) => NodeKind::ExprNamed, + AnyNodeRef::ExprBinOp(_) => NodeKind::ExprBinOp, + AnyNodeRef::ExprUnaryOp(_) => NodeKind::ExprUnaryOp, + AnyNodeRef::ExprLambda(_) => NodeKind::ExprLambda, + AnyNodeRef::ExprIf(_) => NodeKind::ExprIf, + AnyNodeRef::ExprDict(_) => NodeKind::ExprDict, + AnyNodeRef::ExprSet(_) => NodeKind::ExprSet, + AnyNodeRef::ExprListComp(_) => NodeKind::ExprListComp, + AnyNodeRef::ExprSetComp(_) => NodeKind::ExprSetComp, + AnyNodeRef::ExprDictComp(_) => NodeKind::ExprDictComp, + AnyNodeRef::ExprGenerator(_) => NodeKind::ExprGenerator, + AnyNodeRef::ExprAwait(_) => NodeKind::ExprAwait, + AnyNodeRef::ExprYield(_) => NodeKind::ExprYield, + AnyNodeRef::ExprYieldFrom(_) => NodeKind::ExprYieldFrom, + AnyNodeRef::ExprCompare(_) => NodeKind::ExprCompare, + AnyNodeRef::ExprCall(_) => NodeKind::ExprCall, + AnyNodeRef::ExprFString(_) => NodeKind::ExprFString, + AnyNodeRef::ExprStringLiteral(_) => NodeKind::ExprStringLiteral, + AnyNodeRef::ExprBytesLiteral(_) => NodeKind::ExprBytesLiteral, + AnyNodeRef::ExprNumberLiteral(_) => NodeKind::ExprNumberLiteral, + AnyNodeRef::ExprBooleanLiteral(_) => NodeKind::ExprBooleanLiteral, + AnyNodeRef::ExprNoneLiteral(_) => NodeKind::ExprNoneLiteral, + AnyNodeRef::ExprEllipsisLiteral(_) => NodeKind::ExprEllipsisLiteral, + AnyNodeRef::ExprAttribute(_) => NodeKind::ExprAttribute, + AnyNodeRef::ExprSubscript(_) => NodeKind::ExprSubscript, + AnyNodeRef::ExprStarred(_) => NodeKind::ExprStarred, + AnyNodeRef::ExprName(_) => NodeKind::ExprName, + AnyNodeRef::ExprList(_) => NodeKind::ExprList, + AnyNodeRef::ExprTuple(_) => NodeKind::ExprTuple, + AnyNodeRef::ExprSlice(_) => NodeKind::ExprSlice, + AnyNodeRef::ExprIpyEscapeCommand(_) => NodeKind::ExprIpyEscapeCommand, + AnyNodeRef::ExceptHandlerExceptHandler(_) => NodeKind::ExceptHandlerExceptHandler, + AnyNodeRef::FStringExpressionElement(_) => NodeKind::FStringExpressionElement, + AnyNodeRef::FStringLiteralElement(_) => NodeKind::FStringLiteralElement, + AnyNodeRef::PatternMatchValue(_) => NodeKind::PatternMatchValue, + AnyNodeRef::PatternMatchSingleton(_) => NodeKind::PatternMatchSingleton, + AnyNodeRef::PatternMatchSequence(_) => NodeKind::PatternMatchSequence, + AnyNodeRef::PatternMatchMapping(_) => NodeKind::PatternMatchMapping, + AnyNodeRef::PatternMatchClass(_) => NodeKind::PatternMatchClass, + AnyNodeRef::PatternMatchStar(_) => NodeKind::PatternMatchStar, + AnyNodeRef::PatternMatchAs(_) => NodeKind::PatternMatchAs, + AnyNodeRef::PatternMatchOr(_) => NodeKind::PatternMatchOr, + AnyNodeRef::TypeParamTypeVar(_) => NodeKind::TypeParamTypeVar, + AnyNodeRef::TypeParamTypeVarTuple(_) => NodeKind::TypeParamTypeVarTuple, + AnyNodeRef::TypeParamParamSpec(_) => NodeKind::TypeParamParamSpec, + AnyNodeRef::FStringFormatSpec(_) => NodeKind::FStringFormatSpec, + AnyNodeRef::PatternArguments(_) => NodeKind::PatternArguments, + AnyNodeRef::PatternKeyword(_) => NodeKind::PatternKeyword, + AnyNodeRef::Comprehension(_) => NodeKind::Comprehension, + AnyNodeRef::Arguments(_) => NodeKind::Arguments, + AnyNodeRef::Parameters(_) => NodeKind::Parameters, + AnyNodeRef::Parameter(_) => NodeKind::Parameter, + AnyNodeRef::ParameterWithDefault(_) => NodeKind::ParameterWithDefault, + AnyNodeRef::Keyword(_) => NodeKind::Keyword, + AnyNodeRef::Alias(_) => NodeKind::Alias, + AnyNodeRef::WithItem(_) => NodeKind::WithItem, + AnyNodeRef::MatchCase(_) => NodeKind::MatchCase, + AnyNodeRef::Decorator(_) => NodeKind::Decorator, + AnyNodeRef::ElifElseClause(_) => NodeKind::ElifElseClause, + AnyNodeRef::TypeParams(_) => NodeKind::TypeParams, + AnyNodeRef::FString(_) => NodeKind::FString, + AnyNodeRef::StringLiteral(_) => NodeKind::StringLiteral, + AnyNodeRef::BytesLiteral(_) => NodeKind::BytesLiteral, + AnyNodeRef::Identifier(_) => NodeKind::Identifier, + } + } +} + +impl crate::AstNode for Mod { + type Ref<'a> = ModRef<'a>; + + fn cast(node: AnyNode) -> Option + where + Self: Sized, + { + match node { + AnyNode::ModModule(node) => Some(Mod::Module(node)), + AnyNode::ModExpression(node) => Some(Mod::Expression(node)), + + _ => None, + } + } + + fn cast_ref(node: AnyNodeRef) -> Option> { + match node { + AnyNodeRef::ModModule(node) => Some(ModRef::Module(node)), + AnyNodeRef::ModExpression(node) => Some(ModRef::Expression(node)), + + _ => None, + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ModModule | NodeKind::ModExpression) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ModModule { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ModModule(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ModModule(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ModModule) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ModExpression { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ModExpression(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ModExpression(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ModExpression) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for Stmt { + type Ref<'a> = StatementRef<'a>; + + fn cast(node: AnyNode) -> Option + where + Self: Sized, + { + match node { + AnyNode::StmtFunctionDef(node) => Some(Stmt::FunctionDef(node)), + AnyNode::StmtClassDef(node) => Some(Stmt::ClassDef(node)), + AnyNode::StmtReturn(node) => Some(Stmt::Return(node)), + AnyNode::StmtDelete(node) => Some(Stmt::Delete(node)), + AnyNode::StmtTypeAlias(node) => Some(Stmt::TypeAlias(node)), + AnyNode::StmtAssign(node) => Some(Stmt::Assign(node)), + AnyNode::StmtAugAssign(node) => Some(Stmt::AugAssign(node)), + AnyNode::StmtAnnAssign(node) => Some(Stmt::AnnAssign(node)), + AnyNode::StmtFor(node) => Some(Stmt::For(node)), + AnyNode::StmtWhile(node) => Some(Stmt::While(node)), + AnyNode::StmtIf(node) => Some(Stmt::If(node)), + AnyNode::StmtWith(node) => Some(Stmt::With(node)), + AnyNode::StmtMatch(node) => Some(Stmt::Match(node)), + AnyNode::StmtRaise(node) => Some(Stmt::Raise(node)), + AnyNode::StmtTry(node) => Some(Stmt::Try(node)), + AnyNode::StmtAssert(node) => Some(Stmt::Assert(node)), + AnyNode::StmtImport(node) => Some(Stmt::Import(node)), + AnyNode::StmtImportFrom(node) => Some(Stmt::ImportFrom(node)), + AnyNode::StmtGlobal(node) => Some(Stmt::Global(node)), + AnyNode::StmtNonlocal(node) => Some(Stmt::Nonlocal(node)), + AnyNode::StmtExpr(node) => Some(Stmt::Expr(node)), + AnyNode::StmtPass(node) => Some(Stmt::Pass(node)), + AnyNode::StmtBreak(node) => Some(Stmt::Break(node)), + AnyNode::StmtContinue(node) => Some(Stmt::Continue(node)), + AnyNode::StmtIpyEscapeCommand(node) => Some(Stmt::IpyEscapeCommand(node)), + + _ => None, + } + } + + fn cast_ref(node: AnyNodeRef) -> Option> { + match node { + AnyNodeRef::StmtFunctionDef(node) => Some(StatementRef::FunctionDef(node)), + AnyNodeRef::StmtClassDef(node) => Some(StatementRef::ClassDef(node)), + AnyNodeRef::StmtReturn(node) => Some(StatementRef::Return(node)), + AnyNodeRef::StmtDelete(node) => Some(StatementRef::Delete(node)), + AnyNodeRef::StmtTypeAlias(node) => Some(StatementRef::TypeAlias(node)), + AnyNodeRef::StmtAssign(node) => Some(StatementRef::Assign(node)), + AnyNodeRef::StmtAugAssign(node) => Some(StatementRef::AugAssign(node)), + AnyNodeRef::StmtAnnAssign(node) => Some(StatementRef::AnnAssign(node)), + AnyNodeRef::StmtFor(node) => Some(StatementRef::For(node)), + AnyNodeRef::StmtWhile(node) => Some(StatementRef::While(node)), + AnyNodeRef::StmtIf(node) => Some(StatementRef::If(node)), + AnyNodeRef::StmtWith(node) => Some(StatementRef::With(node)), + AnyNodeRef::StmtMatch(node) => Some(StatementRef::Match(node)), + AnyNodeRef::StmtRaise(node) => Some(StatementRef::Raise(node)), + AnyNodeRef::StmtTry(node) => Some(StatementRef::Try(node)), + AnyNodeRef::StmtAssert(node) => Some(StatementRef::Assert(node)), + AnyNodeRef::StmtImport(node) => Some(StatementRef::Import(node)), + AnyNodeRef::StmtImportFrom(node) => Some(StatementRef::ImportFrom(node)), + AnyNodeRef::StmtGlobal(node) => Some(StatementRef::Global(node)), + AnyNodeRef::StmtNonlocal(node) => Some(StatementRef::Nonlocal(node)), + AnyNodeRef::StmtExpr(node) => Some(StatementRef::Expr(node)), + AnyNodeRef::StmtPass(node) => Some(StatementRef::Pass(node)), + AnyNodeRef::StmtBreak(node) => Some(StatementRef::Break(node)), + AnyNodeRef::StmtContinue(node) => Some(StatementRef::Continue(node)), + AnyNodeRef::StmtIpyEscapeCommand(node) => Some(StatementRef::IpyEscapeCommand(node)), + + _ => None, + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!( + kind, + NodeKind::StmtFunctionDef + | NodeKind::StmtClassDef + | NodeKind::StmtReturn + | NodeKind::StmtDelete + | NodeKind::StmtTypeAlias + | NodeKind::StmtAssign + | NodeKind::StmtAugAssign + | NodeKind::StmtAnnAssign + | NodeKind::StmtFor + | NodeKind::StmtWhile + | NodeKind::StmtIf + | NodeKind::StmtWith + | NodeKind::StmtMatch + | NodeKind::StmtRaise + | NodeKind::StmtTry + | NodeKind::StmtAssert + | NodeKind::StmtImport + | NodeKind::StmtImportFrom + | NodeKind::StmtGlobal + | NodeKind::StmtNonlocal + | NodeKind::StmtExpr + | NodeKind::StmtPass + | NodeKind::StmtBreak + | NodeKind::StmtContinue + | NodeKind::StmtIpyEscapeCommand + ) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtFunctionDef { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtFunctionDef(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtFunctionDef(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtFunctionDef) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtClassDef { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtClassDef(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtClassDef(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtClassDef) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtReturn { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtReturn(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtReturn(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtReturn) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtDelete { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtDelete(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtDelete(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtDelete) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtTypeAlias { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtTypeAlias(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtTypeAlias(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtTypeAlias) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtAssign { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtAssign(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtAssign(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtAssign) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtAugAssign { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtAugAssign(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtAugAssign(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtAugAssign) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtAnnAssign { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtAnnAssign(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtAnnAssign(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtAnnAssign) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtFor { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtFor(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtFor(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtFor) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtWhile { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtWhile(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtWhile(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtWhile) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtIf { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtIf(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtIf(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtIf) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtWith { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtWith(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtWith(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtWith) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtMatch { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtMatch(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtMatch(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtMatch) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtRaise { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtRaise(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtRaise(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtRaise) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtTry { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtTry(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtTry(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtTry) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtAssert { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtAssert(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtAssert(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtAssert) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtImport { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtImport(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtImport(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtImport) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtImportFrom { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtImportFrom(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtImportFrom(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtImportFrom) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtGlobal { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtGlobal(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtGlobal(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtGlobal) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtNonlocal { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtNonlocal(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtNonlocal(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtNonlocal) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtExpr { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtExpr(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtExpr(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtExpr) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtPass { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtPass(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtPass(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtPass) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtBreak { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtBreak(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtBreak(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtBreak) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtContinue { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtContinue(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtContinue(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtContinue) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StmtIpyEscapeCommand { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StmtIpyEscapeCommand(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StmtIpyEscapeCommand(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StmtIpyEscapeCommand) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for Expr { + type Ref<'a> = ExpressionRef<'a>; + + fn cast(node: AnyNode) -> Option + where + Self: Sized, + { + match node { + AnyNode::ExprBoolOp(node) => Some(Expr::BoolOp(node)), + AnyNode::ExprNamed(node) => Some(Expr::Named(node)), + AnyNode::ExprBinOp(node) => Some(Expr::BinOp(node)), + AnyNode::ExprUnaryOp(node) => Some(Expr::UnaryOp(node)), + AnyNode::ExprLambda(node) => Some(Expr::Lambda(node)), + AnyNode::ExprIf(node) => Some(Expr::If(node)), + AnyNode::ExprDict(node) => Some(Expr::Dict(node)), + AnyNode::ExprSet(node) => Some(Expr::Set(node)), + AnyNode::ExprListComp(node) => Some(Expr::ListComp(node)), + AnyNode::ExprSetComp(node) => Some(Expr::SetComp(node)), + AnyNode::ExprDictComp(node) => Some(Expr::DictComp(node)), + AnyNode::ExprGenerator(node) => Some(Expr::Generator(node)), + AnyNode::ExprAwait(node) => Some(Expr::Await(node)), + AnyNode::ExprYield(node) => Some(Expr::Yield(node)), + AnyNode::ExprYieldFrom(node) => Some(Expr::YieldFrom(node)), + AnyNode::ExprCompare(node) => Some(Expr::Compare(node)), + AnyNode::ExprCall(node) => Some(Expr::Call(node)), + AnyNode::ExprFString(node) => Some(Expr::FString(node)), + AnyNode::ExprStringLiteral(node) => Some(Expr::StringLiteral(node)), + AnyNode::ExprBytesLiteral(node) => Some(Expr::BytesLiteral(node)), + AnyNode::ExprNumberLiteral(node) => Some(Expr::NumberLiteral(node)), + AnyNode::ExprBooleanLiteral(node) => Some(Expr::BooleanLiteral(node)), + AnyNode::ExprNoneLiteral(node) => Some(Expr::NoneLiteral(node)), + AnyNode::ExprEllipsisLiteral(node) => Some(Expr::EllipsisLiteral(node)), + AnyNode::ExprAttribute(node) => Some(Expr::Attribute(node)), + AnyNode::ExprSubscript(node) => Some(Expr::Subscript(node)), + AnyNode::ExprStarred(node) => Some(Expr::Starred(node)), + AnyNode::ExprName(node) => Some(Expr::Name(node)), + AnyNode::ExprList(node) => Some(Expr::List(node)), + AnyNode::ExprTuple(node) => Some(Expr::Tuple(node)), + AnyNode::ExprSlice(node) => Some(Expr::Slice(node)), + AnyNode::ExprIpyEscapeCommand(node) => Some(Expr::IpyEscapeCommand(node)), + + _ => None, + } + } + + fn cast_ref(node: AnyNodeRef) -> Option> { + match node { + AnyNodeRef::ExprBoolOp(node) => Some(ExpressionRef::BoolOp(node)), + AnyNodeRef::ExprNamed(node) => Some(ExpressionRef::Named(node)), + AnyNodeRef::ExprBinOp(node) => Some(ExpressionRef::BinOp(node)), + AnyNodeRef::ExprUnaryOp(node) => Some(ExpressionRef::UnaryOp(node)), + AnyNodeRef::ExprLambda(node) => Some(ExpressionRef::Lambda(node)), + AnyNodeRef::ExprIf(node) => Some(ExpressionRef::If(node)), + AnyNodeRef::ExprDict(node) => Some(ExpressionRef::Dict(node)), + AnyNodeRef::ExprSet(node) => Some(ExpressionRef::Set(node)), + AnyNodeRef::ExprListComp(node) => Some(ExpressionRef::ListComp(node)), + AnyNodeRef::ExprSetComp(node) => Some(ExpressionRef::SetComp(node)), + AnyNodeRef::ExprDictComp(node) => Some(ExpressionRef::DictComp(node)), + AnyNodeRef::ExprGenerator(node) => Some(ExpressionRef::Generator(node)), + AnyNodeRef::ExprAwait(node) => Some(ExpressionRef::Await(node)), + AnyNodeRef::ExprYield(node) => Some(ExpressionRef::Yield(node)), + AnyNodeRef::ExprYieldFrom(node) => Some(ExpressionRef::YieldFrom(node)), + AnyNodeRef::ExprCompare(node) => Some(ExpressionRef::Compare(node)), + AnyNodeRef::ExprCall(node) => Some(ExpressionRef::Call(node)), + AnyNodeRef::ExprFString(node) => Some(ExpressionRef::FString(node)), + AnyNodeRef::ExprStringLiteral(node) => Some(ExpressionRef::StringLiteral(node)), + AnyNodeRef::ExprBytesLiteral(node) => Some(ExpressionRef::BytesLiteral(node)), + AnyNodeRef::ExprNumberLiteral(node) => Some(ExpressionRef::NumberLiteral(node)), + AnyNodeRef::ExprBooleanLiteral(node) => Some(ExpressionRef::BooleanLiteral(node)), + AnyNodeRef::ExprNoneLiteral(node) => Some(ExpressionRef::NoneLiteral(node)), + AnyNodeRef::ExprEllipsisLiteral(node) => Some(ExpressionRef::EllipsisLiteral(node)), + AnyNodeRef::ExprAttribute(node) => Some(ExpressionRef::Attribute(node)), + AnyNodeRef::ExprSubscript(node) => Some(ExpressionRef::Subscript(node)), + AnyNodeRef::ExprStarred(node) => Some(ExpressionRef::Starred(node)), + AnyNodeRef::ExprName(node) => Some(ExpressionRef::Name(node)), + AnyNodeRef::ExprList(node) => Some(ExpressionRef::List(node)), + AnyNodeRef::ExprTuple(node) => Some(ExpressionRef::Tuple(node)), + AnyNodeRef::ExprSlice(node) => Some(ExpressionRef::Slice(node)), + AnyNodeRef::ExprIpyEscapeCommand(node) => Some(ExpressionRef::IpyEscapeCommand(node)), + + _ => None, + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!( + kind, + NodeKind::ExprBoolOp + | NodeKind::ExprNamed + | NodeKind::ExprBinOp + | NodeKind::ExprUnaryOp + | NodeKind::ExprLambda + | NodeKind::ExprIf + | NodeKind::ExprDict + | NodeKind::ExprSet + | NodeKind::ExprListComp + | NodeKind::ExprSetComp + | NodeKind::ExprDictComp + | NodeKind::ExprGenerator + | NodeKind::ExprAwait + | NodeKind::ExprYield + | NodeKind::ExprYieldFrom + | NodeKind::ExprCompare + | NodeKind::ExprCall + | NodeKind::ExprFString + | NodeKind::ExprStringLiteral + | NodeKind::ExprBytesLiteral + | NodeKind::ExprNumberLiteral + | NodeKind::ExprBooleanLiteral + | NodeKind::ExprNoneLiteral + | NodeKind::ExprEllipsisLiteral + | NodeKind::ExprAttribute + | NodeKind::ExprSubscript + | NodeKind::ExprStarred + | NodeKind::ExprName + | NodeKind::ExprList + | NodeKind::ExprTuple + | NodeKind::ExprSlice + | NodeKind::ExprIpyEscapeCommand + ) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprBoolOp { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprBoolOp(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprBoolOp(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprBoolOp) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprNamed { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprNamed(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprNamed(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprNamed) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprBinOp { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprBinOp(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprBinOp(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprBinOp) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprUnaryOp { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprUnaryOp(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprUnaryOp(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprUnaryOp) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprLambda { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprLambda(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprLambda(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprLambda) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprIf { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprIf(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprIf(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprIf) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprDict { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprDict(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprDict(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprDict) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprSet { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprSet(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprSet(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprSet) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprListComp { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprListComp(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprListComp(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprListComp) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprSetComp { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprSetComp(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprSetComp(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprSetComp) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprDictComp { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprDictComp(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprDictComp(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprDictComp) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprGenerator { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprGenerator(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprGenerator(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprGenerator) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprAwait { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprAwait(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprAwait(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprAwait) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprYield { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprYield(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprYield(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprYield) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprYieldFrom { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprYieldFrom(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprYieldFrom(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprYieldFrom) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprCompare { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprCompare(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprCompare(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprCompare) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprCall { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprCall(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprCall(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprCall) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprFString { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprFString(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprFString(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprFString) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprStringLiteral { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprStringLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprStringLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprStringLiteral) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprBytesLiteral { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprBytesLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprBytesLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprBytesLiteral) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprNumberLiteral { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprNumberLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprNumberLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprNumberLiteral) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprBooleanLiteral { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprBooleanLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprBooleanLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprBooleanLiteral) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprNoneLiteral { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprNoneLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprNoneLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprNoneLiteral) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprEllipsisLiteral { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprEllipsisLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprEllipsisLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprEllipsisLiteral) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprAttribute { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprAttribute(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprAttribute(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprAttribute) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprSubscript { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprSubscript(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprSubscript(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprSubscript) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprStarred { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprStarred(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprStarred(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprStarred) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprName { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprName(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprName(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprName) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprList { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprList(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprList(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprList) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprTuple { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprTuple(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprTuple(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprTuple) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprSlice { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprSlice(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprSlice(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprSlice) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExprIpyEscapeCommand { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExprIpyEscapeCommand(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExprIpyEscapeCommand(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExprIpyEscapeCommand) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for ExceptHandler { + type Ref<'a> = ExceptHandlerRef<'a>; + + fn cast(node: AnyNode) -> Option + where + Self: Sized, + { + match node { + AnyNode::ExceptHandlerExceptHandler(node) => Some(ExceptHandler::ExceptHandler(node)), + + _ => None, + } + } + + fn cast_ref(node: AnyNodeRef) -> Option> { + match node { + AnyNodeRef::ExceptHandlerExceptHandler(node) => { + Some(ExceptHandlerRef::ExceptHandler(node)) + } + + _ => None, + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExceptHandlerExceptHandler) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ExceptHandlerExceptHandler { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ExceptHandlerExceptHandler(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ExceptHandlerExceptHandler(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ExceptHandlerExceptHandler) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for FStringElement { + type Ref<'a> = FStringElementRef<'a>; + + fn cast(node: AnyNode) -> Option + where + Self: Sized, + { + match node { + AnyNode::FStringExpressionElement(node) => Some(FStringElement::Expression(node)), + AnyNode::FStringLiteralElement(node) => Some(FStringElement::Literal(node)), + + _ => None, + } + } + + fn cast_ref(node: AnyNodeRef) -> Option> { + match node { + AnyNodeRef::FStringExpressionElement(node) => Some(FStringElementRef::Expression(node)), + AnyNodeRef::FStringLiteralElement(node) => Some(FStringElementRef::Literal(node)), + + _ => None, + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!( + kind, + NodeKind::FStringExpressionElement | NodeKind::FStringLiteralElement + ) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::FStringExpressionElement { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::FStringExpressionElement(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::FStringExpressionElement(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::FStringExpressionElement) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::FStringLiteralElement { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::FStringLiteralElement(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::FStringLiteralElement(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::FStringLiteralElement) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for Pattern { + type Ref<'a> = PatternRef<'a>; + + fn cast(node: AnyNode) -> Option + where + Self: Sized, + { + match node { + AnyNode::PatternMatchValue(node) => Some(Pattern::MatchValue(node)), + AnyNode::PatternMatchSingleton(node) => Some(Pattern::MatchSingleton(node)), + AnyNode::PatternMatchSequence(node) => Some(Pattern::MatchSequence(node)), + AnyNode::PatternMatchMapping(node) => Some(Pattern::MatchMapping(node)), + AnyNode::PatternMatchClass(node) => Some(Pattern::MatchClass(node)), + AnyNode::PatternMatchStar(node) => Some(Pattern::MatchStar(node)), + AnyNode::PatternMatchAs(node) => Some(Pattern::MatchAs(node)), + AnyNode::PatternMatchOr(node) => Some(Pattern::MatchOr(node)), + + _ => None, + } + } + + fn cast_ref(node: AnyNodeRef) -> Option> { + match node { + AnyNodeRef::PatternMatchValue(node) => Some(PatternRef::MatchValue(node)), + AnyNodeRef::PatternMatchSingleton(node) => Some(PatternRef::MatchSingleton(node)), + AnyNodeRef::PatternMatchSequence(node) => Some(PatternRef::MatchSequence(node)), + AnyNodeRef::PatternMatchMapping(node) => Some(PatternRef::MatchMapping(node)), + AnyNodeRef::PatternMatchClass(node) => Some(PatternRef::MatchClass(node)), + AnyNodeRef::PatternMatchStar(node) => Some(PatternRef::MatchStar(node)), + AnyNodeRef::PatternMatchAs(node) => Some(PatternRef::MatchAs(node)), + AnyNodeRef::PatternMatchOr(node) => Some(PatternRef::MatchOr(node)), + + _ => None, + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!( + kind, + NodeKind::PatternMatchValue + | NodeKind::PatternMatchSingleton + | NodeKind::PatternMatchSequence + | NodeKind::PatternMatchMapping + | NodeKind::PatternMatchClass + | NodeKind::PatternMatchStar + | NodeKind::PatternMatchAs + | NodeKind::PatternMatchOr + ) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::PatternMatchValue { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::PatternMatchValue(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::PatternMatchValue(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::PatternMatchValue) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::PatternMatchSingleton { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::PatternMatchSingleton(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::PatternMatchSingleton(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::PatternMatchSingleton) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::PatternMatchSequence { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::PatternMatchSequence(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::PatternMatchSequence(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::PatternMatchSequence) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::PatternMatchMapping { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::PatternMatchMapping(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::PatternMatchMapping(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::PatternMatchMapping) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::PatternMatchClass { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::PatternMatchClass(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::PatternMatchClass(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::PatternMatchClass) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::PatternMatchStar { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::PatternMatchStar(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::PatternMatchStar(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::PatternMatchStar) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::PatternMatchAs { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::PatternMatchAs(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::PatternMatchAs(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::PatternMatchAs) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::PatternMatchOr { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::PatternMatchOr(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::PatternMatchOr(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::PatternMatchOr) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for TypeParam { + type Ref<'a> = TypeParamRef<'a>; + + fn cast(node: AnyNode) -> Option + where + Self: Sized, + { + match node { + AnyNode::TypeParamTypeVar(node) => Some(TypeParam::TypeVar(node)), + AnyNode::TypeParamTypeVarTuple(node) => Some(TypeParam::TypeVarTuple(node)), + AnyNode::TypeParamParamSpec(node) => Some(TypeParam::ParamSpec(node)), + + _ => None, + } + } + + fn cast_ref(node: AnyNodeRef) -> Option> { + match node { + AnyNodeRef::TypeParamTypeVar(node) => Some(TypeParamRef::TypeVar(node)), + AnyNodeRef::TypeParamTypeVarTuple(node) => Some(TypeParamRef::TypeVarTuple(node)), + AnyNodeRef::TypeParamParamSpec(node) => Some(TypeParamRef::ParamSpec(node)), + + _ => None, + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!( + kind, + NodeKind::TypeParamTypeVar + | NodeKind::TypeParamTypeVarTuple + | NodeKind::TypeParamParamSpec + ) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::TypeParamTypeVar { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::TypeParamTypeVar(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::TypeParamTypeVar(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::TypeParamTypeVar) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::TypeParamTypeVarTuple { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::TypeParamTypeVarTuple(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::TypeParamTypeVarTuple(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::TypeParamTypeVarTuple) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::TypeParamParamSpec { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::TypeParamParamSpec(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::TypeParamParamSpec(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::TypeParamParamSpec) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::FStringFormatSpec { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::FStringFormatSpec(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::FStringFormatSpec(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::FStringFormatSpec) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::PatternArguments { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::PatternArguments(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::PatternArguments(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::PatternArguments) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::PatternKeyword { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::PatternKeyword(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::PatternKeyword(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::PatternKeyword) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::Comprehension { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::Comprehension(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::Comprehension(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::Comprehension) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::Arguments { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::Arguments(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::Arguments(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::Arguments) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::Parameters { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::Parameters(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::Parameters(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::Parameters) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::Parameter { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::Parameter(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::Parameter(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::Parameter) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ParameterWithDefault { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ParameterWithDefault(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ParameterWithDefault(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ParameterWithDefault) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::Keyword { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::Keyword(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::Keyword(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::Keyword) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::Alias { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::Alias(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::Alias(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::Alias) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::WithItem { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::WithItem(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::WithItem(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::WithItem) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::MatchCase { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::MatchCase(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::MatchCase(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::MatchCase) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::Decorator { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::Decorator(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::Decorator(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::Decorator) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::ElifElseClause { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::ElifElseClause(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::ElifElseClause(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::ElifElseClause) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::TypeParams { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::TypeParams(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::TypeParams(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::TypeParams) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::FString { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::FString(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::FString(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::FString) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::StringLiteral { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::StringLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::StringLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::StringLiteral) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::BytesLiteral { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::BytesLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::BytesLiteral(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::BytesLiteral) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} + +impl crate::AstNode for crate::Identifier { + type Ref<'a> = &'a Self; + + fn cast(kind: AnyNode) -> Option + where + Self: Sized, + { + if let AnyNode::Identifier(node) = kind { + Some(node) + } else { + None + } + } + + fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + if let AnyNodeRef::Identifier(node) = kind { + Some(node) + } else { + None + } + } + + fn can_cast(kind: NodeKind) -> bool { + matches!(kind, NodeKind::Identifier) + } + + fn as_any_node_ref(&self) -> AnyNodeRef { + AnyNodeRef::from(self) + } + + fn into_any_node(self) -> AnyNode { + AnyNode::from(self) + } +} diff --git a/crates/ruff_python_ast/src/lib.rs b/crates/ruff_python_ast/src/lib.rs index 53c3a72db834b..d397d5ef5c5d6 100644 --- a/crates/ruff_python_ast/src/lib.rs +++ b/crates/ruff_python_ast/src/lib.rs @@ -2,13 +2,15 @@ use std::ffi::OsStr; use std::path::Path; pub use expression::*; +pub use generated::*; pub use int::*; -pub use node::{AnyNode, AnyNodeRef, AstNode, NodeKind}; +pub use node::AstNode; pub use nodes::*; pub mod comparable; pub mod docstrings; mod expression; +mod generated; pub mod helpers; pub mod identifier; mod int; diff --git a/crates/ruff_python_ast/src/node.rs b/crates/ruff_python_ast/src/node.rs index 0eb967e0c440a..cc61f4a759f43 100644 --- a/crates/ruff_python_ast/src/node.rs +++ b/crates/ruff_python_ast/src/node.rs @@ -1,16 +1,9 @@ use crate::visitor::source_order::SourceOrderVisitor; use crate::{ - self as ast, Alias, AnyParameterRef, ArgOrKeyword, Arguments, Comprehension, Decorator, - ExceptHandler, Expr, FStringElement, Keyword, MatchCase, Mod, Parameter, ParameterWithDefault, - Parameters, Pattern, PatternArguments, PatternKeyword, Stmt, StmtAnnAssign, StmtAssert, - StmtAssign, StmtAugAssign, StmtBreak, StmtClassDef, StmtContinue, StmtDelete, StmtExpr, - StmtFor, StmtFunctionDef, StmtGlobal, StmtIf, StmtImport, StmtImportFrom, StmtIpyEscapeCommand, - StmtMatch, StmtNonlocal, StmtPass, StmtRaise, StmtReturn, StmtTry, StmtTypeAlias, StmtWhile, - StmtWith, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams, - WithItem, + self as ast, Alias, AnyNode, AnyNodeRef, AnyParameterRef, ArgOrKeyword, MatchCase, NodeKind, + PatternArguments, PatternKeyword, }; -use ruff_text_size::{Ranged, TextRange}; -use std::ptr::NonNull; +use ruff_text_size::Ranged; pub trait AstNode: Ranged { type Ref<'a>; @@ -27,663 +20,10 @@ pub trait AstNode: Ranged { /// Consumes `self` and returns its [`AnyNode`] representation. fn into_any_node(self) -> AnyNode; - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) - where - V: SourceOrderVisitor<'a> + ?Sized; -} - -#[derive(Clone, Debug, is_macro::Is, PartialEq)] -pub enum AnyNode { - ModModule(ast::ModModule), - ModExpression(ast::ModExpression), - StmtFunctionDef(ast::StmtFunctionDef), - StmtClassDef(ast::StmtClassDef), - StmtReturn(ast::StmtReturn), - StmtDelete(ast::StmtDelete), - StmtTypeAlias(ast::StmtTypeAlias), - StmtAssign(ast::StmtAssign), - StmtAugAssign(ast::StmtAugAssign), - StmtAnnAssign(ast::StmtAnnAssign), - StmtFor(ast::StmtFor), - StmtWhile(ast::StmtWhile), - StmtIf(ast::StmtIf), - StmtWith(ast::StmtWith), - StmtMatch(ast::StmtMatch), - StmtRaise(ast::StmtRaise), - StmtTry(ast::StmtTry), - StmtAssert(ast::StmtAssert), - StmtImport(ast::StmtImport), - StmtImportFrom(ast::StmtImportFrom), - StmtGlobal(ast::StmtGlobal), - StmtNonlocal(ast::StmtNonlocal), - StmtExpr(ast::StmtExpr), - StmtPass(ast::StmtPass), - StmtBreak(ast::StmtBreak), - StmtContinue(ast::StmtContinue), - StmtIpyEscapeCommand(ast::StmtIpyEscapeCommand), - ExprBoolOp(ast::ExprBoolOp), - ExprNamed(ast::ExprNamed), - ExprBinOp(ast::ExprBinOp), - ExprUnaryOp(ast::ExprUnaryOp), - ExprLambda(ast::ExprLambda), - ExprIf(ast::ExprIf), - ExprDict(ast::ExprDict), - ExprSet(ast::ExprSet), - ExprListComp(ast::ExprListComp), - ExprSetComp(ast::ExprSetComp), - ExprDictComp(ast::ExprDictComp), - ExprGenerator(ast::ExprGenerator), - ExprAwait(ast::ExprAwait), - ExprYield(ast::ExprYield), - ExprYieldFrom(ast::ExprYieldFrom), - ExprCompare(ast::ExprCompare), - ExprCall(ast::ExprCall), - ExprFString(ast::ExprFString), - ExprStringLiteral(ast::ExprStringLiteral), - ExprBytesLiteral(ast::ExprBytesLiteral), - ExprNumberLiteral(ast::ExprNumberLiteral), - ExprBooleanLiteral(ast::ExprBooleanLiteral), - ExprNoneLiteral(ast::ExprNoneLiteral), - ExprEllipsisLiteral(ast::ExprEllipsisLiteral), - ExprAttribute(ast::ExprAttribute), - ExprSubscript(ast::ExprSubscript), - ExprStarred(ast::ExprStarred), - ExprName(ast::ExprName), - ExprList(ast::ExprList), - ExprTuple(ast::ExprTuple), - ExprSlice(ast::ExprSlice), - ExprIpyEscapeCommand(ast::ExprIpyEscapeCommand), - ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler), - FStringExpressionElement(ast::FStringExpressionElement), - FStringLiteralElement(ast::FStringLiteralElement), - FStringFormatSpec(ast::FStringFormatSpec), - PatternMatchValue(ast::PatternMatchValue), - PatternMatchSingleton(ast::PatternMatchSingleton), - PatternMatchSequence(ast::PatternMatchSequence), - PatternMatchMapping(ast::PatternMatchMapping), - PatternMatchClass(ast::PatternMatchClass), - PatternMatchStar(ast::PatternMatchStar), - PatternMatchAs(ast::PatternMatchAs), - PatternMatchOr(ast::PatternMatchOr), - PatternArguments(PatternArguments), - PatternKeyword(PatternKeyword), - Comprehension(Comprehension), - Arguments(Arguments), - Parameters(Parameters), - Parameter(Parameter), - ParameterWithDefault(ParameterWithDefault), - Keyword(Keyword), - Alias(Alias), - WithItem(WithItem), - MatchCase(MatchCase), - Decorator(Decorator), - ElifElseClause(ast::ElifElseClause), - TypeParams(TypeParams), - TypeParamTypeVar(TypeParamTypeVar), - TypeParamTypeVarTuple(TypeParamTypeVarTuple), - TypeParamParamSpec(TypeParamParamSpec), - FString(ast::FString), - StringLiteral(ast::StringLiteral), - BytesLiteral(ast::BytesLiteral), - Identifier(ast::Identifier), -} - -impl AnyNode { - pub fn statement(self) -> Option { - Stmt::cast(self) - } - - pub fn expression(self) -> Option { - match self { - AnyNode::ExprBoolOp(node) => Some(Expr::BoolOp(node)), - AnyNode::ExprNamed(node) => Some(Expr::Named(node)), - AnyNode::ExprBinOp(node) => Some(Expr::BinOp(node)), - AnyNode::ExprUnaryOp(node) => Some(Expr::UnaryOp(node)), - AnyNode::ExprLambda(node) => Some(Expr::Lambda(node)), - AnyNode::ExprIf(node) => Some(Expr::If(node)), - AnyNode::ExprDict(node) => Some(Expr::Dict(node)), - AnyNode::ExprSet(node) => Some(Expr::Set(node)), - AnyNode::ExprListComp(node) => Some(Expr::ListComp(node)), - AnyNode::ExprSetComp(node) => Some(Expr::SetComp(node)), - AnyNode::ExprDictComp(node) => Some(Expr::DictComp(node)), - AnyNode::ExprGenerator(node) => Some(Expr::Generator(node)), - AnyNode::ExprAwait(node) => Some(Expr::Await(node)), - AnyNode::ExprYield(node) => Some(Expr::Yield(node)), - AnyNode::ExprYieldFrom(node) => Some(Expr::YieldFrom(node)), - AnyNode::ExprCompare(node) => Some(Expr::Compare(node)), - AnyNode::ExprCall(node) => Some(Expr::Call(node)), - AnyNode::ExprFString(node) => Some(Expr::FString(node)), - AnyNode::ExprStringLiteral(node) => Some(Expr::StringLiteral(node)), - AnyNode::ExprBytesLiteral(node) => Some(Expr::BytesLiteral(node)), - AnyNode::ExprNumberLiteral(node) => Some(Expr::NumberLiteral(node)), - AnyNode::ExprBooleanLiteral(node) => Some(Expr::BooleanLiteral(node)), - AnyNode::ExprNoneLiteral(node) => Some(Expr::NoneLiteral(node)), - AnyNode::ExprEllipsisLiteral(node) => Some(Expr::EllipsisLiteral(node)), - AnyNode::ExprAttribute(node) => Some(Expr::Attribute(node)), - AnyNode::ExprSubscript(node) => Some(Expr::Subscript(node)), - AnyNode::ExprStarred(node) => Some(Expr::Starred(node)), - AnyNode::ExprName(node) => Some(Expr::Name(node)), - AnyNode::ExprList(node) => Some(Expr::List(node)), - AnyNode::ExprTuple(node) => Some(Expr::Tuple(node)), - AnyNode::ExprSlice(node) => Some(Expr::Slice(node)), - AnyNode::ExprIpyEscapeCommand(node) => Some(Expr::IpyEscapeCommand(node)), - - AnyNode::ModModule(_) - | AnyNode::ModExpression(_) - | AnyNode::StmtFunctionDef(_) - | AnyNode::StmtClassDef(_) - | AnyNode::StmtReturn(_) - | AnyNode::StmtDelete(_) - | AnyNode::StmtTypeAlias(_) - | AnyNode::StmtAssign(_) - | AnyNode::StmtAugAssign(_) - | AnyNode::StmtAnnAssign(_) - | AnyNode::StmtFor(_) - | AnyNode::StmtWhile(_) - | AnyNode::StmtIf(_) - | AnyNode::StmtWith(_) - | AnyNode::StmtMatch(_) - | AnyNode::StmtRaise(_) - | AnyNode::StmtTry(_) - | AnyNode::StmtAssert(_) - | AnyNode::StmtImport(_) - | AnyNode::StmtImportFrom(_) - | AnyNode::StmtGlobal(_) - | AnyNode::StmtNonlocal(_) - | AnyNode::StmtExpr(_) - | AnyNode::StmtPass(_) - | AnyNode::StmtBreak(_) - | AnyNode::StmtContinue(_) - | AnyNode::StmtIpyEscapeCommand(_) - | AnyNode::ExceptHandlerExceptHandler(_) - | AnyNode::FStringExpressionElement(_) - | AnyNode::FStringLiteralElement(_) - | AnyNode::FStringFormatSpec(_) - | AnyNode::PatternMatchValue(_) - | AnyNode::PatternMatchSingleton(_) - | AnyNode::PatternMatchSequence(_) - | AnyNode::PatternMatchMapping(_) - | AnyNode::PatternMatchClass(_) - | AnyNode::PatternMatchStar(_) - | AnyNode::PatternMatchAs(_) - | AnyNode::PatternMatchOr(_) - | AnyNode::PatternArguments(_) - | AnyNode::PatternKeyword(_) - | AnyNode::Comprehension(_) - | AnyNode::Arguments(_) - | AnyNode::Parameters(_) - | AnyNode::Parameter(_) - | AnyNode::ParameterWithDefault(_) - | AnyNode::Keyword(_) - | AnyNode::Alias(_) - | AnyNode::WithItem(_) - | AnyNode::MatchCase(_) - | AnyNode::Decorator(_) - | AnyNode::TypeParams(_) - | AnyNode::TypeParamTypeVar(_) - | AnyNode::TypeParamTypeVarTuple(_) - | AnyNode::TypeParamParamSpec(_) - | AnyNode::FString(_) - | AnyNode::StringLiteral(_) - | AnyNode::BytesLiteral(_) - | AnyNode::Identifier(_) - | AnyNode::ElifElseClause(_) => None, - } - } - - pub fn module(self) -> Option { - match self { - AnyNode::ModModule(node) => Some(Mod::Module(node)), - AnyNode::ModExpression(node) => Some(Mod::Expression(node)), - - AnyNode::StmtFunctionDef(_) - | AnyNode::StmtClassDef(_) - | AnyNode::StmtReturn(_) - | AnyNode::StmtDelete(_) - | AnyNode::StmtTypeAlias(_) - | AnyNode::StmtAssign(_) - | AnyNode::StmtAugAssign(_) - | AnyNode::StmtAnnAssign(_) - | AnyNode::StmtFor(_) - | AnyNode::StmtWhile(_) - | AnyNode::StmtIf(_) - | AnyNode::StmtWith(_) - | AnyNode::StmtMatch(_) - | AnyNode::StmtRaise(_) - | AnyNode::StmtTry(_) - | AnyNode::StmtAssert(_) - | AnyNode::StmtImport(_) - | AnyNode::StmtImportFrom(_) - | AnyNode::StmtGlobal(_) - | AnyNode::StmtNonlocal(_) - | AnyNode::StmtExpr(_) - | AnyNode::StmtPass(_) - | AnyNode::StmtBreak(_) - | AnyNode::StmtContinue(_) - | AnyNode::StmtIpyEscapeCommand(_) - | AnyNode::ExprBoolOp(_) - | AnyNode::ExprNamed(_) - | AnyNode::ExprBinOp(_) - | AnyNode::ExprUnaryOp(_) - | AnyNode::ExprLambda(_) - | AnyNode::ExprIf(_) - | AnyNode::ExprDict(_) - | AnyNode::ExprSet(_) - | AnyNode::ExprListComp(_) - | AnyNode::ExprSetComp(_) - | AnyNode::ExprDictComp(_) - | AnyNode::ExprGenerator(_) - | AnyNode::ExprAwait(_) - | AnyNode::ExprYield(_) - | AnyNode::ExprYieldFrom(_) - | AnyNode::ExprCompare(_) - | AnyNode::ExprCall(_) - | AnyNode::FStringExpressionElement(_) - | AnyNode::FStringLiteralElement(_) - | AnyNode::FStringFormatSpec(_) - | AnyNode::ExprFString(_) - | AnyNode::ExprStringLiteral(_) - | AnyNode::ExprBytesLiteral(_) - | AnyNode::ExprNumberLiteral(_) - | AnyNode::ExprBooleanLiteral(_) - | AnyNode::ExprNoneLiteral(_) - | AnyNode::ExprEllipsisLiteral(_) - | AnyNode::ExprAttribute(_) - | AnyNode::ExprSubscript(_) - | AnyNode::ExprStarred(_) - | AnyNode::ExprName(_) - | AnyNode::ExprList(_) - | AnyNode::ExprTuple(_) - | AnyNode::ExprSlice(_) - | AnyNode::ExprIpyEscapeCommand(_) - | AnyNode::ExceptHandlerExceptHandler(_) - | AnyNode::PatternMatchValue(_) - | AnyNode::PatternMatchSingleton(_) - | AnyNode::PatternMatchSequence(_) - | AnyNode::PatternMatchMapping(_) - | AnyNode::PatternMatchClass(_) - | AnyNode::PatternMatchStar(_) - | AnyNode::PatternMatchAs(_) - | AnyNode::PatternMatchOr(_) - | AnyNode::PatternArguments(_) - | AnyNode::PatternKeyword(_) - | AnyNode::Comprehension(_) - | AnyNode::Arguments(_) - | AnyNode::Parameters(_) - | AnyNode::Parameter(_) - | AnyNode::ParameterWithDefault(_) - | AnyNode::Keyword(_) - | AnyNode::Alias(_) - | AnyNode::WithItem(_) - | AnyNode::MatchCase(_) - | AnyNode::Decorator(_) - | AnyNode::TypeParams(_) - | AnyNode::TypeParamTypeVar(_) - | AnyNode::TypeParamTypeVarTuple(_) - | AnyNode::TypeParamParamSpec(_) - | AnyNode::FString(_) - | AnyNode::StringLiteral(_) - | AnyNode::BytesLiteral(_) - | AnyNode::Identifier(_) - | AnyNode::ElifElseClause(_) => None, - } - } - - pub fn pattern(self) -> Option { - match self { - AnyNode::PatternMatchValue(node) => Some(Pattern::MatchValue(node)), - AnyNode::PatternMatchSingleton(node) => Some(Pattern::MatchSingleton(node)), - AnyNode::PatternMatchSequence(node) => Some(Pattern::MatchSequence(node)), - AnyNode::PatternMatchMapping(node) => Some(Pattern::MatchMapping(node)), - AnyNode::PatternMatchClass(node) => Some(Pattern::MatchClass(node)), - AnyNode::PatternMatchStar(node) => Some(Pattern::MatchStar(node)), - AnyNode::PatternMatchAs(node) => Some(Pattern::MatchAs(node)), - AnyNode::PatternMatchOr(node) => Some(Pattern::MatchOr(node)), - - AnyNode::ModModule(_) - | AnyNode::ModExpression(_) - | AnyNode::StmtFunctionDef(_) - | AnyNode::StmtClassDef(_) - | AnyNode::StmtReturn(_) - | AnyNode::StmtDelete(_) - | AnyNode::StmtTypeAlias(_) - | AnyNode::StmtAssign(_) - | AnyNode::StmtAugAssign(_) - | AnyNode::StmtAnnAssign(_) - | AnyNode::StmtFor(_) - | AnyNode::StmtWhile(_) - | AnyNode::StmtIf(_) - | AnyNode::StmtWith(_) - | AnyNode::StmtMatch(_) - | AnyNode::StmtRaise(_) - | AnyNode::StmtTry(_) - | AnyNode::StmtAssert(_) - | AnyNode::StmtImport(_) - | AnyNode::StmtImportFrom(_) - | AnyNode::StmtGlobal(_) - | AnyNode::StmtNonlocal(_) - | AnyNode::StmtExpr(_) - | AnyNode::StmtPass(_) - | AnyNode::StmtBreak(_) - | AnyNode::StmtContinue(_) - | AnyNode::StmtIpyEscapeCommand(_) - | AnyNode::ExprBoolOp(_) - | AnyNode::ExprNamed(_) - | AnyNode::ExprBinOp(_) - | AnyNode::ExprUnaryOp(_) - | AnyNode::ExprLambda(_) - | AnyNode::ExprIf(_) - | AnyNode::ExprDict(_) - | AnyNode::ExprSet(_) - | AnyNode::ExprListComp(_) - | AnyNode::ExprSetComp(_) - | AnyNode::ExprDictComp(_) - | AnyNode::ExprGenerator(_) - | AnyNode::ExprAwait(_) - | AnyNode::ExprYield(_) - | AnyNode::ExprYieldFrom(_) - | AnyNode::ExprCompare(_) - | AnyNode::ExprCall(_) - | AnyNode::FStringExpressionElement(_) - | AnyNode::FStringLiteralElement(_) - | AnyNode::FStringFormatSpec(_) - | AnyNode::ExprFString(_) - | AnyNode::ExprStringLiteral(_) - | AnyNode::ExprBytesLiteral(_) - | AnyNode::ExprNumberLiteral(_) - | AnyNode::ExprBooleanLiteral(_) - | AnyNode::ExprNoneLiteral(_) - | AnyNode::ExprEllipsisLiteral(_) - | AnyNode::ExprAttribute(_) - | AnyNode::ExprSubscript(_) - | AnyNode::ExprStarred(_) - | AnyNode::ExprName(_) - | AnyNode::ExprList(_) - | AnyNode::ExprTuple(_) - | AnyNode::ExprSlice(_) - | AnyNode::ExprIpyEscapeCommand(_) - | AnyNode::ExceptHandlerExceptHandler(_) - | AnyNode::PatternArguments(_) - | AnyNode::PatternKeyword(_) - | AnyNode::Comprehension(_) - | AnyNode::Arguments(_) - | AnyNode::Parameters(_) - | AnyNode::Parameter(_) - | AnyNode::ParameterWithDefault(_) - | AnyNode::Keyword(_) - | AnyNode::Alias(_) - | AnyNode::WithItem(_) - | AnyNode::MatchCase(_) - | AnyNode::Decorator(_) - | AnyNode::TypeParams(_) - | AnyNode::TypeParamTypeVar(_) - | AnyNode::TypeParamTypeVarTuple(_) - | AnyNode::TypeParamParamSpec(_) - | AnyNode::FString(_) - | AnyNode::StringLiteral(_) - | AnyNode::BytesLiteral(_) - | AnyNode::Identifier(_) - | AnyNode::ElifElseClause(_) => None, - } - } - - pub fn except_handler(self) -> Option { - match self { - AnyNode::ExceptHandlerExceptHandler(node) => Some(ExceptHandler::ExceptHandler(node)), - - AnyNode::ModModule(_) - | AnyNode::ModExpression(_) - | AnyNode::StmtFunctionDef(_) - | AnyNode::StmtClassDef(_) - | AnyNode::StmtReturn(_) - | AnyNode::StmtDelete(_) - | AnyNode::StmtTypeAlias(_) - | AnyNode::StmtAssign(_) - | AnyNode::StmtAugAssign(_) - | AnyNode::StmtAnnAssign(_) - | AnyNode::StmtFor(_) - | AnyNode::StmtWhile(_) - | AnyNode::StmtIf(_) - | AnyNode::StmtWith(_) - | AnyNode::StmtMatch(_) - | AnyNode::StmtRaise(_) - | AnyNode::StmtTry(_) - | AnyNode::StmtAssert(_) - | AnyNode::StmtImport(_) - | AnyNode::StmtImportFrom(_) - | AnyNode::StmtGlobal(_) - | AnyNode::StmtNonlocal(_) - | AnyNode::StmtExpr(_) - | AnyNode::StmtPass(_) - | AnyNode::StmtBreak(_) - | AnyNode::StmtContinue(_) - | AnyNode::StmtIpyEscapeCommand(_) - | AnyNode::ExprBoolOp(_) - | AnyNode::ExprNamed(_) - | AnyNode::ExprBinOp(_) - | AnyNode::ExprUnaryOp(_) - | AnyNode::ExprLambda(_) - | AnyNode::ExprIf(_) - | AnyNode::ExprDict(_) - | AnyNode::ExprSet(_) - | AnyNode::ExprListComp(_) - | AnyNode::ExprSetComp(_) - | AnyNode::ExprDictComp(_) - | AnyNode::ExprGenerator(_) - | AnyNode::ExprAwait(_) - | AnyNode::ExprYield(_) - | AnyNode::ExprYieldFrom(_) - | AnyNode::ExprCompare(_) - | AnyNode::ExprCall(_) - | AnyNode::FStringExpressionElement(_) - | AnyNode::FStringLiteralElement(_) - | AnyNode::FStringFormatSpec(_) - | AnyNode::ExprFString(_) - | AnyNode::ExprStringLiteral(_) - | AnyNode::ExprBytesLiteral(_) - | AnyNode::ExprNumberLiteral(_) - | AnyNode::ExprBooleanLiteral(_) - | AnyNode::ExprNoneLiteral(_) - | AnyNode::ExprEllipsisLiteral(_) - | AnyNode::ExprAttribute(_) - | AnyNode::ExprSubscript(_) - | AnyNode::ExprStarred(_) - | AnyNode::ExprName(_) - | AnyNode::ExprList(_) - | AnyNode::ExprTuple(_) - | AnyNode::ExprSlice(_) - | AnyNode::ExprIpyEscapeCommand(_) - | AnyNode::PatternMatchValue(_) - | AnyNode::PatternMatchSingleton(_) - | AnyNode::PatternMatchSequence(_) - | AnyNode::PatternMatchMapping(_) - | AnyNode::PatternMatchClass(_) - | AnyNode::PatternMatchStar(_) - | AnyNode::PatternMatchAs(_) - | AnyNode::PatternMatchOr(_) - | AnyNode::PatternArguments(_) - | AnyNode::PatternKeyword(_) - | AnyNode::Comprehension(_) - | AnyNode::Arguments(_) - | AnyNode::Parameters(_) - | AnyNode::Parameter(_) - | AnyNode::ParameterWithDefault(_) - | AnyNode::Keyword(_) - | AnyNode::Alias(_) - | AnyNode::WithItem(_) - | AnyNode::MatchCase(_) - | AnyNode::Decorator(_) - | AnyNode::TypeParams(_) - | AnyNode::TypeParamTypeVar(_) - | AnyNode::TypeParamTypeVarTuple(_) - | AnyNode::TypeParamParamSpec(_) - | AnyNode::FString(_) - | AnyNode::StringLiteral(_) - | AnyNode::BytesLiteral(_) - | AnyNode::Identifier(_) - | AnyNode::ElifElseClause(_) => None, - } - } - - pub const fn is_statement(&self) -> bool { - self.as_ref().is_statement() - } - - pub const fn is_expression(&self) -> bool { - self.as_ref().is_expression() - } - - pub const fn is_module(&self) -> bool { - self.as_ref().is_module() - } - - pub const fn is_pattern(&self) -> bool { - self.as_ref().is_pattern() - } - - pub const fn is_except_handler(&self) -> bool { - self.as_ref().is_except_handler() - } - - pub const fn as_ref(&self) -> AnyNodeRef { - match self { - Self::ModModule(node) => AnyNodeRef::ModModule(node), - Self::ModExpression(node) => AnyNodeRef::ModExpression(node), - Self::StmtFunctionDef(node) => AnyNodeRef::StmtFunctionDef(node), - Self::StmtClassDef(node) => AnyNodeRef::StmtClassDef(node), - Self::StmtReturn(node) => AnyNodeRef::StmtReturn(node), - Self::StmtDelete(node) => AnyNodeRef::StmtDelete(node), - Self::StmtTypeAlias(node) => AnyNodeRef::StmtTypeAlias(node), - Self::StmtAssign(node) => AnyNodeRef::StmtAssign(node), - Self::StmtAugAssign(node) => AnyNodeRef::StmtAugAssign(node), - Self::StmtAnnAssign(node) => AnyNodeRef::StmtAnnAssign(node), - Self::StmtFor(node) => AnyNodeRef::StmtFor(node), - Self::StmtWhile(node) => AnyNodeRef::StmtWhile(node), - Self::StmtIf(node) => AnyNodeRef::StmtIf(node), - Self::StmtWith(node) => AnyNodeRef::StmtWith(node), - Self::StmtMatch(node) => AnyNodeRef::StmtMatch(node), - Self::StmtRaise(node) => AnyNodeRef::StmtRaise(node), - Self::StmtTry(node) => AnyNodeRef::StmtTry(node), - Self::StmtAssert(node) => AnyNodeRef::StmtAssert(node), - Self::StmtImport(node) => AnyNodeRef::StmtImport(node), - Self::StmtImportFrom(node) => AnyNodeRef::StmtImportFrom(node), - Self::StmtGlobal(node) => AnyNodeRef::StmtGlobal(node), - Self::StmtNonlocal(node) => AnyNodeRef::StmtNonlocal(node), - Self::StmtExpr(node) => AnyNodeRef::StmtExpr(node), - Self::StmtPass(node) => AnyNodeRef::StmtPass(node), - Self::StmtBreak(node) => AnyNodeRef::StmtBreak(node), - Self::StmtContinue(node) => AnyNodeRef::StmtContinue(node), - Self::StmtIpyEscapeCommand(node) => AnyNodeRef::StmtIpyEscapeCommand(node), - Self::ExprBoolOp(node) => AnyNodeRef::ExprBoolOp(node), - Self::ExprNamed(node) => AnyNodeRef::ExprNamed(node), - Self::ExprBinOp(node) => AnyNodeRef::ExprBinOp(node), - Self::ExprUnaryOp(node) => AnyNodeRef::ExprUnaryOp(node), - Self::ExprLambda(node) => AnyNodeRef::ExprLambda(node), - Self::ExprIf(node) => AnyNodeRef::ExprIf(node), - Self::ExprDict(node) => AnyNodeRef::ExprDict(node), - Self::ExprSet(node) => AnyNodeRef::ExprSet(node), - Self::ExprListComp(node) => AnyNodeRef::ExprListComp(node), - Self::ExprSetComp(node) => AnyNodeRef::ExprSetComp(node), - Self::ExprDictComp(node) => AnyNodeRef::ExprDictComp(node), - Self::ExprGenerator(node) => AnyNodeRef::ExprGenerator(node), - Self::ExprAwait(node) => AnyNodeRef::ExprAwait(node), - Self::ExprYield(node) => AnyNodeRef::ExprYield(node), - Self::ExprYieldFrom(node) => AnyNodeRef::ExprYieldFrom(node), - Self::ExprCompare(node) => AnyNodeRef::ExprCompare(node), - Self::ExprCall(node) => AnyNodeRef::ExprCall(node), - Self::FStringExpressionElement(node) => AnyNodeRef::FStringExpressionElement(node), - Self::FStringLiteralElement(node) => AnyNodeRef::FStringLiteralElement(node), - Self::FStringFormatSpec(node) => AnyNodeRef::FStringFormatSpec(node), - Self::ExprFString(node) => AnyNodeRef::ExprFString(node), - Self::ExprStringLiteral(node) => AnyNodeRef::ExprStringLiteral(node), - Self::ExprBytesLiteral(node) => AnyNodeRef::ExprBytesLiteral(node), - Self::ExprNumberLiteral(node) => AnyNodeRef::ExprNumberLiteral(node), - Self::ExprBooleanLiteral(node) => AnyNodeRef::ExprBooleanLiteral(node), - Self::ExprNoneLiteral(node) => AnyNodeRef::ExprNoneLiteral(node), - Self::ExprEllipsisLiteral(node) => AnyNodeRef::ExprEllipsisLiteral(node), - Self::ExprAttribute(node) => AnyNodeRef::ExprAttribute(node), - Self::ExprSubscript(node) => AnyNodeRef::ExprSubscript(node), - Self::ExprStarred(node) => AnyNodeRef::ExprStarred(node), - Self::ExprName(node) => AnyNodeRef::ExprName(node), - Self::ExprList(node) => AnyNodeRef::ExprList(node), - Self::ExprTuple(node) => AnyNodeRef::ExprTuple(node), - Self::ExprSlice(node) => AnyNodeRef::ExprSlice(node), - Self::ExprIpyEscapeCommand(node) => AnyNodeRef::ExprIpyEscapeCommand(node), - Self::ExceptHandlerExceptHandler(node) => AnyNodeRef::ExceptHandlerExceptHandler(node), - Self::PatternMatchValue(node) => AnyNodeRef::PatternMatchValue(node), - Self::PatternMatchSingleton(node) => AnyNodeRef::PatternMatchSingleton(node), - Self::PatternMatchSequence(node) => AnyNodeRef::PatternMatchSequence(node), - Self::PatternMatchMapping(node) => AnyNodeRef::PatternMatchMapping(node), - Self::PatternMatchClass(node) => AnyNodeRef::PatternMatchClass(node), - Self::PatternMatchStar(node) => AnyNodeRef::PatternMatchStar(node), - Self::PatternMatchAs(node) => AnyNodeRef::PatternMatchAs(node), - Self::PatternMatchOr(node) => AnyNodeRef::PatternMatchOr(node), - Self::PatternArguments(node) => AnyNodeRef::PatternArguments(node), - Self::PatternKeyword(node) => AnyNodeRef::PatternKeyword(node), - Self::Comprehension(node) => AnyNodeRef::Comprehension(node), - Self::Arguments(node) => AnyNodeRef::Arguments(node), - Self::Parameters(node) => AnyNodeRef::Parameters(node), - Self::Parameter(node) => AnyNodeRef::Parameter(node), - Self::ParameterWithDefault(node) => AnyNodeRef::ParameterWithDefault(node), - Self::Keyword(node) => AnyNodeRef::Keyword(node), - Self::Alias(node) => AnyNodeRef::Alias(node), - Self::WithItem(node) => AnyNodeRef::WithItem(node), - Self::MatchCase(node) => AnyNodeRef::MatchCase(node), - Self::Decorator(node) => AnyNodeRef::Decorator(node), - Self::TypeParams(node) => AnyNodeRef::TypeParams(node), - Self::TypeParamTypeVar(node) => AnyNodeRef::TypeParamTypeVar(node), - Self::TypeParamTypeVarTuple(node) => AnyNodeRef::TypeParamTypeVarTuple(node), - Self::TypeParamParamSpec(node) => AnyNodeRef::TypeParamParamSpec(node), - Self::FString(node) => AnyNodeRef::FString(node), - Self::StringLiteral(node) => AnyNodeRef::StringLiteral(node), - Self::BytesLiteral(node) => AnyNodeRef::BytesLiteral(node), - Self::ElifElseClause(node) => AnyNodeRef::ElifElseClause(node), - Self::Identifier(node) => AnyNodeRef::Identifier(node), - } - } - - /// Returns the node's [`kind`](NodeKind) that has no data associated and is [`Copy`]. - pub const fn kind(&self) -> NodeKind { - self.as_ref().kind() - } } -impl AstNode for ast::ModModule { - type Ref<'a> = &'a Self; - - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ModModule(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ModModule(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ModModule) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ModModule { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -692,41 +32,8 @@ impl AstNode for ast::ModModule { } } -impl AstNode for ast::ModExpression { - type Ref<'a> = &'a Self; - - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ModExpression(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ModExpression(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ModExpression) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ModExpression { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -734,41 +41,9 @@ impl AstNode for ast::ModExpression { visitor.visit_expr(body); } } -impl AstNode for ast::StmtFunctionDef { - type Ref<'a> = &'a Self; - - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtFunctionDef(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtFunctionDef(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtFunctionDef) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtFunctionDef { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -798,41 +73,9 @@ impl AstNode for ast::StmtFunctionDef { visitor.visit_body(body); } } -impl AstNode for ast::StmtClassDef { - type Ref<'a> = &'a Self; - - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtClassDef(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtClassDef(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtClassDef) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtClassDef { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -859,40 +102,9 @@ impl AstNode for ast::StmtClassDef { visitor.visit_body(body); } } -impl AstNode for ast::StmtReturn { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtReturn(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtReturn(node) = kind { - Some(node) - } else { - None - } - } - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtReturn) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtReturn { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -902,40 +114,9 @@ impl AstNode for ast::StmtReturn { } } } -impl AstNode for ast::StmtDelete { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtDelete(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtDelete(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtDelete) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtDelete { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -945,40 +126,9 @@ impl AstNode for ast::StmtDelete { } } } -impl AstNode for ast::StmtTypeAlias { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtTypeAlias(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtTypeAlias(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtTypeAlias) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtTypeAlias { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -996,40 +146,9 @@ impl AstNode for ast::StmtTypeAlias { visitor.visit_expr(value); } } -impl AstNode for ast::StmtAssign { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtAssign(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtAssign(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtAssign) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtAssign { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -1046,40 +165,9 @@ impl AstNode for ast::StmtAssign { visitor.visit_expr(value); } } -impl AstNode for ast::StmtAugAssign { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtAugAssign(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtAugAssign(node) = kind { - Some(node) - } else { - None - } - } - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtAugAssign) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtAugAssign { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -1095,40 +183,9 @@ impl AstNode for ast::StmtAugAssign { visitor.visit_expr(value); } } -impl AstNode for ast::StmtAnnAssign { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtAnnAssign(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtAnnAssign(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtAnnAssign) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtAnnAssign { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -1147,40 +204,9 @@ impl AstNode for ast::StmtAnnAssign { } } } -impl AstNode for ast::StmtFor { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtFor(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtFor(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtFor) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtFor { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -1198,40 +224,9 @@ impl AstNode for ast::StmtFor { visitor.visit_body(orelse); } } -impl AstNode for ast::StmtWhile { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtWhile(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtWhile(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtWhile) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtWhile { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -1247,40 +242,9 @@ impl AstNode for ast::StmtWhile { visitor.visit_body(orelse); } } -impl AstNode for ast::StmtIf { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtIf(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtIf(node) = kind { - Some(node) - } else { - None - } - } - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtIf) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtIf { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -1298,40 +262,9 @@ impl AstNode for ast::StmtIf { } } } -impl AstNode for ast::ElifElseClause { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ElifElseClause(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ElifElseClause(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ElifElseClause) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ElifElseClause { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -1346,40 +279,9 @@ impl AstNode for ast::ElifElseClause { visitor.visit_body(body); } } -impl AstNode for ast::StmtWith { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtWith(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtWith(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtWith) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtWith { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -1396,42 +298,11 @@ impl AstNode for ast::StmtWith { visitor.visit_body(body); } } -impl AstNode for ast::StmtMatch { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option + +impl ast::StmtMatch { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - Self: Sized, - { - if let AnyNode::StmtMatch(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtMatch(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtMatch) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) - where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a> + ?Sized, { let ast::StmtMatch { subject, @@ -1445,40 +316,9 @@ impl AstNode for ast::StmtMatch { } } } -impl AstNode for ast::StmtRaise { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtRaise(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtRaise(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtRaise) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtRaise { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -1496,40 +336,9 @@ impl AstNode for ast::StmtRaise { }; } } -impl AstNode for ast::StmtTry { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtTry(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtTry(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtTry) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtTry { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -1550,40 +359,9 @@ impl AstNode for ast::StmtTry { visitor.visit_body(finalbody); } } -impl AstNode for ast::StmtAssert { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtAssert(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtAssert(node) = kind { - Some(node) - } else { - None - } - } - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtAssert) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtAssert { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -1598,40 +376,9 @@ impl AstNode for ast::StmtAssert { } } } -impl AstNode for ast::StmtImport { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtImport(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtImport(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtImport) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtImport { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -1642,40 +389,9 @@ impl AstNode for ast::StmtImport { } } } -impl AstNode for ast::StmtImportFrom { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtImportFrom(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtImportFrom(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtImportFrom) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtImportFrom { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -1691,322 +407,83 @@ impl AstNode for ast::StmtImportFrom { } } } -impl AstNode for ast::StmtGlobal { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtGlobal(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtGlobal(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtGlobal) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } +impl ast::StmtGlobal { #[inline] - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { + let ast::StmtGlobal { range: _, names: _ } = self; } } -impl AstNode for ast::StmtNonlocal { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtNonlocal(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtNonlocal(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtNonlocal) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } +impl ast::StmtNonlocal { #[inline] - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { + let ast::StmtNonlocal { range: _, names: _ } = self; } } -impl AstNode for ast::StmtExpr { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtExpr(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtExpr(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtExpr) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::StmtExpr { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { let ast::StmtExpr { value, range: _ } = self; - visitor.visit_expr(value); } } -impl AstNode for ast::StmtPass { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtPass(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtPass(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtPass) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } +impl ast::StmtPass { #[inline] - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { + let ast::StmtPass { range: _ } = self; } } -impl AstNode for ast::StmtBreak { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtBreak(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtBreak(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtBreak) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } +impl ast::StmtBreak { #[inline] - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { + let ast::StmtBreak { range: _ } = self; } } -impl AstNode for ast::StmtContinue { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtContinue(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtContinue(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtContinue) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } +impl ast::StmtContinue { #[inline] - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { + let ast::StmtContinue { range: _ } = self; } } -impl AstNode for ast::StmtIpyEscapeCommand { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StmtIpyEscapeCommand(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StmtIpyEscapeCommand(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StmtIpyEscapeCommand) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } +impl ast::StmtIpyEscapeCommand { #[inline] - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { + let ast::StmtIpyEscapeCommand { + range: _, + kind: _, + value: _, + } = self; } } -impl AstNode for ast::ExprBoolOp { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprBoolOp(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprBoolOp(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprBoolOp) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprBoolOp { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2029,40 +506,9 @@ impl AstNode for ast::ExprBoolOp { } } } -impl AstNode for ast::ExprNamed { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprNamed(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprNamed(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprNamed) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprNamed { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2075,40 +521,9 @@ impl AstNode for ast::ExprNamed { visitor.visit_expr(value); } } -impl AstNode for ast::ExprBinOp { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprBinOp(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprBinOp(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprBinOp) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprBinOp { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2123,40 +538,9 @@ impl AstNode for ast::ExprBinOp { visitor.visit_expr(right); } } -impl AstNode for ast::ExprUnaryOp { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprUnaryOp(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprUnaryOp(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprUnaryOp) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprUnaryOp { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2170,41 +554,9 @@ impl AstNode for ast::ExprUnaryOp { visitor.visit_expr(operand); } } -impl AstNode for ast::ExprLambda { - type Ref<'a> = &'a Self; - - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprLambda(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprLambda(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprLambda) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprLambda { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2220,40 +572,9 @@ impl AstNode for ast::ExprLambda { visitor.visit_expr(body); } } -impl AstNode for ast::ExprIf { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprIf(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprIf(node) = kind { - Some(node) - } else { - None - } - } - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprIf) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprIf { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2270,40 +591,9 @@ impl AstNode for ast::ExprIf { visitor.visit_expr(orelse); } } -impl AstNode for ast::ExprDict { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprDict(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprDict(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprDict) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprDict { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2317,40 +607,9 @@ impl AstNode for ast::ExprDict { } } } -impl AstNode for ast::ExprSet { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprSet(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprSet(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprSet) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprSet { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2361,40 +620,9 @@ impl AstNode for ast::ExprSet { } } } -impl AstNode for ast::ExprListComp { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprListComp(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprListComp(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprListComp) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprListComp { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2410,40 +638,9 @@ impl AstNode for ast::ExprListComp { } } } -impl AstNode for ast::ExprSetComp { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprSetComp(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprSetComp(node) = kind { - Some(node) - } else { - None - } - } - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprSetComp) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprSetComp { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2459,40 +656,9 @@ impl AstNode for ast::ExprSetComp { } } } -impl AstNode for ast::ExprDictComp { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprDictComp(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprDictComp(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprDictComp) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprDictComp { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2511,40 +677,9 @@ impl AstNode for ast::ExprDictComp { } } } -impl AstNode for ast::ExprGenerator { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprGenerator(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprGenerator(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprGenerator) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprGenerator { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2560,40 +695,9 @@ impl AstNode for ast::ExprGenerator { } } } -impl AstNode for ast::ExprAwait { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprAwait(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprAwait(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprAwait) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprAwait { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2601,40 +705,9 @@ impl AstNode for ast::ExprAwait { visitor.visit_expr(value); } } -impl AstNode for ast::ExprYield { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprYield(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprYield(node) = kind { - Some(node) - } else { - None - } - } - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprYield) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprYield { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2644,40 +717,9 @@ impl AstNode for ast::ExprYield { } } } -impl AstNode for ast::ExprYieldFrom { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprYieldFrom(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprYieldFrom(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprYieldFrom) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprYieldFrom { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2685,40 +727,9 @@ impl AstNode for ast::ExprYieldFrom { visitor.visit_expr(value); } } -impl AstNode for ast::ExprCompare { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprCompare(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprCompare(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprCompare) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprCompare { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2737,40 +748,9 @@ impl AstNode for ast::ExprCompare { } } } -impl AstNode for ast::ExprCall { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprCall(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprCall(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprCall) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprCall { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2783,40 +763,9 @@ impl AstNode for ast::ExprCall { visitor.visit_arguments(arguments); } } -impl AstNode for ast::FStringFormatSpec { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::FStringFormatSpec(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::FStringFormatSpec(node) = kind { - Some(node) - } else { - None - } - } - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::FStringFormatSpec) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::FStringFormatSpec { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2825,40 +774,9 @@ impl AstNode for ast::FStringFormatSpec { } } } -impl AstNode for ast::FStringExpressionElement { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::FStringExpressionElement(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::FStringExpressionElement(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::FStringExpressionElement) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::FStringExpressionElement { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2876,79 +794,18 @@ impl AstNode for ast::FStringExpressionElement { } } } -impl AstNode for ast::FStringLiteralElement { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::FStringLiteralElement(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::FStringLiteralElement(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::FStringLiteralElement) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) +impl ast::FStringLiteralElement { + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { + let ast::FStringLiteralElement { range: _, value: _ } = self; } } -impl AstNode for ast::ExprFString { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprFString(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprFString(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprFString) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprFString { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -2966,40 +823,9 @@ impl AstNode for ast::ExprFString { } } } -impl AstNode for ast::ExprStringLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprStringLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprStringLiteral(node) = kind { - Some(node) - } else { - None - } - } - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprStringLiteral) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprStringLiteral { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3010,40 +836,9 @@ impl AstNode for ast::ExprStringLiteral { } } } -impl AstNode for ast::ExprBytesLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprBytesLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprBytesLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprBytesLiteral) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprBytesLiteral { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3054,196 +849,49 @@ impl AstNode for ast::ExprBytesLiteral { } } } -impl AstNode for ast::ExprNumberLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option + +impl ast::ExprNumberLiteral { + #[inline] + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - Self: Sized, - { - if let AnyNode::ExprNumberLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprNumberLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprNumberLiteral) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) - where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a> + ?Sized, { + let ast::ExprNumberLiteral { range: _, value: _ } = self; } } -impl AstNode for ast::ExprBooleanLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprBooleanLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprBooleanLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprBooleanLiteral) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) +impl ast::ExprBooleanLiteral { + #[inline] + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { + let ast::ExprBooleanLiteral { range: _, value: _ } = self; } } -impl AstNode for ast::ExprNoneLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprNoneLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprNoneLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprNoneLiteral) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) +impl ast::ExprNoneLiteral { + #[inline] + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { + let ast::ExprNoneLiteral { range: _ } = self; } } -impl AstNode for ast::ExprEllipsisLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprEllipsisLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprEllipsisLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprEllipsisLiteral) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) +impl ast::ExprEllipsisLiteral { + #[inline] + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { + let ast::ExprEllipsisLiteral { range: _ } = self; } } -impl AstNode for ast::ExprAttribute { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprAttribute(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprAttribute(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprAttribute) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprAttribute { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3257,40 +905,9 @@ impl AstNode for ast::ExprAttribute { visitor.visit_expr(value); } } -impl AstNode for ast::ExprSubscript { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprSubscript(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprSubscript(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprSubscript) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprSubscript { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3304,40 +921,9 @@ impl AstNode for ast::ExprSubscript { visitor.visit_expr(slice); } } -impl AstNode for ast::ExprStarred { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprStarred(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprStarred(node) = kind { - Some(node) - } else { - None - } - } - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprStarred) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprStarred { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3350,85 +936,23 @@ impl AstNode for ast::ExprStarred { visitor.visit_expr(value); } } -impl AstNode for ast::ExprName { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprName(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprName(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprName) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } +impl ast::ExprName { #[inline] - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { let ast::ExprName { + range: _, id: _, ctx: _, - range: _, } = self; } } -impl AstNode for ast::ExprList { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprList(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprList(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprList) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprList { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3443,40 +967,9 @@ impl AstNode for ast::ExprList { } } } -impl AstNode for ast::ExprTuple { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprTuple(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprTuple(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprTuple) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprTuple { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3492,39 +985,9 @@ impl AstNode for ast::ExprTuple { } } } -impl AstNode for ast::ExprSlice { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprSlice(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprSlice(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprSlice) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExprSlice { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3546,41 +1009,10 @@ impl AstNode for ast::ExprSlice { } } } -impl AstNode for ast::ExprIpyEscapeCommand { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExprIpyEscapeCommand(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExprIpyEscapeCommand(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExprIpyEscapeCommand) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } +impl ast::ExprIpyEscapeCommand { #[inline] - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3591,40 +1023,9 @@ impl AstNode for ast::ExprIpyEscapeCommand { } = self; } } -impl AstNode for ast::ExceptHandlerExceptHandler { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ExceptHandlerExceptHandler(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ExceptHandlerExceptHandler(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ExceptHandlerExceptHandler) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ExceptHandlerExceptHandler { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3640,40 +1041,9 @@ impl AstNode for ast::ExceptHandlerExceptHandler { visitor.visit_body(body); } } -impl AstNode for ast::PatternMatchValue { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::PatternMatchValue(node) = kind { - Some(node) - } else { - None - } - } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::PatternMatchValue(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::PatternMatchValue) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::PatternMatchValue { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3681,40 +1051,9 @@ impl AstNode for ast::PatternMatchValue { visitor.visit_expr(value); } } -impl AstNode for ast::PatternMatchSingleton { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::PatternMatchSingleton(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::PatternMatchSingleton(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::PatternMatchSingleton) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::PatternMatchSingleton { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3722,40 +1061,9 @@ impl AstNode for ast::PatternMatchSingleton { visitor.visit_singleton(value); } } -impl AstNode for ast::PatternMatchSequence { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::PatternMatchSequence(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::PatternMatchSequence(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::PatternMatchSequence) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::PatternMatchSequence { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3765,40 +1073,9 @@ impl AstNode for ast::PatternMatchSequence { } } } -impl AstNode for ast::PatternMatchMapping { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::PatternMatchMapping(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::PatternMatchMapping(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::PatternMatchMapping) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::PatternMatchMapping { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3814,127 +1091,34 @@ impl AstNode for ast::PatternMatchMapping { } } } -impl AstNode for ast::PatternMatchClass { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option + +impl ast::PatternMatchClass { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - Self: Sized, + V: SourceOrderVisitor<'a> + ?Sized, { - if let AnyNode::PatternMatchClass(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::PatternMatchClass(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::PatternMatchClass) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) - where - V: SourceOrderVisitor<'a> + ?Sized, - { - let ast::PatternMatchClass { - cls, - arguments: parameters, - range: _, - } = self; - visitor.visit_expr(cls); - visitor.visit_pattern_arguments(parameters); - } -} -impl AstNode for ast::PatternMatchStar { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::PatternMatchStar(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::PatternMatchStar(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::PatternMatchStar) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) + let ast::PatternMatchClass { + cls, + arguments: parameters, + range: _, + } = self; + visitor.visit_expr(cls); + visitor.visit_pattern_arguments(parameters); } +} +impl ast::PatternMatchStar { #[inline] - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { let ast::PatternMatchStar { range: _, name: _ } = self; } } -impl AstNode for ast::PatternMatchAs { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::PatternMatchAs(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::PatternMatchAs(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::PatternMatchAs) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::PatternMatchAs { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3948,40 +1132,9 @@ impl AstNode for ast::PatternMatchAs { } } } -impl AstNode for ast::PatternMatchOr { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::PatternMatchOr(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::PatternMatchOr(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::PatternMatchOr) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::PatternMatchOr { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -3991,40 +1144,9 @@ impl AstNode for ast::PatternMatchOr { } } } -impl AstNode for PatternArguments { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::PatternArguments(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::PatternArguments(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::PatternArguments) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::PatternArguments { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4043,40 +1165,9 @@ impl AstNode for PatternArguments { } } } -impl AstNode for PatternKeyword { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::PatternKeyword(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::PatternKeyword(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::PatternKeyword) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::PatternKeyword { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4090,40 +1181,8 @@ impl AstNode for PatternKeyword { } } -impl AstNode for Comprehension { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::Comprehension(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::Comprehension(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::Comprehension) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::Comprehension { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4142,40 +1201,9 @@ impl AstNode for Comprehension { } } } -impl AstNode for Arguments { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::Arguments(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::Arguments(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::Arguments) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::Arguments { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4187,40 +1215,9 @@ impl AstNode for Arguments { } } } -impl AstNode for Parameters { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::Parameters(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::Parameters(node) = kind { - Some(node) - } else { - None - } - } - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::Parameters) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::Parameters { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4234,40 +1231,9 @@ impl AstNode for Parameters { } } } -impl AstNode for Parameter { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::Parameter(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::Parameter(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::Parameter) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::Parameter { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4282,40 +1248,9 @@ impl AstNode for Parameter { } } } -impl AstNode for ParameterWithDefault { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::ParameterWithDefault(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::ParameterWithDefault(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::ParameterWithDefault) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::ParameterWithDefault { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4330,40 +1265,9 @@ impl AstNode for ParameterWithDefault { } } } -impl AstNode for Keyword { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::Keyword(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::Keyword(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::Keyword) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::Keyword { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4376,41 +1280,10 @@ impl AstNode for Keyword { visitor.visit_expr(value); } } -impl AstNode for Alias { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::Alias(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::Alias(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::Alias) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } +impl Alias { #[inline] - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4421,40 +1294,9 @@ impl AstNode for Alias { } = self; } } -impl AstNode for WithItem { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::WithItem(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::WithItem(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::WithItem) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::WithItem { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4471,40 +1313,9 @@ impl AstNode for WithItem { } } } -impl AstNode for MatchCase { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::MatchCase(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::MatchCase(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::MatchCase) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::MatchCase { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4523,40 +1334,8 @@ impl AstNode for MatchCase { } } -impl AstNode for Decorator { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::Decorator(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::Decorator(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::Decorator) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::Decorator { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4568,40 +1347,9 @@ impl AstNode for Decorator { visitor.visit_expr(expression); } } -impl AstNode for ast::TypeParams { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::TypeParams(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::TypeParams(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::TypeParams) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::TypeParams { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4615,42 +1363,11 @@ impl AstNode for ast::TypeParams { } } } -impl AstNode for ast::TypeParamTypeVar { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option + +impl ast::TypeParamTypeVar { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - Self: Sized, - { - if let AnyNode::TypeParamTypeVar(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::TypeParamTypeVar(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::TypeParamTypeVar) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) - where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a> + ?Sized, { let ast::TypeParamTypeVar { bound, @@ -4667,41 +1384,10 @@ impl AstNode for ast::TypeParamTypeVar { } } } -impl AstNode for ast::TypeParamTypeVarTuple { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::TypeParamTypeVarTuple(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::TypeParamTypeVarTuple(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::TypeParamTypeVarTuple) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } +impl ast::TypeParamTypeVarTuple { #[inline] - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4715,41 +1401,10 @@ impl AstNode for ast::TypeParamTypeVarTuple { } } } -impl AstNode for ast::TypeParamParamSpec { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::TypeParamParamSpec(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::TypeParamParamSpec(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::TypeParamParamSpec) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } +impl ast::TypeParamParamSpec { #[inline] - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4763,40 +1418,9 @@ impl AstNode for ast::TypeParamParamSpec { } } } -impl AstNode for ast::FString { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::FString(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::FString(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::FString) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) +impl ast::FString { + pub(crate) fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { @@ -4811,3494 +1435,232 @@ impl AstNode for ast::FString { } } } -impl AstNode for ast::StringLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::StringLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::StringLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::StringLiteral) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) +impl ast::StringLiteral { + #[inline] + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { + let ast::StringLiteral { + range: _, + value: _, + flags: _, + } = self; } } -impl AstNode for ast::BytesLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - if let AnyNode::BytesLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { - if let AnyNodeRef::BytesLiteral(node) = kind { - Some(node) - } else { - None - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::BytesLiteral) - } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } - - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } - - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) +impl ast::BytesLiteral { + #[inline] + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where V: SourceOrderVisitor<'a> + ?Sized, { + let ast::BytesLiteral { + range: _, + value: _, + flags: _, + } = self; } } -impl AstNode for ast::Identifier { - type Ref<'a> = &'a Self; - - fn cast(kind: AnyNode) -> Option +impl ast::Identifier { + #[inline] + pub(crate) fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - Self: Sized, + V: SourceOrderVisitor<'a> + ?Sized, { - if let AnyNode::Identifier(node) = kind { - Some(node) - } else { - None - } + let ast::Identifier { range: _, id: _ } = self; } +} - fn cast_ref(kind: AnyNodeRef<'_>) -> Option> { - if let AnyNodeRef::Identifier(node) = kind { - Some(node) - } else { - None - } +impl<'a> AnyNodeRef<'a> { + /// Compares two any node refs by their pointers (referential equality). + pub fn ptr_eq(self, other: AnyNodeRef) -> bool { + self.as_ptr().eq(&other.as_ptr()) && self.kind() == other.kind() } - fn can_cast(kind: NodeKind) -> bool { - matches!(kind, NodeKind::Identifier) + /// In our AST, only some alternative branches are represented as a node. This has historical + /// reasons, e.g. we added a node for elif/else in if statements which was not originally + /// present in the parser. + pub const fn is_alternative_branch_with_node(self) -> bool { + matches!( + self, + AnyNodeRef::ExceptHandlerExceptHandler(_) | AnyNodeRef::ElifElseClause(_) + ) } - fn as_any_node_ref(&self) -> AnyNodeRef { - AnyNodeRef::from(self) - } + /// The last child of the last branch, if the node has multiple branches. + pub fn last_child_in_body(&self) -> Option> { + let body = match self { + AnyNodeRef::StmtFunctionDef(ast::StmtFunctionDef { body, .. }) + | AnyNodeRef::StmtClassDef(ast::StmtClassDef { body, .. }) + | AnyNodeRef::StmtWith(ast::StmtWith { body, .. }) + | AnyNodeRef::MatchCase(MatchCase { body, .. }) + | AnyNodeRef::ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler { + body, + .. + }) + | AnyNodeRef::ElifElseClause(ast::ElifElseClause { body, .. }) => body, + AnyNodeRef::StmtIf(ast::StmtIf { + body, + elif_else_clauses, + .. + }) => elif_else_clauses.last().map_or(body, |clause| &clause.body), - fn into_any_node(self) -> AnyNode { - AnyNode::from(self) - } + AnyNodeRef::StmtFor(ast::StmtFor { body, orelse, .. }) + | AnyNodeRef::StmtWhile(ast::StmtWhile { body, orelse, .. }) => { + if orelse.is_empty() { + body + } else { + orelse + } + } - fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) - where - V: SourceOrderVisitor<'a> + ?Sized, - { - } -} + AnyNodeRef::StmtMatch(ast::StmtMatch { cases, .. }) => { + return cases.last().map(AnyNodeRef::from); + } -impl AstNode for Stmt { - type Ref<'a> = StatementRef<'a>; - - fn cast(kind: AnyNode) -> Option { - match kind { - AnyNode::StmtFunctionDef(node) => Some(Stmt::FunctionDef(node)), - AnyNode::StmtClassDef(node) => Some(Stmt::ClassDef(node)), - AnyNode::StmtReturn(node) => Some(Stmt::Return(node)), - AnyNode::StmtDelete(node) => Some(Stmt::Delete(node)), - AnyNode::StmtTypeAlias(node) => Some(Stmt::TypeAlias(node)), - AnyNode::StmtAssign(node) => Some(Stmt::Assign(node)), - AnyNode::StmtAugAssign(node) => Some(Stmt::AugAssign(node)), - AnyNode::StmtAnnAssign(node) => Some(Stmt::AnnAssign(node)), - AnyNode::StmtFor(node) => Some(Stmt::For(node)), - AnyNode::StmtWhile(node) => Some(Stmt::While(node)), - AnyNode::StmtIf(node) => Some(Stmt::If(node)), - AnyNode::StmtWith(node) => Some(Stmt::With(node)), - AnyNode::StmtMatch(node) => Some(Stmt::Match(node)), - AnyNode::StmtRaise(node) => Some(Stmt::Raise(node)), - AnyNode::StmtTry(node) => Some(Stmt::Try(node)), - AnyNode::StmtAssert(node) => Some(Stmt::Assert(node)), - AnyNode::StmtImport(node) => Some(Stmt::Import(node)), - AnyNode::StmtImportFrom(node) => Some(Stmt::ImportFrom(node)), - AnyNode::StmtGlobal(node) => Some(Stmt::Global(node)), - AnyNode::StmtNonlocal(node) => Some(Stmt::Nonlocal(node)), - AnyNode::StmtExpr(node) => Some(Stmt::Expr(node)), - AnyNode::StmtPass(node) => Some(Stmt::Pass(node)), - AnyNode::StmtBreak(node) => Some(Stmt::Break(node)), - AnyNode::StmtContinue(node) => Some(Stmt::Continue(node)), - AnyNode::StmtIpyEscapeCommand(node) => Some(Stmt::IpyEscapeCommand(node)), - - AnyNode::ModModule(_) - | AnyNode::ModExpression(_) - | AnyNode::ExprBoolOp(_) - | AnyNode::ExprNamed(_) - | AnyNode::ExprBinOp(_) - | AnyNode::ExprUnaryOp(_) - | AnyNode::ExprLambda(_) - | AnyNode::ExprIf(_) - | AnyNode::ExprDict(_) - | AnyNode::ExprSet(_) - | AnyNode::ExprListComp(_) - | AnyNode::ExprSetComp(_) - | AnyNode::ExprDictComp(_) - | AnyNode::ExprGenerator(_) - | AnyNode::ExprAwait(_) - | AnyNode::ExprYield(_) - | AnyNode::ExprYieldFrom(_) - | AnyNode::ExprCompare(_) - | AnyNode::ExprCall(_) - | AnyNode::FStringExpressionElement(_) - | AnyNode::FStringLiteralElement(_) - | AnyNode::FStringFormatSpec(_) - | AnyNode::ExprFString(_) - | AnyNode::ExprStringLiteral(_) - | AnyNode::ExprBytesLiteral(_) - | AnyNode::ExprNumberLiteral(_) - | AnyNode::ExprBooleanLiteral(_) - | AnyNode::ExprNoneLiteral(_) - | AnyNode::ExprEllipsisLiteral(_) - | AnyNode::ExprAttribute(_) - | AnyNode::ExprSubscript(_) - | AnyNode::ExprStarred(_) - | AnyNode::ExprName(_) - | AnyNode::ExprList(_) - | AnyNode::ExprTuple(_) - | AnyNode::ExprSlice(_) - | AnyNode::ExprIpyEscapeCommand(_) - | AnyNode::ExceptHandlerExceptHandler(_) - | AnyNode::PatternMatchValue(_) - | AnyNode::PatternMatchSingleton(_) - | AnyNode::PatternMatchSequence(_) - | AnyNode::PatternMatchMapping(_) - | AnyNode::PatternMatchClass(_) - | AnyNode::PatternMatchStar(_) - | AnyNode::PatternMatchAs(_) - | AnyNode::PatternMatchOr(_) - | AnyNode::PatternArguments(_) - | AnyNode::PatternKeyword(_) - | AnyNode::Comprehension(_) - | AnyNode::Arguments(_) - | AnyNode::Parameters(_) - | AnyNode::Parameter(_) - | AnyNode::ParameterWithDefault(_) - | AnyNode::Keyword(_) - | AnyNode::Alias(_) - | AnyNode::WithItem(_) - | AnyNode::MatchCase(_) - | AnyNode::Decorator(_) - | AnyNode::TypeParams(_) - | AnyNode::TypeParamTypeVar(_) - | AnyNode::TypeParamTypeVarTuple(_) - | AnyNode::TypeParamParamSpec(_) - | AnyNode::FString(_) - | AnyNode::StringLiteral(_) - | AnyNode::BytesLiteral(_) - | AnyNode::Identifier(_) - | AnyNode::ElifElseClause(_) => None, - } + AnyNodeRef::StmtTry(ast::StmtTry { + body, + handlers, + orelse, + finalbody, + .. + }) => { + if finalbody.is_empty() { + if orelse.is_empty() { + if handlers.is_empty() { + body + } else { + return handlers.last().map(AnyNodeRef::from); + } + } else { + orelse + } + } else { + finalbody + } + } + + // Not a node that contains an indented child node. + _ => return None, + }; + + body.last().map(AnyNodeRef::from) } - fn cast_ref(kind: AnyNodeRef) -> Option> { - match kind { - AnyNodeRef::StmtFunctionDef(statement) => Some(StatementRef::FunctionDef(statement)), - AnyNodeRef::StmtClassDef(statement) => Some(StatementRef::ClassDef(statement)), - AnyNodeRef::StmtReturn(statement) => Some(StatementRef::Return(statement)), - AnyNodeRef::StmtDelete(statement) => Some(StatementRef::Delete(statement)), - AnyNodeRef::StmtTypeAlias(statement) => Some(StatementRef::TypeAlias(statement)), - AnyNodeRef::StmtAssign(statement) => Some(StatementRef::Assign(statement)), - AnyNodeRef::StmtAugAssign(statement) => Some(StatementRef::AugAssign(statement)), - AnyNodeRef::StmtAnnAssign(statement) => Some(StatementRef::AnnAssign(statement)), - AnyNodeRef::StmtFor(statement) => Some(StatementRef::For(statement)), - AnyNodeRef::StmtWhile(statement) => Some(StatementRef::While(statement)), - AnyNodeRef::StmtIf(statement) => Some(StatementRef::If(statement)), - AnyNodeRef::StmtWith(statement) => Some(StatementRef::With(statement)), - AnyNodeRef::StmtMatch(statement) => Some(StatementRef::Match(statement)), - AnyNodeRef::StmtRaise(statement) => Some(StatementRef::Raise(statement)), - AnyNodeRef::StmtTry(statement) => Some(StatementRef::Try(statement)), - AnyNodeRef::StmtAssert(statement) => Some(StatementRef::Assert(statement)), - AnyNodeRef::StmtImport(statement) => Some(StatementRef::Import(statement)), - AnyNodeRef::StmtImportFrom(statement) => Some(StatementRef::ImportFrom(statement)), - AnyNodeRef::StmtGlobal(statement) => Some(StatementRef::Global(statement)), - AnyNodeRef::StmtNonlocal(statement) => Some(StatementRef::Nonlocal(statement)), - AnyNodeRef::StmtExpr(statement) => Some(StatementRef::Expr(statement)), - AnyNodeRef::StmtPass(statement) => Some(StatementRef::Pass(statement)), - AnyNodeRef::StmtBreak(statement) => Some(StatementRef::Break(statement)), - AnyNodeRef::StmtContinue(statement) => Some(StatementRef::Continue(statement)), - AnyNodeRef::StmtIpyEscapeCommand(statement) => { - Some(StatementRef::IpyEscapeCommand(statement)) + /// Check if the given statement is the first statement after the colon of a branch, be it in if + /// statements, for statements, after each part of a try-except-else-finally or function/class + /// definitions. + /// + /// + /// ```python + /// if True: <- has body + /// a <- first statement + /// b + /// elif b: <- has body + /// c <- first statement + /// d + /// else: <- has body + /// e <- first statement + /// f + /// + /// class: <- has body + /// a: int <- first statement + /// b: int + /// + /// ``` + /// + /// For nodes with multiple bodies, we check all bodies that don't have their own node. For + /// try-except-else-finally, each except branch has it's own node, so for the `StmtTry`, we check + /// the `try:`, `else:` and `finally:`, bodies, while `ExceptHandlerExceptHandler` has it's own + /// check. For for-else and while-else, we check both branches for the whole statement. + /// + /// ```python + /// try: <- has body (a) + /// 6/8 <- first statement (a) + /// 1/0 + /// except: <- has body (b) + /// a <- first statement (b) + /// b + /// else: + /// c <- first statement (a) + /// d + /// finally: + /// e <- first statement (a) + /// f + /// ``` + pub fn is_first_statement_in_body(&self, body: AnyNodeRef) -> bool { + match body { + AnyNodeRef::StmtFor(ast::StmtFor { body, orelse, .. }) + | AnyNodeRef::StmtWhile(ast::StmtWhile { body, orelse, .. }) => { + are_same_optional(*self, body.first()) || are_same_optional(*self, orelse.first()) } - AnyNodeRef::ModModule(_) - | AnyNodeRef::ModExpression(_) - | AnyNodeRef::ExprBoolOp(_) - | AnyNodeRef::ExprNamed(_) - | AnyNodeRef::ExprBinOp(_) - | AnyNodeRef::ExprUnaryOp(_) - | AnyNodeRef::ExprLambda(_) - | AnyNodeRef::ExprIf(_) - | AnyNodeRef::ExprDict(_) - | AnyNodeRef::ExprSet(_) - | AnyNodeRef::ExprListComp(_) - | AnyNodeRef::ExprSetComp(_) - | AnyNodeRef::ExprDictComp(_) - | AnyNodeRef::ExprGenerator(_) - | AnyNodeRef::ExprAwait(_) - | AnyNodeRef::ExprYield(_) - | AnyNodeRef::ExprYieldFrom(_) - | AnyNodeRef::ExprCompare(_) - | AnyNodeRef::ExprCall(_) - | AnyNodeRef::FStringExpressionElement(_) - | AnyNodeRef::FStringLiteralElement(_) - | AnyNodeRef::FStringFormatSpec(_) - | AnyNodeRef::ExprFString(_) - | AnyNodeRef::ExprStringLiteral(_) - | AnyNodeRef::ExprBytesLiteral(_) - | AnyNodeRef::ExprNumberLiteral(_) - | AnyNodeRef::ExprBooleanLiteral(_) - | AnyNodeRef::ExprNoneLiteral(_) - | AnyNodeRef::ExprEllipsisLiteral(_) - | AnyNodeRef::ExprAttribute(_) - | AnyNodeRef::ExprSubscript(_) - | AnyNodeRef::ExprStarred(_) - | AnyNodeRef::ExprName(_) - | AnyNodeRef::ExprList(_) - | AnyNodeRef::ExprTuple(_) - | AnyNodeRef::ExprSlice(_) - | AnyNodeRef::ExprIpyEscapeCommand(_) - | AnyNodeRef::ExceptHandlerExceptHandler(_) - | AnyNodeRef::PatternMatchValue(_) - | AnyNodeRef::PatternMatchSingleton(_) - | AnyNodeRef::PatternMatchSequence(_) - | AnyNodeRef::PatternMatchMapping(_) - | AnyNodeRef::PatternMatchClass(_) - | AnyNodeRef::PatternMatchStar(_) - | AnyNodeRef::PatternMatchAs(_) - | AnyNodeRef::PatternMatchOr(_) - | AnyNodeRef::PatternArguments(_) - | AnyNodeRef::PatternKeyword(_) - | AnyNodeRef::Comprehension(_) - | AnyNodeRef::Arguments(_) - | AnyNodeRef::Parameters(_) - | AnyNodeRef::Parameter(_) - | AnyNodeRef::ParameterWithDefault(_) - | AnyNodeRef::Keyword(_) - | AnyNodeRef::Alias(_) - | AnyNodeRef::WithItem(_) - | AnyNodeRef::MatchCase(_) - | AnyNodeRef::Decorator(_) - | AnyNodeRef::TypeParams(_) - | AnyNodeRef::TypeParamTypeVar(_) - | AnyNodeRef::TypeParamTypeVarTuple(_) - | AnyNodeRef::TypeParamParamSpec(_) - | AnyNodeRef::FString(_) - | AnyNodeRef::StringLiteral(_) - | AnyNodeRef::BytesLiteral(_) - | AnyNodeRef::Identifier(_) - | AnyNodeRef::ElifElseClause(_) => None, - } - } - fn can_cast(kind: NodeKind) -> bool { - match kind { - NodeKind::StmtClassDef - | NodeKind::StmtReturn - | NodeKind::StmtDelete - | NodeKind::StmtTypeAlias - | NodeKind::StmtAssign - | NodeKind::StmtAugAssign - | NodeKind::StmtAnnAssign - | NodeKind::StmtFor - | NodeKind::StmtWhile - | NodeKind::StmtIf - | NodeKind::StmtWith - | NodeKind::StmtMatch - | NodeKind::StmtRaise - | NodeKind::StmtTry - | NodeKind::StmtAssert - | NodeKind::StmtImport - | NodeKind::StmtImportFrom - | NodeKind::StmtGlobal - | NodeKind::StmtNonlocal - | NodeKind::StmtIpyEscapeCommand - | NodeKind::StmtExpr - | NodeKind::StmtPass - | NodeKind::StmtBreak - | NodeKind::StmtContinue => true, - NodeKind::ExprBoolOp - | NodeKind::ModModule - | NodeKind::ModInteractive - | NodeKind::ModExpression - | NodeKind::ModFunctionType - | NodeKind::StmtFunctionDef - | NodeKind::ExprNamed - | NodeKind::ExprBinOp - | NodeKind::ExprUnaryOp - | NodeKind::ExprLambda - | NodeKind::ExprIf - | NodeKind::ExprDict - | NodeKind::ExprSet - | NodeKind::ExprListComp - | NodeKind::ExprSetComp - | NodeKind::ExprDictComp - | NodeKind::ExprGenerator - | NodeKind::ExprAwait - | NodeKind::ExprYield - | NodeKind::ExprYieldFrom - | NodeKind::ExprCompare - | NodeKind::ExprCall - | NodeKind::FStringExpressionElement - | NodeKind::FStringLiteralElement - | NodeKind::FStringFormatSpec - | NodeKind::ExprFString - | NodeKind::ExprStringLiteral - | NodeKind::ExprBytesLiteral - | NodeKind::ExprNumberLiteral - | NodeKind::ExprBooleanLiteral - | NodeKind::ExprNoneLiteral - | NodeKind::ExprEllipsisLiteral - | NodeKind::ExprAttribute - | NodeKind::ExprSubscript - | NodeKind::ExprStarred - | NodeKind::ExprName - | NodeKind::ExprList - | NodeKind::ExprTuple - | NodeKind::ExprSlice - | NodeKind::ExprIpyEscapeCommand - | NodeKind::ExceptHandlerExceptHandler - | NodeKind::PatternMatchValue - | NodeKind::PatternMatchSingleton - | NodeKind::PatternMatchSequence - | NodeKind::PatternMatchMapping - | NodeKind::PatternMatchClass - | NodeKind::PatternMatchStar - | NodeKind::PatternMatchAs - | NodeKind::PatternMatchOr - | NodeKind::PatternArguments - | NodeKind::PatternKeyword - | NodeKind::TypeIgnoreTypeIgnore - | NodeKind::Comprehension - | NodeKind::Arguments - | NodeKind::Parameters - | NodeKind::Parameter - | NodeKind::ParameterWithDefault - | NodeKind::Keyword - | NodeKind::Alias - | NodeKind::WithItem - | NodeKind::MatchCase - | NodeKind::Decorator - | NodeKind::ElifElseClause - | NodeKind::TypeParams - | NodeKind::TypeParamTypeVar - | NodeKind::TypeParamTypeVarTuple - | NodeKind::TypeParamParamSpec - | NodeKind::FString - | NodeKind::StringLiteral - | NodeKind::Identifier - | NodeKind::BytesLiteral => false, - } - } + AnyNodeRef::StmtTry(ast::StmtTry { + body, + orelse, + finalbody, + .. + }) => { + are_same_optional(*self, body.first()) + || are_same_optional(*self, orelse.first()) + || are_same_optional(*self, finalbody.first()) + } - fn as_any_node_ref(&self) -> AnyNodeRef { - match self { - Stmt::FunctionDef(stmt) => stmt.as_any_node_ref(), - Stmt::ClassDef(stmt) => stmt.as_any_node_ref(), - Stmt::Return(stmt) => stmt.as_any_node_ref(), - Stmt::Delete(stmt) => stmt.as_any_node_ref(), - Stmt::Assign(stmt) => stmt.as_any_node_ref(), - Stmt::AugAssign(stmt) => stmt.as_any_node_ref(), - Stmt::AnnAssign(stmt) => stmt.as_any_node_ref(), - Stmt::TypeAlias(stmt) => stmt.as_any_node_ref(), - Stmt::For(stmt) => stmt.as_any_node_ref(), - Stmt::While(stmt) => stmt.as_any_node_ref(), - Stmt::If(stmt) => stmt.as_any_node_ref(), - Stmt::With(stmt) => stmt.as_any_node_ref(), - Stmt::Match(stmt) => stmt.as_any_node_ref(), - Stmt::Raise(stmt) => stmt.as_any_node_ref(), - Stmt::Try(stmt) => stmt.as_any_node_ref(), - Stmt::Assert(stmt) => stmt.as_any_node_ref(), - Stmt::Import(stmt) => stmt.as_any_node_ref(), - Stmt::ImportFrom(stmt) => stmt.as_any_node_ref(), - Stmt::Global(stmt) => stmt.as_any_node_ref(), - Stmt::Nonlocal(stmt) => stmt.as_any_node_ref(), - Stmt::Expr(stmt) => stmt.as_any_node_ref(), - Stmt::Pass(stmt) => stmt.as_any_node_ref(), - Stmt::Break(stmt) => stmt.as_any_node_ref(), - Stmt::Continue(stmt) => stmt.as_any_node_ref(), - Stmt::IpyEscapeCommand(stmt) => stmt.as_any_node_ref(), - } - } + AnyNodeRef::StmtIf(ast::StmtIf { body, .. }) + | AnyNodeRef::ElifElseClause(ast::ElifElseClause { body, .. }) + | AnyNodeRef::StmtWith(ast::StmtWith { body, .. }) + | AnyNodeRef::ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler { + body, + .. + }) + | AnyNodeRef::MatchCase(MatchCase { body, .. }) + | AnyNodeRef::StmtFunctionDef(ast::StmtFunctionDef { body, .. }) + | AnyNodeRef::StmtClassDef(ast::StmtClassDef { body, .. }) => { + are_same_optional(*self, body.first()) + } - fn into_any_node(self) -> AnyNode { - match self { - Stmt::FunctionDef(stmt) => stmt.into_any_node(), - Stmt::ClassDef(stmt) => stmt.into_any_node(), - Stmt::Return(stmt) => stmt.into_any_node(), - Stmt::Delete(stmt) => stmt.into_any_node(), - Stmt::Assign(stmt) => stmt.into_any_node(), - Stmt::AugAssign(stmt) => stmt.into_any_node(), - Stmt::AnnAssign(stmt) => stmt.into_any_node(), - Stmt::TypeAlias(stmt) => stmt.into_any_node(), - Stmt::For(stmt) => stmt.into_any_node(), - Stmt::While(stmt) => stmt.into_any_node(), - Stmt::If(stmt) => stmt.into_any_node(), - Stmt::With(stmt) => stmt.into_any_node(), - Stmt::Match(stmt) => stmt.into_any_node(), - Stmt::Raise(stmt) => stmt.into_any_node(), - Stmt::Try(stmt) => stmt.into_any_node(), - Stmt::Assert(stmt) => stmt.into_any_node(), - Stmt::Import(stmt) => stmt.into_any_node(), - Stmt::ImportFrom(stmt) => stmt.into_any_node(), - Stmt::Global(stmt) => stmt.into_any_node(), - Stmt::Nonlocal(stmt) => stmt.into_any_node(), - Stmt::Expr(stmt) => stmt.into_any_node(), - Stmt::Pass(stmt) => stmt.into_any_node(), - Stmt::Break(stmt) => stmt.into_any_node(), - Stmt::Continue(stmt) => stmt.into_any_node(), - Stmt::IpyEscapeCommand(stmt) => stmt.into_any_node(), - } - } + AnyNodeRef::StmtMatch(ast::StmtMatch { cases, .. }) => { + are_same_optional(*self, cases.first()) + } - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) - where - V: SourceOrderVisitor<'a> + ?Sized, - { - match self { - Stmt::FunctionDef(stmt) => stmt.visit_source_order(visitor), - Stmt::ClassDef(stmt) => stmt.visit_source_order(visitor), - Stmt::Return(stmt) => stmt.visit_source_order(visitor), - Stmt::Delete(stmt) => stmt.visit_source_order(visitor), - Stmt::Assign(stmt) => stmt.visit_source_order(visitor), - Stmt::AugAssign(stmt) => stmt.visit_source_order(visitor), - Stmt::AnnAssign(stmt) => stmt.visit_source_order(visitor), - Stmt::TypeAlias(stmt) => stmt.visit_source_order(visitor), - Stmt::For(stmt) => stmt.visit_source_order(visitor), - Stmt::While(stmt) => stmt.visit_source_order(visitor), - Stmt::If(stmt) => stmt.visit_source_order(visitor), - Stmt::With(stmt) => stmt.visit_source_order(visitor), - Stmt::Match(stmt) => stmt.visit_source_order(visitor), - Stmt::Raise(stmt) => stmt.visit_source_order(visitor), - Stmt::Try(stmt) => stmt.visit_source_order(visitor), - Stmt::Assert(stmt) => stmt.visit_source_order(visitor), - Stmt::Import(stmt) => stmt.visit_source_order(visitor), - Stmt::ImportFrom(stmt) => stmt.visit_source_order(visitor), - Stmt::Global(stmt) => stmt.visit_source_order(visitor), - Stmt::Nonlocal(stmt) => stmt.visit_source_order(visitor), - Stmt::Expr(stmt) => stmt.visit_source_order(visitor), - Stmt::Pass(stmt) => stmt.visit_source_order(visitor), - Stmt::Break(stmt) => stmt.visit_source_order(visitor), - Stmt::Continue(stmt) => stmt.visit_source_order(visitor), - Stmt::IpyEscapeCommand(stmt) => stmt.visit_source_order(visitor), + _ => false, } } -} - -impl AstNode for TypeParam { - type Ref<'a> = TypeParamRef<'a>; - fn cast(kind: AnyNode) -> Option - where - Self: Sized, - { - match kind { - AnyNode::TypeParamTypeVar(node) => Some(TypeParam::TypeVar(node)), - AnyNode::TypeParamTypeVarTuple(node) => Some(TypeParam::TypeVarTuple(node)), - AnyNode::TypeParamParamSpec(node) => Some(TypeParam::ParamSpec(node)), - _ => None, - } - } - - fn cast_ref(kind: AnyNodeRef) -> Option> { - match kind { - AnyNodeRef::TypeParamTypeVar(node) => Some(TypeParamRef::TypeVar(node)), - AnyNodeRef::TypeParamTypeVarTuple(node) => Some(TypeParamRef::TypeVarTuple(node)), - AnyNodeRef::TypeParamParamSpec(node) => Some(TypeParamRef::ParamSpec(node)), - _ => None, - } - } - - fn can_cast(kind: NodeKind) -> bool { - matches!( - kind, - NodeKind::TypeParamTypeVar - | NodeKind::TypeParamTypeVarTuple - | NodeKind::TypeParamParamSpec - ) - } - - fn as_any_node_ref(&self) -> AnyNodeRef { - match self { - TypeParam::TypeVar(node) => node.as_any_node_ref(), - TypeParam::TypeVarTuple(node) => node.as_any_node_ref(), - TypeParam::ParamSpec(node) => node.as_any_node_ref(), - } - } - - fn into_any_node(self) -> AnyNode { - match self { - TypeParam::TypeVar(node) => node.into_any_node(), - TypeParam::TypeVarTuple(node) => node.into_any_node(), - TypeParam::ParamSpec(node) => node.into_any_node(), - } - } - - fn visit_source_order<'a, V>(&'a self, visitor: &mut V) - where - V: SourceOrderVisitor<'a> + ?Sized, - { - match self { - TypeParam::TypeVar(node) => node.visit_source_order(visitor), - TypeParam::TypeVarTuple(node) => node.visit_source_order(visitor), - TypeParam::ParamSpec(node) => node.visit_source_order(visitor), - } - } -} - -impl From for AnyNode { - fn from(stmt: Stmt) -> Self { - match stmt { - Stmt::FunctionDef(node) => AnyNode::StmtFunctionDef(node), - Stmt::ClassDef(node) => AnyNode::StmtClassDef(node), - Stmt::Return(node) => AnyNode::StmtReturn(node), - Stmt::Delete(node) => AnyNode::StmtDelete(node), - Stmt::TypeAlias(node) => AnyNode::StmtTypeAlias(node), - Stmt::Assign(node) => AnyNode::StmtAssign(node), - Stmt::AugAssign(node) => AnyNode::StmtAugAssign(node), - Stmt::AnnAssign(node) => AnyNode::StmtAnnAssign(node), - Stmt::For(node) => AnyNode::StmtFor(node), - Stmt::While(node) => AnyNode::StmtWhile(node), - Stmt::If(node) => AnyNode::StmtIf(node), - Stmt::With(node) => AnyNode::StmtWith(node), - Stmt::Match(node) => AnyNode::StmtMatch(node), - Stmt::Raise(node) => AnyNode::StmtRaise(node), - Stmt::Try(node) => AnyNode::StmtTry(node), - Stmt::Assert(node) => AnyNode::StmtAssert(node), - Stmt::Import(node) => AnyNode::StmtImport(node), - Stmt::ImportFrom(node) => AnyNode::StmtImportFrom(node), - Stmt::Global(node) => AnyNode::StmtGlobal(node), - Stmt::Nonlocal(node) => AnyNode::StmtNonlocal(node), - Stmt::Expr(node) => AnyNode::StmtExpr(node), - Stmt::Pass(node) => AnyNode::StmtPass(node), - Stmt::Break(node) => AnyNode::StmtBreak(node), - Stmt::Continue(node) => AnyNode::StmtContinue(node), - Stmt::IpyEscapeCommand(node) => AnyNode::StmtIpyEscapeCommand(node), - } - } -} - -impl From for AnyNode { - fn from(expr: Expr) -> Self { - match expr { - Expr::BoolOp(node) => AnyNode::ExprBoolOp(node), - Expr::Named(node) => AnyNode::ExprNamed(node), - Expr::BinOp(node) => AnyNode::ExprBinOp(node), - Expr::UnaryOp(node) => AnyNode::ExprUnaryOp(node), - Expr::Lambda(node) => AnyNode::ExprLambda(node), - Expr::If(node) => AnyNode::ExprIf(node), - Expr::Dict(node) => AnyNode::ExprDict(node), - Expr::Set(node) => AnyNode::ExprSet(node), - Expr::ListComp(node) => AnyNode::ExprListComp(node), - Expr::SetComp(node) => AnyNode::ExprSetComp(node), - Expr::DictComp(node) => AnyNode::ExprDictComp(node), - Expr::Generator(node) => AnyNode::ExprGenerator(node), - Expr::Await(node) => AnyNode::ExprAwait(node), - Expr::Yield(node) => AnyNode::ExprYield(node), - Expr::YieldFrom(node) => AnyNode::ExprYieldFrom(node), - Expr::Compare(node) => AnyNode::ExprCompare(node), - Expr::Call(node) => AnyNode::ExprCall(node), - Expr::FString(node) => AnyNode::ExprFString(node), - Expr::StringLiteral(node) => AnyNode::ExprStringLiteral(node), - Expr::BytesLiteral(node) => AnyNode::ExprBytesLiteral(node), - Expr::NumberLiteral(node) => AnyNode::ExprNumberLiteral(node), - Expr::BooleanLiteral(node) => AnyNode::ExprBooleanLiteral(node), - Expr::NoneLiteral(node) => AnyNode::ExprNoneLiteral(node), - Expr::EllipsisLiteral(node) => AnyNode::ExprEllipsisLiteral(node), - Expr::Attribute(node) => AnyNode::ExprAttribute(node), - Expr::Subscript(node) => AnyNode::ExprSubscript(node), - Expr::Starred(node) => AnyNode::ExprStarred(node), - Expr::Name(node) => AnyNode::ExprName(node), - Expr::List(node) => AnyNode::ExprList(node), - Expr::Tuple(node) => AnyNode::ExprTuple(node), - Expr::Slice(node) => AnyNode::ExprSlice(node), - Expr::IpyEscapeCommand(node) => AnyNode::ExprIpyEscapeCommand(node), - } - } -} - -impl From for AnyNode { - fn from(module: Mod) -> Self { - match module { - Mod::Module(node) => AnyNode::ModModule(node), - Mod::Expression(node) => AnyNode::ModExpression(node), - } - } -} - -impl From for AnyNode { - fn from(element: FStringElement) -> Self { - match element { - FStringElement::Literal(node) => AnyNode::FStringLiteralElement(node), - FStringElement::Expression(node) => AnyNode::FStringExpressionElement(node), - } - } -} - -impl From for AnyNode { - fn from(pattern: Pattern) -> Self { - match pattern { - Pattern::MatchValue(node) => AnyNode::PatternMatchValue(node), - Pattern::MatchSingleton(node) => AnyNode::PatternMatchSingleton(node), - Pattern::MatchSequence(node) => AnyNode::PatternMatchSequence(node), - Pattern::MatchMapping(node) => AnyNode::PatternMatchMapping(node), - Pattern::MatchClass(node) => AnyNode::PatternMatchClass(node), - Pattern::MatchStar(node) => AnyNode::PatternMatchStar(node), - Pattern::MatchAs(node) => AnyNode::PatternMatchAs(node), - Pattern::MatchOr(node) => AnyNode::PatternMatchOr(node), - } - } -} - -impl From for AnyNode { - fn from(handler: ExceptHandler) -> Self { - match handler { - ExceptHandler::ExceptHandler(handler) => AnyNode::ExceptHandlerExceptHandler(handler), - } - } -} - -impl From for AnyNode { - fn from(node: ast::ModModule) -> Self { - AnyNode::ModModule(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ModExpression) -> Self { - AnyNode::ModExpression(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtFunctionDef) -> Self { - AnyNode::StmtFunctionDef(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtClassDef) -> Self { - AnyNode::StmtClassDef(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtReturn) -> Self { - AnyNode::StmtReturn(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtDelete) -> Self { - AnyNode::StmtDelete(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtTypeAlias) -> Self { - AnyNode::StmtTypeAlias(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtAssign) -> Self { - AnyNode::StmtAssign(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtAugAssign) -> Self { - AnyNode::StmtAugAssign(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtAnnAssign) -> Self { - AnyNode::StmtAnnAssign(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtFor) -> Self { - AnyNode::StmtFor(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtWhile) -> Self { - AnyNode::StmtWhile(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtIf) -> Self { - AnyNode::StmtIf(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ElifElseClause) -> Self { - AnyNode::ElifElseClause(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtWith) -> Self { - AnyNode::StmtWith(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtMatch) -> Self { - AnyNode::StmtMatch(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtRaise) -> Self { - AnyNode::StmtRaise(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtTry) -> Self { - AnyNode::StmtTry(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtAssert) -> Self { - AnyNode::StmtAssert(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtImport) -> Self { - AnyNode::StmtImport(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtImportFrom) -> Self { - AnyNode::StmtImportFrom(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtGlobal) -> Self { - AnyNode::StmtGlobal(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtNonlocal) -> Self { - AnyNode::StmtNonlocal(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtExpr) -> Self { - AnyNode::StmtExpr(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtPass) -> Self { - AnyNode::StmtPass(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtBreak) -> Self { - AnyNode::StmtBreak(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtContinue) -> Self { - AnyNode::StmtContinue(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StmtIpyEscapeCommand) -> Self { - AnyNode::StmtIpyEscapeCommand(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprBoolOp) -> Self { - AnyNode::ExprBoolOp(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprNamed) -> Self { - AnyNode::ExprNamed(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprBinOp) -> Self { - AnyNode::ExprBinOp(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprUnaryOp) -> Self { - AnyNode::ExprUnaryOp(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprLambda) -> Self { - AnyNode::ExprLambda(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprIf) -> Self { - AnyNode::ExprIf(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprDict) -> Self { - AnyNode::ExprDict(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprSet) -> Self { - AnyNode::ExprSet(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprListComp) -> Self { - AnyNode::ExprListComp(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprSetComp) -> Self { - AnyNode::ExprSetComp(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprDictComp) -> Self { - AnyNode::ExprDictComp(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprGenerator) -> Self { - AnyNode::ExprGenerator(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprAwait) -> Self { - AnyNode::ExprAwait(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprYield) -> Self { - AnyNode::ExprYield(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprYieldFrom) -> Self { - AnyNode::ExprYieldFrom(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprCompare) -> Self { - AnyNode::ExprCompare(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprCall) -> Self { - AnyNode::ExprCall(node) - } -} - -impl From for AnyNode { - fn from(node: ast::FStringExpressionElement) -> Self { - AnyNode::FStringExpressionElement(node) - } -} - -impl From for AnyNode { - fn from(node: ast::FStringLiteralElement) -> Self { - AnyNode::FStringLiteralElement(node) - } -} - -impl From for AnyNode { - fn from(node: ast::FStringFormatSpec) -> Self { - AnyNode::FStringFormatSpec(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprFString) -> Self { - AnyNode::ExprFString(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprStringLiteral) -> Self { - AnyNode::ExprStringLiteral(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprBytesLiteral) -> Self { - AnyNode::ExprBytesLiteral(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprNumberLiteral) -> Self { - AnyNode::ExprNumberLiteral(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprBooleanLiteral) -> Self { - AnyNode::ExprBooleanLiteral(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprNoneLiteral) -> Self { - AnyNode::ExprNoneLiteral(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprEllipsisLiteral) -> Self { - AnyNode::ExprEllipsisLiteral(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprAttribute) -> Self { - AnyNode::ExprAttribute(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprSubscript) -> Self { - AnyNode::ExprSubscript(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprStarred) -> Self { - AnyNode::ExprStarred(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprName) -> Self { - AnyNode::ExprName(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprList) -> Self { - AnyNode::ExprList(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprTuple) -> Self { - AnyNode::ExprTuple(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprSlice) -> Self { - AnyNode::ExprSlice(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExprIpyEscapeCommand) -> Self { - AnyNode::ExprIpyEscapeCommand(node) - } -} - -impl From for AnyNode { - fn from(node: ast::ExceptHandlerExceptHandler) -> Self { - AnyNode::ExceptHandlerExceptHandler(node) - } -} - -impl From for AnyNode { - fn from(node: ast::PatternMatchValue) -> Self { - AnyNode::PatternMatchValue(node) - } -} - -impl From for AnyNode { - fn from(node: ast::PatternMatchSingleton) -> Self { - AnyNode::PatternMatchSingleton(node) - } -} - -impl From for AnyNode { - fn from(node: ast::PatternMatchSequence) -> Self { - AnyNode::PatternMatchSequence(node) - } -} - -impl From for AnyNode { - fn from(node: ast::PatternMatchMapping) -> Self { - AnyNode::PatternMatchMapping(node) - } -} - -impl From for AnyNode { - fn from(node: ast::PatternMatchClass) -> Self { - AnyNode::PatternMatchClass(node) - } -} - -impl From for AnyNode { - fn from(node: ast::PatternMatchStar) -> Self { - AnyNode::PatternMatchStar(node) - } -} - -impl From for AnyNode { - fn from(node: ast::PatternMatchAs) -> Self { - AnyNode::PatternMatchAs(node) - } -} - -impl From for AnyNode { - fn from(node: ast::PatternMatchOr) -> Self { - AnyNode::PatternMatchOr(node) - } -} - -impl From for AnyNode { - fn from(node: PatternArguments) -> Self { - AnyNode::PatternArguments(node) - } -} - -impl From for AnyNode { - fn from(node: PatternKeyword) -> Self { - AnyNode::PatternKeyword(node) - } -} - -impl From for AnyNode { - fn from(node: Comprehension) -> Self { - AnyNode::Comprehension(node) - } -} -impl From for AnyNode { - fn from(node: Arguments) -> Self { - AnyNode::Arguments(node) - } -} -impl From for AnyNode { - fn from(node: Parameters) -> Self { - AnyNode::Parameters(node) - } -} -impl From for AnyNode { - fn from(node: Parameter) -> Self { - AnyNode::Parameter(node) - } -} -impl From for AnyNode { - fn from(node: ParameterWithDefault) -> Self { - AnyNode::ParameterWithDefault(node) - } -} -impl From for AnyNode { - fn from(node: Keyword) -> Self { - AnyNode::Keyword(node) - } -} -impl From for AnyNode { - fn from(node: Alias) -> Self { - AnyNode::Alias(node) - } -} -impl From for AnyNode { - fn from(node: WithItem) -> Self { - AnyNode::WithItem(node) - } -} -impl From for AnyNode { - fn from(node: MatchCase) -> Self { - AnyNode::MatchCase(node) - } -} -impl From for AnyNode { - fn from(node: Decorator) -> Self { - AnyNode::Decorator(node) - } -} -impl From for AnyNode { - fn from(node: TypeParams) -> Self { - AnyNode::TypeParams(node) - } -} -impl From for AnyNode { - fn from(node: TypeParamTypeVar) -> Self { - AnyNode::TypeParamTypeVar(node) - } -} - -impl From for AnyNode { - fn from(node: TypeParamTypeVarTuple) -> Self { - AnyNode::TypeParamTypeVarTuple(node) - } -} - -impl From for AnyNode { - fn from(node: TypeParamParamSpec) -> Self { - AnyNode::TypeParamParamSpec(node) - } -} - -impl From for AnyNode { - fn from(node: ast::FString) -> Self { - AnyNode::FString(node) - } -} - -impl From for AnyNode { - fn from(node: ast::StringLiteral) -> Self { - AnyNode::StringLiteral(node) - } -} - -impl From for AnyNode { - fn from(node: ast::BytesLiteral) -> Self { - AnyNode::BytesLiteral(node) - } -} - -impl From for AnyNode { - fn from(node: ast::Identifier) -> Self { - AnyNode::Identifier(node) - } -} - -impl Ranged for AnyNode { - fn range(&self) -> TextRange { - match self { - AnyNode::ModModule(node) => node.range(), - AnyNode::ModExpression(node) => node.range(), - AnyNode::StmtFunctionDef(node) => node.range(), - AnyNode::StmtClassDef(node) => node.range(), - AnyNode::StmtReturn(node) => node.range(), - AnyNode::StmtDelete(node) => node.range(), - AnyNode::StmtTypeAlias(node) => node.range(), - AnyNode::StmtAssign(node) => node.range(), - AnyNode::StmtAugAssign(node) => node.range(), - AnyNode::StmtAnnAssign(node) => node.range(), - AnyNode::StmtFor(node) => node.range(), - AnyNode::StmtWhile(node) => node.range(), - AnyNode::StmtIf(node) => node.range(), - AnyNode::StmtWith(node) => node.range(), - AnyNode::StmtMatch(node) => node.range(), - AnyNode::StmtRaise(node) => node.range(), - AnyNode::StmtTry(node) => node.range(), - AnyNode::StmtAssert(node) => node.range(), - AnyNode::StmtImport(node) => node.range(), - AnyNode::StmtImportFrom(node) => node.range(), - AnyNode::StmtGlobal(node) => node.range(), - AnyNode::StmtNonlocal(node) => node.range(), - AnyNode::StmtExpr(node) => node.range(), - AnyNode::StmtPass(node) => node.range(), - AnyNode::StmtBreak(node) => node.range(), - AnyNode::StmtContinue(node) => node.range(), - AnyNode::StmtIpyEscapeCommand(node) => node.range(), - AnyNode::ExprBoolOp(node) => node.range(), - AnyNode::ExprNamed(node) => node.range(), - AnyNode::ExprBinOp(node) => node.range(), - AnyNode::ExprUnaryOp(node) => node.range(), - AnyNode::ExprLambda(node) => node.range(), - AnyNode::ExprIf(node) => node.range(), - AnyNode::ExprDict(node) => node.range(), - AnyNode::ExprSet(node) => node.range(), - AnyNode::ExprListComp(node) => node.range(), - AnyNode::ExprSetComp(node) => node.range(), - AnyNode::ExprDictComp(node) => node.range(), - AnyNode::ExprGenerator(node) => node.range(), - AnyNode::ExprAwait(node) => node.range(), - AnyNode::ExprYield(node) => node.range(), - AnyNode::ExprYieldFrom(node) => node.range(), - AnyNode::ExprCompare(node) => node.range(), - AnyNode::ExprCall(node) => node.range(), - AnyNode::FStringExpressionElement(node) => node.range(), - AnyNode::FStringLiteralElement(node) => node.range(), - AnyNode::FStringFormatSpec(node) => node.range(), - AnyNode::ExprFString(node) => node.range(), - AnyNode::ExprStringLiteral(node) => node.range(), - AnyNode::ExprBytesLiteral(node) => node.range(), - AnyNode::ExprNumberLiteral(node) => node.range(), - AnyNode::ExprBooleanLiteral(node) => node.range(), - AnyNode::ExprNoneLiteral(node) => node.range(), - AnyNode::ExprEllipsisLiteral(node) => node.range(), - AnyNode::ExprAttribute(node) => node.range(), - AnyNode::ExprSubscript(node) => node.range(), - AnyNode::ExprStarred(node) => node.range(), - AnyNode::ExprName(node) => node.range(), - AnyNode::ExprList(node) => node.range(), - AnyNode::ExprTuple(node) => node.range(), - AnyNode::ExprSlice(node) => node.range(), - AnyNode::ExprIpyEscapeCommand(node) => node.range(), - AnyNode::ExceptHandlerExceptHandler(node) => node.range(), - AnyNode::PatternMatchValue(node) => node.range(), - AnyNode::PatternMatchSingleton(node) => node.range(), - AnyNode::PatternMatchSequence(node) => node.range(), - AnyNode::PatternMatchMapping(node) => node.range(), - AnyNode::PatternMatchClass(node) => node.range(), - AnyNode::PatternMatchStar(node) => node.range(), - AnyNode::PatternMatchAs(node) => node.range(), - AnyNode::PatternMatchOr(node) => node.range(), - AnyNode::PatternArguments(node) => node.range(), - AnyNode::PatternKeyword(node) => node.range(), - AnyNode::Comprehension(node) => node.range(), - AnyNode::Arguments(node) => node.range(), - AnyNode::Parameters(node) => node.range(), - AnyNode::Parameter(node) => node.range(), - AnyNode::ParameterWithDefault(node) => node.range(), - AnyNode::Keyword(node) => node.range(), - AnyNode::Alias(node) => node.range(), - AnyNode::WithItem(node) => node.range(), - AnyNode::MatchCase(node) => node.range(), - AnyNode::Decorator(node) => node.range(), - AnyNode::TypeParams(node) => node.range(), - AnyNode::TypeParamTypeVar(node) => node.range(), - AnyNode::TypeParamTypeVarTuple(node) => node.range(), - AnyNode::TypeParamParamSpec(node) => node.range(), - AnyNode::FString(node) => node.range(), - AnyNode::StringLiteral(node) => node.range(), - AnyNode::BytesLiteral(node) => node.range(), - AnyNode::ElifElseClause(node) => node.range(), - AnyNode::Identifier(node) => node.range(), - } - } -} - -#[derive(Copy, Clone, Debug, is_macro::Is, PartialEq)] -pub enum AnyNodeRef<'a> { - ModModule(&'a ast::ModModule), - ModExpression(&'a ast::ModExpression), - StmtFunctionDef(&'a ast::StmtFunctionDef), - StmtClassDef(&'a ast::StmtClassDef), - StmtReturn(&'a ast::StmtReturn), - StmtDelete(&'a ast::StmtDelete), - StmtTypeAlias(&'a ast::StmtTypeAlias), - StmtAssign(&'a ast::StmtAssign), - StmtAugAssign(&'a ast::StmtAugAssign), - StmtAnnAssign(&'a ast::StmtAnnAssign), - StmtFor(&'a ast::StmtFor), - StmtWhile(&'a ast::StmtWhile), - StmtIf(&'a ast::StmtIf), - StmtWith(&'a ast::StmtWith), - StmtMatch(&'a ast::StmtMatch), - StmtRaise(&'a ast::StmtRaise), - StmtTry(&'a ast::StmtTry), - StmtAssert(&'a ast::StmtAssert), - StmtImport(&'a ast::StmtImport), - StmtImportFrom(&'a ast::StmtImportFrom), - StmtGlobal(&'a ast::StmtGlobal), - StmtNonlocal(&'a ast::StmtNonlocal), - StmtExpr(&'a ast::StmtExpr), - StmtPass(&'a ast::StmtPass), - StmtBreak(&'a ast::StmtBreak), - StmtContinue(&'a ast::StmtContinue), - StmtIpyEscapeCommand(&'a ast::StmtIpyEscapeCommand), - ExprBoolOp(&'a ast::ExprBoolOp), - ExprNamed(&'a ast::ExprNamed), - ExprBinOp(&'a ast::ExprBinOp), - ExprUnaryOp(&'a ast::ExprUnaryOp), - ExprLambda(&'a ast::ExprLambda), - ExprIf(&'a ast::ExprIf), - ExprDict(&'a ast::ExprDict), - ExprSet(&'a ast::ExprSet), - ExprListComp(&'a ast::ExprListComp), - ExprSetComp(&'a ast::ExprSetComp), - ExprDictComp(&'a ast::ExprDictComp), - ExprGenerator(&'a ast::ExprGenerator), - ExprAwait(&'a ast::ExprAwait), - ExprYield(&'a ast::ExprYield), - ExprYieldFrom(&'a ast::ExprYieldFrom), - ExprCompare(&'a ast::ExprCompare), - ExprCall(&'a ast::ExprCall), - FStringExpressionElement(&'a ast::FStringExpressionElement), - FStringLiteralElement(&'a ast::FStringLiteralElement), - FStringFormatSpec(&'a ast::FStringFormatSpec), - ExprFString(&'a ast::ExprFString), - ExprStringLiteral(&'a ast::ExprStringLiteral), - ExprBytesLiteral(&'a ast::ExprBytesLiteral), - ExprNumberLiteral(&'a ast::ExprNumberLiteral), - ExprBooleanLiteral(&'a ast::ExprBooleanLiteral), - ExprNoneLiteral(&'a ast::ExprNoneLiteral), - ExprEllipsisLiteral(&'a ast::ExprEllipsisLiteral), - ExprAttribute(&'a ast::ExprAttribute), - ExprSubscript(&'a ast::ExprSubscript), - ExprStarred(&'a ast::ExprStarred), - ExprName(&'a ast::ExprName), - ExprList(&'a ast::ExprList), - ExprTuple(&'a ast::ExprTuple), - ExprSlice(&'a ast::ExprSlice), - ExprIpyEscapeCommand(&'a ast::ExprIpyEscapeCommand), - ExceptHandlerExceptHandler(&'a ast::ExceptHandlerExceptHandler), - PatternMatchValue(&'a ast::PatternMatchValue), - PatternMatchSingleton(&'a ast::PatternMatchSingleton), - PatternMatchSequence(&'a ast::PatternMatchSequence), - PatternMatchMapping(&'a ast::PatternMatchMapping), - PatternMatchClass(&'a ast::PatternMatchClass), - PatternMatchStar(&'a ast::PatternMatchStar), - PatternMatchAs(&'a ast::PatternMatchAs), - PatternMatchOr(&'a ast::PatternMatchOr), - PatternArguments(&'a ast::PatternArguments), - PatternKeyword(&'a ast::PatternKeyword), - Comprehension(&'a Comprehension), - Arguments(&'a Arguments), - Parameters(&'a Parameters), - Parameter(&'a Parameter), - ParameterWithDefault(&'a ParameterWithDefault), - Keyword(&'a Keyword), - Alias(&'a Alias), - WithItem(&'a WithItem), - MatchCase(&'a MatchCase), - Decorator(&'a Decorator), - TypeParams(&'a TypeParams), - TypeParamTypeVar(&'a TypeParamTypeVar), - TypeParamTypeVarTuple(&'a TypeParamTypeVarTuple), - TypeParamParamSpec(&'a TypeParamParamSpec), - FString(&'a ast::FString), - StringLiteral(&'a ast::StringLiteral), - BytesLiteral(&'a ast::BytesLiteral), - ElifElseClause(&'a ast::ElifElseClause), - Identifier(&'a ast::Identifier), -} - -impl<'a> AnyNodeRef<'a> { - pub fn as_ptr(&self) -> NonNull<()> { - match self { - AnyNodeRef::ModModule(node) => NonNull::from(*node).cast(), - AnyNodeRef::ModExpression(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtFunctionDef(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtClassDef(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtReturn(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtDelete(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtTypeAlias(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtAssign(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtAugAssign(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtAnnAssign(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtFor(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtWhile(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtIf(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtWith(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtMatch(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtRaise(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtTry(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtAssert(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtImport(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtImportFrom(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtGlobal(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtNonlocal(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtExpr(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtPass(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtBreak(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtContinue(node) => NonNull::from(*node).cast(), - AnyNodeRef::StmtIpyEscapeCommand(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprBoolOp(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprNamed(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprBinOp(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprUnaryOp(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprLambda(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprIf(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprDict(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprSet(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprListComp(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprSetComp(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprDictComp(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprGenerator(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprAwait(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprYield(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprYieldFrom(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprCompare(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprCall(node) => NonNull::from(*node).cast(), - AnyNodeRef::FStringExpressionElement(node) => NonNull::from(*node).cast(), - AnyNodeRef::FStringLiteralElement(node) => NonNull::from(*node).cast(), - AnyNodeRef::FStringFormatSpec(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprFString(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprStringLiteral(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprBytesLiteral(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprNumberLiteral(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprBooleanLiteral(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprNoneLiteral(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprEllipsisLiteral(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprAttribute(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprSubscript(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprStarred(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprName(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprList(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprTuple(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprSlice(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExprIpyEscapeCommand(node) => NonNull::from(*node).cast(), - AnyNodeRef::ExceptHandlerExceptHandler(node) => NonNull::from(*node).cast(), - AnyNodeRef::PatternMatchValue(node) => NonNull::from(*node).cast(), - AnyNodeRef::PatternMatchSingleton(node) => NonNull::from(*node).cast(), - AnyNodeRef::PatternMatchSequence(node) => NonNull::from(*node).cast(), - AnyNodeRef::PatternMatchMapping(node) => NonNull::from(*node).cast(), - AnyNodeRef::PatternMatchClass(node) => NonNull::from(*node).cast(), - AnyNodeRef::PatternMatchStar(node) => NonNull::from(*node).cast(), - AnyNodeRef::PatternMatchAs(node) => NonNull::from(*node).cast(), - AnyNodeRef::PatternMatchOr(node) => NonNull::from(*node).cast(), - AnyNodeRef::PatternArguments(node) => NonNull::from(*node).cast(), - AnyNodeRef::PatternKeyword(node) => NonNull::from(*node).cast(), - AnyNodeRef::Comprehension(node) => NonNull::from(*node).cast(), - AnyNodeRef::Arguments(node) => NonNull::from(*node).cast(), - AnyNodeRef::Parameters(node) => NonNull::from(*node).cast(), - AnyNodeRef::Parameter(node) => NonNull::from(*node).cast(), - AnyNodeRef::ParameterWithDefault(node) => NonNull::from(*node).cast(), - AnyNodeRef::Keyword(node) => NonNull::from(*node).cast(), - AnyNodeRef::Alias(node) => NonNull::from(*node).cast(), - AnyNodeRef::WithItem(node) => NonNull::from(*node).cast(), - AnyNodeRef::MatchCase(node) => NonNull::from(*node).cast(), - AnyNodeRef::Decorator(node) => NonNull::from(*node).cast(), - AnyNodeRef::TypeParams(node) => NonNull::from(*node).cast(), - AnyNodeRef::TypeParamTypeVar(node) => NonNull::from(*node).cast(), - AnyNodeRef::TypeParamTypeVarTuple(node) => NonNull::from(*node).cast(), - AnyNodeRef::TypeParamParamSpec(node) => NonNull::from(*node).cast(), - AnyNodeRef::FString(node) => NonNull::from(*node).cast(), - AnyNodeRef::StringLiteral(node) => NonNull::from(*node).cast(), - AnyNodeRef::BytesLiteral(node) => NonNull::from(*node).cast(), - AnyNodeRef::ElifElseClause(node) => NonNull::from(*node).cast(), - AnyNodeRef::Identifier(node) => NonNull::from(*node).cast(), - } - } - - /// Compares two any node refs by their pointers (referential equality). - pub fn ptr_eq(self, other: AnyNodeRef) -> bool { - self.as_ptr().eq(&other.as_ptr()) && self.kind() == other.kind() - } - - /// Returns the node's [`kind`](NodeKind) that has no data associated and is [`Copy`]. - pub const fn kind(self) -> NodeKind { - match self { - AnyNodeRef::ModModule(_) => NodeKind::ModModule, - AnyNodeRef::ModExpression(_) => NodeKind::ModExpression, - AnyNodeRef::StmtFunctionDef(_) => NodeKind::StmtFunctionDef, - AnyNodeRef::StmtClassDef(_) => NodeKind::StmtClassDef, - AnyNodeRef::StmtReturn(_) => NodeKind::StmtReturn, - AnyNodeRef::StmtDelete(_) => NodeKind::StmtDelete, - AnyNodeRef::StmtTypeAlias(_) => NodeKind::StmtTypeAlias, - AnyNodeRef::StmtAssign(_) => NodeKind::StmtAssign, - AnyNodeRef::StmtAugAssign(_) => NodeKind::StmtAugAssign, - AnyNodeRef::StmtAnnAssign(_) => NodeKind::StmtAnnAssign, - AnyNodeRef::StmtFor(_) => NodeKind::StmtFor, - AnyNodeRef::StmtWhile(_) => NodeKind::StmtWhile, - AnyNodeRef::StmtIf(_) => NodeKind::StmtIf, - AnyNodeRef::StmtWith(_) => NodeKind::StmtWith, - AnyNodeRef::StmtMatch(_) => NodeKind::StmtMatch, - AnyNodeRef::StmtRaise(_) => NodeKind::StmtRaise, - AnyNodeRef::StmtTry(_) => NodeKind::StmtTry, - AnyNodeRef::StmtAssert(_) => NodeKind::StmtAssert, - AnyNodeRef::StmtImport(_) => NodeKind::StmtImport, - AnyNodeRef::StmtImportFrom(_) => NodeKind::StmtImportFrom, - AnyNodeRef::StmtGlobal(_) => NodeKind::StmtGlobal, - AnyNodeRef::StmtNonlocal(_) => NodeKind::StmtNonlocal, - AnyNodeRef::StmtExpr(_) => NodeKind::StmtExpr, - AnyNodeRef::StmtPass(_) => NodeKind::StmtPass, - AnyNodeRef::StmtBreak(_) => NodeKind::StmtBreak, - AnyNodeRef::StmtContinue(_) => NodeKind::StmtContinue, - AnyNodeRef::StmtIpyEscapeCommand(_) => NodeKind::StmtIpyEscapeCommand, - AnyNodeRef::ExprBoolOp(_) => NodeKind::ExprBoolOp, - AnyNodeRef::ExprNamed(_) => NodeKind::ExprNamed, - AnyNodeRef::ExprBinOp(_) => NodeKind::ExprBinOp, - AnyNodeRef::ExprUnaryOp(_) => NodeKind::ExprUnaryOp, - AnyNodeRef::ExprLambda(_) => NodeKind::ExprLambda, - AnyNodeRef::ExprIf(_) => NodeKind::ExprIf, - AnyNodeRef::ExprDict(_) => NodeKind::ExprDict, - AnyNodeRef::ExprSet(_) => NodeKind::ExprSet, - AnyNodeRef::ExprListComp(_) => NodeKind::ExprListComp, - AnyNodeRef::ExprSetComp(_) => NodeKind::ExprSetComp, - AnyNodeRef::ExprDictComp(_) => NodeKind::ExprDictComp, - AnyNodeRef::ExprGenerator(_) => NodeKind::ExprGenerator, - AnyNodeRef::ExprAwait(_) => NodeKind::ExprAwait, - AnyNodeRef::ExprYield(_) => NodeKind::ExprYield, - AnyNodeRef::ExprYieldFrom(_) => NodeKind::ExprYieldFrom, - AnyNodeRef::ExprCompare(_) => NodeKind::ExprCompare, - AnyNodeRef::ExprCall(_) => NodeKind::ExprCall, - AnyNodeRef::FStringExpressionElement(_) => NodeKind::FStringExpressionElement, - AnyNodeRef::FStringLiteralElement(_) => NodeKind::FStringLiteralElement, - AnyNodeRef::FStringFormatSpec(_) => NodeKind::FStringFormatSpec, - AnyNodeRef::ExprFString(_) => NodeKind::ExprFString, - AnyNodeRef::ExprStringLiteral(_) => NodeKind::ExprStringLiteral, - AnyNodeRef::ExprBytesLiteral(_) => NodeKind::ExprBytesLiteral, - AnyNodeRef::ExprNumberLiteral(_) => NodeKind::ExprNumberLiteral, - AnyNodeRef::ExprBooleanLiteral(_) => NodeKind::ExprBooleanLiteral, - AnyNodeRef::ExprNoneLiteral(_) => NodeKind::ExprNoneLiteral, - AnyNodeRef::ExprEllipsisLiteral(_) => NodeKind::ExprEllipsisLiteral, - AnyNodeRef::ExprAttribute(_) => NodeKind::ExprAttribute, - AnyNodeRef::ExprSubscript(_) => NodeKind::ExprSubscript, - AnyNodeRef::ExprStarred(_) => NodeKind::ExprStarred, - AnyNodeRef::ExprName(_) => NodeKind::ExprName, - AnyNodeRef::ExprList(_) => NodeKind::ExprList, - AnyNodeRef::ExprTuple(_) => NodeKind::ExprTuple, - AnyNodeRef::ExprSlice(_) => NodeKind::ExprSlice, - AnyNodeRef::ExprIpyEscapeCommand(_) => NodeKind::ExprIpyEscapeCommand, - AnyNodeRef::ExceptHandlerExceptHandler(_) => NodeKind::ExceptHandlerExceptHandler, - AnyNodeRef::PatternMatchValue(_) => NodeKind::PatternMatchValue, - AnyNodeRef::PatternMatchSingleton(_) => NodeKind::PatternMatchSingleton, - AnyNodeRef::PatternMatchSequence(_) => NodeKind::PatternMatchSequence, - AnyNodeRef::PatternMatchMapping(_) => NodeKind::PatternMatchMapping, - AnyNodeRef::PatternMatchClass(_) => NodeKind::PatternMatchClass, - AnyNodeRef::PatternMatchStar(_) => NodeKind::PatternMatchStar, - AnyNodeRef::PatternMatchAs(_) => NodeKind::PatternMatchAs, - AnyNodeRef::PatternMatchOr(_) => NodeKind::PatternMatchOr, - AnyNodeRef::PatternArguments(_) => NodeKind::PatternArguments, - AnyNodeRef::PatternKeyword(_) => NodeKind::PatternKeyword, - AnyNodeRef::Comprehension(_) => NodeKind::Comprehension, - AnyNodeRef::Arguments(_) => NodeKind::Arguments, - AnyNodeRef::Parameters(_) => NodeKind::Parameters, - AnyNodeRef::Parameter(_) => NodeKind::Parameter, - AnyNodeRef::ParameterWithDefault(_) => NodeKind::ParameterWithDefault, - AnyNodeRef::Keyword(_) => NodeKind::Keyword, - AnyNodeRef::Alias(_) => NodeKind::Alias, - AnyNodeRef::WithItem(_) => NodeKind::WithItem, - AnyNodeRef::MatchCase(_) => NodeKind::MatchCase, - AnyNodeRef::Decorator(_) => NodeKind::Decorator, - AnyNodeRef::TypeParams(_) => NodeKind::TypeParams, - AnyNodeRef::TypeParamTypeVar(_) => NodeKind::TypeParamTypeVar, - AnyNodeRef::TypeParamTypeVarTuple(_) => NodeKind::TypeParamTypeVarTuple, - AnyNodeRef::TypeParamParamSpec(_) => NodeKind::TypeParamParamSpec, - AnyNodeRef::FString(_) => NodeKind::FString, - AnyNodeRef::StringLiteral(_) => NodeKind::StringLiteral, - AnyNodeRef::BytesLiteral(_) => NodeKind::BytesLiteral, - AnyNodeRef::ElifElseClause(_) => NodeKind::ElifElseClause, - AnyNodeRef::Identifier(_) => NodeKind::Identifier, - } - } - - pub const fn is_statement(self) -> bool { - match self { - AnyNodeRef::StmtFunctionDef(_) - | AnyNodeRef::StmtClassDef(_) - | AnyNodeRef::StmtReturn(_) - | AnyNodeRef::StmtDelete(_) - | AnyNodeRef::StmtTypeAlias(_) - | AnyNodeRef::StmtAssign(_) - | AnyNodeRef::StmtAugAssign(_) - | AnyNodeRef::StmtAnnAssign(_) - | AnyNodeRef::StmtFor(_) - | AnyNodeRef::StmtWhile(_) - | AnyNodeRef::StmtIf(_) - | AnyNodeRef::StmtWith(_) - | AnyNodeRef::StmtMatch(_) - | AnyNodeRef::StmtRaise(_) - | AnyNodeRef::StmtTry(_) - | AnyNodeRef::StmtAssert(_) - | AnyNodeRef::StmtImport(_) - | AnyNodeRef::StmtImportFrom(_) - | AnyNodeRef::StmtGlobal(_) - | AnyNodeRef::StmtNonlocal(_) - | AnyNodeRef::StmtExpr(_) - | AnyNodeRef::StmtPass(_) - | AnyNodeRef::StmtBreak(_) - | AnyNodeRef::StmtContinue(_) - | AnyNodeRef::StmtIpyEscapeCommand(_) => true, - - AnyNodeRef::ModModule(_) - | AnyNodeRef::ModExpression(_) - | AnyNodeRef::ExprBoolOp(_) - | AnyNodeRef::ExprNamed(_) - | AnyNodeRef::ExprBinOp(_) - | AnyNodeRef::ExprUnaryOp(_) - | AnyNodeRef::ExprLambda(_) - | AnyNodeRef::ExprIf(_) - | AnyNodeRef::ExprDict(_) - | AnyNodeRef::ExprSet(_) - | AnyNodeRef::ExprListComp(_) - | AnyNodeRef::ExprSetComp(_) - | AnyNodeRef::ExprDictComp(_) - | AnyNodeRef::ExprGenerator(_) - | AnyNodeRef::ExprAwait(_) - | AnyNodeRef::ExprYield(_) - | AnyNodeRef::ExprYieldFrom(_) - | AnyNodeRef::ExprCompare(_) - | AnyNodeRef::ExprCall(_) - | AnyNodeRef::FStringExpressionElement(_) - | AnyNodeRef::FStringLiteralElement(_) - | AnyNodeRef::FStringFormatSpec(_) - | AnyNodeRef::ExprFString(_) - | AnyNodeRef::ExprStringLiteral(_) - | AnyNodeRef::ExprBytesLiteral(_) - | AnyNodeRef::ExprNumberLiteral(_) - | AnyNodeRef::ExprBooleanLiteral(_) - | AnyNodeRef::ExprNoneLiteral(_) - | AnyNodeRef::ExprEllipsisLiteral(_) - | AnyNodeRef::ExprAttribute(_) - | AnyNodeRef::ExprSubscript(_) - | AnyNodeRef::ExprStarred(_) - | AnyNodeRef::ExprName(_) - | AnyNodeRef::ExprList(_) - | AnyNodeRef::ExprTuple(_) - | AnyNodeRef::ExprSlice(_) - | AnyNodeRef::ExprIpyEscapeCommand(_) - | AnyNodeRef::ExceptHandlerExceptHandler(_) - | AnyNodeRef::PatternMatchValue(_) - | AnyNodeRef::PatternMatchSingleton(_) - | AnyNodeRef::PatternMatchSequence(_) - | AnyNodeRef::PatternMatchMapping(_) - | AnyNodeRef::PatternMatchClass(_) - | AnyNodeRef::PatternMatchStar(_) - | AnyNodeRef::PatternMatchAs(_) - | AnyNodeRef::PatternMatchOr(_) - | AnyNodeRef::PatternArguments(_) - | AnyNodeRef::PatternKeyword(_) - | AnyNodeRef::Comprehension(_) - | AnyNodeRef::Arguments(_) - | AnyNodeRef::Parameters(_) - | AnyNodeRef::Parameter(_) - | AnyNodeRef::ParameterWithDefault(_) - | AnyNodeRef::Keyword(_) - | AnyNodeRef::Alias(_) - | AnyNodeRef::WithItem(_) - | AnyNodeRef::MatchCase(_) - | AnyNodeRef::Decorator(_) - | AnyNodeRef::TypeParams(_) - | AnyNodeRef::TypeParamTypeVar(_) - | AnyNodeRef::TypeParamTypeVarTuple(_) - | AnyNodeRef::TypeParamParamSpec(_) - | AnyNodeRef::FString(_) - | AnyNodeRef::StringLiteral(_) - | AnyNodeRef::BytesLiteral(_) - | AnyNodeRef::Identifier(_) - | AnyNodeRef::ElifElseClause(_) => false, - } - } - - pub const fn is_expression(self) -> bool { - match self { - AnyNodeRef::ExprBoolOp(_) - | AnyNodeRef::ExprNamed(_) - | AnyNodeRef::ExprBinOp(_) - | AnyNodeRef::ExprUnaryOp(_) - | AnyNodeRef::ExprLambda(_) - | AnyNodeRef::ExprIf(_) - | AnyNodeRef::ExprDict(_) - | AnyNodeRef::ExprSet(_) - | AnyNodeRef::ExprListComp(_) - | AnyNodeRef::ExprSetComp(_) - | AnyNodeRef::ExprDictComp(_) - | AnyNodeRef::ExprGenerator(_) - | AnyNodeRef::ExprAwait(_) - | AnyNodeRef::ExprYield(_) - | AnyNodeRef::ExprYieldFrom(_) - | AnyNodeRef::ExprCompare(_) - | AnyNodeRef::ExprCall(_) - | AnyNodeRef::ExprFString(_) - | AnyNodeRef::ExprStringLiteral(_) - | AnyNodeRef::ExprBytesLiteral(_) - | AnyNodeRef::ExprNumberLiteral(_) - | AnyNodeRef::ExprBooleanLiteral(_) - | AnyNodeRef::ExprNoneLiteral(_) - | AnyNodeRef::ExprEllipsisLiteral(_) - | AnyNodeRef::ExprAttribute(_) - | AnyNodeRef::ExprSubscript(_) - | AnyNodeRef::ExprStarred(_) - | AnyNodeRef::ExprName(_) - | AnyNodeRef::ExprList(_) - | AnyNodeRef::ExprTuple(_) - | AnyNodeRef::ExprSlice(_) - | AnyNodeRef::ExprIpyEscapeCommand(_) => true, - - AnyNodeRef::ModModule(_) - | AnyNodeRef::ModExpression(_) - | AnyNodeRef::StmtFunctionDef(_) - | AnyNodeRef::StmtClassDef(_) - | AnyNodeRef::StmtReturn(_) - | AnyNodeRef::StmtDelete(_) - | AnyNodeRef::StmtTypeAlias(_) - | AnyNodeRef::StmtAssign(_) - | AnyNodeRef::StmtAugAssign(_) - | AnyNodeRef::StmtAnnAssign(_) - | AnyNodeRef::StmtFor(_) - | AnyNodeRef::StmtWhile(_) - | AnyNodeRef::StmtIf(_) - | AnyNodeRef::StmtWith(_) - | AnyNodeRef::StmtMatch(_) - | AnyNodeRef::StmtRaise(_) - | AnyNodeRef::StmtTry(_) - | AnyNodeRef::StmtAssert(_) - | AnyNodeRef::StmtImport(_) - | AnyNodeRef::StmtImportFrom(_) - | AnyNodeRef::StmtGlobal(_) - | AnyNodeRef::StmtNonlocal(_) - | AnyNodeRef::StmtExpr(_) - | AnyNodeRef::StmtPass(_) - | AnyNodeRef::StmtBreak(_) - | AnyNodeRef::StmtContinue(_) - | AnyNodeRef::StmtIpyEscapeCommand(_) - | AnyNodeRef::ExceptHandlerExceptHandler(_) - | AnyNodeRef::FStringExpressionElement(_) - | AnyNodeRef::FStringLiteralElement(_) - | AnyNodeRef::FStringFormatSpec(_) - | AnyNodeRef::PatternMatchValue(_) - | AnyNodeRef::PatternMatchSingleton(_) - | AnyNodeRef::PatternMatchSequence(_) - | AnyNodeRef::PatternMatchMapping(_) - | AnyNodeRef::PatternMatchClass(_) - | AnyNodeRef::PatternMatchStar(_) - | AnyNodeRef::PatternMatchAs(_) - | AnyNodeRef::PatternMatchOr(_) - | AnyNodeRef::PatternArguments(_) - | AnyNodeRef::PatternKeyword(_) - | AnyNodeRef::Comprehension(_) - | AnyNodeRef::Arguments(_) - | AnyNodeRef::Parameters(_) - | AnyNodeRef::Parameter(_) - | AnyNodeRef::ParameterWithDefault(_) - | AnyNodeRef::Keyword(_) - | AnyNodeRef::Alias(_) - | AnyNodeRef::WithItem(_) - | AnyNodeRef::MatchCase(_) - | AnyNodeRef::Decorator(_) - | AnyNodeRef::TypeParams(_) - | AnyNodeRef::TypeParamTypeVar(_) - | AnyNodeRef::TypeParamTypeVarTuple(_) - | AnyNodeRef::TypeParamParamSpec(_) - | AnyNodeRef::FString(_) - | AnyNodeRef::StringLiteral(_) - | AnyNodeRef::BytesLiteral(_) - | AnyNodeRef::Identifier(_) - | AnyNodeRef::ElifElseClause(_) => false, - } - } - - pub const fn is_module(self) -> bool { - match self { - AnyNodeRef::ModModule(_) | AnyNodeRef::ModExpression(_) => true, - - AnyNodeRef::StmtFunctionDef(_) - | AnyNodeRef::StmtClassDef(_) - | AnyNodeRef::StmtReturn(_) - | AnyNodeRef::StmtDelete(_) - | AnyNodeRef::StmtTypeAlias(_) - | AnyNodeRef::StmtAssign(_) - | AnyNodeRef::StmtAugAssign(_) - | AnyNodeRef::StmtAnnAssign(_) - | AnyNodeRef::StmtFor(_) - | AnyNodeRef::StmtWhile(_) - | AnyNodeRef::StmtIf(_) - | AnyNodeRef::StmtWith(_) - | AnyNodeRef::StmtMatch(_) - | AnyNodeRef::StmtRaise(_) - | AnyNodeRef::StmtTry(_) - | AnyNodeRef::StmtAssert(_) - | AnyNodeRef::StmtImport(_) - | AnyNodeRef::StmtImportFrom(_) - | AnyNodeRef::StmtGlobal(_) - | AnyNodeRef::StmtNonlocal(_) - | AnyNodeRef::StmtExpr(_) - | AnyNodeRef::StmtPass(_) - | AnyNodeRef::StmtBreak(_) - | AnyNodeRef::StmtContinue(_) - | AnyNodeRef::StmtIpyEscapeCommand(_) - | AnyNodeRef::ExprBoolOp(_) - | AnyNodeRef::ExprNamed(_) - | AnyNodeRef::ExprBinOp(_) - | AnyNodeRef::ExprUnaryOp(_) - | AnyNodeRef::ExprLambda(_) - | AnyNodeRef::ExprIf(_) - | AnyNodeRef::ExprDict(_) - | AnyNodeRef::ExprSet(_) - | AnyNodeRef::ExprListComp(_) - | AnyNodeRef::ExprSetComp(_) - | AnyNodeRef::ExprDictComp(_) - | AnyNodeRef::ExprGenerator(_) - | AnyNodeRef::ExprAwait(_) - | AnyNodeRef::ExprYield(_) - | AnyNodeRef::ExprYieldFrom(_) - | AnyNodeRef::ExprCompare(_) - | AnyNodeRef::ExprCall(_) - | AnyNodeRef::FStringExpressionElement(_) - | AnyNodeRef::FStringLiteralElement(_) - | AnyNodeRef::FStringFormatSpec(_) - | AnyNodeRef::ExprFString(_) - | AnyNodeRef::ExprStringLiteral(_) - | AnyNodeRef::ExprBytesLiteral(_) - | AnyNodeRef::ExprNumberLiteral(_) - | AnyNodeRef::ExprBooleanLiteral(_) - | AnyNodeRef::ExprNoneLiteral(_) - | AnyNodeRef::ExprEllipsisLiteral(_) - | AnyNodeRef::ExprAttribute(_) - | AnyNodeRef::ExprSubscript(_) - | AnyNodeRef::ExprStarred(_) - | AnyNodeRef::ExprName(_) - | AnyNodeRef::ExprList(_) - | AnyNodeRef::ExprTuple(_) - | AnyNodeRef::ExprSlice(_) - | AnyNodeRef::ExprIpyEscapeCommand(_) - | AnyNodeRef::ExceptHandlerExceptHandler(_) - | AnyNodeRef::PatternMatchValue(_) - | AnyNodeRef::PatternMatchSingleton(_) - | AnyNodeRef::PatternMatchSequence(_) - | AnyNodeRef::PatternMatchMapping(_) - | AnyNodeRef::PatternMatchClass(_) - | AnyNodeRef::PatternMatchStar(_) - | AnyNodeRef::PatternMatchAs(_) - | AnyNodeRef::PatternMatchOr(_) - | AnyNodeRef::PatternArguments(_) - | AnyNodeRef::PatternKeyword(_) - | AnyNodeRef::Comprehension(_) - | AnyNodeRef::Arguments(_) - | AnyNodeRef::Parameters(_) - | AnyNodeRef::Parameter(_) - | AnyNodeRef::ParameterWithDefault(_) - | AnyNodeRef::Keyword(_) - | AnyNodeRef::Alias(_) - | AnyNodeRef::WithItem(_) - | AnyNodeRef::MatchCase(_) - | AnyNodeRef::Decorator(_) - | AnyNodeRef::TypeParams(_) - | AnyNodeRef::TypeParamTypeVar(_) - | AnyNodeRef::TypeParamTypeVarTuple(_) - | AnyNodeRef::TypeParamParamSpec(_) - | AnyNodeRef::FString(_) - | AnyNodeRef::StringLiteral(_) - | AnyNodeRef::BytesLiteral(_) - | AnyNodeRef::Identifier(_) - | AnyNodeRef::ElifElseClause(_) => false, - } - } - - pub const fn is_pattern(self) -> bool { - match self { - AnyNodeRef::PatternMatchValue(_) - | AnyNodeRef::PatternMatchSingleton(_) - | AnyNodeRef::PatternMatchSequence(_) - | AnyNodeRef::PatternMatchMapping(_) - | AnyNodeRef::PatternMatchClass(_) - | AnyNodeRef::PatternMatchStar(_) - | AnyNodeRef::PatternMatchAs(_) - | AnyNodeRef::PatternMatchOr(_) => true, - - AnyNodeRef::ModModule(_) - | AnyNodeRef::ModExpression(_) - | AnyNodeRef::StmtFunctionDef(_) - | AnyNodeRef::StmtClassDef(_) - | AnyNodeRef::StmtReturn(_) - | AnyNodeRef::StmtDelete(_) - | AnyNodeRef::StmtTypeAlias(_) - | AnyNodeRef::StmtAssign(_) - | AnyNodeRef::StmtAugAssign(_) - | AnyNodeRef::StmtAnnAssign(_) - | AnyNodeRef::StmtFor(_) - | AnyNodeRef::StmtWhile(_) - | AnyNodeRef::StmtIf(_) - | AnyNodeRef::StmtWith(_) - | AnyNodeRef::StmtMatch(_) - | AnyNodeRef::StmtRaise(_) - | AnyNodeRef::StmtTry(_) - | AnyNodeRef::StmtAssert(_) - | AnyNodeRef::StmtImport(_) - | AnyNodeRef::StmtImportFrom(_) - | AnyNodeRef::StmtGlobal(_) - | AnyNodeRef::StmtNonlocal(_) - | AnyNodeRef::StmtExpr(_) - | AnyNodeRef::StmtPass(_) - | AnyNodeRef::StmtBreak(_) - | AnyNodeRef::StmtContinue(_) - | AnyNodeRef::StmtIpyEscapeCommand(_) - | AnyNodeRef::ExprBoolOp(_) - | AnyNodeRef::ExprNamed(_) - | AnyNodeRef::ExprBinOp(_) - | AnyNodeRef::ExprUnaryOp(_) - | AnyNodeRef::ExprLambda(_) - | AnyNodeRef::ExprIf(_) - | AnyNodeRef::ExprDict(_) - | AnyNodeRef::ExprSet(_) - | AnyNodeRef::ExprListComp(_) - | AnyNodeRef::ExprSetComp(_) - | AnyNodeRef::ExprDictComp(_) - | AnyNodeRef::ExprGenerator(_) - | AnyNodeRef::ExprAwait(_) - | AnyNodeRef::ExprYield(_) - | AnyNodeRef::ExprYieldFrom(_) - | AnyNodeRef::ExprCompare(_) - | AnyNodeRef::ExprCall(_) - | AnyNodeRef::FStringExpressionElement(_) - | AnyNodeRef::FStringLiteralElement(_) - | AnyNodeRef::FStringFormatSpec(_) - | AnyNodeRef::ExprFString(_) - | AnyNodeRef::ExprStringLiteral(_) - | AnyNodeRef::ExprBytesLiteral(_) - | AnyNodeRef::ExprNumberLiteral(_) - | AnyNodeRef::ExprBooleanLiteral(_) - | AnyNodeRef::ExprNoneLiteral(_) - | AnyNodeRef::ExprEllipsisLiteral(_) - | AnyNodeRef::ExprAttribute(_) - | AnyNodeRef::ExprSubscript(_) - | AnyNodeRef::ExprStarred(_) - | AnyNodeRef::ExprName(_) - | AnyNodeRef::ExprList(_) - | AnyNodeRef::ExprTuple(_) - | AnyNodeRef::ExprSlice(_) - | AnyNodeRef::ExprIpyEscapeCommand(_) - | AnyNodeRef::PatternArguments(_) - | AnyNodeRef::PatternKeyword(_) - | AnyNodeRef::ExceptHandlerExceptHandler(_) - | AnyNodeRef::Comprehension(_) - | AnyNodeRef::Arguments(_) - | AnyNodeRef::Parameters(_) - | AnyNodeRef::Parameter(_) - | AnyNodeRef::ParameterWithDefault(_) - | AnyNodeRef::Keyword(_) - | AnyNodeRef::Alias(_) - | AnyNodeRef::WithItem(_) - | AnyNodeRef::MatchCase(_) - | AnyNodeRef::Decorator(_) - | AnyNodeRef::TypeParams(_) - | AnyNodeRef::TypeParamTypeVar(_) - | AnyNodeRef::TypeParamTypeVarTuple(_) - | AnyNodeRef::TypeParamParamSpec(_) - | AnyNodeRef::FString(_) - | AnyNodeRef::StringLiteral(_) - | AnyNodeRef::BytesLiteral(_) - | AnyNodeRef::Identifier(_) - | AnyNodeRef::ElifElseClause(_) => false, - } - } - - pub const fn is_except_handler(self) -> bool { - match self { - AnyNodeRef::ExceptHandlerExceptHandler(_) => true, - - AnyNodeRef::ModModule(_) - | AnyNodeRef::ModExpression(_) - | AnyNodeRef::StmtFunctionDef(_) - | AnyNodeRef::StmtClassDef(_) - | AnyNodeRef::StmtReturn(_) - | AnyNodeRef::StmtDelete(_) - | AnyNodeRef::StmtTypeAlias(_) - | AnyNodeRef::StmtAssign(_) - | AnyNodeRef::StmtAugAssign(_) - | AnyNodeRef::StmtAnnAssign(_) - | AnyNodeRef::StmtFor(_) - | AnyNodeRef::StmtWhile(_) - | AnyNodeRef::StmtIf(_) - | AnyNodeRef::StmtWith(_) - | AnyNodeRef::StmtMatch(_) - | AnyNodeRef::StmtRaise(_) - | AnyNodeRef::StmtTry(_) - | AnyNodeRef::StmtAssert(_) - | AnyNodeRef::StmtImport(_) - | AnyNodeRef::StmtImportFrom(_) - | AnyNodeRef::StmtGlobal(_) - | AnyNodeRef::StmtNonlocal(_) - | AnyNodeRef::StmtExpr(_) - | AnyNodeRef::StmtPass(_) - | AnyNodeRef::StmtBreak(_) - | AnyNodeRef::StmtContinue(_) - | AnyNodeRef::StmtIpyEscapeCommand(_) - | AnyNodeRef::ExprBoolOp(_) - | AnyNodeRef::ExprNamed(_) - | AnyNodeRef::ExprBinOp(_) - | AnyNodeRef::ExprUnaryOp(_) - | AnyNodeRef::ExprLambda(_) - | AnyNodeRef::ExprIf(_) - | AnyNodeRef::ExprDict(_) - | AnyNodeRef::ExprSet(_) - | AnyNodeRef::ExprListComp(_) - | AnyNodeRef::ExprSetComp(_) - | AnyNodeRef::ExprDictComp(_) - | AnyNodeRef::ExprGenerator(_) - | AnyNodeRef::ExprAwait(_) - | AnyNodeRef::ExprYield(_) - | AnyNodeRef::ExprYieldFrom(_) - | AnyNodeRef::ExprCompare(_) - | AnyNodeRef::ExprCall(_) - | AnyNodeRef::FStringExpressionElement(_) - | AnyNodeRef::FStringLiteralElement(_) - | AnyNodeRef::FStringFormatSpec(_) - | AnyNodeRef::ExprFString(_) - | AnyNodeRef::ExprStringLiteral(_) - | AnyNodeRef::ExprBytesLiteral(_) - | AnyNodeRef::ExprNumberLiteral(_) - | AnyNodeRef::ExprBooleanLiteral(_) - | AnyNodeRef::ExprNoneLiteral(_) - | AnyNodeRef::ExprEllipsisLiteral(_) - | AnyNodeRef::ExprAttribute(_) - | AnyNodeRef::ExprSubscript(_) - | AnyNodeRef::ExprStarred(_) - | AnyNodeRef::ExprName(_) - | AnyNodeRef::ExprList(_) - | AnyNodeRef::ExprTuple(_) - | AnyNodeRef::ExprSlice(_) - | AnyNodeRef::ExprIpyEscapeCommand(_) - | AnyNodeRef::PatternMatchValue(_) - | AnyNodeRef::PatternMatchSingleton(_) - | AnyNodeRef::PatternMatchSequence(_) - | AnyNodeRef::PatternMatchMapping(_) - | AnyNodeRef::PatternMatchClass(_) - | AnyNodeRef::PatternMatchStar(_) - | AnyNodeRef::PatternMatchAs(_) - | AnyNodeRef::PatternMatchOr(_) - | AnyNodeRef::PatternArguments(_) - | AnyNodeRef::PatternKeyword(_) - | AnyNodeRef::Comprehension(_) - | AnyNodeRef::Arguments(_) - | AnyNodeRef::Parameters(_) - | AnyNodeRef::Parameter(_) - | AnyNodeRef::ParameterWithDefault(_) - | AnyNodeRef::Keyword(_) - | AnyNodeRef::Alias(_) - | AnyNodeRef::WithItem(_) - | AnyNodeRef::MatchCase(_) - | AnyNodeRef::Decorator(_) - | AnyNodeRef::TypeParams(_) - | AnyNodeRef::TypeParamTypeVar(_) - | AnyNodeRef::TypeParamTypeVarTuple(_) - | AnyNodeRef::TypeParamParamSpec(_) - | AnyNodeRef::FString(_) - | AnyNodeRef::StringLiteral(_) - | AnyNodeRef::BytesLiteral(_) - | AnyNodeRef::Identifier(_) - | AnyNodeRef::ElifElseClause(_) => false, - } - } - - /// In our AST, only some alternative branches are represented as a node. This has historical - /// reasons, e.g. we added a node for elif/else in if statements which was not originally - /// present in the parser. - pub const fn is_alternative_branch_with_node(self) -> bool { - matches!( - self, - AnyNodeRef::ExceptHandlerExceptHandler(_) | AnyNodeRef::ElifElseClause(_) - ) - } - - pub fn visit_preorder<'b, V>(self, visitor: &mut V) - where - V: SourceOrderVisitor<'b> + ?Sized, - 'a: 'b, - { - match self { - AnyNodeRef::ModModule(node) => node.visit_source_order(visitor), - AnyNodeRef::ModExpression(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtFunctionDef(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtClassDef(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtReturn(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtDelete(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtTypeAlias(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtAssign(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtAugAssign(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtAnnAssign(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtFor(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtWhile(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtIf(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtWith(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtMatch(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtRaise(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtTry(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtAssert(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtImport(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtImportFrom(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtGlobal(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtNonlocal(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtExpr(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtPass(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtBreak(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtContinue(node) => node.visit_source_order(visitor), - AnyNodeRef::StmtIpyEscapeCommand(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprBoolOp(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprNamed(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprBinOp(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprUnaryOp(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprLambda(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprIf(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprDict(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprSet(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprListComp(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprSetComp(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprDictComp(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprGenerator(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprAwait(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprYield(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprYieldFrom(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprCompare(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprCall(node) => node.visit_source_order(visitor), - AnyNodeRef::FStringExpressionElement(node) => node.visit_source_order(visitor), - AnyNodeRef::FStringLiteralElement(node) => node.visit_source_order(visitor), - AnyNodeRef::FStringFormatSpec(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprFString(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprStringLiteral(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprBytesLiteral(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprNumberLiteral(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprBooleanLiteral(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprNoneLiteral(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprEllipsisLiteral(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprAttribute(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprSubscript(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprStarred(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprName(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprList(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprTuple(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprSlice(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprIpyEscapeCommand(node) => node.visit_source_order(visitor), - AnyNodeRef::ExceptHandlerExceptHandler(node) => node.visit_source_order(visitor), - AnyNodeRef::PatternMatchValue(node) => node.visit_source_order(visitor), - AnyNodeRef::PatternMatchSingleton(node) => node.visit_source_order(visitor), - AnyNodeRef::PatternMatchSequence(node) => node.visit_source_order(visitor), - AnyNodeRef::PatternMatchMapping(node) => node.visit_source_order(visitor), - AnyNodeRef::PatternMatchClass(node) => node.visit_source_order(visitor), - AnyNodeRef::PatternMatchStar(node) => node.visit_source_order(visitor), - AnyNodeRef::PatternMatchAs(node) => node.visit_source_order(visitor), - AnyNodeRef::PatternMatchOr(node) => node.visit_source_order(visitor), - AnyNodeRef::PatternArguments(node) => node.visit_source_order(visitor), - AnyNodeRef::PatternKeyword(node) => node.visit_source_order(visitor), - AnyNodeRef::Comprehension(node) => node.visit_source_order(visitor), - AnyNodeRef::Arguments(node) => node.visit_source_order(visitor), - AnyNodeRef::Parameters(node) => node.visit_source_order(visitor), - AnyNodeRef::Parameter(node) => node.visit_source_order(visitor), - AnyNodeRef::ParameterWithDefault(node) => node.visit_source_order(visitor), - AnyNodeRef::Keyword(node) => node.visit_source_order(visitor), - AnyNodeRef::Alias(node) => node.visit_source_order(visitor), - AnyNodeRef::WithItem(node) => node.visit_source_order(visitor), - AnyNodeRef::MatchCase(node) => node.visit_source_order(visitor), - AnyNodeRef::Decorator(node) => node.visit_source_order(visitor), - AnyNodeRef::TypeParams(node) => node.visit_source_order(visitor), - AnyNodeRef::TypeParamTypeVar(node) => node.visit_source_order(visitor), - AnyNodeRef::TypeParamTypeVarTuple(node) => node.visit_source_order(visitor), - AnyNodeRef::TypeParamParamSpec(node) => node.visit_source_order(visitor), - AnyNodeRef::FString(node) => node.visit_source_order(visitor), - AnyNodeRef::StringLiteral(node) => node.visit_source_order(visitor), - AnyNodeRef::BytesLiteral(node) => node.visit_source_order(visitor), - AnyNodeRef::ElifElseClause(node) => node.visit_source_order(visitor), - AnyNodeRef::Identifier(node) => node.visit_source_order(visitor), - } - } - - /// The last child of the last branch, if the node has multiple branches. - pub fn last_child_in_body(&self) -> Option> { - let body = match self { - AnyNodeRef::StmtFunctionDef(ast::StmtFunctionDef { body, .. }) - | AnyNodeRef::StmtClassDef(ast::StmtClassDef { body, .. }) - | AnyNodeRef::StmtWith(ast::StmtWith { body, .. }) - | AnyNodeRef::MatchCase(MatchCase { body, .. }) - | AnyNodeRef::ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler { - body, - .. - }) - | AnyNodeRef::ElifElseClause(ast::ElifElseClause { body, .. }) => body, - AnyNodeRef::StmtIf(ast::StmtIf { - body, - elif_else_clauses, - .. - }) => elif_else_clauses.last().map_or(body, |clause| &clause.body), - - AnyNodeRef::StmtFor(ast::StmtFor { body, orelse, .. }) - | AnyNodeRef::StmtWhile(ast::StmtWhile { body, orelse, .. }) => { - if orelse.is_empty() { - body - } else { - orelse - } - } - - AnyNodeRef::StmtMatch(ast::StmtMatch { cases, .. }) => { - return cases.last().map(AnyNodeRef::from); - } - - AnyNodeRef::StmtTry(ast::StmtTry { - body, - handlers, - orelse, - finalbody, - .. - }) => { - if finalbody.is_empty() { - if orelse.is_empty() { - if handlers.is_empty() { - body - } else { - return handlers.last().map(AnyNodeRef::from); - } - } else { - orelse - } - } else { - finalbody - } - } - - // Not a node that contains an indented child node. - _ => return None, - }; - - body.last().map(AnyNodeRef::from) - } - - /// Check if the given statement is the first statement after the colon of a branch, be it in if - /// statements, for statements, after each part of a try-except-else-finally or function/class - /// definitions. - /// - /// - /// ```python - /// if True: <- has body - /// a <- first statement - /// b - /// elif b: <- has body - /// c <- first statement - /// d - /// else: <- has body - /// e <- first statement - /// f - /// - /// class: <- has body - /// a: int <- first statement - /// b: int - /// - /// ``` - /// - /// For nodes with multiple bodies, we check all bodies that don't have their own node. For - /// try-except-else-finally, each except branch has it's own node, so for the `StmtTry`, we check - /// the `try:`, `else:` and `finally:`, bodies, while `ExceptHandlerExceptHandler` has it's own - /// check. For for-else and while-else, we check both branches for the whole statement. - /// - /// ```python - /// try: <- has body (a) - /// 6/8 <- first statement (a) - /// 1/0 - /// except: <- has body (b) - /// a <- first statement (b) - /// b - /// else: - /// c <- first statement (a) - /// d - /// finally: - /// e <- first statement (a) - /// f - /// ``` - pub fn is_first_statement_in_body(&self, body: AnyNodeRef) -> bool { - match body { - AnyNodeRef::StmtFor(ast::StmtFor { body, orelse, .. }) - | AnyNodeRef::StmtWhile(ast::StmtWhile { body, orelse, .. }) => { - are_same_optional(*self, body.first()) || are_same_optional(*self, orelse.first()) - } - - AnyNodeRef::StmtTry(ast::StmtTry { - body, - orelse, - finalbody, - .. - }) => { - are_same_optional(*self, body.first()) - || are_same_optional(*self, orelse.first()) - || are_same_optional(*self, finalbody.first()) - } - - AnyNodeRef::StmtIf(ast::StmtIf { body, .. }) - | AnyNodeRef::ElifElseClause(ast::ElifElseClause { body, .. }) - | AnyNodeRef::StmtWith(ast::StmtWith { body, .. }) - | AnyNodeRef::ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler { - body, - .. - }) - | AnyNodeRef::MatchCase(MatchCase { body, .. }) - | AnyNodeRef::StmtFunctionDef(ast::StmtFunctionDef { body, .. }) - | AnyNodeRef::StmtClassDef(ast::StmtClassDef { body, .. }) => { - are_same_optional(*self, body.first()) - } - - AnyNodeRef::StmtMatch(ast::StmtMatch { cases, .. }) => { - are_same_optional(*self, cases.first()) - } - - _ => false, - } - } - - /// Returns `true` if `statement` is the first statement in an alternate `body` (e.g. the else of an if statement) - pub fn is_first_statement_in_alternate_body(&self, body: AnyNodeRef) -> bool { - match body { - AnyNodeRef::StmtFor(ast::StmtFor { orelse, .. }) - | AnyNodeRef::StmtWhile(ast::StmtWhile { orelse, .. }) => { - are_same_optional(*self, orelse.first()) - } - - AnyNodeRef::StmtTry(ast::StmtTry { - handlers, - orelse, - finalbody, - .. - }) => { - are_same_optional(*self, handlers.first()) - || are_same_optional(*self, orelse.first()) - || are_same_optional(*self, finalbody.first()) - } - - AnyNodeRef::StmtIf(ast::StmtIf { - elif_else_clauses, .. - }) => are_same_optional(*self, elif_else_clauses.first()), - _ => false, - } - } -} - -/// Returns `true` if `right` is `Some` and `left` and `right` are referentially equal. -fn are_same_optional<'a, T>(left: AnyNodeRef, right: Option) -> bool -where - T: Into>, -{ - right.is_some_and(|right| left.ptr_eq(right.into())) -} - -impl<'a> From<&'a ast::ModModule> for AnyNodeRef<'a> { - fn from(node: &'a ast::ModModule) -> Self { - AnyNodeRef::ModModule(node) - } -} - -impl<'a> From<&'a ast::ModExpression> for AnyNodeRef<'a> { - fn from(node: &'a ast::ModExpression) -> Self { - AnyNodeRef::ModExpression(node) - } -} - -impl<'a> From<&'a ast::StmtFunctionDef> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtFunctionDef) -> Self { - AnyNodeRef::StmtFunctionDef(node) - } -} - -impl<'a> From<&'a ast::StmtClassDef> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtClassDef) -> Self { - AnyNodeRef::StmtClassDef(node) - } -} - -impl<'a> From<&'a ast::StmtReturn> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtReturn) -> Self { - AnyNodeRef::StmtReturn(node) - } -} - -impl<'a> From<&'a ast::StmtDelete> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtDelete) -> Self { - AnyNodeRef::StmtDelete(node) - } -} - -impl<'a> From<&'a ast::StmtTypeAlias> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtTypeAlias) -> Self { - AnyNodeRef::StmtTypeAlias(node) - } -} - -impl<'a> From<&'a ast::StmtAssign> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtAssign) -> Self { - AnyNodeRef::StmtAssign(node) - } -} - -impl<'a> From<&'a ast::StmtAugAssign> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtAugAssign) -> Self { - AnyNodeRef::StmtAugAssign(node) - } -} - -impl<'a> From<&'a ast::StmtAnnAssign> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtAnnAssign) -> Self { - AnyNodeRef::StmtAnnAssign(node) - } -} - -impl<'a> From<&'a ast::StmtFor> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtFor) -> Self { - AnyNodeRef::StmtFor(node) - } -} - -impl<'a> From<&'a ast::StmtWhile> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtWhile) -> Self { - AnyNodeRef::StmtWhile(node) - } -} - -impl<'a> From<&'a ast::StmtIf> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtIf) -> Self { - AnyNodeRef::StmtIf(node) - } -} - -impl<'a> From<&'a ast::ElifElseClause> for AnyNodeRef<'a> { - fn from(node: &'a ast::ElifElseClause) -> Self { - AnyNodeRef::ElifElseClause(node) - } -} - -impl<'a> From<&'a ast::StmtWith> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtWith) -> Self { - AnyNodeRef::StmtWith(node) - } -} - -impl<'a> From<&'a ast::StmtMatch> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtMatch) -> Self { - AnyNodeRef::StmtMatch(node) - } -} - -impl<'a> From<&'a ast::StmtRaise> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtRaise) -> Self { - AnyNodeRef::StmtRaise(node) - } -} - -impl<'a> From<&'a ast::StmtTry> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtTry) -> Self { - AnyNodeRef::StmtTry(node) - } -} - -impl<'a> From<&'a ast::StmtAssert> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtAssert) -> Self { - AnyNodeRef::StmtAssert(node) - } -} - -impl<'a> From<&'a ast::StmtImport> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtImport) -> Self { - AnyNodeRef::StmtImport(node) - } -} - -impl<'a> From<&'a ast::StmtImportFrom> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtImportFrom) -> Self { - AnyNodeRef::StmtImportFrom(node) - } -} - -impl<'a> From<&'a ast::StmtGlobal> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtGlobal) -> Self { - AnyNodeRef::StmtGlobal(node) - } -} - -impl<'a> From<&'a ast::StmtNonlocal> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtNonlocal) -> Self { - AnyNodeRef::StmtNonlocal(node) - } -} - -impl<'a> From<&'a ast::StmtExpr> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtExpr) -> Self { - AnyNodeRef::StmtExpr(node) - } -} - -impl<'a> From<&'a ast::StmtPass> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtPass) -> Self { - AnyNodeRef::StmtPass(node) - } -} - -impl<'a> From<&'a ast::StmtBreak> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtBreak) -> Self { - AnyNodeRef::StmtBreak(node) - } -} - -impl<'a> From<&'a ast::StmtContinue> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtContinue) -> Self { - AnyNodeRef::StmtContinue(node) - } -} - -impl<'a> From<&'a ast::StmtIpyEscapeCommand> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtIpyEscapeCommand) -> Self { - AnyNodeRef::StmtIpyEscapeCommand(node) - } -} - -impl<'a> From<&'a ast::ExprBoolOp> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprBoolOp) -> Self { - AnyNodeRef::ExprBoolOp(node) - } -} - -impl<'a> From<&'a ast::ExprNamed> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprNamed) -> Self { - AnyNodeRef::ExprNamed(node) - } -} - -impl<'a> From<&'a ast::ExprBinOp> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprBinOp) -> Self { - AnyNodeRef::ExprBinOp(node) - } -} - -impl<'a> From<&'a ast::ExprUnaryOp> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprUnaryOp) -> Self { - AnyNodeRef::ExprUnaryOp(node) - } -} - -impl<'a> From<&'a ast::ExprLambda> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprLambda) -> Self { - AnyNodeRef::ExprLambda(node) - } -} - -impl<'a> From<&'a ast::ExprIf> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprIf) -> Self { - AnyNodeRef::ExprIf(node) - } -} - -impl<'a> From<&'a ast::ExprDict> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprDict) -> Self { - AnyNodeRef::ExprDict(node) - } -} - -impl<'a> From<&'a ast::ExprSet> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprSet) -> Self { - AnyNodeRef::ExprSet(node) - } -} - -impl<'a> From<&'a ast::ExprListComp> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprListComp) -> Self { - AnyNodeRef::ExprListComp(node) - } -} - -impl<'a> From<&'a ast::ExprSetComp> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprSetComp) -> Self { - AnyNodeRef::ExprSetComp(node) - } -} - -impl<'a> From<&'a ast::ExprDictComp> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprDictComp) -> Self { - AnyNodeRef::ExprDictComp(node) - } -} - -impl<'a> From<&'a ast::ExprGenerator> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprGenerator) -> Self { - AnyNodeRef::ExprGenerator(node) - } -} - -impl<'a> From<&'a ast::ExprAwait> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprAwait) -> Self { - AnyNodeRef::ExprAwait(node) - } -} - -impl<'a> From<&'a ast::ExprYield> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprYield) -> Self { - AnyNodeRef::ExprYield(node) - } -} - -impl<'a> From<&'a ast::ExprYieldFrom> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprYieldFrom) -> Self { - AnyNodeRef::ExprYieldFrom(node) - } -} - -impl<'a> From<&'a ast::ExprCompare> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprCompare) -> Self { - AnyNodeRef::ExprCompare(node) - } -} - -impl<'a> From<&'a ast::ExprCall> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprCall) -> Self { - AnyNodeRef::ExprCall(node) - } -} - -impl<'a> From<&'a ast::FStringExpressionElement> for AnyNodeRef<'a> { - fn from(node: &'a ast::FStringExpressionElement) -> Self { - AnyNodeRef::FStringExpressionElement(node) - } -} - -impl<'a> From<&'a ast::FStringLiteralElement> for AnyNodeRef<'a> { - fn from(node: &'a ast::FStringLiteralElement) -> Self { - AnyNodeRef::FStringLiteralElement(node) - } -} - -impl<'a> From<&'a ast::FStringFormatSpec> for AnyNodeRef<'a> { - fn from(node: &'a ast::FStringFormatSpec) -> Self { - AnyNodeRef::FStringFormatSpec(node) - } -} - -impl<'a> From<&'a ast::ExprFString> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprFString) -> Self { - AnyNodeRef::ExprFString(node) - } -} - -impl<'a> From<&'a ast::ExprStringLiteral> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprStringLiteral) -> Self { - AnyNodeRef::ExprStringLiteral(node) - } -} - -impl<'a> From<&'a ast::ExprBytesLiteral> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprBytesLiteral) -> Self { - AnyNodeRef::ExprBytesLiteral(node) - } -} - -impl<'a> From<&'a ast::ExprNumberLiteral> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprNumberLiteral) -> Self { - AnyNodeRef::ExprNumberLiteral(node) - } -} - -impl<'a> From<&'a ast::ExprBooleanLiteral> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprBooleanLiteral) -> Self { - AnyNodeRef::ExprBooleanLiteral(node) - } -} - -impl<'a> From<&'a ast::ExprNoneLiteral> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprNoneLiteral) -> Self { - AnyNodeRef::ExprNoneLiteral(node) - } -} - -impl<'a> From<&'a ast::ExprEllipsisLiteral> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprEllipsisLiteral) -> Self { - AnyNodeRef::ExprEllipsisLiteral(node) - } -} - -impl<'a> From<&'a ast::ExprAttribute> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprAttribute) -> Self { - AnyNodeRef::ExprAttribute(node) - } -} - -impl<'a> From<&'a ast::ExprSubscript> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprSubscript) -> Self { - AnyNodeRef::ExprSubscript(node) - } -} - -impl<'a> From<&'a ast::ExprStarred> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprStarred) -> Self { - AnyNodeRef::ExprStarred(node) - } -} - -impl<'a> From<&'a ast::ExprName> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprName) -> Self { - AnyNodeRef::ExprName(node) - } -} - -impl<'a> From<&'a ast::ExprList> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprList) -> Self { - AnyNodeRef::ExprList(node) - } -} - -impl<'a> From<&'a ast::ExprTuple> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprTuple) -> Self { - AnyNodeRef::ExprTuple(node) - } -} - -impl<'a> From<&'a ast::ExprSlice> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprSlice) -> Self { - AnyNodeRef::ExprSlice(node) - } -} - -impl<'a> From<&'a ast::ExprIpyEscapeCommand> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprIpyEscapeCommand) -> Self { - AnyNodeRef::ExprIpyEscapeCommand(node) - } -} - -impl<'a> From<&'a ast::ExceptHandlerExceptHandler> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExceptHandlerExceptHandler) -> Self { - AnyNodeRef::ExceptHandlerExceptHandler(node) - } -} - -impl<'a> From<&'a ast::PatternMatchValue> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchValue) -> Self { - AnyNodeRef::PatternMatchValue(node) - } -} - -impl<'a> From<&'a ast::PatternMatchSingleton> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchSingleton) -> Self { - AnyNodeRef::PatternMatchSingleton(node) - } -} - -impl<'a> From<&'a ast::PatternMatchSequence> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchSequence) -> Self { - AnyNodeRef::PatternMatchSequence(node) - } -} - -impl<'a> From<&'a ast::PatternMatchMapping> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchMapping) -> Self { - AnyNodeRef::PatternMatchMapping(node) - } -} - -impl<'a> From<&'a ast::PatternMatchClass> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchClass) -> Self { - AnyNodeRef::PatternMatchClass(node) - } -} - -impl<'a> From<&'a ast::PatternMatchStar> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchStar) -> Self { - AnyNodeRef::PatternMatchStar(node) - } -} - -impl<'a> From<&'a ast::PatternMatchAs> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchAs) -> Self { - AnyNodeRef::PatternMatchAs(node) - } -} - -impl<'a> From<&'a ast::PatternMatchOr> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchOr) -> Self { - AnyNodeRef::PatternMatchOr(node) - } -} - -impl<'a> From<&'a ast::PatternArguments> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternArguments) -> Self { - AnyNodeRef::PatternArguments(node) - } -} - -impl<'a> From<&'a ast::PatternKeyword> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternKeyword) -> Self { - AnyNodeRef::PatternKeyword(node) - } -} - -impl<'a> From<&'a Decorator> for AnyNodeRef<'a> { - fn from(node: &'a Decorator) -> Self { - AnyNodeRef::Decorator(node) - } -} - -impl<'a> From<&'a ast::TypeParams> for AnyNodeRef<'a> { - fn from(node: &'a ast::TypeParams) -> Self { - AnyNodeRef::TypeParams(node) - } -} -impl<'a> From<&'a TypeParamTypeVar> for AnyNodeRef<'a> { - fn from(node: &'a TypeParamTypeVar) -> Self { - AnyNodeRef::TypeParamTypeVar(node) - } -} - -impl<'a> From<&'a TypeParamTypeVarTuple> for AnyNodeRef<'a> { - fn from(node: &'a TypeParamTypeVarTuple) -> Self { - AnyNodeRef::TypeParamTypeVarTuple(node) - } -} - -impl<'a> From<&'a TypeParamParamSpec> for AnyNodeRef<'a> { - fn from(node: &'a TypeParamParamSpec) -> Self { - AnyNodeRef::TypeParamParamSpec(node) - } -} - -impl<'a> From<&'a ast::FString> for AnyNodeRef<'a> { - fn from(node: &'a ast::FString) -> Self { - AnyNodeRef::FString(node) - } -} - -impl<'a> From<&'a ast::StringLiteral> for AnyNodeRef<'a> { - fn from(node: &'a ast::StringLiteral) -> Self { - AnyNodeRef::StringLiteral(node) - } -} - -impl<'a> From<&'a ast::BytesLiteral> for AnyNodeRef<'a> { - fn from(node: &'a ast::BytesLiteral) -> Self { - AnyNodeRef::BytesLiteral(node) - } -} - -impl<'a> From<&'a Stmt> for AnyNodeRef<'a> { - fn from(stmt: &'a Stmt) -> Self { - match stmt { - Stmt::FunctionDef(node) => AnyNodeRef::StmtFunctionDef(node), - Stmt::ClassDef(node) => AnyNodeRef::StmtClassDef(node), - Stmt::Return(node) => AnyNodeRef::StmtReturn(node), - Stmt::Delete(node) => AnyNodeRef::StmtDelete(node), - Stmt::TypeAlias(node) => AnyNodeRef::StmtTypeAlias(node), - Stmt::Assign(node) => AnyNodeRef::StmtAssign(node), - Stmt::AugAssign(node) => AnyNodeRef::StmtAugAssign(node), - Stmt::AnnAssign(node) => AnyNodeRef::StmtAnnAssign(node), - Stmt::For(node) => AnyNodeRef::StmtFor(node), - Stmt::While(node) => AnyNodeRef::StmtWhile(node), - Stmt::If(node) => AnyNodeRef::StmtIf(node), - Stmt::With(node) => AnyNodeRef::StmtWith(node), - Stmt::Match(node) => AnyNodeRef::StmtMatch(node), - Stmt::Raise(node) => AnyNodeRef::StmtRaise(node), - Stmt::Try(node) => AnyNodeRef::StmtTry(node), - Stmt::Assert(node) => AnyNodeRef::StmtAssert(node), - Stmt::Import(node) => AnyNodeRef::StmtImport(node), - Stmt::ImportFrom(node) => AnyNodeRef::StmtImportFrom(node), - Stmt::Global(node) => AnyNodeRef::StmtGlobal(node), - Stmt::Nonlocal(node) => AnyNodeRef::StmtNonlocal(node), - Stmt::Expr(node) => AnyNodeRef::StmtExpr(node), - Stmt::Pass(node) => AnyNodeRef::StmtPass(node), - Stmt::Break(node) => AnyNodeRef::StmtBreak(node), - Stmt::Continue(node) => AnyNodeRef::StmtContinue(node), - Stmt::IpyEscapeCommand(node) => AnyNodeRef::StmtIpyEscapeCommand(node), - } - } -} - -impl<'a> From<&'a Expr> for AnyNodeRef<'a> { - fn from(expr: &'a Expr) -> Self { - match expr { - Expr::BoolOp(node) => AnyNodeRef::ExprBoolOp(node), - Expr::Named(node) => AnyNodeRef::ExprNamed(node), - Expr::BinOp(node) => AnyNodeRef::ExprBinOp(node), - Expr::UnaryOp(node) => AnyNodeRef::ExprUnaryOp(node), - Expr::Lambda(node) => AnyNodeRef::ExprLambda(node), - Expr::If(node) => AnyNodeRef::ExprIf(node), - Expr::Dict(node) => AnyNodeRef::ExprDict(node), - Expr::Set(node) => AnyNodeRef::ExprSet(node), - Expr::ListComp(node) => AnyNodeRef::ExprListComp(node), - Expr::SetComp(node) => AnyNodeRef::ExprSetComp(node), - Expr::DictComp(node) => AnyNodeRef::ExprDictComp(node), - Expr::Generator(node) => AnyNodeRef::ExprGenerator(node), - Expr::Await(node) => AnyNodeRef::ExprAwait(node), - Expr::Yield(node) => AnyNodeRef::ExprYield(node), - Expr::YieldFrom(node) => AnyNodeRef::ExprYieldFrom(node), - Expr::Compare(node) => AnyNodeRef::ExprCompare(node), - Expr::Call(node) => AnyNodeRef::ExprCall(node), - Expr::FString(node) => AnyNodeRef::ExprFString(node), - Expr::StringLiteral(node) => AnyNodeRef::ExprStringLiteral(node), - Expr::BytesLiteral(node) => AnyNodeRef::ExprBytesLiteral(node), - Expr::NumberLiteral(node) => AnyNodeRef::ExprNumberLiteral(node), - Expr::BooleanLiteral(node) => AnyNodeRef::ExprBooleanLiteral(node), - Expr::NoneLiteral(node) => AnyNodeRef::ExprNoneLiteral(node), - Expr::EllipsisLiteral(node) => AnyNodeRef::ExprEllipsisLiteral(node), - Expr::Attribute(node) => AnyNodeRef::ExprAttribute(node), - Expr::Subscript(node) => AnyNodeRef::ExprSubscript(node), - Expr::Starred(node) => AnyNodeRef::ExprStarred(node), - Expr::Name(node) => AnyNodeRef::ExprName(node), - Expr::List(node) => AnyNodeRef::ExprList(node), - Expr::Tuple(node) => AnyNodeRef::ExprTuple(node), - Expr::Slice(node) => AnyNodeRef::ExprSlice(node), - Expr::IpyEscapeCommand(node) => AnyNodeRef::ExprIpyEscapeCommand(node), - } - } -} - -impl<'a> From<&'a Mod> for AnyNodeRef<'a> { - fn from(module: &'a Mod) -> Self { - match module { - Mod::Module(node) => AnyNodeRef::ModModule(node), - Mod::Expression(node) => AnyNodeRef::ModExpression(node), - } - } -} - -impl<'a> From<&'a FStringElement> for AnyNodeRef<'a> { - fn from(element: &'a FStringElement) -> Self { - match element { - FStringElement::Expression(node) => AnyNodeRef::FStringExpressionElement(node), - FStringElement::Literal(node) => AnyNodeRef::FStringLiteralElement(node), - } - } -} - -impl<'a> From<&'a Pattern> for AnyNodeRef<'a> { - fn from(pattern: &'a Pattern) -> Self { - match pattern { - Pattern::MatchValue(node) => AnyNodeRef::PatternMatchValue(node), - Pattern::MatchSingleton(node) => AnyNodeRef::PatternMatchSingleton(node), - Pattern::MatchSequence(node) => AnyNodeRef::PatternMatchSequence(node), - Pattern::MatchMapping(node) => AnyNodeRef::PatternMatchMapping(node), - Pattern::MatchClass(node) => AnyNodeRef::PatternMatchClass(node), - Pattern::MatchStar(node) => AnyNodeRef::PatternMatchStar(node), - Pattern::MatchAs(node) => AnyNodeRef::PatternMatchAs(node), - Pattern::MatchOr(node) => AnyNodeRef::PatternMatchOr(node), - } - } -} - -impl<'a> From<&'a TypeParam> for AnyNodeRef<'a> { - fn from(type_param: &'a TypeParam) -> Self { - match type_param { - TypeParam::TypeVar(node) => AnyNodeRef::TypeParamTypeVar(node), - TypeParam::TypeVarTuple(node) => AnyNodeRef::TypeParamTypeVarTuple(node), - TypeParam::ParamSpec(node) => AnyNodeRef::TypeParamParamSpec(node), - } - } -} - -impl<'a> From<&'a ExceptHandler> for AnyNodeRef<'a> { - fn from(handler: &'a ExceptHandler) -> Self { - match handler { - ExceptHandler::ExceptHandler(handler) => { - AnyNodeRef::ExceptHandlerExceptHandler(handler) + /// Returns `true` if `statement` is the first statement in an alternate `body` (e.g. the else of an if statement) + pub fn is_first_statement_in_alternate_body(&self, body: AnyNodeRef) -> bool { + match body { + AnyNodeRef::StmtFor(ast::StmtFor { orelse, .. }) + | AnyNodeRef::StmtWhile(ast::StmtWhile { orelse, .. }) => { + are_same_optional(*self, orelse.first()) } - } - } -} - -impl<'a> From<&'a Comprehension> for AnyNodeRef<'a> { - fn from(node: &'a Comprehension) -> Self { - AnyNodeRef::Comprehension(node) - } -} -impl<'a> From<&'a Arguments> for AnyNodeRef<'a> { - fn from(node: &'a Arguments) -> Self { - AnyNodeRef::Arguments(node) - } -} -impl<'a> From<&'a Parameters> for AnyNodeRef<'a> { - fn from(node: &'a Parameters) -> Self { - AnyNodeRef::Parameters(node) - } -} -impl<'a> From<&'a Parameter> for AnyNodeRef<'a> { - fn from(node: &'a Parameter) -> Self { - AnyNodeRef::Parameter(node) - } -} -impl<'a> From<&'a ParameterWithDefault> for AnyNodeRef<'a> { - fn from(node: &'a ParameterWithDefault) -> Self { - AnyNodeRef::ParameterWithDefault(node) - } -} -impl<'a> From<&'a Keyword> for AnyNodeRef<'a> { - fn from(node: &'a Keyword) -> Self { - AnyNodeRef::Keyword(node) - } -} -impl<'a> From<&'a Alias> for AnyNodeRef<'a> { - fn from(node: &'a Alias) -> Self { - AnyNodeRef::Alias(node) - } -} -impl<'a> From<&'a WithItem> for AnyNodeRef<'a> { - fn from(node: &'a WithItem) -> Self { - AnyNodeRef::WithItem(node) - } -} -impl<'a> From<&'a MatchCase> for AnyNodeRef<'a> { - fn from(node: &'a MatchCase) -> Self { - AnyNodeRef::MatchCase(node) - } -} -impl<'a> From<&'a ast::Identifier> for AnyNodeRef<'a> { - fn from(node: &'a ast::Identifier) -> Self { - AnyNodeRef::Identifier(node) - } -} - -impl Ranged for AnyNodeRef<'_> { - fn range(&self) -> TextRange { - match self { - AnyNodeRef::ModModule(node) => node.range(), - AnyNodeRef::ModExpression(node) => node.range(), - AnyNodeRef::StmtFunctionDef(node) => node.range(), - AnyNodeRef::StmtClassDef(node) => node.range(), - AnyNodeRef::StmtReturn(node) => node.range(), - AnyNodeRef::StmtDelete(node) => node.range(), - AnyNodeRef::StmtTypeAlias(node) => node.range(), - AnyNodeRef::StmtAssign(node) => node.range(), - AnyNodeRef::StmtAugAssign(node) => node.range(), - AnyNodeRef::StmtAnnAssign(node) => node.range(), - AnyNodeRef::StmtFor(node) => node.range(), - AnyNodeRef::StmtWhile(node) => node.range(), - AnyNodeRef::StmtIf(node) => node.range(), - AnyNodeRef::StmtWith(node) => node.range(), - AnyNodeRef::StmtMatch(node) => node.range(), - AnyNodeRef::StmtRaise(node) => node.range(), - AnyNodeRef::StmtTry(node) => node.range(), - AnyNodeRef::StmtAssert(node) => node.range(), - AnyNodeRef::StmtImport(node) => node.range(), - AnyNodeRef::StmtImportFrom(node) => node.range(), - AnyNodeRef::StmtGlobal(node) => node.range(), - AnyNodeRef::StmtNonlocal(node) => node.range(), - AnyNodeRef::StmtExpr(node) => node.range(), - AnyNodeRef::StmtPass(node) => node.range(), - AnyNodeRef::StmtBreak(node) => node.range(), - AnyNodeRef::StmtContinue(node) => node.range(), - AnyNodeRef::StmtIpyEscapeCommand(node) => node.range(), - AnyNodeRef::ExprBoolOp(node) => node.range(), - AnyNodeRef::ExprNamed(node) => node.range(), - AnyNodeRef::ExprBinOp(node) => node.range(), - AnyNodeRef::ExprUnaryOp(node) => node.range(), - AnyNodeRef::ExprLambda(node) => node.range(), - AnyNodeRef::ExprIf(node) => node.range(), - AnyNodeRef::ExprDict(node) => node.range(), - AnyNodeRef::ExprSet(node) => node.range(), - AnyNodeRef::ExprListComp(node) => node.range(), - AnyNodeRef::ExprSetComp(node) => node.range(), - AnyNodeRef::ExprDictComp(node) => node.range(), - AnyNodeRef::ExprGenerator(node) => node.range(), - AnyNodeRef::ExprAwait(node) => node.range(), - AnyNodeRef::ExprYield(node) => node.range(), - AnyNodeRef::ExprYieldFrom(node) => node.range(), - AnyNodeRef::ExprCompare(node) => node.range(), - AnyNodeRef::ExprCall(node) => node.range(), - AnyNodeRef::FStringExpressionElement(node) => node.range(), - AnyNodeRef::FStringLiteralElement(node) => node.range(), - AnyNodeRef::FStringFormatSpec(node) => node.range(), - AnyNodeRef::ExprFString(node) => node.range(), - AnyNodeRef::ExprStringLiteral(node) => node.range(), - AnyNodeRef::ExprBytesLiteral(node) => node.range(), - AnyNodeRef::ExprNumberLiteral(node) => node.range(), - AnyNodeRef::ExprBooleanLiteral(node) => node.range(), - AnyNodeRef::ExprNoneLiteral(node) => node.range(), - AnyNodeRef::ExprEllipsisLiteral(node) => node.range(), - AnyNodeRef::ExprAttribute(node) => node.range(), - AnyNodeRef::ExprSubscript(node) => node.range(), - AnyNodeRef::ExprStarred(node) => node.range(), - AnyNodeRef::ExprName(node) => node.range(), - AnyNodeRef::ExprList(node) => node.range(), - AnyNodeRef::ExprTuple(node) => node.range(), - AnyNodeRef::ExprSlice(node) => node.range(), - AnyNodeRef::ExprIpyEscapeCommand(node) => node.range(), - AnyNodeRef::ExceptHandlerExceptHandler(node) => node.range(), - AnyNodeRef::PatternMatchValue(node) => node.range(), - AnyNodeRef::PatternMatchSingleton(node) => node.range(), - AnyNodeRef::PatternMatchSequence(node) => node.range(), - AnyNodeRef::PatternMatchMapping(node) => node.range(), - AnyNodeRef::PatternMatchClass(node) => node.range(), - AnyNodeRef::PatternMatchStar(node) => node.range(), - AnyNodeRef::PatternMatchAs(node) => node.range(), - AnyNodeRef::PatternMatchOr(node) => node.range(), - AnyNodeRef::PatternArguments(node) => node.range(), - AnyNodeRef::PatternKeyword(node) => node.range(), - AnyNodeRef::Comprehension(node) => node.range(), - AnyNodeRef::Arguments(node) => node.range(), - AnyNodeRef::Parameters(node) => node.range(), - AnyNodeRef::Parameter(node) => node.range(), - AnyNodeRef::ParameterWithDefault(node) => node.range(), - AnyNodeRef::Keyword(node) => node.range(), - AnyNodeRef::Alias(node) => node.range(), - AnyNodeRef::WithItem(node) => node.range(), - AnyNodeRef::MatchCase(node) => node.range(), - AnyNodeRef::Decorator(node) => node.range(), - AnyNodeRef::ElifElseClause(node) => node.range(), - AnyNodeRef::TypeParams(node) => node.range(), - AnyNodeRef::TypeParamTypeVar(node) => node.range(), - AnyNodeRef::TypeParamTypeVarTuple(node) => node.range(), - AnyNodeRef::TypeParamParamSpec(node) => node.range(), - AnyNodeRef::FString(node) => node.range(), - AnyNodeRef::StringLiteral(node) => node.range(), - AnyNodeRef::BytesLiteral(node) => node.range(), - AnyNodeRef::Identifier(node) => node.range(), - } - } -} - -#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] -pub enum NodeKind { - ModModule, - ModInteractive, - ModExpression, - ModFunctionType, - StmtFunctionDef, - StmtClassDef, - StmtReturn, - StmtDelete, - StmtTypeAlias, - StmtAssign, - StmtAugAssign, - StmtAnnAssign, - StmtFor, - StmtWhile, - StmtIf, - StmtWith, - StmtMatch, - StmtRaise, - StmtTry, - StmtAssert, - StmtImport, - StmtImportFrom, - StmtGlobal, - StmtNonlocal, - StmtIpyEscapeCommand, - StmtExpr, - StmtPass, - StmtBreak, - StmtContinue, - ExprBoolOp, - ExprNamed, - ExprBinOp, - ExprUnaryOp, - ExprLambda, - ExprIf, - ExprDict, - ExprSet, - ExprListComp, - ExprSetComp, - ExprDictComp, - ExprGenerator, - ExprAwait, - ExprYield, - ExprYieldFrom, - ExprCompare, - ExprCall, - FStringExpressionElement, - FStringLiteralElement, - FStringFormatSpec, - ExprFString, - ExprStringLiteral, - ExprBytesLiteral, - ExprNumberLiteral, - ExprBooleanLiteral, - ExprNoneLiteral, - ExprEllipsisLiteral, - ExprAttribute, - ExprSubscript, - ExprStarred, - ExprName, - ExprList, - ExprTuple, - ExprSlice, - ExprIpyEscapeCommand, - ExceptHandlerExceptHandler, - PatternMatchValue, - PatternMatchSingleton, - PatternMatchSequence, - PatternMatchMapping, - PatternMatchClass, - PatternMatchStar, - PatternMatchAs, - PatternMatchOr, - PatternArguments, - PatternKeyword, - TypeIgnoreTypeIgnore, - Comprehension, - Arguments, - Parameters, - Parameter, - ParameterWithDefault, - Keyword, - Alias, - WithItem, - MatchCase, - Decorator, - ElifElseClause, - TypeParams, - TypeParamTypeVar, - TypeParamTypeVarTuple, - TypeParamParamSpec, - FString, - StringLiteral, - BytesLiteral, - Identifier, -} - -// FIXME: The `StatementRef` here allows us to implement `AstNode` for `Stmt` which otherwise wouldn't be possible -// because of the `cast_ref` method that needs to return a `&Stmt` for a specific statement node. -// Implementing `AstNode` for `Stmt` is desired to have `AstId.upcast` work where the Id then represents -// any `Stmt` instead of a specific statement. -// The existing solution "works" in the sense that `upcast` etc can be implemented. However, `StatementRef` -// doesn't implement `AstNode` itself and thus, can't be used as `AstNodeKey` or passed to query the `ast_id` (because that requires that the node implements `HasAstId` which extends `AstNode`). -// I don't know how a solution to this would look like but this isn't the first time where this problem has come up. -#[derive(Clone, Copy, Debug, PartialEq)] -pub enum StatementRef<'a> { - FunctionDef(&'a StmtFunctionDef), - ClassDef(&'a StmtClassDef), - Return(&'a StmtReturn), - Delete(&'a StmtDelete), - Assign(&'a StmtAssign), - AugAssign(&'a StmtAugAssign), - AnnAssign(&'a StmtAnnAssign), - TypeAlias(&'a StmtTypeAlias), - For(&'a StmtFor), - While(&'a StmtWhile), - If(&'a StmtIf), - With(&'a StmtWith), - Match(&'a StmtMatch), - Raise(&'a StmtRaise), - Try(&'a StmtTry), - Assert(&'a StmtAssert), - Import(&'a StmtImport), - ImportFrom(&'a StmtImportFrom), - Global(&'a StmtGlobal), - Nonlocal(&'a StmtNonlocal), - Expr(&'a StmtExpr), - Pass(&'a StmtPass), - Break(&'a StmtBreak), - Continue(&'a StmtContinue), - IpyEscapeCommand(&'a StmtIpyEscapeCommand), -} -impl<'a> From<&'a StmtFunctionDef> for StatementRef<'a> { - fn from(value: &'a StmtFunctionDef) -> Self { - Self::FunctionDef(value) - } -} -impl<'a> From<&'a StmtClassDef> for StatementRef<'a> { - fn from(value: &'a StmtClassDef) -> Self { - Self::ClassDef(value) - } -} -impl<'a> From<&'a StmtReturn> for StatementRef<'a> { - fn from(value: &'a StmtReturn) -> Self { - Self::Return(value) - } -} -impl<'a> From<&'a StmtDelete> for StatementRef<'a> { - fn from(value: &'a StmtDelete) -> Self { - Self::Delete(value) - } -} -impl<'a> From<&'a StmtAssign> for StatementRef<'a> { - fn from(value: &'a StmtAssign) -> Self { - Self::Assign(value) - } -} -impl<'a> From<&'a StmtAugAssign> for StatementRef<'a> { - fn from(value: &'a StmtAugAssign) -> Self { - Self::AugAssign(value) - } -} -impl<'a> From<&'a StmtAnnAssign> for StatementRef<'a> { - fn from(value: &'a StmtAnnAssign) -> Self { - Self::AnnAssign(value) - } -} -impl<'a> From<&'a StmtTypeAlias> for StatementRef<'a> { - fn from(value: &'a StmtTypeAlias) -> Self { - Self::TypeAlias(value) - } -} -impl<'a> From<&'a StmtFor> for StatementRef<'a> { - fn from(value: &'a StmtFor) -> Self { - Self::For(value) - } -} -impl<'a> From<&'a StmtWhile> for StatementRef<'a> { - fn from(value: &'a StmtWhile) -> Self { - Self::While(value) - } -} -impl<'a> From<&'a StmtIf> for StatementRef<'a> { - fn from(value: &'a StmtIf) -> Self { - Self::If(value) - } -} -impl<'a> From<&'a StmtWith> for StatementRef<'a> { - fn from(value: &'a StmtWith) -> Self { - Self::With(value) - } -} -impl<'a> From<&'a StmtMatch> for StatementRef<'a> { - fn from(value: &'a StmtMatch) -> Self { - Self::Match(value) - } -} -impl<'a> From<&'a StmtRaise> for StatementRef<'a> { - fn from(value: &'a StmtRaise) -> Self { - Self::Raise(value) - } -} -impl<'a> From<&'a StmtTry> for StatementRef<'a> { - fn from(value: &'a StmtTry) -> Self { - Self::Try(value) - } -} -impl<'a> From<&'a StmtAssert> for StatementRef<'a> { - fn from(value: &'a StmtAssert) -> Self { - Self::Assert(value) - } -} -impl<'a> From<&'a StmtImport> for StatementRef<'a> { - fn from(value: &'a StmtImport) -> Self { - Self::Import(value) - } -} -impl<'a> From<&'a StmtImportFrom> for StatementRef<'a> { - fn from(value: &'a StmtImportFrom) -> Self { - Self::ImportFrom(value) - } -} -impl<'a> From<&'a StmtGlobal> for StatementRef<'a> { - fn from(value: &'a StmtGlobal) -> Self { - Self::Global(value) - } -} -impl<'a> From<&'a StmtNonlocal> for StatementRef<'a> { - fn from(value: &'a StmtNonlocal) -> Self { - Self::Nonlocal(value) - } -} -impl<'a> From<&'a StmtExpr> for StatementRef<'a> { - fn from(value: &'a StmtExpr) -> Self { - Self::Expr(value) - } -} -impl<'a> From<&'a StmtPass> for StatementRef<'a> { - fn from(value: &'a StmtPass) -> Self { - Self::Pass(value) - } -} -impl<'a> From<&'a StmtBreak> for StatementRef<'a> { - fn from(value: &'a StmtBreak) -> Self { - Self::Break(value) - } -} -impl<'a> From<&'a StmtContinue> for StatementRef<'a> { - fn from(value: &'a StmtContinue) -> Self { - Self::Continue(value) - } -} -impl<'a> From<&'a StmtIpyEscapeCommand> for StatementRef<'a> { - fn from(value: &'a StmtIpyEscapeCommand) -> Self { - Self::IpyEscapeCommand(value) - } -} + AnyNodeRef::StmtTry(ast::StmtTry { + handlers, + orelse, + finalbody, + .. + }) => { + are_same_optional(*self, handlers.first()) + || are_same_optional(*self, orelse.first()) + || are_same_optional(*self, finalbody.first()) + } -impl<'a> From<&'a Stmt> for StatementRef<'a> { - fn from(value: &'a Stmt) -> Self { - match value { - Stmt::FunctionDef(statement) => Self::FunctionDef(statement), - Stmt::ClassDef(statement) => Self::ClassDef(statement), - Stmt::Return(statement) => Self::Return(statement), - Stmt::Delete(statement) => Self::Delete(statement), - Stmt::Assign(statement) => Self::Assign(statement), - Stmt::AugAssign(statement) => Self::AugAssign(statement), - Stmt::AnnAssign(statement) => Self::AnnAssign(statement), - Stmt::TypeAlias(statement) => Self::TypeAlias(statement), - Stmt::For(statement) => Self::For(statement), - Stmt::While(statement) => Self::While(statement), - Stmt::If(statement) => Self::If(statement), - Stmt::With(statement) => Self::With(statement), - Stmt::Match(statement) => Self::Match(statement), - Stmt::Raise(statement) => Self::Raise(statement), - Stmt::Try(statement) => Self::Try(statement), - Stmt::Assert(statement) => Self::Assert(statement), - Stmt::Import(statement) => Self::Import(statement), - Stmt::ImportFrom(statement) => Self::ImportFrom(statement), - Stmt::Global(statement) => Self::Global(statement), - Stmt::Nonlocal(statement) => Self::Nonlocal(statement), - Stmt::Expr(statement) => Self::Expr(statement), - Stmt::Pass(statement) => Self::Pass(statement), - Stmt::Break(statement) => Self::Break(statement), - Stmt::Continue(statement) => Self::Continue(statement), - Stmt::IpyEscapeCommand(statement) => Self::IpyEscapeCommand(statement), + AnyNodeRef::StmtIf(ast::StmtIf { + elif_else_clauses, .. + }) => are_same_optional(*self, elif_else_clauses.first()), + _ => false, } } } -#[derive(Copy, Clone, Debug, PartialEq)] -pub enum TypeParamRef<'a> { - TypeVar(&'a TypeParamTypeVar), - ParamSpec(&'a TypeParamParamSpec), - TypeVarTuple(&'a TypeParamTypeVarTuple), -} - -impl<'a> From<&'a TypeParamTypeVar> for TypeParamRef<'a> { - fn from(value: &'a TypeParamTypeVar) -> Self { - Self::TypeVar(value) - } -} - -impl<'a> From<&'a TypeParamParamSpec> for TypeParamRef<'a> { - fn from(value: &'a TypeParamParamSpec) -> Self { - Self::ParamSpec(value) - } -} - -impl<'a> From<&'a TypeParamTypeVarTuple> for TypeParamRef<'a> { - fn from(value: &'a TypeParamTypeVarTuple) -> Self { - Self::TypeVarTuple(value) - } -} - -impl<'a> From<&'a TypeParam> for TypeParamRef<'a> { - fn from(value: &'a TypeParam) -> Self { - match value { - TypeParam::TypeVar(value) => Self::TypeVar(value), - TypeParam::ParamSpec(value) => Self::ParamSpec(value), - TypeParam::TypeVarTuple(value) => Self::TypeVarTuple(value), - } - } +/// Returns `true` if `right` is `Some` and `left` and `right` are referentially equal. +fn are_same_optional<'a, T>(left: AnyNodeRef, right: Option) -> bool +where + T: Into>, +{ + right.is_some_and(|right| left.ptr_eq(right.into())) } diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index 183803a3baf20..ac779c56a6cea 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -18,16 +18,9 @@ use crate::{ int, str::Quote, str_prefix::{AnyStringPrefix, ByteStringPrefix, FStringPrefix, StringLiteralPrefix}, - LiteralExpressionRef, + ExceptHandler, Expr, FStringElement, LiteralExpressionRef, Pattern, Stmt, TypeParam, }; -/// See also [mod](https://docs.python.org/3/library/ast.html#ast.mod) -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum Mod { - Module(ModModule), - Expression(ModExpression), -} - /// See also [Module](https://docs.python.org/3/library/ast.html#ast.Module) #[derive(Clone, Debug, PartialEq)] pub struct ModModule { @@ -35,12 +28,6 @@ pub struct ModModule { pub body: Vec, } -impl From for Mod { - fn from(payload: ModModule) -> Self { - Mod::Module(payload) - } -} - /// See also [Expression](https://docs.python.org/3/library/ast.html#ast.Expression) #[derive(Clone, Debug, PartialEq)] pub struct ModExpression { @@ -48,69 +35,6 @@ pub struct ModExpression { pub body: Box, } -impl From for Mod { - fn from(payload: ModExpression) -> Self { - Mod::Expression(payload) - } -} - -/// See also [stmt](https://docs.python.org/3/library/ast.html#ast.stmt) -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum Stmt { - #[is(name = "function_def_stmt")] - FunctionDef(StmtFunctionDef), - #[is(name = "class_def_stmt")] - ClassDef(StmtClassDef), - #[is(name = "return_stmt")] - Return(StmtReturn), - #[is(name = "delete_stmt")] - Delete(StmtDelete), - #[is(name = "assign_stmt")] - Assign(StmtAssign), - #[is(name = "aug_assign_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 = "while_stmt")] - While(StmtWhile), - #[is(name = "if_stmt")] - If(StmtIf), - #[is(name = "with_stmt")] - With(StmtWith), - #[is(name = "match_stmt")] - Match(StmtMatch), - #[is(name = "raise_stmt")] - Raise(StmtRaise), - #[is(name = "try_stmt")] - Try(StmtTry), - #[is(name = "assert_stmt")] - Assert(StmtAssert), - #[is(name = "import_stmt")] - Import(StmtImport), - #[is(name = "import_from_stmt")] - ImportFrom(StmtImportFrom), - #[is(name = "global_stmt")] - Global(StmtGlobal), - #[is(name = "nonlocal_stmt")] - Nonlocal(StmtNonlocal), - #[is(name = "expr_stmt")] - Expr(StmtExpr), - #[is(name = "pass_stmt")] - Pass(StmtPass), - #[is(name = "break_stmt")] - Break(StmtBreak), - #[is(name = "continue_stmt")] - Continue(StmtContinue), - - // Jupyter notebook specific - #[is(name = "ipy_escape_command_stmt")] - IpyEscapeCommand(StmtIpyEscapeCommand), -} - /// An AST node used to represent a IPython escape command at the statement level. /// /// For example, @@ -171,12 +95,6 @@ pub struct StmtIpyEscapeCommand { pub value: Box, } -impl From for Stmt { - fn from(payload: StmtIpyEscapeCommand) -> Self { - Stmt::IpyEscapeCommand(payload) - } -} - /// See also [FunctionDef](https://docs.python.org/3/library/ast.html#ast.FunctionDef) and /// [AsyncFunctionDef](https://docs.python.org/3/library/ast.html#ast.AsyncFunctionDef). /// @@ -194,12 +112,6 @@ pub struct StmtFunctionDef { pub body: Vec, } -impl From for Stmt { - fn from(payload: StmtFunctionDef) -> Self { - Stmt::FunctionDef(payload) - } -} - /// See also [ClassDef](https://docs.python.org/3/library/ast.html#ast.ClassDef) #[derive(Clone, Debug, PartialEq)] pub struct StmtClassDef { @@ -229,12 +141,6 @@ impl StmtClassDef { } } -impl From for Stmt { - fn from(payload: StmtClassDef) -> Self { - Stmt::ClassDef(payload) - } -} - /// See also [Return](https://docs.python.org/3/library/ast.html#ast.Return) #[derive(Clone, Debug, PartialEq)] pub struct StmtReturn { @@ -242,12 +148,6 @@ pub struct StmtReturn { pub value: Option>, } -impl From for Stmt { - fn from(payload: StmtReturn) -> Self { - Stmt::Return(payload) - } -} - /// See also [Delete](https://docs.python.org/3/library/ast.html#ast.Delete) #[derive(Clone, Debug, PartialEq)] pub struct StmtDelete { @@ -255,12 +155,6 @@ pub struct StmtDelete { pub targets: Vec, } -impl From for Stmt { - fn from(payload: StmtDelete) -> Self { - Stmt::Delete(payload) - } -} - /// See also [TypeAlias](https://docs.python.org/3/library/ast.html#ast.TypeAlias) #[derive(Clone, Debug, PartialEq)] pub struct StmtTypeAlias { @@ -270,12 +164,6 @@ pub struct StmtTypeAlias { pub value: Box, } -impl From for Stmt { - fn from(payload: StmtTypeAlias) -> Self { - Stmt::TypeAlias(payload) - } -} - /// See also [Assign](https://docs.python.org/3/library/ast.html#ast.Assign) #[derive(Clone, Debug, PartialEq)] pub struct StmtAssign { @@ -284,12 +172,6 @@ pub struct StmtAssign { pub value: Box, } -impl From for Stmt { - fn from(payload: StmtAssign) -> Self { - Stmt::Assign(payload) - } -} - /// See also [AugAssign](https://docs.python.org/3/library/ast.html#ast.AugAssign) #[derive(Clone, Debug, PartialEq)] pub struct StmtAugAssign { @@ -299,12 +181,6 @@ pub struct StmtAugAssign { pub value: Box, } -impl From for Stmt { - fn from(payload: StmtAugAssign) -> Self { - Stmt::AugAssign(payload) - } -} - /// See also [AnnAssign](https://docs.python.org/3/library/ast.html#ast.AnnAssign) #[derive(Clone, Debug, PartialEq)] pub struct StmtAnnAssign { @@ -315,12 +191,6 @@ pub struct StmtAnnAssign { pub simple: bool, } -impl From for Stmt { - fn from(payload: StmtAnnAssign) -> Self { - Stmt::AnnAssign(payload) - } -} - /// See also [For](https://docs.python.org/3/library/ast.html#ast.For) and /// [AsyncFor](https://docs.python.org/3/library/ast.html#ast.AsyncFor). /// @@ -336,12 +206,6 @@ pub struct StmtFor { pub orelse: Vec, } -impl From for Stmt { - fn from(payload: StmtFor) -> Self { - Stmt::For(payload) - } -} - /// See also [While](https://docs.python.org/3/library/ast.html#ast.While) and /// [AsyncWhile](https://docs.python.org/3/library/ast.html#ast.AsyncWhile). #[derive(Clone, Debug, PartialEq)] @@ -352,12 +216,6 @@ pub struct StmtWhile { pub orelse: Vec, } -impl From for Stmt { - fn from(payload: StmtWhile) -> Self { - Stmt::While(payload) - } -} - /// See also [If](https://docs.python.org/3/library/ast.html#ast.If) #[derive(Clone, Debug, PartialEq)] pub struct StmtIf { @@ -367,12 +225,6 @@ pub struct StmtIf { pub elif_else_clauses: Vec, } -impl From for Stmt { - fn from(payload: StmtIf) -> Self { - Stmt::If(payload) - } -} - #[derive(Clone, Debug, PartialEq)] pub struct ElifElseClause { pub range: TextRange, @@ -393,12 +245,6 @@ pub struct StmtWith { pub body: Vec, } -impl From for Stmt { - fn from(payload: StmtWith) -> Self { - Stmt::With(payload) - } -} - /// See also [Match](https://docs.python.org/3/library/ast.html#ast.Match) #[derive(Clone, Debug, PartialEq)] pub struct StmtMatch { @@ -407,12 +253,6 @@ pub struct StmtMatch { pub cases: Vec, } -impl From for Stmt { - fn from(payload: StmtMatch) -> Self { - Stmt::Match(payload) - } -} - /// See also [Raise](https://docs.python.org/3/library/ast.html#ast.Raise) #[derive(Clone, Debug, PartialEq)] pub struct StmtRaise { @@ -421,12 +261,6 @@ pub struct StmtRaise { pub cause: Option>, } -impl From for Stmt { - fn from(payload: StmtRaise) -> Self { - Stmt::Raise(payload) - } -} - /// See also [Try](https://docs.python.org/3/library/ast.html#ast.Try) and /// [TryStar](https://docs.python.org/3/library/ast.html#ast.TryStar) #[derive(Clone, Debug, PartialEq)] @@ -439,12 +273,6 @@ pub struct StmtTry { pub is_star: bool, } -impl From for Stmt { - fn from(payload: StmtTry) -> Self { - Stmt::Try(payload) - } -} - /// See also [Assert](https://docs.python.org/3/library/ast.html#ast.Assert) #[derive(Clone, Debug, PartialEq)] pub struct StmtAssert { @@ -453,12 +281,6 @@ pub struct StmtAssert { pub msg: Option>, } -impl From for Stmt { - fn from(payload: StmtAssert) -> Self { - Stmt::Assert(payload) - } -} - /// See also [Import](https://docs.python.org/3/library/ast.html#ast.Import) #[derive(Clone, Debug, PartialEq)] pub struct StmtImport { @@ -466,12 +288,6 @@ pub struct StmtImport { pub names: Vec, } -impl From for Stmt { - fn from(payload: StmtImport) -> Self { - Stmt::Import(payload) - } -} - /// See also [ImportFrom](https://docs.python.org/3/library/ast.html#ast.ImportFrom) #[derive(Clone, Debug, PartialEq)] pub struct StmtImportFrom { @@ -481,12 +297,6 @@ pub struct StmtImportFrom { pub level: u32, } -impl From for Stmt { - fn from(payload: StmtImportFrom) -> Self { - Stmt::ImportFrom(payload) - } -} - /// See also [Global](https://docs.python.org/3/library/ast.html#ast.Global) #[derive(Clone, Debug, PartialEq)] pub struct StmtGlobal { @@ -494,12 +304,6 @@ pub struct StmtGlobal { pub names: Vec, } -impl From for Stmt { - fn from(payload: StmtGlobal) -> Self { - Stmt::Global(payload) - } -} - /// See also [Nonlocal](https://docs.python.org/3/library/ast.html#ast.Nonlocal) #[derive(Clone, Debug, PartialEq)] pub struct StmtNonlocal { @@ -507,12 +311,6 @@ pub struct StmtNonlocal { pub names: Vec, } -impl From for Stmt { - fn from(payload: StmtNonlocal) -> Self { - Stmt::Nonlocal(payload) - } -} - /// See also [Expr](https://docs.python.org/3/library/ast.html#ast.Expr) #[derive(Clone, Debug, PartialEq)] pub struct StmtExpr { @@ -520,119 +318,24 @@ pub struct StmtExpr { pub value: Box, } -impl From for Stmt { - fn from(payload: StmtExpr) -> Self { - Stmt::Expr(payload) - } -} - /// See also [Pass](https://docs.python.org/3/library/ast.html#ast.Pass) #[derive(Clone, Debug, PartialEq)] pub struct StmtPass { pub range: TextRange, } -impl From for Stmt { - fn from(payload: StmtPass) -> Self { - Stmt::Pass(payload) - } -} - /// See also [Break](https://docs.python.org/3/library/ast.html#ast.Break) #[derive(Clone, Debug, PartialEq)] pub struct StmtBreak { pub range: TextRange, } -impl From for Stmt { - fn from(payload: StmtBreak) -> Self { - Stmt::Break(payload) - } -} - /// See also [Continue](https://docs.python.org/3/library/ast.html#ast.Continue) #[derive(Clone, Debug, PartialEq)] pub struct StmtContinue { pub range: TextRange, } -impl From for Stmt { - fn from(payload: StmtContinue) -> Self { - Stmt::Continue(payload) - } -} - -/// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr) -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum Expr { - #[is(name = "bool_op_expr")] - BoolOp(ExprBoolOp), - #[is(name = "named_expr")] - Named(ExprNamed), - #[is(name = "bin_op_expr")] - BinOp(ExprBinOp), - #[is(name = "unary_op_expr")] - UnaryOp(ExprUnaryOp), - #[is(name = "lambda_expr")] - Lambda(ExprLambda), - #[is(name = "if_expr")] - If(ExprIf), - #[is(name = "dict_expr")] - Dict(ExprDict), - #[is(name = "set_expr")] - Set(ExprSet), - #[is(name = "list_comp_expr")] - ListComp(ExprListComp), - #[is(name = "set_comp_expr")] - SetComp(ExprSetComp), - #[is(name = "dict_comp_expr")] - DictComp(ExprDictComp), - #[is(name = "generator_expr")] - Generator(ExprGenerator), - #[is(name = "await_expr")] - Await(ExprAwait), - #[is(name = "yield_expr")] - Yield(ExprYield), - #[is(name = "yield_from_expr")] - YieldFrom(ExprYieldFrom), - #[is(name = "compare_expr")] - Compare(ExprCompare), - #[is(name = "call_expr")] - Call(ExprCall), - #[is(name = "f_string_expr")] - FString(ExprFString), - #[is(name = "string_literal_expr")] - StringLiteral(ExprStringLiteral), - #[is(name = "bytes_literal_expr")] - BytesLiteral(ExprBytesLiteral), - #[is(name = "number_literal_expr")] - NumberLiteral(ExprNumberLiteral), - #[is(name = "boolean_literal_expr")] - BooleanLiteral(ExprBooleanLiteral), - #[is(name = "none_literal_expr")] - NoneLiteral(ExprNoneLiteral), - #[is(name = "ellipsis_literal_expr")] - EllipsisLiteral(ExprEllipsisLiteral), - #[is(name = "attribute_expr")] - Attribute(ExprAttribute), - #[is(name = "subscript_expr")] - Subscript(ExprSubscript), - #[is(name = "starred_expr")] - Starred(ExprStarred), - #[is(name = "name_expr")] - Name(ExprName), - #[is(name = "list_expr")] - List(ExprList), - #[is(name = "tuple_expr")] - Tuple(ExprTuple), - #[is(name = "slice_expr")] - Slice(ExprSlice), - - // Jupyter notebook specific - #[is(name = "ipy_escape_command_expr")] - IpyEscapeCommand(ExprIpyEscapeCommand), -} - impl Expr { /// Returns `true` if the expression is a literal expression. /// @@ -682,12 +385,6 @@ pub struct ExprIpyEscapeCommand { pub value: Box, } -impl From for Expr { - fn from(payload: ExprIpyEscapeCommand) -> Self { - Expr::IpyEscapeCommand(payload) - } -} - /// See also [BoolOp](https://docs.python.org/3/library/ast.html#ast.BoolOp) #[derive(Clone, Debug, PartialEq)] pub struct ExprBoolOp { @@ -696,12 +393,6 @@ pub struct ExprBoolOp { pub values: Vec, } -impl From for Expr { - fn from(payload: ExprBoolOp) -> Self { - Expr::BoolOp(payload) - } -} - /// See also [NamedExpr](https://docs.python.org/3/library/ast.html#ast.NamedExpr) #[derive(Clone, Debug, PartialEq)] pub struct ExprNamed { @@ -710,12 +401,6 @@ pub struct ExprNamed { pub value: Box, } -impl From for Expr { - fn from(payload: ExprNamed) -> Self { - Expr::Named(payload) - } -} - /// See also [BinOp](https://docs.python.org/3/library/ast.html#ast.BinOp) #[derive(Clone, Debug, PartialEq)] pub struct ExprBinOp { @@ -725,12 +410,6 @@ pub struct ExprBinOp { pub right: Box, } -impl From for Expr { - fn from(payload: ExprBinOp) -> Self { - Expr::BinOp(payload) - } -} - /// See also [UnaryOp](https://docs.python.org/3/library/ast.html#ast.UnaryOp) #[derive(Clone, Debug, PartialEq)] pub struct ExprUnaryOp { @@ -739,12 +418,6 @@ pub struct ExprUnaryOp { pub operand: Box, } -impl From for Expr { - fn from(payload: ExprUnaryOp) -> Self { - Expr::UnaryOp(payload) - } -} - /// See also [Lambda](https://docs.python.org/3/library/ast.html#ast.Lambda) #[derive(Clone, Debug, PartialEq)] pub struct ExprLambda { @@ -753,12 +426,6 @@ pub struct ExprLambda { pub body: Box, } -impl From for Expr { - fn from(payload: ExprLambda) -> Self { - Expr::Lambda(payload) - } -} - /// See also [IfExp](https://docs.python.org/3/library/ast.html#ast.IfExp) #[derive(Clone, Debug, PartialEq)] pub struct ExprIf { @@ -768,12 +435,6 @@ pub struct ExprIf { pub orelse: Box, } -impl From for Expr { - fn from(payload: ExprIf) -> Self { - Expr::If(payload) - } -} - /// Represents an item in a [dictionary literal display][1]. /// /// Consider the following Python dictionary literal: @@ -880,12 +541,6 @@ impl<'a> IntoIterator for &'a ExprDict { } } -impl From for Expr { - fn from(payload: ExprDict) -> Self { - Expr::Dict(payload) - } -} - #[derive(Debug, Clone)] pub struct DictKeyIterator<'a> { items: Iter<'a, DictItem>, @@ -1000,12 +655,6 @@ impl<'a> IntoIterator for &'a ExprSet { } } -impl From for Expr { - fn from(payload: ExprSet) -> Self { - Expr::Set(payload) - } -} - /// See also [ListComp](https://docs.python.org/3/library/ast.html#ast.ListComp) #[derive(Clone, Debug, PartialEq)] pub struct ExprListComp { @@ -1014,12 +663,6 @@ pub struct ExprListComp { pub generators: Vec, } -impl From for Expr { - fn from(payload: ExprListComp) -> Self { - Expr::ListComp(payload) - } -} - /// See also [SetComp](https://docs.python.org/3/library/ast.html#ast.SetComp) #[derive(Clone, Debug, PartialEq)] pub struct ExprSetComp { @@ -1028,12 +671,6 @@ pub struct ExprSetComp { pub generators: Vec, } -impl From for Expr { - fn from(payload: ExprSetComp) -> Self { - Expr::SetComp(payload) - } -} - /// See also [DictComp](https://docs.python.org/3/library/ast.html#ast.DictComp) #[derive(Clone, Debug, PartialEq)] pub struct ExprDictComp { @@ -1043,12 +680,6 @@ pub struct ExprDictComp { pub generators: Vec, } -impl From for Expr { - fn from(payload: ExprDictComp) -> Self { - Expr::DictComp(payload) - } -} - /// See also [GeneratorExp](https://docs.python.org/3/library/ast.html#ast.GeneratorExp) #[derive(Clone, Debug, PartialEq)] pub struct ExprGenerator { @@ -1058,12 +689,6 @@ pub struct ExprGenerator { pub parenthesized: bool, } -impl From for Expr { - fn from(payload: ExprGenerator) -> Self { - Expr::Generator(payload) - } -} - /// See also [Await](https://docs.python.org/3/library/ast.html#ast.Await) #[derive(Clone, Debug, PartialEq)] pub struct ExprAwait { @@ -1071,12 +696,6 @@ pub struct ExprAwait { pub value: Box, } -impl From for Expr { - fn from(payload: ExprAwait) -> Self { - Expr::Await(payload) - } -} - /// See also [Yield](https://docs.python.org/3/library/ast.html#ast.Yield) #[derive(Clone, Debug, PartialEq)] pub struct ExprYield { @@ -1084,12 +703,6 @@ pub struct ExprYield { pub value: Option>, } -impl From for Expr { - fn from(payload: ExprYield) -> Self { - Expr::Yield(payload) - } -} - /// See also [YieldFrom](https://docs.python.org/3/library/ast.html#ast.YieldFrom) #[derive(Clone, Debug, PartialEq)] pub struct ExprYieldFrom { @@ -1097,12 +710,6 @@ pub struct ExprYieldFrom { pub value: Box, } -impl From for Expr { - fn from(payload: ExprYieldFrom) -> Self { - Expr::YieldFrom(payload) - } -} - /// See also [Compare](https://docs.python.org/3/library/ast.html#ast.Compare) #[derive(Clone, Debug, PartialEq)] pub struct ExprCompare { @@ -1112,12 +719,6 @@ pub struct ExprCompare { pub comparators: Box<[Expr]>, } -impl From for Expr { - fn from(payload: ExprCompare) -> Self { - Expr::Compare(payload) - } -} - /// See also [Call](https://docs.python.org/3/library/ast.html#ast.Call) #[derive(Clone, Debug, PartialEq)] pub struct ExprCall { @@ -1126,24 +727,12 @@ pub struct ExprCall { pub arguments: Arguments, } -impl From for Expr { - fn from(payload: ExprCall) -> Self { - Expr::Call(payload) - } -} - #[derive(Clone, Debug, PartialEq)] pub struct FStringFormatSpec { pub range: TextRange, pub elements: FStringElements, } -impl Ranged for FStringFormatSpec { - fn range(&self) -> TextRange { - self.range - } -} - /// See also [FormattedValue](https://docs.python.org/3/library/ast.html#ast.FormattedValue) #[derive(Clone, Debug, PartialEq)] pub struct FStringExpressionElement { @@ -1154,12 +743,6 @@ pub struct FStringExpressionElement { pub format_spec: Option>, } -impl Ranged for FStringExpressionElement { - fn range(&self) -> TextRange { - self.range - } -} - /// An `FStringLiteralElement` with an empty `value` is an invalid f-string element. #[derive(Clone, Debug, PartialEq)] pub struct FStringLiteralElement { @@ -1173,12 +756,6 @@ impl FStringLiteralElement { } } -impl Ranged for FStringLiteralElement { - fn range(&self) -> TextRange { - self.range - } -} - impl Deref for FStringLiteralElement { type Target = str; @@ -1235,12 +812,6 @@ pub struct ExprFString { pub value: FStringValue, } -impl From for Expr { - fn from(payload: ExprFString) -> Self { - Expr::FString(payload) - } -} - /// The value representing an [`ExprFString`]. #[derive(Clone, Debug, PartialEq)] pub struct FStringValue { @@ -1578,12 +1149,6 @@ pub struct FString { pub flags: FStringFlags, } -impl Ranged for FString { - fn range(&self) -> TextRange { - self.range - } -} - impl From for Expr { fn from(payload: FString) -> Self { ExprFString { @@ -1654,21 +1219,6 @@ impl fmt::Debug for FStringElements { } } -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum FStringElement { - Literal(FStringLiteralElement), - Expression(FStringExpressionElement), -} - -impl Ranged for FStringElement { - fn range(&self) -> TextRange { - match self { - FStringElement::Literal(node) => node.range(), - FStringElement::Expression(node) => node.range(), - } - } -} - /// An AST node that represents either a single string literal or an implicitly /// concatenated string literals. #[derive(Clone, Debug, Default, PartialEq)] @@ -1677,18 +1227,6 @@ pub struct ExprStringLiteral { pub value: StringLiteralValue, } -impl From for Expr { - fn from(payload: ExprStringLiteral) -> Self { - Expr::StringLiteral(payload) - } -} - -impl Ranged for ExprStringLiteral { - fn range(&self) -> TextRange { - self.range - } -} - /// The value representing a [`ExprStringLiteral`]. #[derive(Clone, Debug, Default, PartialEq)] pub struct StringLiteralValue { @@ -1989,12 +1527,6 @@ pub struct StringLiteral { pub flags: StringLiteralFlags, } -impl Ranged for StringLiteral { - fn range(&self) -> TextRange { - self.range - } -} - impl Deref for StringLiteral { type Target = str; @@ -2079,18 +1611,6 @@ pub struct ExprBytesLiteral { pub value: BytesLiteralValue, } -impl From for Expr { - fn from(payload: ExprBytesLiteral) -> Self { - Expr::BytesLiteral(payload) - } -} - -impl Ranged for ExprBytesLiteral { - fn range(&self) -> TextRange { - self.range - } -} - /// The value representing a [`ExprBytesLiteral`]. #[derive(Clone, Debug, Default, PartialEq)] pub struct BytesLiteralValue { @@ -2352,12 +1872,6 @@ pub struct BytesLiteral { pub flags: BytesLiteralFlags, } -impl Ranged for BytesLiteral { - fn range(&self) -> TextRange { - self.range - } -} - impl Deref for BytesLiteral { type Target = [u8]; @@ -2686,18 +2200,6 @@ pub struct ExprNumberLiteral { pub value: Number, } -impl From for Expr { - fn from(payload: ExprNumberLiteral) -> Self { - Expr::NumberLiteral(payload) - } -} - -impl Ranged for ExprNumberLiteral { - fn range(&self) -> TextRange { - self.range - } -} - #[derive(Clone, Debug, PartialEq, is_macro::Is)] pub enum Number { Int(int::Int), @@ -2711,52 +2213,16 @@ pub struct ExprBooleanLiteral { pub value: bool, } -impl From for Expr { - fn from(payload: ExprBooleanLiteral) -> Self { - Expr::BooleanLiteral(payload) - } -} - -impl Ranged for ExprBooleanLiteral { - fn range(&self) -> TextRange { - self.range - } -} - #[derive(Clone, Debug, Default, PartialEq)] pub struct ExprNoneLiteral { pub range: TextRange, } -impl From for Expr { - fn from(payload: ExprNoneLiteral) -> Self { - Expr::NoneLiteral(payload) - } -} - -impl Ranged for ExprNoneLiteral { - fn range(&self) -> TextRange { - self.range - } -} - #[derive(Clone, Debug, Default, PartialEq)] pub struct ExprEllipsisLiteral { pub range: TextRange, } -impl From for Expr { - fn from(payload: ExprEllipsisLiteral) -> Self { - Expr::EllipsisLiteral(payload) - } -} - -impl Ranged for ExprEllipsisLiteral { - fn range(&self) -> TextRange { - self.range - } -} - /// See also [Attribute](https://docs.python.org/3/library/ast.html#ast.Attribute) #[derive(Clone, Debug, PartialEq)] pub struct ExprAttribute { @@ -2766,12 +2232,6 @@ pub struct ExprAttribute { pub ctx: ExprContext, } -impl From for Expr { - fn from(payload: ExprAttribute) -> Self { - Expr::Attribute(payload) - } -} - /// See also [Subscript](https://docs.python.org/3/library/ast.html#ast.Subscript) #[derive(Clone, Debug, PartialEq)] pub struct ExprSubscript { @@ -2781,12 +2241,6 @@ pub struct ExprSubscript { pub ctx: ExprContext, } -impl From for Expr { - fn from(payload: ExprSubscript) -> Self { - Expr::Subscript(payload) - } -} - /// See also [Starred](https://docs.python.org/3/library/ast.html#ast.Starred) #[derive(Clone, Debug, PartialEq)] pub struct ExprStarred { @@ -2795,12 +2249,6 @@ pub struct ExprStarred { pub ctx: ExprContext, } -impl From for Expr { - fn from(payload: ExprStarred) -> Self { - Expr::Starred(payload) - } -} - /// See also [Name](https://docs.python.org/3/library/ast.html#ast.Name) #[derive(Clone, Debug, PartialEq)] pub struct ExprName { @@ -2815,12 +2263,6 @@ impl ExprName { } } -impl From for Expr { - fn from(payload: ExprName) -> Self { - Expr::Name(payload) - } -} - /// See also [List](https://docs.python.org/3/library/ast.html#ast.List) #[derive(Clone, Debug, PartialEq)] pub struct ExprList { @@ -2852,12 +2294,6 @@ impl<'a> IntoIterator for &'a ExprList { } } -impl From for Expr { - fn from(payload: ExprList) -> Self { - Expr::List(payload) - } -} - /// See also [Tuple](https://docs.python.org/3/library/ast.html#ast.Tuple) #[derive(Clone, Debug, PartialEq)] pub struct ExprTuple { @@ -2892,12 +2328,6 @@ impl<'a> IntoIterator for &'a ExprTuple { } } -impl From for Expr { - fn from(payload: ExprTuple) -> Self { - Expr::Tuple(payload) - } -} - /// See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice) #[derive(Clone, Debug, PartialEq)] pub struct ExprSlice { @@ -2907,12 +2337,6 @@ pub struct ExprSlice { pub step: Option>, } -impl From for Expr { - fn from(payload: ExprSlice) -> Self { - Expr::Slice(payload) - } -} - /// See also [expr_context](https://docs.python.org/3/library/ast.html#ast.expr_context) #[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)] pub enum ExprContext { @@ -3135,12 +2559,6 @@ pub struct Comprehension { pub is_async: bool, } -/// See also [excepthandler](https://docs.python.org/3/library/ast.html#ast.excepthandler) -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum ExceptHandler { - ExceptHandler(ExceptHandlerExceptHandler), -} - /// See also [ExceptHandler](https://docs.python.org/3/library/ast.html#ast.ExceptHandler) #[derive(Clone, Debug, PartialEq)] pub struct ExceptHandlerExceptHandler { @@ -3150,12 +2568,6 @@ pub struct ExceptHandlerExceptHandler { pub body: Vec, } -impl From for ExceptHandler { - fn from(payload: ExceptHandlerExceptHandler) -> Self { - ExceptHandler::ExceptHandler(payload) - } -} - /// See also [arg](https://docs.python.org/3/library/ast.html#ast.arg) #[derive(Clone, Debug, PartialEq)] pub struct Parameter { @@ -3197,19 +2609,6 @@ pub struct MatchCase { pub body: Vec, } -/// See also [pattern](https://docs.python.org/3/library/ast.html#ast.pattern) -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum Pattern { - MatchValue(PatternMatchValue), - MatchSingleton(PatternMatchSingleton), - MatchSequence(PatternMatchSequence), - MatchMapping(PatternMatchMapping), - MatchClass(PatternMatchClass), - MatchStar(PatternMatchStar), - MatchAs(PatternMatchAs), - MatchOr(PatternMatchOr), -} - impl Pattern { /// Checks if the [`Pattern`] is an [irrefutable pattern]. /// @@ -3255,12 +2654,6 @@ pub struct PatternMatchValue { pub value: Box, } -impl From for Pattern { - fn from(payload: PatternMatchValue) -> Self { - Pattern::MatchValue(payload) - } -} - /// See also [MatchSingleton](https://docs.python.org/3/library/ast.html#ast.MatchSingleton) #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchSingleton { @@ -3268,12 +2661,6 @@ pub struct PatternMatchSingleton { pub value: Singleton, } -impl From for Pattern { - fn from(payload: PatternMatchSingleton) -> Self { - Pattern::MatchSingleton(payload) - } -} - /// See also [MatchSequence](https://docs.python.org/3/library/ast.html#ast.MatchSequence) #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchSequence { @@ -3281,12 +2668,6 @@ pub struct PatternMatchSequence { pub patterns: Vec, } -impl From for Pattern { - fn from(payload: PatternMatchSequence) -> Self { - Pattern::MatchSequence(payload) - } -} - /// See also [MatchMapping](https://docs.python.org/3/library/ast.html#ast.MatchMapping) #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchMapping { @@ -3296,12 +2677,6 @@ pub struct PatternMatchMapping { pub rest: Option, } -impl From for Pattern { - fn from(payload: PatternMatchMapping) -> Self { - Pattern::MatchMapping(payload) - } -} - /// See also [MatchClass](https://docs.python.org/3/library/ast.html#ast.MatchClass) #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchClass { @@ -3310,12 +2685,6 @@ pub struct PatternMatchClass { pub arguments: PatternArguments, } -impl From for Pattern { - fn from(payload: PatternMatchClass) -> Self { - Pattern::MatchClass(payload) - } -} - /// An AST node to represent the arguments to a [`PatternMatchClass`], i.e., the /// parenthesized contents in `case Point(1, x=0, y=0)`. /// @@ -3345,12 +2714,6 @@ pub struct PatternMatchStar { pub name: Option, } -impl From for Pattern { - fn from(payload: PatternMatchStar) -> Self { - Pattern::MatchStar(payload) - } -} - /// See also [MatchAs](https://docs.python.org/3/library/ast.html#ast.MatchAs) #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchAs { @@ -3359,12 +2722,6 @@ pub struct PatternMatchAs { pub name: Option, } -impl From for Pattern { - fn from(payload: PatternMatchAs) -> Self { - Pattern::MatchAs(payload) - } -} - /// See also [MatchOr](https://docs.python.org/3/library/ast.html#ast.MatchOr) #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchOr { @@ -3372,20 +2729,6 @@ pub struct PatternMatchOr { pub patterns: Vec, } -impl From for Pattern { - fn from(payload: PatternMatchOr) -> Self { - Pattern::MatchOr(payload) - } -} - -/// 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), -} - impl TypeParam { pub const fn name(&self) -> &Identifier { match self { @@ -3413,12 +2756,6 @@ pub struct TypeParamTypeVar { pub default: Option>, } -impl From for TypeParam { - fn from(payload: TypeParamTypeVar) -> Self { - TypeParam::TypeVar(payload) - } -} - /// See also [ParamSpec](https://docs.python.org/3/library/ast.html#ast.ParamSpec) #[derive(Clone, Debug, PartialEq)] pub struct TypeParamParamSpec { @@ -3427,12 +2764,6 @@ pub struct TypeParamParamSpec { pub default: Option>, } -impl From for TypeParam { - fn from(payload: TypeParamParamSpec) -> Self { - TypeParam::ParamSpec(payload) - } -} - /// See also [TypeVarTuple](https://docs.python.org/3/library/ast.html#ast.TypeVarTuple) #[derive(Clone, Debug, PartialEq)] pub struct TypeParamTypeVarTuple { @@ -3441,12 +2772,6 @@ pub struct TypeParamTypeVarTuple { pub default: Option>, } -impl From for TypeParam { - fn from(payload: TypeParamTypeVarTuple) -> Self { - TypeParam::TypeVarTuple(payload) - } -} - /// See also [decorator](https://docs.python.org/3/library/ast.html#ast.decorator) #[derive(Clone, Debug, PartialEq)] pub struct Decorator { @@ -4101,12 +3426,6 @@ impl From for Name { } } -impl Ranged for Identifier { - fn range(&self) -> TextRange { - self.range - } -} - #[derive(Clone, Copy, Debug, PartialEq)] pub enum Singleton { None, @@ -4124,517 +3443,13 @@ impl From for Singleton { } } -impl Ranged for crate::nodes::ModModule { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ModExpression { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::Mod { - fn range(&self) -> TextRange { - match self { - Self::Module(node) => node.range(), - Self::Expression(node) => node.range(), - } - } -} - -impl Ranged for crate::nodes::StmtFunctionDef { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtClassDef { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtReturn { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtDelete { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtTypeAlias { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtAssign { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtAugAssign { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtAnnAssign { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtFor { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtWhile { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtIf { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ElifElseClause { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtWith { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtMatch { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtRaise { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtTry { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtAssert { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtImport { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtImportFrom { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtGlobal { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtNonlocal { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtExpr { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtPass { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtBreak { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtContinue { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::StmtIpyEscapeCommand { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::Stmt { - fn range(&self) -> TextRange { - match self { - Self::FunctionDef(node) => node.range(), - 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(), - Self::For(node) => node.range(), - Self::While(node) => node.range(), - Self::If(node) => node.range(), - Self::With(node) => node.range(), - Self::Match(node) => node.range(), - Self::Raise(node) => node.range(), - Self::Try(node) => node.range(), - Self::Assert(node) => node.range(), - Self::Import(node) => node.range(), - Self::ImportFrom(node) => node.range(), - Self::Global(node) => node.range(), - Self::Nonlocal(node) => node.range(), - Self::Expr(node) => node.range(), - Self::Pass(node) => node.range(), - Self::Break(node) => node.range(), - Self::Continue(node) => node.range(), - Stmt::IpyEscapeCommand(node) => node.range(), - } - } -} - -impl Ranged for crate::nodes::ExprBoolOp { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprNamed { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprBinOp { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprUnaryOp { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprLambda { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprIf { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprDict { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprSet { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprListComp { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprSetComp { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprDictComp { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprGenerator { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprAwait { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprYield { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprYieldFrom { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprCompare { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprCall { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprFString { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprAttribute { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprSubscript { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprStarred { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprName { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprList { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprTuple { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprSlice { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExprIpyEscapeCommand { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::Expr { - fn range(&self) -> TextRange { - match self { - Self::BoolOp(node) => node.range(), - Self::Named(node) => node.range(), - Self::BinOp(node) => node.range(), - Self::UnaryOp(node) => node.range(), - Self::Lambda(node) => node.range(), - Self::If(node) => node.range(), - Self::Dict(node) => node.range(), - Self::Set(node) => node.range(), - Self::ListComp(node) => node.range(), - Self::SetComp(node) => node.range(), - Self::DictComp(node) => node.range(), - Self::Generator(node) => node.range(), - Self::Await(node) => node.range(), - Self::Yield(node) => node.range(), - Self::YieldFrom(node) => node.range(), - Self::Compare(node) => node.range(), - Self::Call(node) => node.range(), - Self::FString(node) => node.range(), - Self::StringLiteral(node) => node.range(), - Self::BytesLiteral(node) => node.range(), - Self::NumberLiteral(node) => node.range(), - Self::BooleanLiteral(node) => node.range(), - Self::NoneLiteral(node) => node.range(), - Self::EllipsisLiteral(node) => node.range(), - Self::Attribute(node) => node.range(), - Self::Subscript(node) => node.range(), - Self::Starred(node) => node.range(), - Self::Name(node) => node.range(), - Self::List(node) => node.range(), - Self::Tuple(node) => node.range(), - Self::Slice(node) => node.range(), - Self::IpyEscapeCommand(node) => node.range(), - } - } -} -impl Ranged for crate::nodes::Comprehension { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ExceptHandlerExceptHandler { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::ExceptHandler { - fn range(&self) -> TextRange { - match self { - Self::ExceptHandler(node) => node.range(), - } - } -} -impl Ranged for crate::nodes::Parameter { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::Keyword { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::Alias { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::WithItem { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::MatchCase { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::PatternMatchValue { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::PatternMatchSingleton { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::PatternMatchSequence { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::PatternMatchMapping { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::PatternMatchClass { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::PatternMatchStar { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::PatternMatchAs { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::PatternMatchOr { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::Pattern { - fn range(&self) -> TextRange { - match self { - Self::MatchValue(node) => node.range(), - Self::MatchSingleton(node) => node.range(), - Self::MatchSequence(node) => node.range(), - Self::MatchMapping(node) => node.range(), - Self::MatchClass(node) => node.range(), - Self::MatchStar(node) => node.range(), - Self::MatchAs(node) => node.range(), - Self::MatchOr(node) => node.range(), - } - } -} -impl Ranged for crate::nodes::PatternArguments { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::PatternKeyword { - fn range(&self) -> TextRange { - self.range - } -} - -impl Ranged for crate::nodes::TypeParams { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::TypeParamTypeVar { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::TypeParamTypeVarTuple { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::TypeParamParamSpec { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::TypeParam { - fn range(&self) -> TextRange { - match self { - Self::TypeVar(node) => node.range(), - Self::TypeVarTuple(node) => node.range(), - Self::ParamSpec(node) => node.range(), - } - } -} -impl Ranged for crate::nodes::Decorator { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::Arguments { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::Parameters { - fn range(&self) -> TextRange { - self.range - } -} -impl Ranged for crate::nodes::ParameterWithDefault { - fn range(&self) -> TextRange { - self.range - } -} - #[cfg(test)] mod tests { #[allow(clippy::wildcard_imports)] use super::*; + use crate::Mod; + #[test] #[cfg(target_pointer_width = "64")] fn size() { diff --git a/crates/ruff_python_ast/src/visitor/source_order.rs b/crates/ruff_python_ast/src/visitor/source_order.rs index 894835b9654e1..9092cbc2af620 100644 --- a/crates/ruff_python_ast/src/visitor/source_order.rs +++ b/crates/ruff_python_ast/src/visitor/source_order.rs @@ -1,10 +1,10 @@ +use crate::AnyNodeRef; use crate::{ Alias, Arguments, BoolOp, BytesLiteral, CmpOp, Comprehension, Decorator, ElifElseClause, ExceptHandler, Expr, FString, FStringElement, Keyword, MatchCase, Mod, Operator, Parameter, ParameterWithDefault, Parameters, Pattern, PatternArguments, PatternKeyword, Singleton, Stmt, StringLiteral, TypeParam, TypeParams, UnaryOp, WithItem, }; -use crate::{AnyNodeRef, AstNode}; /// Visitor that traverses all nodes recursively in the order they appear in the source. /// @@ -203,33 +203,7 @@ where let node = AnyNodeRef::from(stmt); if visitor.enter_node(node).is_traverse() { - match stmt { - Stmt::Expr(stmt) => stmt.visit_source_order(visitor), - Stmt::FunctionDef(stmt) => stmt.visit_source_order(visitor), - Stmt::ClassDef(stmt) => stmt.visit_source_order(visitor), - Stmt::Return(stmt) => stmt.visit_source_order(visitor), - Stmt::Delete(stmt) => stmt.visit_source_order(visitor), - Stmt::TypeAlias(stmt) => stmt.visit_source_order(visitor), - Stmt::Assign(stmt) => stmt.visit_source_order(visitor), - Stmt::AugAssign(stmt) => stmt.visit_source_order(visitor), - Stmt::AnnAssign(stmt) => stmt.visit_source_order(visitor), - Stmt::For(stmt) => stmt.visit_source_order(visitor), - Stmt::While(stmt) => stmt.visit_source_order(visitor), - Stmt::If(stmt) => stmt.visit_source_order(visitor), - Stmt::With(stmt) => stmt.visit_source_order(visitor), - Stmt::Match(stmt) => stmt.visit_source_order(visitor), - Stmt::Raise(stmt) => stmt.visit_source_order(visitor), - Stmt::Try(stmt) => stmt.visit_source_order(visitor), - Stmt::Assert(stmt) => stmt.visit_source_order(visitor), - Stmt::Import(stmt) => stmt.visit_source_order(visitor), - Stmt::ImportFrom(stmt) => stmt.visit_source_order(visitor), - Stmt::Pass(stmt) => stmt.visit_source_order(visitor), - Stmt::Break(stmt) => stmt.visit_source_order(visitor), - Stmt::Continue(stmt) => stmt.visit_source_order(visitor), - Stmt::Global(stmt) => stmt.visit_source_order(visitor), - Stmt::Nonlocal(stmt) => stmt.visit_source_order(visitor), - Stmt::IpyEscapeCommand(stmt) => stmt.visit_source_order(visitor), - } + stmt.visit_source_order(visitor); } visitor.leave_node(node); @@ -455,11 +429,7 @@ where { let node = AnyNodeRef::from(type_param); if visitor.enter_node(node).is_traverse() { - match type_param { - TypeParam::TypeVar(type_param) => type_param.visit_source_order(visitor), - TypeParam::TypeVarTuple(type_param) => type_param.visit_source_order(visitor), - TypeParam::ParamSpec(type_param) => type_param.visit_source_order(visitor), - } + type_param.visit_source_order(visitor); } visitor.leave_node(node); } diff --git a/crates/ruff_python_formatter/generate.py b/crates/ruff_python_formatter/generate.py index 3196e7550754d..2a662cdefe8c6 100755 --- a/crates/ruff_python_formatter/generate.py +++ b/crates/ruff_python_formatter/generate.py @@ -24,7 +24,7 @@ def rustfmt(code: str) -> str: root.joinpath("crates") .joinpath("ruff_python_ast") .joinpath("src") - .joinpath("node.rs") + .joinpath("generated.rs") .read_text() ) node_lines = ( diff --git a/crates/ruff_python_formatter/src/generated.rs b/crates/ruff_python_formatter/src/generated.rs index f60202d1281a3..9bb79d80accaf 100644 --- a/crates/ruff_python_formatter/src/generated.rs +++ b/crates/ruff_python_formatter/src/generated.rs @@ -2392,6 +2392,114 @@ impl<'ast> IntoFormat> for ast::PatternMatchOr { } } +impl FormatRule> + for crate::type_param::type_param_type_var::FormatTypeParamTypeVar +{ + #[inline] + fn fmt(&self, node: &ast::TypeParamTypeVar, f: &mut PyFormatter) -> FormatResult<()> { + FormatNodeRule::::fmt(self, node, f) + } +} +impl<'ast> AsFormat> for ast::TypeParamTypeVar { + type Format<'a> = FormatRefWithRule< + 'a, + ast::TypeParamTypeVar, + crate::type_param::type_param_type_var::FormatTypeParamTypeVar, + PyFormatContext<'ast>, + >; + fn format(&self) -> Self::Format<'_> { + FormatRefWithRule::new( + self, + crate::type_param::type_param_type_var::FormatTypeParamTypeVar::default(), + ) + } +} +impl<'ast> IntoFormat> for ast::TypeParamTypeVar { + type Format = FormatOwnedWithRule< + ast::TypeParamTypeVar, + crate::type_param::type_param_type_var::FormatTypeParamTypeVar, + PyFormatContext<'ast>, + >; + fn into_format(self) -> Self::Format { + FormatOwnedWithRule::new( + self, + crate::type_param::type_param_type_var::FormatTypeParamTypeVar::default(), + ) + } +} + +impl FormatRule> + for crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple +{ + #[inline] + fn fmt(&self, node: &ast::TypeParamTypeVarTuple, f: &mut PyFormatter) -> FormatResult<()> { + FormatNodeRule::::fmt(self, node, f) + } +} +impl<'ast> AsFormat> for ast::TypeParamTypeVarTuple { + type Format<'a> = FormatRefWithRule< + 'a, + ast::TypeParamTypeVarTuple, + crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple, + PyFormatContext<'ast>, + >; + fn format(&self) -> Self::Format<'_> { + FormatRefWithRule::new( + self, + crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple::default(), + ) + } +} +impl<'ast> IntoFormat> for ast::TypeParamTypeVarTuple { + type Format = FormatOwnedWithRule< + ast::TypeParamTypeVarTuple, + crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple, + PyFormatContext<'ast>, + >; + fn into_format(self) -> Self::Format { + FormatOwnedWithRule::new( + self, + crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple::default(), + ) + } +} + +impl FormatRule> + for crate::type_param::type_param_param_spec::FormatTypeParamParamSpec +{ + #[inline] + fn fmt(&self, node: &ast::TypeParamParamSpec, f: &mut PyFormatter) -> FormatResult<()> { + FormatNodeRule::::fmt(self, node, f) + } +} +impl<'ast> AsFormat> for ast::TypeParamParamSpec { + type Format<'a> = FormatRefWithRule< + 'a, + ast::TypeParamParamSpec, + crate::type_param::type_param_param_spec::FormatTypeParamParamSpec, + PyFormatContext<'ast>, + >; + fn format(&self) -> Self::Format<'_> { + FormatRefWithRule::new( + self, + crate::type_param::type_param_param_spec::FormatTypeParamParamSpec::default(), + ) + } +} +impl<'ast> IntoFormat> for ast::TypeParamParamSpec { + type Format = FormatOwnedWithRule< + ast::TypeParamParamSpec, + crate::type_param::type_param_param_spec::FormatTypeParamParamSpec, + PyFormatContext<'ast>, + >; + fn into_format(self) -> Self::Format { + FormatOwnedWithRule::new( + self, + crate::type_param::type_param_param_spec::FormatTypeParamParamSpec::default(), + ) + } +} + impl FormatRule> for crate::pattern::pattern_arguments::FormatPatternArguments { @@ -2827,114 +2935,6 @@ impl<'ast> IntoFormat> for ast::TypeParams { } } -impl FormatRule> - for crate::type_param::type_param_type_var::FormatTypeParamTypeVar -{ - #[inline] - fn fmt(&self, node: &ast::TypeParamTypeVar, f: &mut PyFormatter) -> FormatResult<()> { - FormatNodeRule::::fmt(self, node, f) - } -} -impl<'ast> AsFormat> for ast::TypeParamTypeVar { - type Format<'a> = FormatRefWithRule< - 'a, - ast::TypeParamTypeVar, - crate::type_param::type_param_type_var::FormatTypeParamTypeVar, - PyFormatContext<'ast>, - >; - fn format(&self) -> Self::Format<'_> { - FormatRefWithRule::new( - self, - crate::type_param::type_param_type_var::FormatTypeParamTypeVar::default(), - ) - } -} -impl<'ast> IntoFormat> for ast::TypeParamTypeVar { - type Format = FormatOwnedWithRule< - ast::TypeParamTypeVar, - crate::type_param::type_param_type_var::FormatTypeParamTypeVar, - PyFormatContext<'ast>, - >; - fn into_format(self) -> Self::Format { - FormatOwnedWithRule::new( - self, - crate::type_param::type_param_type_var::FormatTypeParamTypeVar::default(), - ) - } -} - -impl FormatRule> - for crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple -{ - #[inline] - fn fmt(&self, node: &ast::TypeParamTypeVarTuple, f: &mut PyFormatter) -> FormatResult<()> { - FormatNodeRule::::fmt(self, node, f) - } -} -impl<'ast> AsFormat> for ast::TypeParamTypeVarTuple { - type Format<'a> = FormatRefWithRule< - 'a, - ast::TypeParamTypeVarTuple, - crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple, - PyFormatContext<'ast>, - >; - fn format(&self) -> Self::Format<'_> { - FormatRefWithRule::new( - self, - crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple::default(), - ) - } -} -impl<'ast> IntoFormat> for ast::TypeParamTypeVarTuple { - type Format = FormatOwnedWithRule< - ast::TypeParamTypeVarTuple, - crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple, - PyFormatContext<'ast>, - >; - fn into_format(self) -> Self::Format { - FormatOwnedWithRule::new( - self, - crate::type_param::type_param_type_var_tuple::FormatTypeParamTypeVarTuple::default(), - ) - } -} - -impl FormatRule> - for crate::type_param::type_param_param_spec::FormatTypeParamParamSpec -{ - #[inline] - fn fmt(&self, node: &ast::TypeParamParamSpec, f: &mut PyFormatter) -> FormatResult<()> { - FormatNodeRule::::fmt(self, node, f) - } -} -impl<'ast> AsFormat> for ast::TypeParamParamSpec { - type Format<'a> = FormatRefWithRule< - 'a, - ast::TypeParamParamSpec, - crate::type_param::type_param_param_spec::FormatTypeParamParamSpec, - PyFormatContext<'ast>, - >; - fn format(&self) -> Self::Format<'_> { - FormatRefWithRule::new( - self, - crate::type_param::type_param_param_spec::FormatTypeParamParamSpec::default(), - ) - } -} -impl<'ast> IntoFormat> for ast::TypeParamParamSpec { - type Format = FormatOwnedWithRule< - ast::TypeParamParamSpec, - crate::type_param::type_param_param_spec::FormatTypeParamParamSpec, - PyFormatContext<'ast>, - >; - fn into_format(self) -> Self::Format { - FormatOwnedWithRule::new( - self, - crate::type_param::type_param_param_spec::FormatTypeParamParamSpec::default(), - ) - } -} - impl FormatRule> for crate::other::f_string::FormatFString { #[inline] fn fmt(&self, node: &ast::FString, f: &mut PyFormatter) -> FormatResult<()> { diff --git a/pyproject.toml b/pyproject.toml index a6969bbfcd2fe..b00c5a217624c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -71,6 +71,9 @@ ignore = [ # Conflicts with the formatter "COM812", "ISC001" ] +extend-select = [ + "I", +] [tool.black] force-exclude = '''