简体   繁体   English

OCaml AST记录类型表示

[英]OCaml AST record type representation

I am trying to learn more about OCaml Extension Points and I am having trouble following the representation of record types in the AST. 我正在尝试了解有关OCaml扩展点的更多信息,并且我无法跟踪AST中记录类型的表示。

I am stealing the example below from this blog post: 我在这篇博客文章中窃取了以下示例:

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

Using the source file (foo.ml): 使用源文件(foo.ml):

 let _ = [%getenv "USER"]

And output of ocamlc -dparsetree fool.ml: 并输出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 parse tree pattern matching of the line 从asttypes.mli和parsetree.mli我可以遵循该行的解析树模式匹配

 Pexp_constant Const_string("USER",None)

However, I can no longer follow what is happening when the parse tree represents record types. 但是,当解析树表示记录类型时,我无法再跟踪发生的情况。 It seems that record fields are not represented in the same order that they appear in the type definition and not all fields are required (or shown) in the parse tree. 似乎记录字段的表示顺序与它们在类型定义中出现的顺序不同,并且解析树中不需要(或显示)所有字段。

From parsetree.mli: 来自parsetree.mli:

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

The parse tree output only seems to show the location and payload, but I am probably reading this incorrectly. 解析树输出似乎只显示位置和有效负载,但我可能读错了。

How do I correctly read the AST for record types? 如何正确读取记录类型的AST? For type expression should it be: 对于类型表达式应该是:

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

It seems you are missing essential tools to study ASTs and use extensions points. 您似乎缺少研究AST和使用扩展点的基本工具。 These tools are the ppx_tools written by Alain Frisch. 这些工具是Alain Frisch编写的ppx_tools One of these tools has precisely be designed to explore the concrete representation of ASTs, its name is dumpast . 其中一个工具正是为了探索AST的具体表现而设计的,它的名字是dumpast Let apply it to the following file ast_record.mli : 我们将它应用于以下文件ast_record.mli

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

The output is 输出是

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 record labels is preserved. 这证实了记录标签的顺序得以保留。

Incidentally, let me suggest you to study the source code of these ppx_tools and maybe also the ppx extension shipped with Lwt. 顺便提一下,我建议你研究这些ppx_tools的源代码,也可能是Lwt附带的ppx扩展。 They are quite short and very-well written and a recommandable source of inspiration. 它们非常简短,写得非常好,是一个值得推荐的灵感来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM