2017-04-11 64 views
1

我想解析RFC 2822使用fparsec,但我無法處理與多行的標題:(它與下一個標題混淆): 這裏是我最好的嘗試:做你有任何提示?fparsec rfc2822解析多個標題行

let str (s:string) = pstring s 

let stringLiteral = 
    manySatisfy (fun c -> c <> ':' && c <> '\r' && c <> '\n') 

let ws = many (pchar ' ') 

let keyValueSimple = stringLiteral .>>. (ws >>. str ":" >>. ws >>. stringLiteral) .>>. pchar '\n' |>> (fun (a,b) -> a) 

let lineValue = ws >>. stringLiteral .>>. (pchar '\n' .>>. (pchar '\t')) |>> (fun (a,b) -> a) 

let lastValue = ws >>. stringLiteral .>>. (pchar '\n' .>> notFollowedBy (pchar '\t')) |>> (fun (a,b) -> a) 

let keyValueComplex = stringLiteral .>>. (ws >>. pchar ':') .>>. (many lineValue) .>>. lastValue |>> (fun (((f),d),b) -> (f,f))      

let headers = many1 (keyValueComplex) 

let parse (fileName:string) = 
    test headers "Return-Path: <[email protected]>\n\twerwe\nDelivered-To: [email protected]\n " 

我得到期待\ t在3,1:投遞至:[email protected]

回答

1

不去管它看起來像我需要(使用嘗試)原路返回,以便解析器不要總是期待一個\ t,而是尋找下一個標題。

let keyValueComplex = stringLiteral >> >>。 (ws >>。pchar':')。>>。 (很多(嘗試lineValue))。>>。 ((((g,h)),d),b) - >(g,(Seq.fold(+)「」d)+ b))

這現在產生:

成功:[(「Return-Path」,「<[email protected]> werwe」); (「Delivered-To」,「[email protected]」)]