PrimParser #
A parser combinator library with precise grades tracking error and consumption
behavior at the type level via Necessity.
Equations
- instReprGrade = { reprPrec := instReprGrade.repr }
Equations
- One or more equations did not get rendered due to their size.
Instances For
Instances For
Instances For
Instances For
Equations
- Grade.instMax = { max := Grade.max }
Equations
- One or more equations did not get rendered due to their size.
Equations
- Grade.instZero = { zero := empty }
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
A successful parse result.
- result : α
- restSize : ℕ
- witness : consumptionWitness self.restSize n consumes
Instances For
Equations
- Parser.Outcome.Sound errors (Parser.failure f) = (possibly ≤ errors)
- Parser.Outcome.Sound errors (Parser.success r) = (errors ≤ possibly)
Instances For
A parser with error type ε, static grade g, and result type α.
The grade tracks error and consumption behavior at the type level.
Instances For
Equations
- (Parser.failure f).handle sound_2 onSuccess onError = onError sound_2 f
- (Parser.success r).handle sound_2 onSuccess onError = onSuccess sound_2 r
Instances For
Reduce handle when the outcome is known to succeed.
Reduce handle when the outcome is known to fail.
Equations
- Parser.instGradedFunctorNecessitySuccess = { gmap := fun {i : Necessity} {α β : Type} => Functor.map }
Equations
- Parser.instParserErrorError = { endOfInput := Parser.Error.eof }
Equations
- Parser.Success.bindParser t x f = match (f x.result).run (t.dropTo x.restSize ⋯) with | Parser.failure e => Parser.failure (e.trans ⋯) | Parser.success y => Parser.success (x.seq y)
Instances For
Equations
- Parser.Outcome.throw e = Parser.failure { error := e, restSize := n, witness := ⋯ }
Instances For
Equations
Instances For
Monadic bind for parsers. The resulting grade is the product (max) of the two grades.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Lift a value into a parser that consumes nothing and never fails.
Equations
- Parser.pure a = { run := fun {n : ℕ} (x : Input σ n) => Parser.success { result := a, restSize := n, witness := ⋯ }, sound := ⋯ }
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 : Input σ n) => ↑(Parser.fixGo✝ h f t), sound := ⋯ }
Instances For
Try p1; if it fails, run p2.
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 the input left at f.restSize
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
A parser that always fails with error e.
Equations
- Parser.throw e c = { run := fun {n : ℕ} (x : Input σ n) => Parser.Outcome.throw e, sound := ⋯ }
Instances For
Equations
- p_2.relaxConsumes = p_2
- p_2.relaxConsumes = p_2
- p_2.relaxConsumes = { result := p_2.result, restSize := p_2.restSize, witness := ⋯ }
Instances For
Equations
- Parser.Except.Sound errors (Except.error a) = (possibly ≤ errors)
- Parser.Except.Sound errors (Except.ok a) = (errors ≤ possibly)
Instances For
Consume a single token.
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
Instances For
Consume a token 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 token that satisfies predicate f, or fail.
Equations
- Parser.satisfy f = Parser.token fun (t : τ) => if f t = true then some t else none
Instances For
Try p; return some result on success or none on failure, never failing itself.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Repeatedly apply p until e succeeds, collecting the results of p.
Equations
Instances For
Apply p zero or more times, collecting results. Requires p to always consume.
Equations
- p.many = { run := fun {n : ℕ} (t : Input σ n) => Parser.success (Parser.many.go p t), sound := ⋯ }
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.
Equations
Instances For
Parse zero or more occurrences of p, each followed by sep.
Equations
- sep.endBy p h = (Parser.endItem✝ sep p h).many
Instances For
Parse one or more occurrences of p, each followed by sep.
Equations
- sep.endBy1 p h = (Parser.endItem✝ sep p h).many1
Instances For
Parse one or more occurrences of p separated by sep, with an optional
trailing sep.
Equations
- sep.sepEndBy1 p h = gcast ⋯ (sep.sepBy1 p h >>=ᵍ fun (xs : NonEmptyList α) => sep.skipOptional.weakenConsumes >>=ᵍ fun (__r : Unit) => gpure xs)
Instances For
Parse zero or more occurrences of p separated by sep, with an optional
trailing sep.
Equations
- sep.sepEndBy p h = sep.sepBy p h >>=ᵍ fun (xs : List α) => sep.skipOptional.weakenConsumes >>=ᵍ fun (__r : Unit) => gpure xs
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.
Equations
- One or more equations did not get rendered due to their size.
- Parser.skipUpTo 0 x✝ = Parser.ok () Parser.token._proof_2 Parser.token._proof_1
Instances For
Parse exactly n occurrences of p separated by sep.
Equations
- sep.sepByN p 0 = Parser.ok List.Vector.nil Parser.token._proof_1 Parser.token._proof_1
- sep.sepByN p n_1.succ = (have sepP := 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.