2 people like it.

Non working

Not working attempt to do a recursive left associative parser

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
open FParsec 

type Thing =
    | Value of string
    | Pair of Thing * Thing

 
let validVariableCharacter x = ['a' .. 'z'] |> Seq.contains x

let thingParser, thingParserImpl = createParserForwardedToRef()
let valueParser = many1Satisfy validVariableCharacter |>> Value
let pairParser = thingParser .>> spaces1 .>>. thingParser |>> Pair

do thingParserImpl := attempt(pairParser) <|> valueParser // this results in a stackoverflowexception

printfn "%A" <| run thingParser "a" // expect Value "a"
printfn "%A" <| run thingParser "a b c" // expect Pair(a, Pair(b, c))
namespace FParsec
type Thing =
  | Value of string
  | Pair of Thing * Thing

Full name: Script.Thing
union case Thing.Value: string -> Thing
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
union case Thing.Pair: Thing * Thing -> Thing
val validVariableCharacter : x:char -> bool

Full name: Script.validVariableCharacter
val x : char
module Seq

from Microsoft.FSharp.Collections
val contains : value:'T -> source:seq<'T> -> bool (requires equality)

Full name: Microsoft.FSharp.Collections.Seq.contains
val thingParser : Parser<Thing,unit>

Full name: Script.thingParser
val thingParserImpl : Parser<Thing,unit> ref

Full name: Script.thingParserImpl
val createParserForwardedToRef : unit -> Parser<'a,'u> * Parser<'a,'u> ref

Full name: FParsec.Primitives.createParserForwardedToRef
val valueParser : Parser<Thing,unit>

Full name: Script.valueParser
val many1Satisfy : (char -> bool) -> Parser<string,'u>

Full name: FParsec.CharParsers.many1Satisfy
val pairParser : Parser<Thing,unit>

Full name: Script.pairParser
val spaces1 : Parser<unit,'u>

Full name: FParsec.CharParsers.spaces1
val attempt : Parser<'a,'u> -> Parser<'a,'u>

Full name: FParsec.Primitives.attempt
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val run : Parser<'Result,unit> -> string -> ParserResult<'Result,unit>

Full name: FParsec.CharParsers.run
Raw view Test code New version

More information

Link:http://fssnip.net/7Se
Posted:7 years ago
Author:Arno den Uijl
Tags: fparsec