itchyny / gojq

Pure Go implementation of jq
MIT License
3.3k stars 119 forks source link

Add files/line/column to run errors like iteratorError? #256

Open LeonB opened 4 months ago

LeonB commented 4 months ago

Would it be feasible and wanted to add files/line/column to things like iterator errors?

I had something in mind like:

Error: Cannot iterate over null (null): test.jq:4
    4 |   | null | map({})
                   ^^^  Cannot iterate over null (null)
LeonB commented 1 month ago

Looked into goyacc but I don't see an obvious way to pass the offset into Query/AST.

wader commented 1 month ago

Hey! that would be nice. jq-lsp uses a modified version of gojq's parser to keep track of token positions, might be a start. To get positions for runtime errors i think you would also have to pass along things while compiling.

LeonB commented 1 month ago

@wader thanks! I'll look into it.

LeonB commented 1 month ago

gojq is executing bytecodes which I didn't know. Adding offset to every bytecode would be a bit much I expect, so I think something like a symbol table should be implemented where we link opcodes to the corresponding symbols and offsets. The codeinfo would be a nice start.

@wader do you have any thoughts about how to implement something like that?

wader commented 1 month ago

No good ideas really, i would guess some kind of location info needs to be tracked on the stack during execution? maybe @itchyny has some ideas? looking at the codeinfo stuff atm it seems to be debug only, so maybe also needs to consider how much performance impact it would have