Performance Type OCaml AST

I'm trying to learn more about OCaml extension points, and I'm having trouble representing record types in AST.

I stole an example from this blog post:

http://whitequark.org/blog/2014/04/16/a-guide-to-extension-points-in-ocaml/

Using the source file (foo.ml):

let _ = [%getenv "USER"] 

And the output of ocamlc -dparsetree fool.ml:

  [ structure_item (test.ml[1,0+0]..[1,0+24]) Pstr_eval expression (test.ml[1,0+8]..[1,0+24]) Pexp_extension "getenv" [ structure_item (test.ml[1,0+17]..[1,0+23]) Pstr_eval expression (test.ml[1,0+17]..[1,0+23]) Pexp_constant Const_string("USER",None) ] ] 

From asttypes.mli and parsetree.mli, I can follow the pattern matching of the parsing tree

  Pexp_constant Const_string("USER",None) 

However, I can no longer keep track of what happens when the parse tree represents record types. It looks like the record fields are not presented in the same order as in the type definition, and not all fields are required (or shown) in the parsing tree.

From parsetree.mli:

  type expression = { pexp_desc: expression_desc; pexp_loc: Location.t; pexp_attributes: attributes; } 

Only the output of the parsing tree shows the location and payload, but I'm probably reading it wrong.

How to read AST for record types? For a type expression, it should be:

  (* record type declaration and pexp_loc field *) expression (test.ml[1,0+8]..[1,0+24]) (* pexp_desc field *) Pexp_extension "getenv" [ ... ] 
+5
source share
1 answer

It seems you lack the necessary tools to study AST and use extension points. These tools are ppx_tools , written by Alain Frisch. One of these tools is specifically designed to study a particular AST representation, its name is dumpast . Let apply it to the following ast_record.mli file:

 type card = { name: string; address: string; age: int; } 

Output signal

 ocamlfind ppx_tools/dumpast ast_record.mli ast_record.mli ==> [{psig_desc = Psig_type [{ptype_name = {txt = "card"}; ptype_params = []; ptype_cstrs = []; ptype_kind = Ptype_record [{pld_name = {txt = "name"}; pld_mutable = Immutable; pld_type = {ptyp_desc = Ptyp_constr ({txt = Lident "string"}, [])}}; {pld_name = {txt = "address"}; pld_mutable = Immutable; pld_type = {ptyp_desc = Ptyp_constr ({txt = Lident "string"}, [])}}; {pld_name = {txt = "age"}; pld_mutable = Immutable; pld_type = {ptyp_desc = Ptyp_constr ({txt = Lident "int"}, [])}}]; ptype_private = Public; ptype_manifest = None}]}] ========= 

which confirms that the order of the sound labels is preserved.

By the way, let me suggest you study the source code of these ppx_tools and possibly also the ppx extension that comes with Lwt. They are quite short and very well written and are a recommended source of inspiration.

+2
source

Source: https://habr.com/ru/post/1239538/


All Articles