How do I test for exactly 2 characters with fparsec?
Asked Answered
M

1

4

I have the following program that runs. It takes a line of text and splits it into two parts, the first is an identifier and the second is the remainder of the line. My parser for the identifier (factID) takes any string of characters as the identifier, which is not (quite) what I want. What I want is a parser that only succeeds when it encounters two consecutive upper case letters. So for example "AA" should succeed while "A", "A1" or "AAA" should not.

What I can't figure out is how construct a parser that looks for a fixed length token. I thought perhaps CharParsers.next2CharsSatisfy might be the function I am looking for, but I can't figure out how to properly use it.

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
Metagalaxy answered 13/5, 2013 at 15:39 Comment(3)
Should it accept any two uppercase chars, e.g., "AB", or only two of the same char?Hotpress
Any two uppercase characters; so "AA", "AB" "ZQ" etc. would all be fine.Metagalaxy
next2CharsSatisfy f1 f2 is used like followedBy (satisfy f1 >>. satisfy f2), i.e. it only looks ahead but doesn't consume input. If you want to consume two letters, you can either combine two single char parsers or use one of the manyMinMaxSatisfy variants, as Daniel proposed. When in doubt, use the labeled variants with an L postfix, so that you get better error messages.Stealthy
H
4

I think this would do it

let pFactID = manyMinMaxSatisfy 2 2 Char.IsUpper
Hotpress answered 13/5, 2013 at 16:20 Comment(1)
The actual code I used is: let factID = manyMinMaxSatisfy 2 2 CharParsers.isUpperMetagalaxy

© 2022 - 2024 — McMap. All rights reserved.