Skip to content

Commit

Permalink
feat: implements true WDL string literal parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
claymcleod committed May 10, 2024
1 parent 35731ec commit 4d6f2bf
Show file tree
Hide file tree
Showing 32 changed files with 2,021 additions and 112 deletions.
7 changes: 7 additions & 0 deletions wdl-ast/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

* String literals are properly parsed
([#34](https://github.com/stjude-rust-labs/wdl/pull/34)).

### Fixed

* Fix ignoring comments in expressions ([#23](https://github.com/stjude-rust-labs/wdl/pull/23)).

## 0.1.0 — 12-17-2023
Expand Down
10 changes: 9 additions & 1 deletion wdl-ast/src/v1/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ impl Document {
/// # Examples
///
/// ```
/// use ast::v1::document::expression::literal::string::inner::Component;
/// use ast::v1::document::expression::literal::string::Inner;
/// use ast::v1::document::expression::literal::String;
/// use ast::v1::document::identifier::singular::Identifier;
/// use grammar::v1::Rule;
/// use wdl_ast as ast;
Expand All @@ -114,7 +117,12 @@ impl Document {
/// assert_eq!(tree.imports().len(), 1);
///
/// let import = tree.imports().first().unwrap();
/// assert_eq!(import.uri(), "../hello.wdl");
/// assert_eq!(
/// import.uri(),
/// &String::DoubleQuoted(Inner::new(vec![Component::LiteralContents(
/// std::string::String::from("../hello.wdl")
/// )]))
/// );
/// assert_eq!(import.r#as().unwrap().as_str(), "hello");
/// assert_eq!(
/// import.aliases().unwrap().get("foo"),
Expand Down
14 changes: 12 additions & 2 deletions wdl-ast/src/v1/document/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,17 @@ impl Builder {
///
/// ```
/// use ast::v1::document;
/// use ast::v1::document::expression::literal::string::inner::Component;
/// use ast::v1::document::expression::literal::string::Inner;
/// use ast::v1::document::expression::literal::String;
/// use ast::v1::document::import::Builder;
/// use wdl_ast as ast;
/// use wdl_grammar as grammar;
///
/// let import = Builder::default()
/// .uri(String::from("../mapping.wdl"))?
/// .uri(String::DoubleQuoted(Inner::new(vec![
/// Component::LiteralContents(std::string::String::from("../mapping.wdl")),
/// ])))?
/// .try_build()?;
///
/// let document = document::Builder::default()
Expand All @@ -116,7 +121,12 @@ impl Builder {
/// .try_build()?;
///
/// let import = document.imports().into_iter().next().unwrap();
/// assert_eq!(import.uri(), "../mapping.wdl");
/// assert_eq!(
/// import.uri(),
/// &String::DoubleQuoted(Inner::new(vec![Component::LiteralContents(
/// std::string::String::from("../mapping.wdl"),
/// )]))
/// );
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
Expand Down
2 changes: 1 addition & 1 deletion wdl-ast/src/v1/document/declaration/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl TryFrom<Pair<'_, grammar::v1::Rule>> for Type {
Rule::object_type => kind = Some(Kind::Object),
Rule::struct_type => {
kind = {
let identifier_node = extract_one!(node, singular_identifier, struct_type)?;
let identifier_node = extract_one!(node, singular_identifier, struct_type);
let identifier =
Identifier::try_from(identifier_node).map_err(Error::Identifier)?;
Some(Kind::Struct(identifier))
Expand Down
12 changes: 3 additions & 9 deletions wdl-ast/src/v1/document/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use pest::pratt_parser::Op;
use pest::pratt_parser::PrattParser;
use wdl_grammar as grammar;
use wdl_macros::check_node;
use wdl_macros::extract_one;

use crate::v1::document::expression::literal::String;
use crate::v1::document::identifier::singular;
use crate::v1::document::identifier::singular::Identifier;

mod array;
mod r#if;
mod literal;
pub mod literal;
mod map;
mod object;
mod pair;
Expand Down Expand Up @@ -246,13 +246,7 @@ fn parse<'a, P: Iterator<Item = pest::iterators::Pair<'a, grammar::v1::Rule>>>(
.parse::<OrderedFloat<f64>>()
.map_err(Error::ParseFloat)?,
))),
Rule::string => {
// TODO: parse strings with placeholders properly.
let inner = extract_one!(node, string_inner, string)?;
Ok(Expression::Literal(Literal::String(
inner.as_str().to_owned(),
)))
}
Rule::string => Ok(Expression::Literal(Literal::String(String::from(node)))),
Rule::none => Ok(Expression::Literal(Literal::None)),
Rule::singular_identifier => {
let identifier =
Expand Down
36 changes: 32 additions & 4 deletions wdl-ast/src/v1/document/expression/if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,21 @@ impl If {
/// # Examples
///
/// ```
/// use ast::v1::document::expression::literal::string::inner::Component;
/// use ast::v1::document::expression::literal::string::Inner;
/// use ast::v1::document::expression::literal::String;
/// use ast::v1::document::expression::If;
/// use ast::v1::document::expression::Literal;
/// use ast::v1::document::Expression;
/// use wdl_ast as ast;
///
/// let r#if = If::new(
/// Box::new(Expression::Literal(Literal::Boolean(false))),
/// Box::new(Expression::Literal(Literal::String(String::from("foo")))),
/// Box::new(Expression::Literal(Literal::String(String::DoubleQuoted(
/// Inner::new(vec![Component::LiteralContents(std::string::String::from(
/// "foo",
/// ))]),
/// )))),
/// Box::new(Expression::Literal(Literal::Boolean(true))),
/// );
///
Expand Down Expand Up @@ -85,14 +92,21 @@ impl If {
/// # Examples
///
/// ```
/// use ast::v1::document::expression::literal::string::inner::Component;
/// use ast::v1::document::expression::literal::string::Inner;
/// use ast::v1::document::expression::literal::String;
/// use ast::v1::document::expression::If;
/// use ast::v1::document::expression::Literal;
/// use ast::v1::document::Expression;
/// use wdl_ast as ast;
///
/// let r#if = If::new(
/// Box::new(Expression::Literal(Literal::Boolean(false))),
/// Box::new(Expression::Literal(Literal::String(String::from("foo")))),
/// Box::new(Expression::Literal(Literal::String(String::DoubleQuoted(
/// Inner::new(vec![Component::LiteralContents(std::string::String::from(
/// "foo",
/// ))]),
/// )))),
/// Box::new(Expression::Literal(Literal::Boolean(true))),
/// );
///
Expand All @@ -110,14 +124,21 @@ impl If {
/// # Examples
///
/// ```
/// use ast::v1::document::expression::literal::string::inner::Component;
/// use ast::v1::document::expression::literal::string::Inner;
/// use ast::v1::document::expression::literal::String;
/// use ast::v1::document::expression::If;
/// use ast::v1::document::expression::Literal;
/// use ast::v1::document::Expression;
/// use wdl_ast as ast;
///
/// let r#if = If::new(
/// Box::new(Expression::Literal(Literal::Boolean(false))),
/// Box::new(Expression::Literal(Literal::String(String::from("foo")))),
/// Box::new(Expression::Literal(Literal::String(String::DoubleQuoted(
/// Inner::new(vec![Component::LiteralContents(std::string::String::from(
/// "foo",
/// ))]),
/// )))),
/// Box::new(Expression::Literal(Literal::Boolean(true))),
/// );
///
Expand All @@ -135,14 +156,21 @@ impl If {
/// # Examples
///
/// ```
/// use ast::v1::document::expression::literal::string::inner::Component;
/// use ast::v1::document::expression::literal::string::Inner;
/// use ast::v1::document::expression::literal::String;
/// use ast::v1::document::expression::If;
/// use ast::v1::document::expression::Literal;
/// use ast::v1::document::Expression;
/// use wdl_ast as ast;
///
/// let r#if = If::new(
/// Box::new(Expression::Literal(Literal::Boolean(false))),
/// Box::new(Expression::Literal(Literal::String(String::from("foo")))),
/// Box::new(Expression::Literal(Literal::String(String::DoubleQuoted(
/// Inner::new(vec![Component::LiteralContents(std::string::String::from(
/// "foo",
/// ))]),
/// )))),
/// Box::new(Expression::Literal(Literal::Boolean(true))),
/// );
/// assert!(matches!(
Expand Down
4 changes: 4 additions & 0 deletions wdl-ast/src/v1/document/expression/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use ordered_float::OrderedFloat;

use crate::v1::document::identifier::singular::Identifier;

pub mod string;

pub use string::String;

/// An literal value within an [`Expression`](super::Expression).
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Literal {
Expand Down
Loading

0 comments on commit 4d6f2bf

Please sign in to comment.