-
Notifications
You must be signed in to change notification settings - Fork 100
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
Feat/pp ast pprintast #527
Conversation
6542d71
to
657f9f6
Compare
Signed-off-by: pedrobslisboa <[email protected]>
Signed-off-by: pedrobslisboa <[email protected]>
Signed-off-by: pedrobslisboa <[email protected]>
657f9f6
to
99c978c
Compare
Signed-off-by: pedrobslisboa <[email protected]>
Do you have a concrete usecase in mind for this? One where |
@NathanReb, I think this is just for debugging and DX usage. I use Pprintast.string_of_expression a lot, and I'm missing something like it, but for printing the last. I think let ast_string expr =
let buffer = Buffer.create 256 in
let formatter = Format.formatter_of_buffer buffer in
let _ = Pp_ast.expression formatter expr
Format.pp_print_flush formatted ();
Buffer.contents buffer But I need to work with an Ast instead of a string (Which is not a problem) Looking at your comment, I see I'm focusing on my use case, which I can solve locally. I don't know if it could be a feature anyone else would like. |
If you'd just like to get a string you can use let ast_string exp =
let config = Pp_ast.Config.make () in
Format.asprintf "%a" (Pp_ast.expression ~config) exp I realize that the config optional argument makes it a bit annoying here as otherwise you could just write: Format.asprintf "%a" Pp_ast.expression exp I haven't really thought the API through. I'll come up with something so that the printer types are compatible with Would that be good enough for you? |
I'm curious to know in which cases you'd need the parsing though. Could you point me to the project where you'd like to use the 'parse source code' + 'turn the AST into a string' function? |
Perfect to me
I'm using it on the (* ... *)
let _ =
print_endline
("\nAST with AST build eint: "
^ Astlib.Pprintast.string_of_expression (three ~loc))
let _ =
print_endline
("Explore the AST of the '4 + 2' expression" ^ Helpers.Pp_ast_helper.(sprint ~kind:Kind.Expression (`Input "4 + 2")))
(* ... *) As I said, I could
This feature is not necessary. Thank you for all the care and for looking for a good solution for me. :3 ❤️ |
Description
@NathanReb after read your feature and PR I thought about do this, what do you think?
Add the sprint function to the pp_ast. The idea is to provide an easy way to debug and improve dx on tests and documentation.
Before, the only way to get a string from it was by creating an AST, like
[%expr 40 + 2]
and applyingPp_ast.expression (Format.formatter_of_buffer buffer) [%expr 40 + 2]
and then collect the value from the buffer, for a simple print it was necessaryPp_ast.expression (Format.std_formatter) [%expr 40 + 2]
Now,
pp_ast
has a sprint function that delivers the ast structure from a string.Usage
It was also added to Ppxlib.Pprintast