Grip.Combinators: a megaparsec-style vocabulary over the grip core #
Ready-made byte parsers (ws, digit, oneOf, ...) and higher-order combinators
(sepBy, between, choice, ...), each with a precise grade where the grade is stable,
or at the Parser (fallible) face where it depends on runtime data. Batteries-only.
Byte parsers #
Skip zero or more whitespace bytes; returns the count.
Instances For
Skip one or more whitespace bytes; returns the count.
Instances For
One decimal digit byte.
Instances For
One hex digit byte.
Instances For
One ASCII letter byte.
Instances For
One letter-or-digit byte.
Instances For
One uppercase letter byte.
Instances For
One lowercase letter byte.
Instances For
One byte from bs.
Equations
- Grip.GParser.oneOf bs = Grip.GParser.satisfy fun (b : UInt8) => bs.contains b
Instances For
One byte not in bs.
Equations
- Grip.GParser.noneOf bs = Grip.GParser.satisfy fun (b : UInt8) => !bs.contains b
Instances For
Higher-order combinators #
One or more p separated by sep; both must always consume.
Equations
Instances For
Zero or more p separated by sep.
Instances For
Zero or more p each followed by sep.
Instances For
One or more p each followed by sep.
Equations
Instances For
One or more p (always-consuming); the grade is the element parser's.
Equations
- p.many1 = Grip.GParser.gcast ⋯ (Grip.GParser.map2 (fun (x : α) (xs : List α) => x :: xs) p p.many)
Instances For
Skip one or more p (always-consuming); returns the count.
Instances For
p, or x if p fails. Never fails; consumption follows p's grade.
Equations
- Grip.GParser.option x p = p.alt (Grip.GParser.pure x)
Instances For
some of p, or none. Never fails; consumption follows p's grade.
Equations
- p.optional = (Grip.GParser.map some p).alt (Grip.GParser.pure none)
Instances For
Zero or more p until endp succeeds; endp's result is discarded and the p
results are collected. Total via fix; both parsers must always consume.
Equations
- One or more equations did not get rendered due to their size.
Instances For
End of input: succeed (consuming nothing) exactly when no byte remains. Defined as
notFollowedBy of the any-byte parser; used to reject trailing input after a top-level parse.
Labelled, since a failure here always means the same thing: something followed what should
have been the last byte.
Equations
- Grip.GParser.eof = (fun (p : Grip.GParser lookahead Unit) (name : String) => Grip.GParser.label name p) (Grip.GParser.satisfy fun (x : UInt8) => true).notFollowedBy "end of input"
Instances For
Ordered choice over a non-empty list at a single grade g. The grade is preserved
via choice_self (choosing between two grade-g parsers is again grade g).
Equations
- x.chooseG xs = List.foldl (fun (acc : Grip.GParser g α) (p : Grip.GParser { errors := g.errors, consumes := g.consumes } α) => Grip.GParser.gcast ⋯ (acc.alt p)) x xs
Instances For
Exactly n copies of p, at the Parser face (grade is n-dependent).
Equations
- Grip.GParser.count 0 p = Pure.pure []
- Grip.GParser.count n_2.succ p = do let x ← p let xs ← Grip.GParser.count n_2 p Pure.pure (x :: xs)
Instances For
Notation #
The familiar parser-combinator operators, at the graded level (grades multiply/combine as
the underlying combinator dictates). scoped, so they activate on open Grip. The
Parser (ungraded) face additionally gets the standard Monad/Alternative operators
for free; these graded versions coexist with those and are chosen when the operands are
graded GParsers. The label operator <?> is global (declared with the core).
Functor map: f <$> p.
Equations
- Grip.«term_<$>_» = Lean.ParserDescr.trailingNode `Grip.«term_<$>_» 100 101 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " <$> ") (Lean.ParserDescr.cat `term 100))
Instances For
Replace the result with a constant: x <$ p.
Equations
- Grip.«term_<$_» = Lean.ParserDescr.trailingNode `Grip.«term_<$_» 100 101 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " <$ ") (Lean.ParserDescr.cat `term 100))
Instances For
Applicative apply: pf <*> px; grades multiply.
Equations
- Grip.«term_<*>_» = Lean.ParserDescr.trailingNode `Grip.«term_<*>_» 60 60 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " <*> ") (Lean.ParserDescr.cat `term 61))
Instances For
Sequence, keep the right result: p *> q; grades multiply.
Equations
- Grip.«term_*>_» = Lean.ParserDescr.trailingNode `Grip.«term_*>_» 60 60 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " *> ") (Lean.ParserDescr.cat `term 61))
Instances For
Sequence, keep the left result: p <* q; grades multiply.
Equations
- Grip.«term_<*_» = Lean.ParserDescr.trailingNode `Grip.«term_<*_» 60 60 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " <* ") (Lean.ParserDescr.cat `term 61))
Instances For
Ordered choice: p <|> q; grade follows Grade.choice.
Equations
- Grip.«term_<|>_» = Lean.ParserDescr.trailingNode `Grip.«term_<|>_» 20 20 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " <|> ") (Lean.ParserDescr.cat `term 21))