From faa420fc32566bd9de81d5d14445dd25bb3694a3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 14 Aug 2021 16:58:46 +0300 Subject: [PATCH] internal: prepare a dedicated module for all operators --- crates/syntax/src/ast.rs | 14 ++++++++--- crates/syntax/src/ast/expr_ext.rs | 37 ++++++++++-------------------- crates/syntax/src/ast/operators.rs | 17 ++++++++++++++ 3 files changed, 40 insertions(+), 28 deletions(-) create mode 100644 crates/syntax/src/ast/operators.rs diff --git a/crates/syntax/src/ast.rs b/crates/syntax/src/ast.rs index a8071b51d2f..fce09851d94 100644 --- a/crates/syntax/src/ast.rs +++ b/crates/syntax/src/ast.rs @@ -5,6 +5,7 @@ mod traits; mod token_ext; mod node_ext; mod expr_ext; +mod operators; pub mod edit; pub mod edit_in_place; pub mod make; @@ -17,14 +18,21 @@ use crate::{ }; pub use self::{ - expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp}, + expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind}, generated::{nodes::*, tokens::*}, node_ext::{ AttrKind, AttrsOwnerNode, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents, StructKind, TypeBoundKind, VisibilityKind, }, - token_ext::*, - traits::*, + operators::{PrefixOp, RangeOp}, + token_ext::{ + CommentKind, CommentPlacement, CommentShape, HasFormatSpecifier, IsString, QuoteOffsets, + Radix, + }, + traits::{ + ArgListOwner, AttrsOwner, CommentIter, DocCommentsOwner, GenericParamsOwner, LoopBodyOwner, + ModuleItemOwner, NameOwner, TypeBoundsOwner, VisibilityOwner, + }, }; /// The main trait to go from untyped `SyntaxNode` to a typed ast. The diff --git a/crates/syntax/src/ast/expr_ext.rs b/crates/syntax/src/ast/expr_ext.rs index 2dfb0d1ad31..f482a45dbed 100644 --- a/crates/syntax/src/ast/expr_ext.rs +++ b/crates/syntax/src/ast/expr_ext.rs @@ -3,7 +3,11 @@ use rowan::WalkEvent; use crate::{ - ast::{self, support, AstChildren, AstNode}, + ast::{ + self, + operators::{PrefixOp, RangeOp}, + support, AstChildren, AstNode, + }, AstToken, SyntaxKind::*, SyntaxToken, T, @@ -193,24 +197,15 @@ impl ast::IfExpr { } } -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -pub enum PrefixOp { - /// The `*` operator for dereferencing - Deref, - /// The `!` operator for logical inversion - Not, - /// The `-` operator for negation - Neg, -} - impl ast::PrefixExpr { pub fn op_kind(&self) -> Option { - match self.op_token()?.kind() { - T![*] => Some(PrefixOp::Deref), - T![!] => Some(PrefixOp::Not), - T![-] => Some(PrefixOp::Neg), - _ => None, - } + let res = match self.op_token()?.kind() { + T![*] => PrefixOp::Deref, + T![!] => PrefixOp::Not, + T![-] => PrefixOp::Neg, + _ => return None, + }; + Some(res) } pub fn op_token(&self) -> Option { @@ -398,14 +393,6 @@ impl std::fmt::Display for BinOp { } } -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -pub enum RangeOp { - /// `..` - Exclusive, - /// `..=` - Inclusive, -} - impl ast::RangeExpr { fn op_details(&self) -> Option<(usize, SyntaxToken, RangeOp)> { self.syntax().children_with_tokens().enumerate().find_map(|(ix, child)| { diff --git a/crates/syntax/src/ast/operators.rs b/crates/syntax/src/ast/operators.rs new file mode 100644 index 00000000000..03fd7da8406 --- /dev/null +++ b/crates/syntax/src/ast/operators.rs @@ -0,0 +1,17 @@ +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +pub enum PrefixOp { + /// The `*` operator for dereferencing + Deref, + /// The `!` operator for logical inversion + Not, + /// The `-` operator for negation + Neg, +} + +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +pub enum RangeOp { + /// `..` + Exclusive, + /// `..=` + Inclusive, +}