-
Notifications
You must be signed in to change notification settings - Fork 19
Add function to generate back textual syntax from AST #29
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, thanks for adding serialize support! i'm a bit curious about your use-case, if possible are you able to share some more details? i'm doing some AST-rewriting for https://github.com/wader/fq using a gojq fork (this commit in the fq
branch of my gojq fork) and wondering if could be a better fit? jqjq might be quite slow, has very poor error handling at the moment and lack some features like include
/import
but it of course depends on your needs :)
); | ||
|
||
def _run_tests: | ||
def _run_tests($testSerialize): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nitpick: snake_case
, but i'm thinking let's ignore most style stuff and i can reformat things after merge
( ($test.expr | lex | parse) as $ast | ||
( ( | ||
($test.expr | lex | parse) | ||
| if $testSerialize then ast_tostring | lex| parse else . end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it too slow to always be done? also thinking if we should do some kind of round-trip assert? ex ast_tostring | . == (lex | parse | ast_tostring)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also thinking if there should be some .test
-tests? round-trip serialize _builtins_src
? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not so slow, actually, I just wanted to add this test without being "too invasive". I agree it could be setup far better. Right now it was a quick way to use the tests for the new function. And it was useful cause that way I found many bugs that I fixed in the last commit. Now all those tests pass, i.e. the results are the same after the round-trip serialize-parse-eval
Played around a bit with the PR, noticed this:
Can try minimize it down a bit later, out traveling atm |
Replying to your previous comment... |
I see, what kind of rewrites do you think you would need? for fq i think i mainly do two types of rewrites, turn
My gojq fork adds support for parsing/serialization and then in fq i add some extra jq builtins to use it, see https://github.com/wader/fq/blob/19b9446f82f5b5df553f9aaa1b178b240d515179/pkg/interp/query.go#L10 so in the end you can do things like this from jq. # _query_try, _query_func etc are some rewrite-helpers in fq
$ fq -rn '"1,2" | _query_fromstring | _query_try(_query_array; _query_func("debug")) | _query_tostring'
try [1, 2] catch debug So if you're fine with using some kind of fork of gojq that might be an alternative. I can help you out with the go code needed so you can do everything from jq. But of course improving jqjq's langauge support, robustness and error handling would be nice also :) Let me know how it goes! |
I started experimenting with fq to do the parsing, it works perfectly. For the AST handling/serialization, in my case I would like to include multiple interlinked jq queries, so it is good for me to have control of the traversal and serialization of the AST, albeit I guess also on that side I could do some interesting things with fq (which, by the way, seems to me an amazing project). In the meanwhile I developed (but needs to be tested thoroughly) a generic AST traversal function roughly following the visitor pattern, to facilitate AST operations in jq. Consequently I also actually made a new implementation of the serialization based on that generic traversal function. Regarding the AST representation in JSON as generated by gojq/fq, is it documented somewhere? How is it different (if it is different) from the one you use in jqjq? |
👍 and you can always switch to something more lightweight in the future if needed.
Include as in pipe/chain multiple queries somehow? should be very possible, composability is one of many things jq really sines at. https://github.com/wader/jq-lsp has a query_walk function but not sure it fit your needs, it walks and keep track of lexical scope and conditionally output {q: <query node>, env: <current scope>} objects. Thanks for the kind words about fq, much of the credit should go to jq :)
Sounds like it could be useful so please share. One think I've thought about is serialise with indent/style support so that one could write a jq code formatter, but it might be quite tricky.
Not documented anywhere unfortunately. It's gojq's internal AST-representation as JSON more or less. Both fq och jqjq uses it. I don't it will change much in the future but if you wan't something very stable I guess you should stick to a specific version of fq or build your own gojo-based thingy. |
Implements the feature described in #28
The new function
ast_tostring
serializes an input AST (as generated byparse
) in jq textual form.It strives to cover all the features currently supported by jqjq