Skip to content

Commit

Permalink
Comment bug fixes, and fully support Tcl-style multiline comments
Browse files Browse the repository at this point in the history
* bugfix: comments were incorrectly being terminated by CR; for example
    jq -n $'1 #foo\r'
  fails to compile because the CR character terminates the comment, and
  CR is not a valid character in jq syntax.

* improvement: comments fully support Tcl-style line continuation.
  Previously this was only "supported" in `-f' scripts, whose first line
  starts with "#!", and second line starts with # and ends with \, only
  for the comment on the second line, only for one extra line.

* man: document comment syntax, which was previously undocumented.

* tests: add regression tests for the bugfix, and some tests for line
  continuation in comments.
  • Loading branch information
emanuele6 committed Nov 3, 2023
1 parent f4929f3 commit 1198ad3
Show file tree
Hide file tree
Showing 7 changed files with 456 additions and 283 deletions.
64 changes: 63 additions & 1 deletion docs/content/manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ sections:
* `-f filename` / `--from-file filename`:
Read filter from the file rather than from a command line, like
awk's -f option. You can also use '#' to make comments.
awk's -f option.
* `-L directory`:
Expand Down Expand Up @@ -3537,6 +3537,68 @@ sections:
(.posts[] | select(.author == "stedolan") | .comments) |=
. + ["terrible."]
- title: Comments

body: |
You can write comments in your jq filters using `#`.
A `#` character (not part of a string) starts a comment.
All characters from `#` to the end of the line are ignored.
If the end of the line is preceded by an odd number of backslash
characters, the following line is also considered part of the
comment and is ignored.
For example, the following code outputs `[1,3,4,7]`
[
1,
# foo \
2,
# bar \\
3,
4, # baz \\\
5, \
6,
7
# comment \
comment \
comment
]
Backslash continuing the comment on the next line can be useful
when writing the "shebang" for a jq script:
#!/bin/sh --
# sum - Output the sum of the given arguments (or stdin)
# usage: sum [numbers...]
# \
exec jq --args -MRnf "$0" -- "$@"
reduce (
$ARGS.positional |
if . == []
then inputs
else .[]
end |
. as $dot |
try
tonumber
catch
(@json "sum: Invalid number \($dot).\n" | halt_error(1))
) as $n (0; . + $n)
The `exec` line is considered a comment by jq, so it is ignored.
But it is not ignored by `sh`, since in `sh` a backslash at the
end of the line does not continue the comment.
With this trick, when the script is invoked as `sum 1 2`,
`/bin/sh -- /path/to/sum 1 2` will be run, and `sh` will then
run `exec jq --args -MRnf /path/to/sum -- 1 2` replacing itself
with a `jq` interpreter invoked with the specified options (`-M`,
`-R`, `-n`, `--args`), that evaluates the current file (`$0`),
with the arguments (`$@`) that were passed to `sh`.
- title: Modules
body: |
Expand Down
72 changes: 70 additions & 2 deletions jq.1.prebuilt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1198ad3

Please sign in to comment.