1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00

Add general ? operator

This commit is contained in:
Nicolas Williams
2014-07-06 00:11:55 -05:00
parent 7fce34292e
commit 7d3a44a1e2
3 changed files with 22 additions and 0 deletions
docs/content/3.manual
parser.y
tests

@ -1584,6 +1584,16 @@ sections:
input: 'true'
output: ['"some exception"']
- title: `?` operator
body: |
The `?` operator, used as `EXP?`, is shorthand for `try EXP`.
examples:
- program: '[.[]|(.a)?]'
input: '[{}, true, {"a":1}'
output: ['[null, 1]']
- title: Advanced features
body: |
Variables are an absolute necessity in most programming languages, but

@ -255,6 +255,10 @@ Term "as" '$' IDENT '|' Exp {
$$ = $2;
} |
Exp '?' {
$$ = gen_try($1, gen_op_simple(BACKTRACK));
} |
Exp '=' Exp {
$$ = gen_call("_assign", BLOCK(gen_lambda($1), gen_lambda($3)));

@ -670,6 +670,14 @@ def inc(x): x |= .+1; inc(.[].a)
[0,1,2,3]
["foo","Cannot index number with string \"a\"",3]
[.[]|(.a, .a)?]
[null,true,{"a":1}]
[null,null,1,1]
[[.[]|[.a,.a]]?]
[null,true,{"a":1}]
[]
# string operations
[.[]|startswith("foo")]
["fo", "foo", "barfoo", "foobar", "barfoob"]