1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00
Commit Graph

74 Commits

Author SHA1 Message Date
David Tolnay
16e8d0b1ab detect invalid path expression (fix #862) 2015-07-24 20:23:07 -07:00
David Tolnay
1628bbf95f Clean up trailing whitespace 2015-07-19 09:38:50 -07:00
Assaf Gordon
7b6a018dff Print offending object in runtime error messages
When reporting an error to the user, add information about the offending
object/value (possibly truncated).

The goal is to give a user some context regarding which input object
caused the runtime error.

Examples:

    $ echo '"hello"' | ./jq '-.'
    jq: error: string ("hello") cannot be negated

    $ echo '"very-long-string"' | ./jq '-.'
    jq: error: string ("very-long-...) cannot be negated

    $ echo '["1",2]' | ./jq '.|join(",")'
    jq: error: string (",") and number (2) cannot be added

    $ echo '["1","2",{"a":{"b":{"c":33}}}]' | ./jq '.|join(",")'
    jq: error: string (",") and object ({"a":{"b":{...) cannot be added

    $ echo '{"a":{"b":{"c":33}}}' | ./jq '.a | @tsv'
    jq: error: object ({"b":{"c":33}}) cannot be tsv-formatted, only array

(Fix #754)
2015-05-21 18:49:24 -05:00
Nicolas Williams
1dcfc2f547 Include filename and lineno in error messages 2015-03-30 15:56:29 -05:00
Nicolas Williams
a83a9e0bc2 Reduce number of msg callback typedefs 2015-02-13 15:58:02 -06:00
Nicolas Williams
ae7f8d6ab9 Further module system revamp (fix #659)
To import a module now use:

    # Import module.jq file:
    import "relative/path/to/module" as foo;

    # Use the module's defs as foo::<def-name>

To import a JSON file:

    # Read file.json:
    import "relative/path/to/file" as $foo;
    #
    # Use as $foo::foo

Using `-L` now drops the builtin library path and appends the requested
path to the empty array (or the result of an earlier `-L`).

Support for the `$JQ_LIBRARY_PATH` environment variable has been
removed.
2014-12-31 20:09:53 -06:00
Nicolas Williams
7dc34b92af Add label $name | EXP; fix break
This is to fix the problem where `break` is dynamic, not lexical.

With this it should be possible to do this sort of thing:

    label $break | inputs | if ... then $break|error else . end

This is a backwards-incompatible change for master, but the previous
`break` hadn't shipped yet.

Still needed:

 - testing
2014-12-30 11:42:45 -06:00
Nicolas Williams
b67bad82cb Allow resetting of jq err callback
This will be useful for the upcoming test-erroneous-programs improvement
to --run-tests, so we can switch between the default error reporting
method (print to stderr) to a method internal to --run-tests, and back.

The idea is that when testing programs that are expected to compile (and
link), it'd be nice if errors continue going to stderr, while when
testing programs that must fail to compile (or link), the error has to
be captured so it can be compared to the error expected by the test.
2014-12-30 10:52:38 -06:00
Nicolas Williams
5df20f4954 Add debug builtin
And refactor setup of jv dump flags.
2014-12-27 18:25:34 -06:00
Nicolas Williams
5bfb9781f7 Add Streaming parser (--stream)
Streaming means that outputs are produced as soon as possible.  With the
`foreach` syntax one can write programs which reduce portions of the
streaming parse of a large input (reduce into proper JSON values, for
example), and discard the rest, processing incrementally.

This:

    $ jq -c --stream .

should produce the same output as this:

    $ jq -c '. as $dot | path(..) as $p | $dot | getpath($p) | [$p,.]'

The output of `jq --stream .` should be a sequence of`[[<path>],<leaf>]`
and `[[<path>]]` values.  The latter indicate that the array/object at
that path ended.

Scalars and empty arrays and objects are leaf values for this purpose.

For example, a truncated input produces a path as soon as possible, then
later the error:

    $ printf '[0,\n'|./jq -c --stream .
    [[0],0]
    parse error: Unfinished JSON term at EOF at line 3, column 0
    $
2014-12-26 23:05:56 -06:00
Nicolas Williams
906d2537b9 Allow C-coded functions to empty
Just return a jv_invalid() without a message.
2014-12-26 23:05:56 -06:00
Nicolas Williams
56ae88d9d5 Module search revamp for pkg managers
The search path listed in an import directive can now be an array.  The
top-level search path is appended.  Null and empty strings in the path
terminate any search.  The "." in "." and "./*" is replaced with the
directory containing the file doing the import (for command-line
programs this is the current directory, though that may be a bad idea).

No version numbers or anything of the sort are gratuitously added to the
search paths.

All this makes external package managers possible by allowing
dependencies to be installed local to dependents.
2014-12-24 02:31:51 -06:00
Nicolas Williams
ae312bd7fe Use __attribute__ __printf__ with GCC 2014-12-23 23:22:57 -06:00
Nicolas Williams
7243989c52 Fix refcount leak, fix #618 2014-11-29 15:24:43 -06:00
Nicolas Williams
46613af64c STOREV/LOADV* should also print refcnts 2014-11-28 20:52:22 -06:00
Nicolas Williams
79000644c6 Enable printing of stack val refcnts 2014-11-28 19:35:29 -06:00
Nicolas Williams
456bafa82f Drop the jq version directory from search path 2014-08-30 00:58:24 -05:00
Nicolas Williams
1ba8c2cfa6 Add module directive, modulemeta builtin
Fix #425.
2014-08-14 03:26:26 -05:00
Nicolas Williams
9f13afa20f Add jq_report_error() function; use it
Put a stop to fprintf(stderr, ...) where we shouldn't.
2014-08-14 03:21:35 -05:00
William Langford
38b939688a Added library system with -l, -L, and JQ_LIBRARY_PATH
Created util.[ch] to hold common utilities.
2014-07-22 22:51:11 -05:00
Nicolas Williams
6e88d419ba Fix #478 assertion failure 2014-07-10 19:23:02 -05:00
Nicolas Williams
d8672e1b2a Make C-coded built-ins take jq_state * argument 2014-07-07 23:37:42 -05:00
Nicolas Williams
8780bc0b8e Better check for lib has only functions (fix #138) 2014-07-07 16:03:32 -05:00
Nicolas Williams
7fce34292e Add try EXP catch EXP 2014-07-06 01:29:42 -05:00
Nicolas Williams
9deee38dc8 Fix off-by-one in TCO
Now we have the ability to define a generator in jq:

        def for(cond; update):
            def _for:
                if cond then ., (update | _for) else . end;
            _for;

        for(. < 10; . + 1) # generates numbers between `.` and 10

    Running this by hand with --debug-dump-disasm (with a fix for that
    coming up next) we can see that the call to _for is optimized:

        _for:0:
          0000 DUP
          0001 CALL_JQ cond:0^1
          0005 JUMP_F 0022
          0007 POP
          0008 FORK 0012
          0010 JUMP 0020
          0012 CALL_JQ update:1^1
          0016 TAIL_CALL_JQ _for:0^1
          0020 JUMP 0023
          0022 POP
          0023 RET

    And timing this with 1000, 10000, 100000 iterations shows that
    indeed we must be applying TCO; otherwise, without TCO, this gets
    very slow very quickly.
2014-07-01 22:40:40 -05:00
Nicolas Williams
436941d48b TCO to the max!
Close #446.

    Currently tested by disassembling and tracing various recursive jq
    programs by hand under valgrind.  An improved test framework that
    can test for errors and specific bytecode patterns is in
    development.
2014-06-30 23:41:20 -05:00
Nicolas Williams
1204e328df Add much commentary about CALL_JQ and call frames 2014-06-30 19:40:56 -05:00
Nicolas Williams
beb0d081bb Improve TCO
Instead of checking for self-recursion check that the thing we're
calling is a function and not a closure, therefore the new frame will
have the same env as the current frame.
2014-06-23 19:59:00 -05:00
Nicolas Williams
4b7c701fdd Tail call optimization (close #437) 2014-06-22 00:24:02 -05:00
Nicolas Williams
fe29d3d3fa Add ?, .[]?, and .. operators
Make XPath-like `//a/b` recursive structure traversal easier in jq,
which then becomes:

    ..|.a?.b?

The `?` operator suppresses errors about . not being an array or object.
The `..` operator is equivalent to calling the new `recurse_down`
built-in, which in turn is equivalent to

    recurse(.[]?)

Note that `..a` is not supported; neither is `...a`.  That could be add
added, but it doesn't seem worth the trouble of saving the need to type
a '|'.
2014-02-20 15:33:07 -06:00
David R. MacIver
17a319d120 args to jq_compile_args were not getting freed when there were errors in the compile 2013-12-08 15:28:29 +00:00
Nicolas Williams
3a1dab5396 Fix double-free typo in print_error() 2013-12-06 23:25:06 -06:00
Nicolas Williams
eb165459aa Add callback interface for errors
Printing to stderr is not the right answer for a library.
2013-12-04 18:21:41 -06:00
Stephen Dolan
1c9e03f800 Merge branch 'header-cleanup' into libjq
Conflicts:
	Makefile.am
2013-06-22 23:27:16 +01:00
Nicolas Williams
298b2a6033 Add libjq autoconf goo 2013-06-21 11:57:12 -05:00
Stephen Dolan
7af88962ee Move cfunction invocation code to the interpreter loop. 2013-06-21 12:06:28 +01:00
Stephen Dolan
b49d65a276 Fold opcode.{c,h} into bytecode.{c,h} 2013-06-18 01:36:18 +01:00
Stephen Dolan
8c4d29ee38 Remove some initialise-to-zero code.
This lets valgrind find more bugs - if a field isn't given a
well-defined value valgrind will now find it instead of seeing it
set to zero with memset.
2013-06-18 00:17:20 +01:00
Stephen Dolan
94931040a8 Merge branch 'stack-refactor'
Conflicts:
	execute.c
2013-06-18 00:06:00 +01:00
Nicolas Williams
3f86e97f70 Fixup API to get closer to a libjq 2013-06-15 17:37:15 -05:00
Stephen Dolan
05d90517b0 Clean up lots of stack and frame logic.
Move frame defs to execute.c
2013-06-14 22:08:18 +01:00
Stephen Dolan
5f5c1dc5a6 Simplify frame logic. 2013-06-14 01:20:24 +01:00
Stephen Dolan
e0524644f8 Unify all stacks. Passes tests, but needs cleanup. 2013-06-13 21:50:32 +01:00
Stephen Dolan
25bf1a0169 Unify frame and data stacks 2013-06-10 01:17:58 +01:00
Brendan Macmillan
6e373942e5 Load library from ~/.jq 2013-05-29 15:05:40 +10:00
Stephen Dolan
fd1ac5dd79 EACH need not make a backtrack point on the last iteration 2013-05-17 22:49:08 +01:00
Stephen Dolan
8fbee891b3 Add LOADVN opcode.
Does a variable load, but sets the variable to be null afterwards.
2013-05-16 15:02:18 +01:00
Stephen Dolan
1e2851cdb3 Remove the YIELD opcode (use RET instead) 2013-05-15 01:23:06 +01:00
Stephen Dolan
81be37b236 Add the range function 2013-05-14 16:09:10 +01:00
Stephen Dolan
e13f24ab45 Better error handling for INSERT opcode
jq 'null | {(.a): 1}' no longer crashes.
2013-05-13 15:42:57 +01:00