Skip to content

Commit

Permalink
fix: ignore comments when parsing Expression. (#23)
Browse files Browse the repository at this point in the history
* fix: ignore comments when parsing `Expression`.

This commit fixes the incorrect filter when iterating over the pairs
provided to the `parse` function for `Expression`.

Fixes #21.

* Update wdl-ast/CHANGELOG.md

Co-authored-by: Clay McLeod <3411613+claymcleod@users.noreply.github.com>

---------

Co-authored-by: Clay McLeod <3411613+claymcleod@users.noreply.github.com>
  • Loading branch information
peterhuene and claymcleod authored May 1, 2024
1 parent 9847e22 commit b3d1106
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions wdl-ast/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

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

## 0.1.0 — 12-17-2023

### Added
Expand Down
22 changes: 20 additions & 2 deletions wdl-ast/src/v1/document/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,10 @@ fn parse<'a, P: Iterator<Item = pest::iterators::Pair<'a, grammar::v1::Rule>>>(
pairs: P,
) -> Result<Expression> {
let pairs = pairs.filter(|node| {
!matches!(node.as_rule(), wdl_grammar::v1::Rule::WHITESPACE)
&& !matches!(node.as_rule(), wdl_grammar::v1::Rule::WHITESPACE)
!matches!(
node.as_rule(),
wdl_grammar::v1::Rule::WHITESPACE | wdl_grammar::v1::Rule::COMMENT
)
});

PRATT_PARSER
Expand Down Expand Up @@ -534,4 +536,20 @@ mod tests {
))))
);
}

#[test]
fn it_ignores_comments_and_whitespace() {
let expr = wdl_macros::test::valid_node!(
"true \n # comment1 \n && \n # comment2 \n false",
expression,
Expression
);
assert_eq!(
expr,
Expression::And(
Expression::Literal(Literal::Boolean(true)).into(),
Expression::Literal(Literal::Boolean(false)).into()
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ impl TryFrom<Pair<'_, grammar::v1::Rule>> for Body {
for node in nodes {
let inner = node
.into_inner()
.filter(|node| {
!matches!(node.as_rule(), Rule::WHITESPACE)
&& !matches!(node.as_rule(), Rule::COMMENT)
})
.filter(|node| !matches!(node.as_rule(), Rule::WHITESPACE | Rule::COMMENT))
.collect::<Vec<_>>();

if inner.len() != 1 && inner.len() != 2 {
Expand Down

0 comments on commit b3d1106

Please sign in to comment.