FParsec チュートリアルを完了した後、SDP (Session Description Protocol 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 まで読みましたが、すべての例は判別共用体に解析されます。結果の照合にはレコード型が最適ですか。または、より良い方法はありますか?