Fix destructuring alternation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Attempting to use the existing FORK_OPT opcode resulted in difficulty knowing when to pop an error message off the stack and when not to. This commit makes DESTRUCTURE_ALT a real opcode that is identical to FORK_OPT, except for never pushing the error message onto the stack when continuing from an error backtrack. Some small changes were necessary to the DUP/POP behavior surrounding destructuring to accomodate this.
authored by William Langford · committed by William Langford · parent 3dc5f4e · Browse files · 4 files·+122 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+122 added−21 removed
- tests:+101 added−0 removed
- other:+21 added−21 removed
Provenance
- Commit
0673dee- Tree
4b6f2de- Author
William Langford <wlangfor@gmail.com>- Committer
William Langford <wlangfor@gmail.com>- Parent
3dc5f4e
4 files changed+122 added−21 removed
modified:src/compile.c+11 added-17 removed
757 unmodified lines758 }759 760 /* Build wrappers around destructuring matchers so that we can chain them761 * when we have errors. The approach is as follows:762- * FORK_OPT NEXT_MATCHER (unless last matcher)762+ * DESTRUCTURE_ALT NEXT_MATCHER (unless last matcher)763 * existing_matcher_block764 * JUMP BODY765 */766 static block bind_alternation_matchers(block matchers, block body) {767 block preamble = {0};768 block altmatchers = {0};769 block mb = {0};770+ block final_matcher = matchers;771 772 // Pass through the matchers to find all destructured names.772- while (matchers.first && matchers.first->op == DESTRUCTURE_ALT) {773- block_append(&altmatchers, inst_block(block_take(&matchers)));773+ while (final_matcher.first && final_matcher.first->op == DESTRUCTURE_ALT) {774+ block_append(&altmatchers, inst_block(block_take(&final_matcher)));775 }776 777 // We don't have any alternations here, so we can use the simplest case.778 if (altmatchers.first == NULL) {778- return bind_matcher(matchers, body);779+ return bind_matcher(final_matcher, body);780 }781 781- // The final matcher needs to strip the error from the previous FORK_OPT782- block final_matcher = BLOCK(gen_op_simple(POP), gen_op_simple(DUP), matchers);783-782 // Collect var names783 jv all_vars = jv_object();784 block_get_unbound_vars(altmatchers, &all_vars);785 block_get_unbound_vars(final_matcher, &all_vars);11 unmodified lines797 // Now we build each matcher in turn798 for (inst *i = altmatchers.first; i; i = i->next) {799 block submatcher = i->subfn;800 803- // Get rid of the error from the previous matcher804- if (mb.first != NULL) {805- submatcher = BLOCK(gen_op_simple(POP), gen_op_simple(DUP), submatcher);806- }807-801 // If we're successful, jump to the end of the matchers802 submatcher = BLOCK(submatcher, gen_op_target(JUMP, final_matcher));803 811- // FORK_OPT to the end of this submatcher so we can skip to the next one on error812- mb = BLOCK(mb, gen_op_target(FORK_OPT, submatcher), submatcher);804+ // DESTRUCTURE_ALT to the end of this submatcher so we can skip to the next one on error805+ mb = BLOCK(mb, gen_op_target(DESTRUCTURE_ALT, submatcher), submatcher);806 807 // We're done with this inst and we don't want it anymore808 // But we can't let it free the submatcher block.809 i->subfn.first = i->subfn.last = NULL;185 unmodified lines995 block top = gen_noop();996 if (body.first && body.first->op == TOP)997 top = inst_block(block_take(&body));998 1006- if (matchers.first && matchers.first->op == DESTRUCTURE_ALT && !block_is_noop(var)) {999+ if (matchers.first && matchers.first->op == DESTRUCTURE_ALT) {1000 block_append(&var, gen_op_simple(DUP));1008- block_append(&matchers, gen_op_simple(POP));1001+ } else {1002+ top = BLOCK(top, gen_op_simple(DUP));1003 }1004 1011- return BLOCK(top, gen_op_simple(DUP), gen_subexp(var), gen_op_simple(POP), bind_alternation_matchers(matchers, body));1005+ return BLOCK(top, gen_subexp(var), gen_op_simple(POP), bind_alternation_matchers(matchers, body));1006 }1007 1008 // Like gen_var_binding(), but bind `break`'s wildcard unbound variable1009 static block gen_wildvar_binding(block var, const char* name, block body) {377 unmodified linesmodified:src/execute.c+9 added-3 removed
797 unmodified lines798 break;799 }800 801 case FORK_OPT:802+ case DESTRUCTURE_ALT:803 case FORK: {804 stack_save(jq, pc - 1, stack_get_pos(jq));805 pc++; // skip offset this time806 break;807 }808 808- case ON_BACKTRACK(FORK_OPT): {809+ case ON_BACKTRACK(FORK_OPT):810+ case ON_BACKTRACK(DESTRUCTURE_ALT): {811 if (jv_is_valid(jq->error)) {812 // `try EXP ...` backtracked here (no value, `empty`), so we backtrack more813 jv_free(stack_pop(jq));814 goto do_backtrack;815 }816 // `try EXP ...` exception caught in EXP817+ // DESTRUCTURE_ALT doesn't want the error message on the stack,818+ // as we would just want to throw it away anyway.819+ if (opcode != ON_BACKTRACK(DESTRUCTURE_ALT)) {815- jv_free(stack_pop(jq)); // free the input816- stack_push(jq, jv_invalid_get_msg(jq->error)); // push the error's message820+ jv_free(stack_pop(jq)); // free the input821+ stack_push(jq, jv_invalid_get_msg(jq->error)); // push the error's message822+ }823 jq->error = jv_null();824 uint16_t offset = *pc++;825 pc += offset;826 break;430 unmodified linesmodified:src/opcode_list.h+1 added-1 removed
42 unmodified lines43 OP(DEPS, CONSTANT, 0, 0)44 OP(MODULEMETA, CONSTANT, 0, 0)45 OP(GENLABEL, NONE, 0, 1)46 47-OP(DESTRUCTURE_ALT, NONE, 0, 0)47+OP(DESTRUCTURE_ALT, BRANCH, 0, 0)48 OP(STOREVN, VARIABLE, 1, 0)modified:tests/jq.test+101 added
724 unmodified lines725 [1, null, 2, 3, null, null]726 [4, 5, null, null, 7, null]727 [null, null, null, null, null, "foo"]728 729+# Destructuring DUP/POP issues730+.[] | . as {a:$a} ?// {a:$a} ?// {a:$a} | $a731+[[3],[4],[5],6]732+# Runtime error: "jq: Cannot index array with string \"c\""733+734+.[] as {a:$a} ?// {a:$a} ?// {a:$a} | $a735+[[3],[4],[5],6]736+# Runtime error: "jq: Cannot index array with string \"c\""737+738+[[3],[4],[5],6][] | . as {a:$a} ?// {a:$a} ?// {a:$a} | $a739+null740+# Runtime error: "jq: Cannot index array with string \"c\""741+742+[[3],[4],[5],6] | .[] as {a:$a} ?// {a:$a} ?// {a:$a} | $a743+null744+# Runtime error: "jq: Cannot index array with string \"c\""745+746+.[] | . as {a:$a} ?// {a:$a} ?// $a | $a747+[[3],[4],[5],6]748+[3]749+[4]750+[5]751+6752+753+.[] as {a:$a} ?// {a:$a} ?// $a | $a754+[[3],[4],[5],6]755+[3]756+[4]757+[5]758+6759+760+[[3],[4],[5],6][] | . as {a:$a} ?// {a:$a} ?// $a | $a761+null762+[3]763+[4]764+[5]765+6766+767+[[3],[4],[5],6] | .[] as {a:$a} ?// {a:$a} ?// $a | $a768+null769+[3]770+[4]771+[5]772+6773+774+.[] | . as {a:$a} ?// $a ?// {a:$a} | $a775+[[3],[4],[5],6]776+[3]777+[4]778+[5]779+6780+781+.[] as {a:$a} ?// $a ?// {a:$a} | $a782+[[3],[4],[5],6]783+[3]784+[4]785+[5]786+6787+788+[[3],[4],[5],6][] | . as {a:$a} ?// $a ?// {a:$a} | $a789+null790+[3]791+[4]792+[5]793+6794+795+[[3],[4],[5],6] | .[] as {a:$a} ?// $a ?// {a:$a} | $a796+null797+[3]798+[4]799+[5]800+6801+802+.[] | . as $a ?// {a:$a} ?// {a:$a} | $a803+[[3],[4],[5],6]804+[3]805+[4]806+[5]807+6808+809+.[] as $a ?// {a:$a} ?// {a:$a} | $a810+[[3],[4],[5],6]811+[3]812+[4]813+[5]814+6815+816+[[3],[4],[5],6][] | . as $a ?// {a:$a} ?// {a:$a} | $a817+null818+[3]819+[4]820+[5]821+6822+823+[[3],[4],[5],6] | .[] as $a ?// {a:$a} ?// {a:$a} | $a824+null825+[3]826+[4]827+[5]828+6829+830 . as $dot|any($dot[];not)831 [1,2,3,4,true,false,1,2,3,4,5]832 true833 686 unmodified lines