如何使用fparsec测试正好2个字符?

时间:2013-05-13 15:39:15

标签: f# fparsec

我有以下程序运行。它需要一行文本并将其分成两部分,第一部分是标识符,第二部分是行的其余部分。我的标识符解析器(factID)将任意字符串作为标识符,这不是我想要的。我想要的是一个解析器只有在遇到两个连续的大写字母时才会成功。因此,例如“AA”应该成功,而“A”,“A1”或“AAA”不应该。

我无法弄清楚的是如何构造一个寻找固定长度令牌的解析器。我想也许CharParsers.next2CharsSatisfy可能是我正在寻找的功能,但我无法弄清楚如何正确使用它。

open FParsec

let test p str =
    match run p str with
    | Success(result, _, _)   -> printfn "Success: %A" result
    | Failure(errorMsg, _, _) -> printfn "Failure: %s" errorMsg

let ws = spaces
let str_ws s = pstring s .>> ws

type StringConstant = StringConstant of string * string

let factID =
    let isIdentifierFirstChar c = isLetter c 
    let isIdentifierChar c = isLetter c 

    many1Satisfy2L isIdentifierFirstChar isIdentifierChar "factID"

let factText =
    let isG c = isLetter c || isDigit c || c = ' ' || c = '.'
    manySatisfy isG 


let factParse = pipe3 factID (str_ws " ") factText
                        (fun id _ str -> StringConstant(id, str))


[<EntryPoint>]
let main argv = 
    test factParse "AA This is some text."      // This should pass
    test factParse "A1 This is some text."      // This should fail
    test factParse "AAA This is some text."     // This passes but I want it to fail
    0 // return an integer exit code

1 个答案:

答案 0 :(得分:4)

我认为这样做会

let pFactID = manyMinMaxSatisfy 2 2 Char.IsUpper