Skip to content

Commit 2750076

Browse files
committed
Allow negation directly after a binary operator.
This should address #294.
1 parent 4a5df2d commit 2750076

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

jaq-core/src/load/lex.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,16 @@ impl<'a> Lexer<&'a str> {
291291
fn token(&mut self) -> Option<Token<&'a str>> {
292292
self.space();
293293

294-
let is_op = |c| "|=!<>+-*/%".contains(c);
294+
let hd_op = |c| "|=!<>+-*/%".contains(c);
295+
let tl_op = |c| hd_op(c) && c != '-';
295296

296297
let mut chars = self.i.chars();
297298
let (s, tok) = match chars.next()? {
298299
'a'..='z' | 'A'..='Z' | '_' => (self.consumed(1, Self::mod_then_ident), Tok::Word),
299300
'$' => (self.consumed(1, Self::ident1), Tok::Var),
300301
'@' => (self.consumed(1, Self::ident1), Tok::Fmt),
301302
'0'..='9' => (self.consumed(1, Self::num), Tok::Num),
302-
c if is_op(c) => (self.consumed(1, |lex| lex.trim(is_op)), Tok::Sym),
303+
c if hd_op(c) => (self.consumed(1, |lex| lex.trim(tl_op)), Tok::Sym),
303304
'.' => match chars.next() {
304305
Some('.') => (self.take(2), Tok::Sym),
305306
Some('a'..='z' | 'A'..='Z' | '_') => (self.consumed(2, Self::ident0), Tok::Sym),

jaq-core/tests/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ yields!(atomic_opt, "def x: 0; try x? catch 1", 0);
141141
yields!(neg_arr_iter1, "[-[][]]", json!([]));
142142
yields!(neg_arr_iter2, "try (-[])[] catch 0", 0);
143143

144+
yields!(neg_after_bin, ".+=-1", -1);
145+
144146
yields!(interpolation, r#"1 | "yields \(.+1)!""#, "yields 2!");
145147
// this diverges from jq, which yields ["2 2", "3 2", "2 4", "3 4"],
146148
// probably due to different order of evaluation addition

0 commit comments

Comments
 (0)