繁体   English   中英

OCaml AST记录类型表示

[英]OCaml AST record type representation

我正在尝试了解有关OCaml扩展点的更多信息,并且我无法跟踪AST中记录类型的表示。

我在这篇博客文章中窃取了以下示例:

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

使用源文件(foo.ml):

 let _ = [%getenv "USER"]

并输出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)
         ]
  ]  

从asttypes.mli和parsetree.mli我可以遵循该行的解析树模式匹配

 Pexp_constant Const_string("USER",None)

但是,当解析树表示记录类型时,我无法再跟踪发生的情况。 似乎记录字段的表示顺序与它们在类型定义中出现的顺序不同,并且解析树中不需要(或显示)所有字段。

来自parsetree.mli:

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

解析树输出似乎只显示位置和有效负载,但我可能读错了。

如何正确读取记录类型的AST? 对于类型表达式应该是:

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

您似乎缺少研究AST和使用扩展点的基本工具。 这些工具是Alain Frisch编写的ppx_tools 其中一个工具正是为了探索AST的具体表现而设计的,它的名字是dumpast 我们将它应用于以下文件ast_record.mli

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

输出是

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}]}]
=========

这证实了记录标签的顺序得以保留。

顺便提一下,我建议你研究这些ppx_tools的源代码,也可能是Lwt附带的ppx扩展。 它们非常简短,写得非常好,是一个值得推荐的灵感来源。

暂无
暂无

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

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