]> granicus.if.org Git - jq/commitdiff
Extend `{foo}` syntax to allow `{"foo"}` as well.
authorStephen Dolan <mu@netsoc.tcd.ie>
Mon, 3 Dec 2012 20:31:40 +0000 (20:31 +0000)
committerStephen Dolan <mu@netsoc.tcd.ie>
Mon, 3 Dec 2012 20:31:40 +0000 (20:31 +0000)
Useful when "foo" contains unusual characters. Should help with
the issues #7, #38, #40, #42.

execute.c
opcode_list.h
parser.y
testdata

index 8fadc7e7db3fb0991ef1a96e64ec89714da9b594..8b8036a1f48f51bf7fac2988df5d3a507cb3a1b0 100644 (file)
--- a/execute.c
+++ b/execute.c
@@ -208,6 +208,17 @@ jv jq_next() {
       break;
     }
 
+    case DUP2: {
+      stackval keep = stack_pop();
+      stackval v = stack_pop();
+      stackval v2 = v;
+      v2.value = jv_copy(v.value);
+      stack_push(v);
+      stack_push(keep);
+      stack_push(v2);
+      break;
+    }
+
     case SWAP: {
       stackval a = stack_pop();
       stackval b = stack_pop();
index a39d30e06cb0354cff70b1d8bd384737542759fe..03cdbc8ba262a64482585411df07bc9326ee1b10 100644 (file)
@@ -1,5 +1,6 @@
 OP(LOADK, CONSTANT, 1, 1)
 OP(DUP,   NONE,     1, 2)
+OP(DUP2,  NONE,     2, 3)
 OP(SWAP,  NONE,     2, 2)
 OP(POP,   NONE,     1, 0)
 OP(LOADV, VARIABLE, 1, 1)
index cee3c9d6a113b153e65b895727a1ab69ef544972..e3d32e45ca2369f41e7f63a8f44ae544851ceae6 100644 (file)
--- a/parser.y
+++ b/parser.y
@@ -414,6 +414,10 @@ MkDictPair
 | String ':' ExpD {
   $$ = gen_dictpair($1, $3);
   }
+| String {
+  $$ = gen_dictpair($1, BLOCK(gen_op_simple(POP), gen_op_simple(DUP2),
+                              gen_op_simple(DUP2), gen_op_simple(INDEX)));
+  }
 | IDENT {
   $$ = gen_dictpair(gen_const(jv_copy($1)),
                     gen_index(gen_noop(), gen_const($1)));
index cc8263b01acd87ec7108ccadcf4313a4c72e9a43..088d255db51e2c2da94fc2de0ea96baafb33f6c5 100644 (file)
--- a/testdata
+++ b/testdata
@@ -64,6 +64,10 @@ null
 {"a":1, "b":2, "c":3, "d":"c"}
 {"a":1, "b":2, "c":1, "e":2}
 
+{"a",b,"a$\(1+1)"}
+{"a":1, "b":2, "c":3, "a$2":4}
+{"a":1, "b":2, "a$2":4}
+
 #
 # Field access, piping
 #