F#, FParsec, and Calling a Stream Parser Recursively

前端 未结 1 1808
陌清茗
陌清茗 2021-01-24 12:18

I\'m developing a multi-part MIME parser using F# and FParsec. I\'m developing iteratively, and so this is highly unrefined, brittle code--it only solves my first immediate prob

相关标签:
1条回答
  • 2021-01-24 12:38

    This is a fragment that might get you going in the right direction.

    Get your parsers to spit out something with the same base type. I prefer to use F#'s discriminated unions for this purpose. If you really do need to push values into a Post type, then walk the returned AST tree. That's just the way I'd approach it.

    #if INTERACTIVE
    #r"""..\..\FParsecCS.dll"""    // ... edit path as appropriate to bin/debug, etc.
    #r"""..\..\FParsec.dll"""
    #endif
    
    let packet = @"content-type: Multipart/related; boundary=""RN-Http-Body-Boundary""; type=""multipart/related""
    
    --RN-Http-Body-Boundary
    Message-ID: <25845033.1160080657073.JavaMail.webmethods@exshaw>
    Mime-Version: 1.0
    Content-Type: multipart/related; type=""application/xml"";
      boundary=""----=_Part_235_11184805.1160080657052""
    
    ------=_Part_235_11184805.1160080657052
    Content-Type: Application/XML
    Content-Transfer-Encoding: binary
    Content-Location: RN-Preamble
    Content-ID: <1430586.1160080657050.JavaMail.webmethods@exshaw>"
    
    //XML document begins here...
    
    type AST =
    | Document of AST list
    | Header of AST list
    /// ie. Content-Type is the tag, and it consists of a list of key value pairs
    | Tag of string * AST list  
    | KeyValue of string * string
    | Body of string
    

    The AST DU above could represent a first pass of the example data you posted in your other question. It could be finer grained than that, but simpler is normally better. I mean, the ultimate destination in your example is a Post type, and you could achieve that with some simple pattern matching.

    0 讨论(0)
提交回复
热议问题