PrimParser #
A parser combinator library with precise grades tracking error and consumption
behavior at the type level via Necessity.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Instances For
Instances For
Instances For
Equations
- One or more equations did not get rendered due to their size.
Relates input size n and remaining size m according to a consumption grade:
always requires strict decrease, possibly allows ≤, never requires equality.
Equations
- Parser.consumptionWitness n m always = (n < m)
- Parser.consumptionWitness n m possibly = (n ≤ m)
- Parser.consumptionWitness n m never = (n = m)
Instances For
The result type of running a parser
Equations
- Parser.Outcome ε n g α = match g.errors with | never => Parser.Success n g.consumes α | possibly => Parser.Failure n ε ⊕ Parser.Success n g.consumes α | always => Parser.Failure n ε
Instances For
Equations
- p_2.handle e_2 s_2 = e_2 Parser.Outcome.handle._proof_1 p_2
- p_2.handle e_2 s_2 = s_2 Parser.Outcome.handle._proof_2 p_2
- Parser.Outcome.handle (Sum.inl x) e_2 s_2 = e_2 Parser.Outcome.handle._proof_3 x
- Parser.Outcome.handle (Sum.inr x) e_2 s_2 = s_2 Parser.Outcome.handle._proof_3 x
Instances For
Equations
- Parser.instGradedFunctorNecessitySuccess = { gmap := fun {i : Necessity} {α β : Type} => Functor.map }
Equations
- Parser.Outcome.throw e t h = Parser.Outcome.throwFailure { error := e, restSize := n, restText := t, witness := ⋯ } h
Instances For
Equations
- One or more equations did not get rendered due to their size.
Equations
- Parser.instGradedMonadGrade = { toGradedApplicative := Parser.instGradedApplicativeGrade, gbind := fun {i j : Grade} {α β : Type} => Parser.bind }
Build a recursive parser via a fixpoint. Termination is guaranteed by requiring the body to always consume input.
Equations
- Parser.fix f h = { run := fun {n : ℕ} (t : Text n) => Parser.fix.go f h t }
Instances For
Equations
- Parser.fix.go f h t_2 = Parser.Outcome.throw default t_2 h
- Parser.fix.go f h t_2 = (f { run := fun {k : ℕ} (t' : Text k) => if k ≤ n_2 then Parser.fix.go f h t' else Parser.Outcome.throw default t' h }).run t_2
Instances For
Try p1; if it fails, try p2. The error grade is the infimum and
the consumption grade is computed via Necessity.ite.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Parser.«term_<|>_» = Lean.ParserDescr.trailingNode `Parser.«term_<|>_» 20 20 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " <|> ") (Lean.ParserDescr.cat `term 21))
Instances For
try p1; if it fails without consuming, then try p2
Equations
- One or more equations did not get rendered due to their size.
Instances For
Try p1 first, if it fails with Failure f, run p2 on f.restText
Equations
- One or more equations did not get rendered due to their size.
Instances For
Try each parser in the list in order, returning the first success.
Equations
- Parser.oneOf l = Parser.oneOf.go ↑l ⋯
Instances For
Equations
- p_2.relaxConsumes = p_2
- p_2.relaxConsumes = p_2
- p_2.relaxConsumes = { result := p_2.result, restSize := p_2.restSize, restText := p_2.restText, witness := ⋯ }
Instances For
Run a parser, returning only the parsed value as an Option.
Equations
- p.runResult? t = (p.run t).handle (fun (x : possibly ≤ ge) (x_1 : Parser.Failure n ε) => none) fun (x : ge ≤ possibly) (r : Parser.Success n gc α) => some r.result
Instances For
Consume and return a single character, or fail on empty input.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Like gpure but with a flexible grade: both ge and gc can be never
or possibly. Useful in match branches where all cases must share the same grade.
Equations
- Parser.ok a he hc_2 = (gpure a).weakenErrors.weakenConsumes
- Parser.ok a he hc_2 = (gpure a).weakenConsumes
- Parser.ok a he hc_2 = (absurd ⋯ Parser.ok._proof_2).weakenConsumes
- Parser.ok a he_2 hc_2 = (gpure a).weakenErrors
- Parser.ok a he_2 hc_2 = gpure a
Instances For
Consume a character and apply f; succeed with the result or fail if f returns none.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Consume a character that satisfies predicate f, or fail.
Instances For
Match a specific character.
Equations
- Parser.char c = Parser.skipSatisfy fun (x : Char) => x == c
Instances For
Equations
- Parser.string.go [] = Parser.throw Parser.Error.fail Parser.Outcome.handle._proof_3
- Parser.string.go [c] = Parser.skipSatisfy fun (x : Char) => x == c
- Parser.string.go (c :: cs) = (Parser.skipSatisfy fun (x : Char) => x == c) >>=ᵍ fun (x : PUnit.{1}) => Parser.string.go cs
Instances For
Try p then apply cont to its result; wrap the final result in Option.
Equations
- p.optionalBind cont = (gcast ⋯ (p >>=ᵍ fun (a : α) => cont a)).optional
Instances For
Consume characters while f holds, returning the collected string.
Equations
Instances For
Consume one or more characters while f holds.
Equations
Instances For
Skip characters while f holds.
Equations
Instances For
Skip one or more characters while f holds.
Equations
Instances For
Skip zero or more whitespace characters.
Instances For
Skip one or more whitespace characters.
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
- Parser.dquote = Parser.char '\"'
Instances For
Equations
- Parser.comma = Parser.char ','
Instances For
Parse a natural number (one or more digits).
Equations
- Parser.nat = Parser.digit >>=ᵍ fun (d : ℕ) => Parser.digit.many >>=ᵍ fun (ds : List ℕ) => gpure (List.foldl (fun (acc d : ℕ) => acc * 10 + d) d ds)
Instances For
Parse an integer (optional leading - followed by digits).
Equations
- Parser.int = gcast Parser.int._proof_1 ((Parser.char '-').optional >>=ᵍ fun (neg : Option PUnit.{1}) => Parser.nat >>=ᵍ fun (n : ℕ) => gpure (if neg.isSome = true then -↑n else ↑n))
Instances For
Equations
- Parser.space = Parser.skipSatisfy fun (x : Char) => x == ' '
Instances For
Equations
- Parser.tab = Parser.skipSatisfy fun (x : Char) => x == '\t'
Instances For
Equations
- Parser.ASCII.lf = Parser.skipSatisfy fun (x : Char) => x == '\n'
Instances For
Equations
- Parser.ASCII.cr = Parser.skipSatisfy fun (x : Char) => x == '\x0d'
Instances For
Match an ASCII uppercase letter.
Instances For
Match an ASCII lowercase letter.
Instances For
Match an ASCII letter.
Equations
Instances For
Match an ASCII letter or digit.
Instances For
Match an ASCII control character.
Equations
- Parser.ASCII.control = Parser.satisfy fun (c : Char) => decide (c.val < 32) || c.val == 127
Instances For
Match a binary digit.
Equations
- Parser.ASCII.binDigit = Parser.token fun (x : Char) => match x with | '0' => some false | '1' => some true | x => none
Instances For
Match an octal digit, returning its numeric value.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Match a hexadecimal digit, returning its numeric value.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Match a line terminator: LF or CRLF.
Equations
- Parser.eol = gcast Parser.eol._proof_1 (Parser.ASCII.cr.optional >>=ᵍ fun (x : Option PUnit.{1}) => Parser.ASCII.lf)
Instances For
Parse zero or more occurrences of p separated by sep.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Parse one or more occurrences of p separated by sep, with an optional
trailing sep.
Equations
Instances For
Parse zero or more occurrences of p separated by sep, with an optional
trailing sep.
Equations
Instances For
Parse exactly n + 1 occurrences of p.
Equations
- Parser.count1 0 p = (fun (x : α) => x ::ᵥ List.Vector.nil) <$>ᵍ p
- Parser.count1 n_2.succ p = gcast ⋯ (p >>=ᵍ fun (x : α) => Parser.count1 n_2 p >>=ᵍ fun (rest : List.Vector α (n_2 + 1)) => gpure (x ::ᵥ rest))
Instances For
Parse exactly n occurrences of p.
Equations
- Parser.count 0 p = Parser.ok List.Vector.nil ⋯ ⋯
- Parser.count n_2.succ p = (Parser.count1 n_2 p).relax
Instances For
Skip up to n occurrences of p; never fails.
Equations
- One or more equations did not get rendered due to their size.
- Parser.skipUpTo 0 x✝ = Parser.ok () Parser.Outcome.handle._proof_2 Parser.Outcome.handle._proof_3
Instances For
Skip n or more occurrences of p.
Equations
- Parser.skipManyN n p = gcast ⋯ (Parser.skip n p >>=ᵍ fun (x : PUnit.{1}) => p.skipMany)
Instances For
Parse exactly n occurrences of p separated by sep.
Equations
- sep.sepByN p 0 = Parser.ok List.Vector.nil Parser.Outcome.handle._proof_3 Parser.Outcome.handle._proof_3
- sep.sepByN p n_1.succ = (have sepP := gcast ⋯ (sep >>=ᵍ fun (x : β) => p); p >>=ᵍ fun (p1 : α) => Parser.count n_1 sepP >>=ᵍ fun (ps : List.Vector α n_1) => gpure (p1 ::ᵥ ps)).weaken
Instances For
Parse one or more occurrences of p separated by left-associative operator op.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Succeed only at end of input, consuming nothing.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Instances For
Run p; if it fails with error e, run recover e. If recovery also
fails, report p's original error.
Equations
- One or more equations did not get rendered due to their size.