Skip to main content

wdl_analysis/
lib.rs

1//! Analysis of Workflow Description Language (WDL) documents.
2//!
3//! An analyzer can be used to implement the [Language Server Protocol (LSP)](https://microsoft.github.io/language-server-protocol/).
4//!
5//! # Examples
6//!
7//! ```no_run
8//! use url::Url;
9//! use wdl_analysis::Analyzer;
10//!
11//! #[tokio::main]
12//! async fn main() {
13//!     let analyzer = Analyzer::default();
14//!     // Add a docuement to the analyzer
15//!     analyzer
16//!         .add_document(Url::parse("file:///path/to/file.wdl").unwrap())
17//!         .await
18//!         .unwrap();
19//!     let results = analyzer.analyze(()).await.unwrap();
20//!     // Process the results
21//!     for result in results {
22//!         // Do something
23//!     }
24//! }
25//! ```
26#![doc = include_str!("../RULES.md")]
27#![warn(missing_docs)]
28#![warn(rust_2018_idioms)]
29#![warn(rust_2021_compatibility)]
30#![warn(missing_debug_implementations)]
31#![warn(clippy::missing_docs_in_private_items)]
32#![warn(rustdoc::broken_intra_doc_links)]
33
34use std::collections::HashSet;
35
36use wdl_ast::AstToken;
37use wdl_ast::Comment;
38use wdl_ast::Direction;
39use wdl_ast::Directive;
40use wdl_ast::SyntaxKind;
41use wdl_ast::SyntaxNode;
42
43mod analyzer;
44pub mod config;
45pub mod diagnostics;
46pub mod document;
47pub mod eval;
48mod graph;
49pub mod handlers;
50mod queue;
51mod rayon;
52mod rules;
53pub mod stdlib;
54pub mod types;
55mod validation;
56mod visitor;
57
58pub use analyzer::*;
59pub use config::Config;
60pub use config::DiagnosticsConfig;
61pub use config::FeatureFlags;
62pub use document::Document;
63pub use rules::*;
64pub use validation::*;
65pub use visitor::*;
66
67/// An extension trait for syntax nodes.
68pub trait Exceptable {
69    /// Gets the AST node's rule exceptions set.
70    ///
71    /// The set is the comma-delimited list of rule identifiers that follows a
72    /// `#@ except:` comment.
73    fn rule_exceptions(&self) -> HashSet<String> {
74        HashSet::new()
75    }
76
77    /// Determines if a given rule id is excepted for the syntax node.
78    fn is_rule_excepted(&self, _id: &str) -> bool {
79        true
80    }
81}
82
83impl Exceptable for SyntaxNode {
84    fn rule_exceptions(&self) -> HashSet<String> {
85        self.siblings_with_tokens(Direction::Prev)
86            .skip(1) // self is included with siblings
87            .map_while(|s| {
88                if s.kind() == SyntaxKind::Whitespace || s.kind() == SyntaxKind::Comment {
89                    s.into_token()
90                } else {
91                    None
92                }
93            })
94            .filter_map(Comment::cast)
95            .filter_map(|c| c.directive())
96            .flat_map(|d| match d {
97                Directive::Except(e) => e,
98            })
99            .collect()
100    }
101
102    fn is_rule_excepted(&self, id: &str) -> bool {
103        self.rule_exceptions().contains(id)
104    }
105}