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 <*> b
will make a parser of type(a * b)
a <*>> b
will chaina
andb
omiting the result ofa
(a <<*> b
exists as well)- You can parse either
a
orb
(a
andb
being parsers of a same typet
) witha <|> b
, that will make a parser of typet
a </> b
parse eithera
orb
, but of different typest
u
, making someparser (either t u)
a <?> "some error message"
attribute a custom error message for any parsermaybe
many
many1
keyword "hey"
match"hey"
and ensure there is no letter after or before itnotFollowedBy
lookAhead
delayMe p
withp: () -> 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 ofp
usingf
. For instance here is a parser for any number of additions (i.e.10 + 30 + 2
,6 + 36
or42
):
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).