mikefarah / yq

yq is a portable command-line YAML, JSON, XML, CSV, TOML and properties processor
https://mikefarah.gitbook.io/yq/
MIT License
11.93k stars 586 forks source link

Double-quote parsing during string interpolation differs from jq #2142

Open kanaka opened 2 weeks ago

kanaka commented 2 weeks ago

Describe the bug

Version of yq: 4.44.3 Operating system: ubuntu 24.04 Installed via: curl

Input Yaml data1.yml:

data:
  - "abc"
  - "def"

data1.json:

{"data":["abc","def"]}

Command The command you ran:

yq '"\(.data | join(" "))"' ./data1.yml

Actual behavior

Error: bad expression, please check expression syntax

Expected behavior

"abc def"

Additional context Using jq on the json of the same data works fine:

$ jq '"\(.data | join(" "))"' ./data1.json
"abc def"

Also, escaping the inner double quotes will make it work in yq, but it will break in jq. I suspect this means that the yq parsing of strings is looking for the first unescaped double-quote in the string but it should be skipping any double-quotes that are within interpolation sections \(....).

kanaka commented 2 weeks ago

Looks like the problem is here: https://github.com/mikefarah/yq/blob/6eb2ae757a3c9e451f0d848ab74adc776b736e30/pkg/yqlib/lexer_participle.go#L185

I don't think pure regex will be able to handle parsing interpolated strings because you will need to keep track of the paren wrapping depth of commands inside of interpolation blocks, i.e. I believe something like this is legal syntax in jq: "prefix\(func1(func2(func3("blah"))))suffix"