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
nix-instantiate: parse to json ast #4731
Conversation
@@ -79,16 +79,28 @@ struct Expr | |||
{ | |||
virtual ~Expr() { }; | |||
virtual void show(std::ostream & str) const; | |||
virtual void showAsAterm(std::ostream & str) const; | |||
virtual void showAsJson(std::ostream & str) const; |
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.
This should be
virtual nlohmann::json to_json() const;
or something like that.
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.
nlohmann::json
sounds like overhead .. intermediary data structure + unnecessary pretty printing + unnecessary string validation (check for invalid utf8)
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.
nlohmann's dump function is similar to my showAsJson
, also using recursion, not iteration (which could be faster)
@@ -31,7 +31,12 @@ void processExpr(EvalState & state, const Strings & attrPaths, | |||
bool evalOnly, OutputKind output, bool location, Expr * e) | |||
{ | |||
if (parseOnly) { | |||
std::cout << format("%1%\n") % *e; | |||
if (output == okJSON) { | |||
std::cout << format("%1%\n") % (ExprAsJson *) *e; |
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.
We shouldn't add this functionality to nix-instantiate
since it's legacy. Better to add a new command like nix parse
.
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.
yes, thanks for remind. also the CLI of nix-instantiate
is not ideal (--json
, --xml
)
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.
first draft in 4c35710
status: working prototype
json ast result{
"type": 6,
"recursive": false,
"attrs": [
{
"type": "attr",
"inherited": false,
"name": "foo",
"value": {
"type": 10,
"value": "bar"
}
}
],
"dynamicAttrs": []
} type 6 = ExprAttrs, type 10 = ExprString, ... see issue: parser resolves relative paths to absolute paths, but should stay relative |
i will remove the other formats like benchmark resultsmy my generating the json and walking the tree has the same speed |
the CLI of
note: concat strings and arithmetic sum have the same node type |
push needed for nix-gui, to avoid stupid workarounds like this |
closing in favor of #5512 |
resolve #4726
closed in favor of #5512
basically there are two possible solutions:
1. recursive: on
struct Expr
and its derived structs, add a methodshowAsJson
.the root expression is printed with
expr->showAsJson(std::cout);
and
showAsJson
prints child expressions withchildExpr->showAsJson(std::cout);
.a similar solution is used in nlohmann/json
2. use an iterative visitor function to print the AST nodes
to make this work, we would have to expose the node type in
struct Expr
-> see
internalType
instruct Value
this could be faster than solution 1, but its harder to implement (manage the stack manually)
1a. Alipha from
##c++
on freenode suggested a more complex solutionhttps://wandbox.org/permlink/luDOhrLzloiSME6h
but imho we dont need that complexity (keep it simple)