【发布时间】:2020-12-26 20:02:15
【问题描述】:
以下顶级 XML 解析器定义返回错误 The value or constructor ‘TOP_LEVEL_RECORD’ is not defined. ...
let xTop_Level, xTop_Level_Ref = createParserForwardedToRef<TOP_LEVEL_RECORD, unit>()
do xTop_Level_Ref :=
pipe4
(opt xDeclaration)
(opt (many xComment_or_CData))
xElement
(opt (many xComment_or_CData))
(fun decl before_root root after_root
-> {Declaration = decl
Before_Root = before_root
Root = root
After_Root = after_root}) |>> TOP_LEVEL_RECORD
// This returns the error -------------------→^^^^^^^^^^^^^^^^
TOP_LEVEL_RECORD 被定义为……
type TOP_LEVEL_RECORD = {Declaration : XDECLARATION option
Before_Root : COMMENTS_OR_CDATA list option
Root : XELEMENT
After_Root : COMMENTS_OR_CDATA list option
}
解析器xDeclaration、xCommentor_Cdata和xElement都被正确定义并返回TOP_LEVEL_RECORD中对应的类型。
let xTop_Level, xTop_Level_Ref = createParserForwardedToRef<TOP_LEVEL_RECORD, unit>() 是 Fparsec 用于递归解析器调用的语法,记录在此:http://www.quanttec.com/fparsec/tutorial.html#parsing-json.createParserForwardedToRef-example。
如果我定义类型 type TOP_LEVEL = TOP_LEVEL_TYPE of TOP_LEVEL_RECORD 并将 TOP_LEVEL_RECORD 替换为 TOP_LEVEL 和 TOP_LEVEL_TYPE 如下......
let xTop_Level, xTop_Level_Ref = createParserForwardedToRef<TOP_LEVEL, unit>()
// Replaced this text ------------------------------------->^^^^^^^^^
do xTop_Level_Ref :=
pipe4
(opt xDeclaration)
(opt (many xComment_or_CData))
xElement
(opt (many xComment_or_CData))
(fun decl before_root root after_root
-> {Declaration = decl
Before_Root = before_root
Root = root
After_Root = after_root}) |>> TOP_LEVEL_TYPE
// Replaced this text ----------------------->^^^^^^^^^^^^^^
...代码编译没有任何错误或警告。
为什么TOP_LEVEL_TYPE在这里有构造函数而不是TOP_LEVEL_RECORD?
您能否指出 F# 或 FParsec 文档的相关部分?
【问题讨论】: