|
| 1 | +// via https://github.com/rust-lang/rust-analyzer/blob/d8887c0758bbd2d5f752d5bd405d4491e90e7ed6/crates/syntax/src/ast.rs |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any |
| 4 | +// person obtaining a copy of this software and associated |
| 5 | +// documentation files (the "Software"), to deal in the |
| 6 | +// Software without restriction, including without |
| 7 | +// limitation the rights to use, copy, modify, merge, |
| 8 | +// publish, distribute, sublicense, and/or sell copies of |
| 9 | +// the Software, and to permit persons to whom the Software |
| 10 | +// is furnished to do so, subject to the following |
| 11 | +// conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice |
| 14 | +// shall be included in all copies or substantial portions |
| 15 | +// of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF |
| 18 | +// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 19 | +// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 20 | +// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 21 | +// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 22 | +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 23 | +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR |
| 24 | +// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 25 | +// DEALINGS IN THE SOFTWARE. |
| 26 | + |
| 27 | +mod nodes; |
| 28 | +mod support; |
| 29 | +mod traits; |
| 30 | + |
| 31 | +mod node_ext; |
| 32 | + |
| 33 | +use std::marker::PhantomData; |
| 34 | + |
| 35 | +use crate::syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken}; |
| 36 | +use squawk_parser::SyntaxKind; |
| 37 | + |
| 38 | +pub use self::{ |
| 39 | + nodes::*, |
| 40 | + // generated::{nodes::*, tokens::*}, |
| 41 | + // node_ext::{ |
| 42 | + // AttrKind, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind, SelfParamKind, |
| 43 | + // SlicePatComponents, StructKind, TraitOrAlias, TypeBoundKind, TypeOrConstParam, |
| 44 | + // VisibilityKind, |
| 45 | + // }, |
| 46 | + // operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp}, |
| 47 | + // token_ext::{CommentKind, CommentPlacement, CommentShape, IsString, QuoteOffsets, Radix}, |
| 48 | + traits::{ |
| 49 | + // AttrDocCommentIter, DocCommentIter, |
| 50 | + HasArgList, // HasAttrs, HasDocComments, HasGenericArgs, |
| 51 | + HasIfExists, |
| 52 | + HasIfNotExists, // HasTypeBounds, |
| 53 | + // HasVisibility, |
| 54 | + // HasGenericParams, HasLoopBody, |
| 55 | + HasModuleItem, |
| 56 | + HasName, |
| 57 | + }, |
| 58 | +}; |
| 59 | + |
| 60 | +/// The main trait to go from untyped `SyntaxNode` to a typed ast. The |
| 61 | +/// conversion itself has zero runtime cost: ast and syntax nodes have exactly |
| 62 | +/// the same representation: a pointer to the tree root and a pointer to the |
| 63 | +/// node itself. |
| 64 | +pub trait AstNode { |
| 65 | + fn can_cast(kind: SyntaxKind) -> bool |
| 66 | + where |
| 67 | + Self: Sized; |
| 68 | + |
| 69 | + fn cast(syntax: SyntaxNode) -> Option<Self> |
| 70 | + where |
| 71 | + Self: Sized; |
| 72 | + |
| 73 | + fn syntax(&self) -> &SyntaxNode; |
| 74 | + fn clone_for_update(&self) -> Self |
| 75 | + where |
| 76 | + Self: Sized, |
| 77 | + { |
| 78 | + Self::cast(self.syntax().clone_for_update()).unwrap() |
| 79 | + } |
| 80 | + fn clone_subtree(&self) -> Self |
| 81 | + where |
| 82 | + Self: Sized, |
| 83 | + { |
| 84 | + Self::cast(self.syntax().clone_subtree()).unwrap() |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// Like `AstNode`, but wraps tokens rather than interior nodes. |
| 89 | +pub trait AstToken { |
| 90 | + fn can_cast(token: SyntaxKind) -> bool |
| 91 | + where |
| 92 | + Self: Sized; |
| 93 | + |
| 94 | + fn cast(syntax: SyntaxToken) -> Option<Self> |
| 95 | + where |
| 96 | + Self: Sized; |
| 97 | + |
| 98 | + fn syntax(&self) -> &SyntaxToken; |
| 99 | + |
| 100 | + fn text(&self) -> &str { |
| 101 | + self.syntax().text() |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/// An iterator over `SyntaxNode` children of a particular AST type. |
| 106 | +#[derive(Debug, Clone)] |
| 107 | +pub struct AstChildren<N> { |
| 108 | + inner: SyntaxNodeChildren, |
| 109 | + ph: PhantomData<N>, |
| 110 | +} |
| 111 | + |
| 112 | +impl<N> AstChildren<N> { |
| 113 | + fn new(parent: &SyntaxNode) -> Self { |
| 114 | + AstChildren { |
| 115 | + inner: parent.children(), |
| 116 | + ph: PhantomData, |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl<N: AstNode> Iterator for AstChildren<N> { |
| 122 | + type Item = N; |
| 123 | + fn next(&mut self) -> Option<N> { |
| 124 | + self.inner.find_map(N::cast) |
| 125 | + } |
| 126 | +} |
0 commit comments