Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine all parser results with pipe. Is this correct or is there any alternative? #90

Open
Idrek opened this issue Apr 19, 2022 · 0 comments

Comments

@Idrek
Copy link

Idrek commented Apr 19, 2022

Hello. I want to parse some ABFN like the following, to a string:

id = 3*(ALPHA / DIGIT)
name = "Z-" [id ":"] 1*(ALPHA / DIGIT / "-")

I'm trying with pipe commands because i cant find another way to concat all strings i match. Is this the right way to go or am i overlooking something? Following is a snippet of code:

open FParsec
open System

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

let id : Parser<string, unit> =
    many1Chars (asciiLetter <|> digit)

let name : Parser<string, unit> =
    pipe4 (pstring "Z-") id (pstring ":") (regex @"[A-Za-z0-9-]+") (fun s1 s2 s3 s4 ->
        String.Concat [|s1; s2; s3; s4|]) 

test name "Z-ABC:MMSUBJ"

Thanks.

@Idrek Idrek changed the title Collect all parser results with pipe. Is this correct or is there any alternative? Combine all parser results with pipe. Is this correct or is there any alternative? Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant