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
Equations
- Grade.impossible = { errors := Necessity.never, consumes := Necessity.always }
Instances For
Equations
- Grade.conditional = { errors := Necessity.possibly, consumes := Necessity.always }
Instances For
Equations
- Grade.flexible = { errors := Necessity.never, consumes := Necessity.possibly }
Instances For
Equations
- Grade.fallible = { errors := Necessity.possibly, consumes := Necessity.possibly }
Instances For
Equations
- Grade.pure = { errors := Necessity.never, consumes := Necessity.never }
Instances For
Equations
- Grade.lookahead = { errors := Necessity.possibly, consumes := Necessity.never }
Instances For
Equations
- Grade.empty = { errors := Necessity.always, consumes := Necessity.never }
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 Necessity.always = (n < m)
- Parser.consumptionWitness n m Necessity.possibly = (n ≤ m)
- Parser.consumptionWitness n m Necessity.never = (n = m)
Instances For
The result type of running a parser
Equations
- Parser.Outcome ε n g α = match g.errors with | Necessity.never => Parser.Success n g.consumes α | Necessity.possibly => ε ⊕ Parser.Success n g.consumes α | Necessity.always => ε
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.instGFunctorNecessitySuccess = { gmap := fun {i : Necessity} {α β : Type} => Functor.map }
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Parser.Outcome.ofSuccess r c_2 = r
- Parser.Outcome.ofSuccess r c_2 = Sum.inr r
Instances For
Equations
- One or more equations did not get rendered due to their size.
Equations
- Parser.instGMonadGrade = { toGApplicative := Parser.instGApplicativeGrade, 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 x = Parser.Outcome.throw default 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 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 each parser in the list in order, returning the first success.
Equations
- Parser.oneOf [x] p_2 = x
- Parser.oneOf (x :: y :: xs) p_2 = cast ⋯ (x <|> Parser.oneOf (y :: xs) ⋯)
Instances For
A parser that always fails with error e.
Equations
- Parser.throw e c = { run := fun {n : ℕ} (x : Text n) => Parser.Outcome.throw e c }
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
Weaken the consumption grade by capping at possibly.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Weaken the error grade by capping at possibly.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Cap both error and consumption grades at possibly.
Equations
Instances For
Equations
Instances For
Forget consumption precision, setting it to possibly.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Forget error precision, setting it to possibly.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Weaken both grades to possibly, yielding a fallible parser.
Equations
Instances For
Run a parser, discarding the error and returning the Success as an Option.
Equations
- p.runOption t = (p.run t).handle (fun (x : Necessity.possibly ≤ ge) (x_1 : ε) => none) fun (x : ge ≤ Necessity.possibly) (r : Parser.Success n gc α) => some r
Instances For
Run a parser, returning only the parsed value as an Option.
Equations
- p.runResult? t = (p.run t).handle (fun (x : Necessity.possibly ≤ ge) (x_1 : ε) => none) fun (x : ge ≤ Necessity.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.satisfy 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.satisfy fun (x : Char) => x == c
- Parser.string.go (c :: cs) = (Parser.satisfy fun (x : Char) => x == c) >>=ᵍ fun (x : Char) => Parser.string.go cs
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
Try p; return the result on success or the default value d on failure.
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
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.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Instances For
Instances For
Equations
- Parser.«term_::₁_» = Lean.ParserDescr.trailingNode `Parser.«term_::₁_» 67 68 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " ::₁ ") (Lean.ParserDescr.cat `term 67))
Instances For
Apply p one or more times, collecting results.
Equations
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 p surrounded by parentheses.
Equations
Instances For
Parse p surrounded by square brackets.
Equations
Instances For
Parse p surrounded by curly braces.
Equations
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
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 exactly n occurrences of p.
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.succ = (have sepP := gcast ⋯ (sep >>=ᵍ fun (x : β) => p); p >>=ᵍ fun (p1 : α) => sepP.count n >>=ᵍ fun (ps : List.Vector α n) => 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
Run p without consuming input, keeping only the result.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Succeed (without consuming) only when p fails.
Equations
- One or more equations did not get rendered due to their size.
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.