Skip to content

Instantly share code, notes, and snippets.

@ia7ck
Last active December 31, 2020 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ia7ck/ade36d8f909c429a9034b04c9cec8ca8 to your computer and use it in GitHub Desktop.
Save ia7ck/ade36d8f909c429a9034b04c9cec8ca8 to your computer and use it in GitHub Desktop.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>fparsec_practice</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FParsec" Version="1.1.1" />
</ItemGroup>
</Project>
open FParsec
let test (p: Parser<'a, unit>) (str: string) =
match run p str with
| Success (result, _, _) -> printfn "Success: %A" result
| Failure (errorMsg, _, _) -> printfn "Failure: %s" errorMsg
let plus (a: float) (b: float) = a + b
[<EntryPoint>]
let main _ =
test pfloat "1.23" // Success: 1.23
test pfloat "a1b2c3" // Failure
test pfloat " 1.23 " // Failure
let number = spaces >>. pfloat .>> spaces
test number "1.23" // Success: 1.23
test number " 1.23 " // Success: 1.23
let opPlus =
spaces >>. stringReturn "plus" plus .>> spaces
let xPlusY =
pipe3 number opPlus number (fun x op y -> op x y)
test xPlusY "1 plus 2" // Success: 3.0
let manyPlus = chainl1 number opPlus
test manyPlus "1 plus 2 plus 3 plus 4" // Success: 10.0
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment