-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds the abstract syntax tree package,
wdl-ast
- Loading branch information
1 parent
66da811
commit b3e9e73
Showing
155 changed files
with
14,278 additions
and
1,634 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
target/ | ||
.vscode/ | ||
|
||
rustc-ice* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[package] | ||
name = "wdl-ast" | ||
version = "0.1.0" | ||
authors = ["Clay McLeod <clay.l.mcleod@gmail.com>"] | ||
edition.workspace = true | ||
license.workspace = true | ||
description = "Abstract syntax tree for Workflow Description Language (WDL) documents" | ||
homepage = "https://github.com/stjude-rust-labs/wdl" | ||
repository = "https://github.com/stjude-rust-labs/wdl" | ||
documentation = "https://docs.rs/wdl-ast" | ||
|
||
[dependencies] | ||
clap = { workspace = true, optional = true } | ||
env_logger = { workspace = true, optional = true } | ||
indexmap.workspace = true | ||
lazy_static.workspace = true | ||
log = { workspace = true, optional = true } | ||
nonempty.workspace = true | ||
ordered-float.workspace = true | ||
pest.workspace = true | ||
regex.workspace = true | ||
tokio = { workspace = true, optional = true } | ||
wdl-core = { path = "../wdl-core", version = "0.1.0" } | ||
wdl-grammar = { path = "../wdl-grammar", version = "0.1.0" } | ||
|
||
[features] | ||
binaries = ["dep:clap", "dep:env_logger", "dep:log", "dep:tokio"] | ||
|
||
[[bin]] | ||
name = "wdl-ast" | ||
path = "src/main.rs" | ||
required-features = ["binaries"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//! Subcommands for the `wdl-ast` command-line tool. | ||
pub mod parse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//! `wdl-ast parse` | ||
use core::lint::warning::display; | ||
use std::path::PathBuf; | ||
|
||
use clap::Parser; | ||
|
||
use log::warn; | ||
use wdl_ast as ast; | ||
use wdl_core as core; | ||
use wdl_grammar as grammar; | ||
|
||
/// An error related to the `wdl-ast parse` subcommand. | ||
#[derive(Debug)] | ||
pub enum Error { | ||
/// An abstract syntax tree error. | ||
Ast(ast::Error), | ||
|
||
/// An input/output error. | ||
InputOutput(std::io::Error), | ||
|
||
/// A grammar error. | ||
Grammar(grammar::Error<grammar::v1::Rule>), | ||
} | ||
|
||
impl std::fmt::Display for Error { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Error::Ast(err) => write!(f, "ast error: {err}"), | ||
Error::InputOutput(err) => write!(f, "i/o error: {err}"), | ||
Error::Grammar(err) => write!(f, "grammar error: {err}"), | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for Error {} | ||
|
||
/// A [`Result`](std::result::Result) with an [`Error`]. | ||
type Result<T> = std::result::Result<T, Error>; | ||
|
||
/// Arguments for the `wdl-ast parse` subcommand. | ||
#[derive(Debug, Parser)] | ||
pub struct Args { | ||
/// Path to the WDL document. | ||
#[clap(value_name = "PATH")] | ||
path: PathBuf, | ||
|
||
/// The Workflow Description Language (WDL) specification version to use. | ||
#[arg(value_name = "VERSION", short = 's', long, default_value_t, value_enum)] | ||
specification_version: core::Version, | ||
} | ||
|
||
/// Main function for this subcommand. | ||
pub fn parse(args: Args) -> Result<()> { | ||
let contents = std::fs::read_to_string(args.path).map_err(Error::InputOutput)?; | ||
|
||
let document = match args.specification_version { | ||
core::Version::V1 => { | ||
let pt = grammar::v1::parse(grammar::v1::Rule::document, &contents) | ||
.map_err(Error::Grammar)?; | ||
|
||
ast::v1::parse(pt).map_err(Error::Ast)? | ||
} | ||
}; | ||
|
||
if let Some(warnings) = document.warnings() { | ||
for warning in warnings { | ||
let mut buffer = String::new(); | ||
warning.display(&mut buffer, display::Mode::OneLine).unwrap(); | ||
warn!("{}", buffer); | ||
} | ||
} | ||
|
||
for (_, task) in document.tasks() { | ||
dbg!(task.runtime()); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//! Common functionality used across all WDL abstract syntax trees. | ||
mod linter; | ||
mod tree; | ||
mod validator; | ||
|
||
pub use linter::Linter; | ||
pub use tree::Tree; | ||
pub use validator::Validator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//! An abstract syntax tree linter. | ||
use wdl_core as core; | ||
|
||
use core::lint; | ||
use core::lint::Warning; | ||
|
||
use crate::v1::Document; | ||
|
||
/// A [`Result`](std::result::Result) for [`Linter::lint()`]. | ||
pub type Result = std::result::Result<Option<Vec<Warning>>, Box<dyn std::error::Error>>; | ||
|
||
/// An abstract syntax tree linter. | ||
#[derive(Debug)] | ||
pub struct Linter; | ||
|
||
impl Linter { | ||
/// Lints a WDL abstract syntax tree according to a set of lint rules and | ||
/// returns a set of lint warnings (if any are detected). | ||
pub fn lint(tree: &Document) -> Result { | ||
let warnings = crate::v1::lint::RULES | ||
.iter() | ||
.map(|rule| rule.check(tree)) | ||
.collect::<std::result::Result<Vec<Option<Vec<lint::Warning>>>, Box<dyn std::error::Error>>>()? | ||
.into_iter() | ||
.flatten() | ||
.flatten() | ||
.collect::<Vec<lint::Warning>>(); | ||
|
||
match warnings.is_empty() { | ||
true => Ok(None), | ||
false => Ok(Some(warnings)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//! An abstract syntax tree. | ||
use wdl_core as core; | ||
|
||
use core::lint; | ||
|
||
use crate::v1::Document; | ||
|
||
/// An abstract syntax tree with a set of lint [`Warning`](lint::Warning)s. | ||
/// | ||
/// **Note:** this struct implements [`std::ops::Deref`] for a parsed WDL | ||
/// [`Document`], so you can treat this exactly as if you were workings with a | ||
/// [`Document`] directly. | ||
#[derive(Debug)] | ||
pub struct Tree { | ||
/// The inner document. | ||
inner: Document, | ||
|
||
/// The lint warnings associated with the parse tree. | ||
warnings: Option<Vec<lint::Warning>>, | ||
} | ||
|
||
impl Tree { | ||
/// Creates a new [`Tree`]. | ||
pub fn new(inner: Document, warnings: Option<Vec<lint::Warning>>) -> Self { | ||
Self { inner, warnings } | ||
} | ||
|
||
/// Gets the inner [`Document`] for the [`Tree`] by reference. | ||
pub fn inner(&self) -> &Document { | ||
&self.inner | ||
} | ||
|
||
/// Consumes `self` to return the inner [`Document`] from the [`Tree`]. | ||
pub fn into_inner(self) -> Document { | ||
self.inner | ||
} | ||
|
||
/// Gets the [`Warning`](lint::Warning)s from the [`Tree`] by reference. | ||
pub fn warnings(&self) -> Option<&Vec<lint::Warning>> { | ||
self.warnings.as_ref() | ||
} | ||
} | ||
|
||
impl std::ops::Deref for Tree { | ||
type Target = Document; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.inner | ||
} | ||
} |
Oops, something went wrong.