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::Direction;
37use wdl_ast::SyntaxKind;
38use wdl_ast::SyntaxNode;
39use wdl_ast::SyntaxToken;
40
41mod analyzer;
42pub mod diagnostics;
43pub mod document;
44pub mod eval;
45mod graph;
46mod queue;
47mod rayon;
48mod rules;
49pub mod stdlib;
50pub mod types;
51mod validation;
52mod visitor;
53
54pub use analyzer::*;
55pub use document::Document;
56pub use rules::*;
57pub use validation::*;
58pub use visitor::*;
59
60/// The prefix of `except` comments.
61pub const EXCEPT_COMMENT_PREFIX: &str = "#@ except:";
62
63/// An extension trait for syntax nodes.
64pub trait SyntaxNodeExt {
65    /// Gets an iterator over the `@except` comments for a syntax node.
66    fn except_comments(&self) -> impl Iterator<Item = SyntaxToken> + '_;
67
68    /// Gets the AST node's rule exceptions set.
69    ///
70    /// The set is the comma-delimited list of rule identifiers that follows a
71    /// `#@ except:` comment.
72    fn rule_exceptions(&self) -> HashSet<String>;
73
74    /// Determines if a given rule id is excepted for the syntax node.
75    fn is_rule_excepted(&self, id: &str) -> bool;
76}
77
78impl SyntaxNodeExt for SyntaxNode {
79    fn except_comments(&self) -> impl Iterator<Item = SyntaxToken> + '_ {
80        self.siblings_with_tokens(Direction::Prev)
81            .skip(1)
82            .map_while(|s| {
83                if s.kind() == SyntaxKind::Whitespace || s.kind() == SyntaxKind::Comment {
84                    s.into_token()
85                } else {
86                    None
87                }
88            })
89            .filter(|t| t.kind() == SyntaxKind::Comment)
90    }
91
92    fn rule_exceptions(&self) -> HashSet<String> {
93        let mut set = HashSet::default();
94        for comment in self.except_comments() {
95            if let Some(ids) = comment.text().strip_prefix(EXCEPT_COMMENT_PREFIX) {
96                for id in ids.split(',') {
97                    let id = id.trim();
98                    set.insert(id.to_string());
99                }
100            }
101        }
102
103        set
104    }
105
106    fn is_rule_excepted(&self, id: &str) -> bool {
107        for comment in self.except_comments() {
108            if let Some(ids) = comment.text().strip_prefix(EXCEPT_COMMENT_PREFIX) {
109                if ids.split(',').any(|i| i.trim() == id) {
110                    return true;
111                }
112            }
113        }
114
115        false
116    }
117}