Skip to content

Commit

Permalink
implement filter qualifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
suaviloquence committed Jul 16, 2024
1 parent 15e9855 commit 26fd392
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
15 changes: 15 additions & 0 deletions examples/inputs/qualifiers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>

<html>
<body>
<ul>
<li>Hello! 1</li>
<li><span class="special">special!</span>Hello! 2</li>
<li>Hello! 3</li>
<li>Hello! 4</li>
<li>Hello! 5</li>
<li>Hello! 6</li>
<li>Hello! 7</li>
</ul>
</body>
</html>
45 changes: 45 additions & 0 deletions examples/outputs/qualifiers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"items": [
{
"special": null,
"txt": "Hello! 1"
},
{
"special": "special!",
"txt": "Hello! 2"
},
{
"special": null,
"txt": "Hello! 3"
},
{
"special": null,
"txt": "Hello! 4"
},
{
"special": null,
"txt": "Hello! 5"
},
{
"special": null,
"txt": "Hello! 6"
},
{
"special": null,
"txt": "Hello! 7"
}
],
"sixth": {
"special": null,
"txt": "Hello! 6"
},
"specials": [
null,
"special!",
null,
null,
null,
null,
null
]
}
7 changes: 7 additions & 0 deletions examples/scrps/qualifiers.scrp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
items: li {
special: .special { txt: $text; }? | take(key: "txt")?;
txt: $text;
}*;

specials: $items | take(key: "special")*;
sixth: $items | nth(i: 5);
4 changes: 3 additions & 1 deletion src/frontend/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ impl<'a> Parser<'a> {
let lx = self.scanner.peek_non_whitespace();

match lx.token {
Token::Id | Token::Less => self.parse_element().map(RValue::Element),
Token::Id | Token::Less | Token::Dot | Token::Hash => {
self.parse_element().map(RValue::Element)
}
_ => self.parse_leaf().map(RValue::Leaf),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn float<'doc>(value: PValue<'doc>) -> anyhow::Result<PValue<'doc>> {
fn nth<'doc>(mut value: ListIter<'doc>, i: i64) -> anyhow::Result<PValue<'doc>> {
match value.nth(i.try_into().context("negative indices are not supported")?) {
Some(x) => Ok(x),
None => anyhow::bail!(""),
None => anyhow::bail!("No element at index {i}"),
}
}

Expand Down
16 changes: 13 additions & 3 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use anyhow::Context;
use execution_mode::ExecutionMode;
use reqwest::IntoUrl;

use value::EValue;
use value::{EValue, ListIter};

use crate::frontend::{
ast::{AstRef, Element, FilterList, Inline, Leaf, RValue, Statement, StatementList},
ast::{AstRef, Element, FilterList, Inline, Leaf, Qualifier, RValue, Statement, StatementList},
AstArena,
};

Expand Down Expand Up @@ -233,7 +233,16 @@ impl<'ast> Interpreter<'ast> {
.map(|arg| Ok((arg.id, ctx.leaf_to_value(&arg.value)?)))
.collect::<Result<BTreeMap<_, _>>>()?;

filter::dispatch_filter(filter.id, value, args, ctx)
match filter.qualifier {
Qualifier::One => filter::dispatch_filter(filter.id, value, args, ctx),
Qualifier::Optional if matches!(value, Value::Null) => Ok(Value::Null),
Qualifier::Optional => filter::dispatch_filter(filter.id, value, args, ctx),
Qualifier::Collection => value
.try_unwrap::<ListIter>()?
.map(|value| filter::dispatch_filter(filter.id, value, args.clone(), ctx))
.collect::<Result<Vec<_>>>()
.map(Value::List),
}
})
.map(EValue::from)
}
Expand Down Expand Up @@ -408,5 +417,6 @@ mod tests {
integration_test! {
abc,
attr,
qualifiers,
}
}

0 comments on commit 26fd392

Please sign in to comment.