从Ocaml类型中提取字段

时间:2013-06-13 21:36:03

标签: ocaml

这似乎是一个愚蠢的问题,因为当我在终端内进行原型设计时,我能够做到这一点。但是当我使用以下特定模块时:

http://caml.inria.fr/pub/docs/manual-ocaml/libref/Lexing.html

和这段代码:

(*Identifiers*)
let ws = [' ' '\t']*
let id = ['A'-'Z' 'a'-'z'] +
let map = id ws  ':' ws  id
let feed = '{' ws  map+ ws '}'
let feeds = '[' ws feed + ws ']'
(*Entry Points *)
rule token  = parse
            [' ' '\t']     { token lexbuf }     (* skip blanks *)
          | ['\n' ]        { EOL }
          | feeds as expr  {   Feeds( expr ) }
          | id as expr     { Id(expr) }
          | feed as expr   {
                let pos = Lexing.lexeme_start_p lexbuf in
                let pos_bol = pos.pos_bol in
                print_string (string_of_int pos_bol);
                print_string "\n";
                Feed(expr) }

我收到以下错误:

Error: Unbound record field label pos_bol

我有点困惑为什么会发生这种情况。在我上面链接的文档中,它说pos_bol是Lexing.position

类型的字段

对不起,我觉得回答时会有一个相当明显的答案,但谢谢!

1 个答案:

答案 0 :(得分:6)

在OCaml中,sum构造函数和记录字段名称的范围在模块内,如标识符。 position记录在Lexing内定义,但未在当前范围内打开,因此您应使用pos.pos_bol代替pos.Lexing.pos_bol