summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--docs/content/3.manual/manual.yml10
-rw-r--r--parser.y4
-rw-r--r--tests/all.test8
3 files changed, 22 insertions, 0 deletions
diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml
index 4d3ac45f..973e8d85 100644
--- a/docs/content/3.manual/manual.yml
+++ b/docs/content/3.manual/manual.yml
@@ -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
diff --git a/parser.y b/parser.y
index bfd09d35..eb63381a 100644
--- a/parser.y
+++ b/parser.y
@@ -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)));
diff --git a/tests/all.test b/tests/all.test
index f5c7bd68..4bd15cee 100644
--- a/tests/all.test
+++ b/tests/all.test
@@ -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"]