Tiny parser combinator library for FStar
Just open the main module:
open StarCombinator- You can chain two parsers
a(outputing things of typet)b(outputing things of typey):a <*> bwill make a parser of type(a * b) a <*>> bwill chainaandbomiting the result ofa(a <<*> bexists as well)- You can parse either
aorb(aandbbeing parsers of a same typet) witha <|> b, that will make a parser of typet a </> bparse eitheraorb, but of different typestu, making someparser (either t u)a <?> "some error message"attribute a custom error message for any parsermaybemanymany1keyword "hey"match"hey"and ensure there is no letter after or before itnotFollowedBylookAheaddelayMe pwithp: () -> parser t(for some typet) makes a parser behave as a lazy function: then you can write stuff likelet myParser () = (number <*>> keyword "+") <<*> maybe (delayMe myParser)- suppose
p: parser t, thenf @<< p(withf: t -> u) is of typeparser u: it maps the output ofpusingf. For instance here is a parser for any number of additions (i.e.10 + 30 + 2,6 + 36or42):
let myParser (): parser int
= (fun (leftNumber, right) -> match right with
| None -> leftNumber
| Some rightNumber -> leftNumber + rightNumber
) @<< (
(number <*>> keyword "+")
<<*> maybe (delayMe myParser)
)The files StarCombinator.Examples.fst and StarCombinator.Examples.While.fst contains examples.
This module is used only for debugging purposes (and for the examples). Using the normal IO module from FStar didn't worked for me (so this MyIO module is basically a copy paste).