Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend path syntax #145

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions jaq-interpret/tests/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ fn index_access() {
#[test]
fn iter_access() {
gives(json!([0, 1, 2]), ".[]", [json!(0), json!(1), json!(2)]);
gives(json!({"a": [1, 2]}), ".a[]", [json!(1), json!(2)]);
gives(json!({"a": [1, 2]}), ".a.[]", [json!(1), json!(2)]);
gives(json!({"a": 1, "b": 2}), ".[]", [json!(1), json!(2)]);
// TODO: correct this
//gives(json!({"b": 2, "a": 1}), ".[]", [json!(2), json!(1)]);
Expand Down
5 changes: 2 additions & 3 deletions jaq-parse/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,8 @@ pub fn filter() -> impl Parser<Token, Spanned<Filter>, Error = Simple<Token>> +

// e.g. `.[].a` or `.a`
let id = just(Token::Dot).map_with_span(|_, span| (Filter::Id, span));
let index = super::path::index(with_comma.clone());
let index_path = index.or_not().chain(atom_path());
let id_with_path = id.then(index_path.collect());
let id_path = super::path::part(with_comma.clone()).chain(atom_path());
let id_with_path = id.then(id_path.or_not().flatten());

let path = atom_with_path.or(id_with_path);

Expand Down
63 changes: 36 additions & 27 deletions jaq-parse/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,52 @@ where
.labelled("object key")
}

pub fn index<T, P>(expr: P) -> impl Parser<Token, (Part<Spanned<T>>, Opt), Error = P::Error> + Clone
fn index<T, P>(expr: P) -> impl Parser<Token, Part<Spanned<T>>, Error = P::Error> + Clone
where
T: From<Str<Spanned<T>>> + From<Call<Spanned<T>>>,
P: Parser<Token, Spanned<T>, Error = Simple<Token>> + Clone,
{
key(expr)
.map_with_span(|id, span| Part::Index((T::from(id), span)))
.then(opt())
key(expr).map_with_span(|id, span| Part::Index((T::from(id), span)))
}

pub fn path<T, P>(expr: P) -> impl Parser<Token, Path<T>, Error = P::Error> + Clone
/// Match `[]`, `[e]`, `[e:]`, `[e:e]`, `[:e]` (all without brackets).
fn range<T, P>(expr: P) -> impl Parser<Token, Part<Spanned<T>>, Error = P::Error> + Clone
where
T: From<Str<Spanned<T>>> + From<Call<Spanned<T>>>,
P: Parser<Token, Spanned<T>, Error = Simple<Token>> + Clone,
{
let range = {
let e2 = just(Token::Colon).ignore_then(expr.clone().or_not());
let starts_with_expr = expr.clone().then(e2.or_not()).map(|(e1, e2)| match e2 {
None => Part::Index(e1),
Some(e2) => Part::Range(Some(e1), e2),
});
let starts_with_colon = just(Token::Colon)
.ignore_then(expr.clone())
.map(|e2| Part::Range(None, Some(e2)));
let e2 = just(Token::Colon).ignore_then(expr.clone().or_not());
let starts_with_expr = expr.clone().then(e2.or_not()).map(|(e1, e2)| match e2 {
None => Part::Index(e1),
Some(e2) => Part::Range(Some(e1), e2),
});
let starts_with_colon = just(Token::Colon)
.ignore_then(expr.clone())
.map(|e2| Part::Range(None, Some(e2)));

starts_with_expr
.or(starts_with_colon)
.or_not()
.map(|o| o.unwrap_or(Part::Range(None, None)))
};

let ranges = Delim::Brack.around(range).then(opt()).repeated();
starts_with_expr
.or(starts_with_colon)
.or_not()
.map(|o| o.unwrap_or(Part::Range(None, None)))
}

let dot_id = just(Token::Dot).ignore_then(index(expr));
/// A path after an atomic filter (that is not the identity filter).
pub fn path<T, P>(expr: P) -> impl Parser<Token, Path<T>, Error = P::Error> + Clone
where
T: From<Str<Spanned<T>>> + From<Call<Spanned<T>>>,
P: Parser<Token, Spanned<T>, Error = Simple<Token>> + Clone,
{
let range = Delim::Brack.around(range(expr.clone()));
let dot_index = just(Token::Dot).ignore_then(index(expr));
let dot_range = just(Token::Dot).or_not().ignore_then(range);
dot_index.or(dot_range).then(opt()).repeated()
}

ranges
.clone()
.chain(dot_id.chain(ranges).repeated().flatten())
.collect()
/// The first part of a path after an identity filter.
pub fn part<T, P>(expr: P) -> impl Parser<Token, (Part<Spanned<T>>, Opt), Error = P::Error> + Clone
where
T: From<Str<Spanned<T>>> + From<Call<Spanned<T>>>,
P: Parser<Token, Spanned<T>, Error = Simple<Token>> + Clone,
{
let range = Delim::Brack.around(range(expr.clone()));
range.or(index(expr)).then(opt())
}