如何使用FParsec解析成记录或对象?

时间:2014-10-07 14:38:39

标签: sdp fparsec

完成FParsec教程后,我决定尝试为SDP编写解析器 (会话描述协议RFC 4366) - 至少前3行。 SDP在ABNF(RFC 4234)中指定;所以,我正在努力解决这个问题。

用户指南第5.1节末尾的注释指出了“你从语法的叶子节点开始使用简单的解析器,然后一步一步向上工作,直到你最终获得完整语法的解析器“有了这个方向,斯蒂芬回答使用管道,这就是我现在所拥有的:

open FParsec
open System.Net

// handy for debugging, supposedly, but I couldn't get it to work
let breakParse (p: Parser<_,_>) stream =
  p stream

// Input
let session = "v=0
o=jdoe 2890844526 2890842807 IN IP4 10.47.16.5
s=SDP Seminar
"

type Sdp = 
  { Version : System.UInt16;
    Origin : Owner;
    SessionName : string }

and Owner = 
  { Username : string
    SessionId : string
    SessionVersion : string
    NetType : NetworkType
    AddrType : AddressType
    Address : UnicastAddress }

and NetworkType = 
  | Undefined
  | Internet

and AddressType = 
  | Undefined
  | IPv4
  | IPv6

and UnicastAddress =
  | IPaddress of System.Net.IPAddress
  | FQDomainName of string
  | ExternalAddress of string

let sep : Parser<unit, unit> = skipChar '='
let getValue typeChar = skipChar typeChar .>> sep
let many1Digit : Parser<string, unit> = many1Satisfy isDigit
let many1Hex : Parser<string, unit> = many1Satisfy isHex

let nonWhitespace : Parser<string, unit> = many1Satisfy (isNoneOf @" \n\r\t") 

//proto-version = %x76 "=" 1*DIGIT CRLF
let getVersion = getValue 'v' >>. many1Digit .>> spaces |>> System.Convert.ToUInt16

//origin-field = %x6f "=" username SP sess-id SP sess-version SP
//               nettype SP addrtype SP unicast-address CRLF
// username cannot contain whitespace; i.e., only visible chars
let getUsername : Parser<string, unit> = getValue 'o' >>. nonWhitespace .>> spaces

//sess-id = 1*DIGIT
let getSessionId = many1Digit .>> spaces

//sess-version = 1*DIGIT
let getSessionVersion = many1Digit .>> spaces

let getNetType : Parser<NetworkType, unit> = 
  pstring "IN" |>> (function 
  | "IN" -> NetworkType.Internet
  | _ -> NetworkType.Undefined)
  .>> spaces

let getAddrType : Parser<AddressType, unit> = 
  anyString 3 |>> (function 
  | "IP4" -> AddressType.IPv4
  | "IP6" -> AddressType.IPv6
  | _ -> AddressType.Undefined)
  .>> spaces

let getAddress : Parser<UnicastAddress, unit> = 
    (restOfLine true) |>> (fun a -> IPAddress.Parse a |> IPaddress )

let getUserSession = pipe3 getUsername getSessionId getSessionVersion (fun u i v -> (u, i, v))
let pipeOrigin = pipe4 getUserSession getNetType getAddrType getAddress 
              (fun us n t a -> 
                let u, i, v = us
                {Username=u; SessionId=i; SessionVersion=v; NetType=n; 
                  AddrType=t; Address=a})

//session-name-field =  %x73 "=" text CRLF
let getSessionName = getValue 's' >>. restOfLine true

let threelines = pipe3 getVersion pipeOrigin getSessionName 
              (fun v o sn -> {Version=v; Origin=o; SessionName=sn})

let sessionDesc = run threelines session

这样可行(除了getAddress还没有处理FQDN或外部地址),结果如下:

val sessionDesc : ParserResult<Sdp,unit> =
  Success: {Version = 0us;
 Origin = {Username = "jdoe";
           SessionId = "2890844526";
           SessionVersion = "2890842807";
           NetType = Internet;
           AddrType = IPv4;
           Address = IPaddress 10.47.16.5;};
 SessionName = "SDP Seminar";}

现在这是目标记录类型Sdp。但通过一些元组将结果输入输出是一种复杂的方式。

我已经阅读了第5.4节中的用户指南,但所有示例都解析为有区别的联盟。记录类型是整理结果的最佳选择;或者,有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

您可以使用pipe functions按顺序应用行的解析器,然后构建记录。 (如果您需要超过5个临时值,您可以轻松组合多个pipex组合子来创建具有更多参数的pipe组合子。)

前三行的解析器似乎还不完整。