Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't use keywords in {$key} syntax or as $variables #1868

Closed
hpo14 opened this issue Mar 22, 2019 · 10 comments
Closed

can't use keywords in {$key} syntax or as $variables #1868

hpo14 opened this issue Mar 22, 2019 · 10 comments
Labels

Comments

@hpo14
Copy link

hpo14 commented Mar 22, 2019

Describe the bug
cant use "module" as key name

To Reproduce
jq -ncsM --arg module SODIMM '{ $module, }'

Expected behavior
{"module":"SODIMM"}

Actual behavior

jq: error: syntax error, unexpected module, expecting IDENT (Unix shell quoting issues?) at <top-level>, line 1:
{ $module, }

Environment (please complete the following information):

  • OS and Version: Ubuntu 16.04
  • jq version: version 1.5-1-a5b5cbe

Additional
If i change to use "_module" then the result is fine.

$ jq -ncs --arg _module SODIMM '{ $_module, }'
$ {"_module":"SODIMM"}
@hpo14 hpo14 changed the title can't use "module " as a key can't use "module" as a key name Mar 22, 2019
@nicowilliams nicowilliams changed the title can't use "module" as a key name can't use keywords in {$key} syntax Mar 22, 2019
@nicowilliams
Copy link
Contributor

It's an easy fix. The workaround for now is jq -cn --arg _module SODIMM '{ module:$_module, }'.

@nicowilliams
Copy link
Contributor

Thanks for the report!

nicowilliams added a commit to nicowilliams/jq that referenced this issue Mar 22, 2019
@nicowilliams nicowilliams changed the title can't use keywords in {$key} syntax can't use keywords in {$key} syntax or as $variables Mar 22, 2019
@hpo14
Copy link
Author

hpo14 commented Mar 22, 2019

Thanks for your quick reply.
so, after your patch, can this work as expected ?
jq -ncsM --arg module SODIMM '{ $module, }'

@nicowilliams
Copy link
Contributor

@hpo14 yes. From this commit on my dlopen/co-routines branch:

diff --git a/src/parser.y b/src/parser.y
index f753ae7..dc0c5df 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -578,6 +578,11 @@ Param:
   jv_free($2);
 } |

+'$' Keyword {
+  $$ = gen_param_regular(jv_string_value($2));
+  jv_free($2);
+} |
+
 "@@" IDENT {
   $$ = gen_param_coexpr(jv_string_value($2));
   jv_free($2);
@@ -746,14 +751,19 @@ FORMAT {
   else
     $$ = BLOCK(gen_subexp(gen_const(jv_object())), $2, gen_op_simple(POP));
 } |
-'$' LOC {
-  $$ = gen_const(JV_OBJECT(jv_string("file"), jv_copy(locations->fname),
-                           jv_string("line"), jv_number(locfile_get_line(locations, @$.start) + 1)));
-} |
 '$' IDENT {
   $$ = gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($2)));
   jv_free($2);
 } |
+'$' Keyword {
+  if (strcmp(jv_string_value($2), "__loc__") == 0) {
+    $$ = gen_const(JV_OBJECT(jv_string("file"), jv_copy(locations->fname),
+                             jv_string("line"), jv_number(locfile_get_line(locations, @$.start) + 1)));
+  } else {
+    $$ = gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($2)));
+  }
+  jv_free($2);
+} |
 IDENT {
   const char *s = jv_string_value($1);
   if (strcmp(s, "false") == 0)
@@ -951,6 +961,10 @@ IDENT ':' ExpD {
   $$ = gen_dictpair(gen_const($2),
                     gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($2))));
   }
+| '$' Keyword {
+  $$ = gen_dictpair(gen_const($2),
+                    gen_location(@$, locations, gen_op_unbound(LOADV, jv_string_value($2))));
+  }
 | IDENT {
   $$ = gen_dictpair(gen_const(jv_copy($1)),
                     gen_index(gen_noop(), gen_const($1)));

@hpo14
Copy link
Author

hpo14 commented Mar 22, 2019

should i close this issue ? or just wait ur PR merged ?

@nicowilliams
Copy link
Contributor

No, please leave it open. I'll close it when a fix is merged.

@muhmuhten
Copy link
Contributor

Since arguments also go into $ARGS.named, jq -ncsM --arg module SODIMM '{module: $ARGS.named.module}' should work too.

($ARGS.named|{module} doesn't work, but $ARGS.named|{"module"} does as well.)

@nicowilliams
Copy link
Contributor

Ahh, so on top of the patch I already have, I need to add:

diff --git a/src/parser.y b/src/parser.y
index dc0c5df..04d25a6 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -969,6 +969,10 @@ IDENT ':' ExpD {
   $$ = gen_dictpair(gen_const(jv_copy($1)),
                     gen_index(gen_noop(), gen_const($1)));
   }
+| Keyword {
+  $$ = gen_dictpair(gen_const(jv_copy($1)),
+                    gen_index(gen_noop(), gen_const($1)));
+  }
 | '(' Exp ')' ':' ExpD {
   jv msg = check_object_key($2);
   if (jv_is_valid(msg)) {

@nicowilliams
Copy link
Contributor

We should probably look at all the places where src/parser.y refers to IDENT and make sure all where it makes sense also have a corresponding rule referring to Keyword instead.

@hpo14
Copy link
Author

hpo14 commented Mar 28, 2019

thank you, guys

rbolsius pushed a commit to rbolsius/jq that referenced this issue Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants