Skip to content

Commit

Permalink
Added annotation traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
SpontanCombust committed Aug 14, 2024
1 parent 29bb57a commit 78a66d3
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 7 deletions.
2 changes: 2 additions & 0 deletions crates/analysis/src/jobs/scan_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ impl SyntaxNodeVisitor for SymbolScannerVisitor<'_> {


FunctionDeclarationTraversalPolicy {
traverse_annotation: false,
traverse_params: traverse,
traverse_definition: traverse,
traverse_errors: false
Expand Down Expand Up @@ -622,6 +623,7 @@ impl SyntaxNodeVisitor for SymbolScannerVisitor<'_> {
}

FunctionDeclarationTraversalPolicy {
traverse_annotation: false,
traverse_params: traverse,
traverse_definition: traverse,
traverse_errors: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use witcherscript::tokens::*;
use witcherscript::ast::*;
use witcherscript_diagnostics::*;


//TODO rewrite for the new traversal methodology
pub fn syntax_analysis(script: &Script, diagnostics: &mut Vec<Diagnostic>) {
let mut visitor = SyntaxErrorVisitor {
diagnostics
Expand Down Expand Up @@ -355,6 +355,7 @@ impl SyntaxNodeVisitor for SyntaxErrorVisitor<'_> {
}

FunctionDeclarationTraversalPolicy {
traverse_annotation: false,
traverse_params,
traverse_definition,
traverse_errors: false
Expand Down Expand Up @@ -392,7 +393,8 @@ impl SyntaxNodeVisitor for SyntaxErrorVisitor<'_> {
}
}

FunctionDeclarationTraversalPolicy {
FunctionDeclarationTraversalPolicy {
traverse_annotation: false,
traverse_params,
traverse_definition,
traverse_errors: false
Expand Down
23 changes: 20 additions & 3 deletions crates/core/src/ast/annotation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use strum_macros::{EnumString, Display, AsRefStr};
use crate::{AnyNode, DebugRange, NamedSyntaxNode, SyntaxNode};
use crate::tokens::{AnnotationIdentifierNode, IdentifierNode};
use crate::{tokens::*, AnyNode, DebugRange, NamedSyntaxNode, SyntaxNode};
use super::*;


mod tags {
Expand Down Expand Up @@ -79,4 +79,21 @@ impl<'script> TryFrom<AnyNode<'script>> for AnnotationNode<'script> {
}
}

//TODO traversal to annotations
impl SyntaxNodeTraversal for AnnotationNode<'_> {
fn accept<V: SyntaxNodeVisitor>(&self, visitor: &mut V, ctx: &mut TraversalContextStack) {
let tp = visitor.visit_annotation(self, ctx);

if tp.any() {
for ch in self.children_detailed().must_be_named(true) {
match ch {
Err(e) if tp.traverse_errors => {
e.accept(visitor, ctx);
},
_ => {}
}
}
}

visitor.exit_annotation(self, ctx);
}
}
5 changes: 5 additions & 0 deletions crates/core/src/ast/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ impl SyntaxNodeTraversal for FunctionDeclarationNode<'_> {
let accept_proper = |self_: &Self, visitor: &mut V, ctx: &mut TraversalContextStack, tp: FunctionDeclarationTraversalPolicy| {
for ch in self_.children_detailed().must_be_named(true) {
match ch {
Ok((annot, Some("annotation"))) if tp.traverse_annotation => {
let annot: AnnotationNode = annot.unsafe_into();

annot.accept(visitor, ctx);
},
Ok((params, Some("params"))) if tp.traverse_params => {
let params: FunctionParametersNode = params.unsafe_into();

Expand Down
38 changes: 38 additions & 0 deletions crates/core/src/ast/traversal/policies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,19 +605,22 @@ impl BitAnd for EnumVariantDeclarationTraversalPolicy {

#[derive(Debug, Clone)]
pub struct MemberVarDeclarationTraversalPolicy {
pub traverse_annotation: bool,
pub traverse_errors: bool
}

impl TraversalPolicy for MemberVarDeclarationTraversalPolicy {
#[inline(always)]
fn default_to(value: bool) -> Self {
Self {
traverse_annotation: value,
traverse_errors: value
}
}

#[inline]
fn any(&self) -> bool {
self.traverse_annotation ||
self.traverse_errors
}
}
Expand All @@ -627,6 +630,7 @@ impl BitAnd for MemberVarDeclarationTraversalPolicy {

fn bitand(self, rhs: Self) -> Self::Output {
Self {
traverse_annotation: self.traverse_annotation && rhs.traverse_annotation,
traverse_errors: self.traverse_errors && rhs.traverse_errors
}
}
Expand Down Expand Up @@ -793,6 +797,7 @@ impl BitAnd for FunctionParameterGroupTraversalPolicy {

#[derive(Debug, Clone)]
pub struct FunctionDeclarationTraversalPolicy {
pub traverse_annotation: bool,
pub traverse_params: bool,
pub traverse_definition: bool,
pub traverse_errors: bool
Expand All @@ -802,6 +807,7 @@ impl TraversalPolicy for FunctionDeclarationTraversalPolicy {
#[inline(always)]
fn default_to(value: bool) -> Self {
Self {
traverse_annotation: value,
traverse_params: value,
traverse_definition: value,
traverse_errors: value
Expand All @@ -810,6 +816,7 @@ impl TraversalPolicy for FunctionDeclarationTraversalPolicy {

#[inline]
fn any(&self) -> bool {
self.traverse_annotation ||
self.traverse_params ||
self.traverse_definition ||
self.traverse_errors
Expand All @@ -821,6 +828,7 @@ impl BitAnd for FunctionDeclarationTraversalPolicy {

fn bitand(self, rhs: Self) -> Self::Output {
Self {
traverse_annotation: self.traverse_annotation && rhs.traverse_annotation,
traverse_params: self.traverse_params && rhs.traverse_params,
traverse_definition: self.traverse_definition && rhs.traverse_definition,
traverse_errors: self.traverse_errors && rhs.traverse_errors
Expand Down Expand Up @@ -1366,6 +1374,36 @@ impl BitAnd for DeleteStatementTraversalPolicy {



#[derive(Debug, Clone)]
pub struct AnnotationTraversalPolicy {
pub traverse_errors: bool
}

impl TraversalPolicy for AnnotationTraversalPolicy {
#[inline(always)]
fn default_to(value: bool) -> Self {
Self {
traverse_errors: value,
}
}

#[inline]
fn any(&self) -> bool {
self.traverse_errors
}
}

impl BitAnd for AnnotationTraversalPolicy {
type Output = Self;

fn bitand(self, rhs: Self) -> Self::Output {
Self {
traverse_errors: self.traverse_errors && rhs.traverse_errors
}
}
}



#[derive(Debug, Clone)]
pub struct ErrorTraversalPolicy {
Expand Down
7 changes: 6 additions & 1 deletion crates/core/src/ast/traversal/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,14 @@ pub trait SyntaxNodeVisitor {
fn visit_nop_stmt(&mut self, n: &NopNode, ctx: &TraversalContextStack) {}


/// Called when visiting an annotation node (e.g. @wrapMethod)
fn visit_annotation(&mut self, n: &AnnotationNode, ctx: &TraversalContextStack) -> AnnotationTraversalPolicy { TraversalPolicy::default_to(self.traversal_policy_default()) }
/// Called after visiting an annotation node and possibly also children nodes specified in traversal policy.
fn exit_annotation(&mut self, n: &AnnotationNode, ctx: &TraversalContextStack) {}


/// Called when visiting a node representing a syntax error.
fn visit_error(&mut self, n: &ErrorNode, ctx: &TraversalContextStack) -> ErrorTraversalPolicy { ErrorTraversalPolicy::default_to(self.traversal_policy_default()) }
fn visit_error(&mut self, n: &ErrorNode, ctx: &TraversalContextStack) -> ErrorTraversalPolicy { TraversalPolicy::default_to(self.traversal_policy_default()) }
/// Called after visiting a node representing a syntax error and possibly also children nodes specified in traversal policy.
fn exit_error(&mut self, n: &ErrorNode, ctx: &TraversalContextStack) {}
}
10 changes: 9 additions & 1 deletion crates/core/src/ast/traversal/visitor_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<'a> SyntaxNodeVisitorChain<'a> {
}
}
}

//TODO add #[inline]s
impl<'a> SyntaxNodeVisitor for SyntaxNodeVisitorChain<'a> {
fn traversal_policy_default(&self) -> bool {
true
Expand Down Expand Up @@ -459,6 +459,14 @@ impl<'a> SyntaxNodeVisitor for SyntaxNodeVisitorChain<'a> {
}


fn visit_annotation(&mut self, n: &AnnotationNode, ctx: &TraversalContextStack) -> AnnotationTraversalPolicy {
self.chain_visit_traversable(move |link| link.visit_annotation(n, ctx))
}

fn exit_annotation(&mut self, n: &AnnotationNode, ctx: &TraversalContextStack) {
self.chain_exit(move |link| link.exit_annotation(n, ctx))
}


fn visit_error(&mut self, n: &ErrorNode, ctx: &TraversalContextStack) -> ErrorTraversalPolicy {
self.chain_visit_traversable(move |link| link.visit_error(n, ctx))
Expand Down
5 changes: 5 additions & 0 deletions crates/core/src/ast/vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ impl SyntaxNodeTraversal for MemberVarDeclarationNode<'_> {
let accept_proper = |self_: &Self, visitor: &mut V, ctx: &mut TraversalContextStack, tp: MemberVarDeclarationTraversalPolicy| {
for ch in self_.children_detailed().must_be_named(true) {
match ch {
Ok((annot, Some("annotation"))) if tp.traverse_annotation => {
let annot: AnnotationNode = annot.unsafe_into();

annot.accept(visitor, ctx);
},
Err(e) if tp.traverse_errors => {
e.accept(visitor, ctx);
},
Expand Down

0 comments on commit 78a66d3

Please sign in to comment.