Skip to content

Instantly share code, notes, and snippets.

@Mr-Byte
Created June 30, 2013 04:22
Show Gist options
  • Save Mr-Byte/5893846 to your computer and use it in GitHub Desktop.
Save Mr-Byte/5893846 to your computer and use it in GitHub Desktop.
let internal unorderedOptList (ps : Parser<'a, 'u> list) =
fun (stream : CharStream<'u>) ->
let keys = [ 0 .. (ps |> List.length) - 1 ]
let parsers = ref (ps |> List.zip keys |> Map.ofList)
let results = keys |> List.map (fun key -> (key, ref (Reply(None)))) |> Map.ofList
let mutable continueLooping = true
let runParser (key : int) (value : Parser<'a, 'u>) =
let state = stream.State
let result : Reply<'a> = (value stream)
if result.Status = Ok then
//NOTE: The below line, if uncommented, allows only one occurence of each parser.
//parsers := !parsers |> Map.remove key
Some(key, result)
else
if stream.State <> state then
stream.BacktrackTo(state)
None
while continueLooping do
match !parsers |> Map.tryPick runParser with
| None -> continueLooping <- false
| Some(key, value) -> results.[key] := Reply(Some(value.Result))
results |> Seq.sortBy (fun kvp -> kvp.Key) |> Seq.map (fun kvp -> !kvp.Value) |> List.ofSeq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment