Skip to content

Commit

Permalink
fix(marzano_code_snippet): inside of MarzanoCodeSnippet::execute()
Browse files Browse the repository at this point in the history
…use `Pattern::AstLeafNode::is_equivalent_class()` (a new method) to ensure we match string_scalar, single_quote_scalar, and double_quote_scalar in yaml

fixes #394
  • Loading branch information
Alex-ley-scrub committed Oct 29, 2024
1 parent ea0997e commit 7a0818e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
6 changes: 6 additions & 0 deletions crates/core/src/ast_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ impl AstLeafNode {
text: text.to_owned(),
})
}
pub fn is_equivalent_class(&self, sort: SortId) -> bool {
if let Some(e) = &self.equivalence_class {
return e.contains(sort);
}
false
}
}

impl AstLeafNodePattern<MarzanoQueryContext> for AstLeafNode {
Expand Down
18 changes: 13 additions & 5 deletions crates/core/src/marzano_code_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,19 @@ impl Matcher<MarzanoQueryContext> for MarzanoCodeSnippet {
return Ok(false);
};

if let Some((_, pattern)) = self
.patterns
.iter()
.find(|(id, _)| *id == node.node.kind_id())
{
if let Some((_, pattern)) = self.patterns.iter().find(|(id, p)| {
let kind_id = node.node.kind_id();
if *id == kind_id {
return true;
}
// use equivalence classes to match 'ubuntu-latest' and "ubuntu-latest" in yaml
// i.e. to match string_scalar, single_quote_scalar, and double_quote_scalar
// see https://github.com/getgrit/gritql/issues/394
match p {
Pattern::AstLeafNode(p) => p.is_equivalent_class(kind_id),
_ => false, // TODO: handle other pattern types?
}
}) {
pattern.execute(resolved, state, context, logs)
} else {
Ok(false)
Expand Down
7 changes: 7 additions & 0 deletions crates/language/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ impl LeafEquivalenceClass {
.is_some_and(|s| s == self.representative)
})
}
pub fn contains(&self, sort: SortId) -> bool {
self.class.iter().any(|c| c.sort == sort)
}
pub(crate) fn new(
representative: &str,
sort: SortId,
Expand Down Expand Up @@ -120,6 +123,10 @@ pub(crate) fn normalize_double_quote_string(s: &str) -> Option<&str> {
s.strip_prefix('"')?.strip_suffix('"')
}

pub(crate) fn normalize_single_quote_string(s: &str) -> Option<&str> {
s.strip_prefix('\'')?.strip_suffix('\'')
}

pub(crate) fn kind_and_field_id_for_field_map(
lang: &TSLanguage,
names: Vec<(&str, &str, FieldExpectationCondition)>,
Expand Down
21 changes: 19 additions & 2 deletions crates/language/src/yaml.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::language::{
fields_for_nodes, normalize_double_quote_string, normalize_identity, Field,
LeafEquivalenceClass, LeafNormalizer, MarzanoLanguage, NodeTypes, SortId, TSLanguage,
fields_for_nodes, normalize_double_quote_string, normalize_identity,
normalize_single_quote_string, Field, LeafEquivalenceClass, LeafNormalizer, MarzanoLanguage,
NodeTypes, SortId, TSLanguage,
};
use grit_util::Language;
use marzano_util::node_with_source::NodeWithSource;
Expand Down Expand Up @@ -45,6 +46,10 @@ impl Yaml {
language.id_for_node_kind("double_quote_scalar", true),
normalize_double_quote_string,
),
LeafNormalizer::new(
language.id_for_node_kind("single_quote_scalar", true),
normalize_single_quote_string,
),
]]
});
let metavariable_sort = language.id_for_node_kind("grit_metavariable", true);
Expand Down Expand Up @@ -154,6 +159,18 @@ mod tests {
assert!(!nodes.is_empty());
}

#[test]
fn simple_strings() {
let snippet = "steps:\n - runs_on: \"ubuntu-latest\"\n - runs_on: 'ubuntu-latest'\n - runs_on: ubuntu-latest";
let lang = Yaml::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
for node in &nodes {
print_node(&node.node)
}
assert!(!nodes.is_empty());
}

#[test]
fn simple_yaml_metavariable() {
let snippet = "$list";
Expand Down

0 comments on commit 7a0818e

Please sign in to comment.