문제

I'm certain there's a really simple answer to this, but I've been staring at this all day and I can't figure it out.

As per the tutorial, I'm implementing a JSON parser. To challenge myself, I'm implementing the number parser myself.

This is what I got so far:

let jnumber =
    let neg = stringReturn "-" -1 <|> preturn 1
    let digit = satisfy (isDigit)
    let digit19 = satisfy (fun c -> isDigit c && c <> '0')
    let digits = many1 digit
    let ``int`` =
        digit
        <|> (many1Satisfy2 (fun c -> isDigit c && c <> '0') isDigit)

The trouble is that digit is a Parser<char,_>, whereas the second option for int is a Parser<string,_>. Would I normally just use a combinator to turn digit into a Parser<char,_>, or is there something else I should do?

도움이 되었습니까?

해결책

The |>> operator is what you're looking for. I quote the FParsec reference:

val (|>>): Parser<'a,'u> -> ('a -> 'b) -> Parser<'b,'u> 

The parser p |>> f applies the parser p and returns the result of the function application f x, where x is the result returned by  p.

p |>> f is an optimized implementation of p >>= fun x -> preturn (f x).

For example:

let jnumber =
    let neg = stringReturn "-" -1 <|> preturn 1
    let digit = satisfy (isDigit)
    let digit19 = satisfy (fun c -> isDigit c && c <> '0')
    let digits = many1 digit
    (digit |>> string) (* The operator is used here *)
    <|> (many1Satisfy2 (fun c -> isDigit c && c <> '0') isDigit)

You may want to read FParsec tutorial on parsing JSON which uses this operator quite intensively.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top