Skip to content
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

Closed

Conversation

pedrobslisboa
Copy link

@pedrobslisboa pedrobslisboa commented Sep 24, 2024

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 applying Pp_ast.expression (Format.formatter_of_buffer buffer) [%expr 40 + 2] and then collect the value from the buffer, for a simple print it was necessary Pp_ast.expression (Format.std_formatter) [%expr 40 + 2]
Now, pp_ast has a sprint function that delivers the ast structure from a string.

Usage

let () = Pp_ast.sprint "42" |> print_endline
(* Pexp_constant (Pconst_integer ( \"42\", None)) *)

It was also added to Ppxlib.Pprintast

let () = Pprintast.sprint "42" |> print_endline
(* Pexp_constant (Pconst_integer ( \"42\", None)) *)

Signed-off-by: pedrobslisboa <[email protected]>
@NathanReb
Copy link
Collaborator

Do you have a concrete usecase in mind for this? One where ppxlib-pp-ast couldn't be used and this would have to be done programmatically.

@pedrobslisboa
Copy link
Author

pedrobslisboa commented Sep 26, 2024

Do you have a concrete usecase in mind for this? One where ppxlib-pp-ast couldn't be used and this would have to be done programmatically.

@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 ppxlib-pp-ast covers all cases for CLI usage, but it's not there for code usage, right?
The Pp_ast.expression could work for it and for my cases. But it's strange to provide like this:

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.

@NathanReb
Copy link
Collaborator

If you'd just like to get a string you can use Format.asprintf:

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 %a format strings.

Would that be good enough for you?

@NathanReb
Copy link
Collaborator

NathanReb commented Sep 27, 2024

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?

@pedrobslisboa
Copy link
Author

pedrobslisboa commented Sep 27, 2024

Would that be good enough for you?

Perfect to me

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?

I'm using it on the ppx-by-example to provide some code samples as AST.
E.g.:

(* ... *)
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

  • do it internally of the ppx-by-example (As I'm doing rn)
  • use the metaquot instead of the string.
  • Or use your approach

This feature is not necessary.

Thank you for all the care and for looking for a good solution for me. :3 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants