meta-introspector / ocaml-libppx-import-yojson-introspector

Using libppx, ppx_import, reflect over ast using yojson Ppx_yojson_conv_lib and [@@deriving yojson_of]
GNU Affero General Public License v3.0
3 stars 0 forks source link

filename #2

Open jmikedupont2 opened 3 months ago

jmikedupont2 commented 3 months ago

from perplexitywrite an example of how to get this information from a extension

To access information from an OCaml extension using ppxlib, you can define a simple extension that utilizes the Expansion_context.Base module to retrieve context information. Here's an example:

open Ppxlib

let my_extension =
  Extension.declare
    "my_extension"
    Extension.Context.structure_item
    Ast_pattern.(pstr nil)
    (fun ~ctxt:_ -> 
      let context = Expansion_context.Base in
      let code_path = Expansion_context.Base.code_path context in
      let input_name = Expansion_context.Base.input_name context in
      let tool_name = Expansion_context.Base.tool_name context in
      Printf.printf "Code path: %s\nInput name: %s\nTool name: %s\n"
        (Code_path.to_string code_path) input_name tool_name;
      [])

This example defines a simple extension my_extension that prints the code path, input name, and tool name from the expansion context. You would register this extension with the Ppxlib.Driver to use it during preprocessing.

jmikedupont2 commented 3 months ago
    let path = Ppxlib.Expansion_context.Base.input_name ctxt in

https://github.com/patricoferris/ocaml-multicore-monorepo/blob/72eb11aff57211c5d2e2a4762f5808edb59aad26/duniverse/bisect_ppx/src/ppx/instrument.ml#L1640

https://discuss.ocaml.org/t/match-context-in-ppx-rewriter/7714?u=jim_dupont