ParseIt core #
Parsers read tokens from a pure iterator Std.Iter (α := ι) τ. The parser state is the iterator
itself, so backtracking is simply reusing an earlier iterator value.
Every successful parse reports its Progress: either the iterator did not move, or it moved
strictly forward along plausible successor steps. Proofs are erased at runtime, so Progress
compiles to a one-bit tag, and that tag is what allows repetition combinators to be total.
it' is reachable from it by one or more plausible successor steps.
Equations
- ParseIt.Advanced it' it = it'.finitelyManySteps!.Rel it.finitelyManySteps!
Instances For
How far a parser moved the iterator from it to it'.
- stay
{ι τ : Type u}
[Std.Iterator ι Id τ]
{it' it : Std.Iter τ}
: it' = it → Progress it' it
The iterator did not move.
- moved
{ι τ : Type u}
[Std.Iterator ι Id τ]
{it' it : Std.Iter τ}
: Advanced it' it → Progress it' it
The iterator moved strictly forward.
Instances For
Whether the iterator moved.
Equations
Instances For
No progress.
Equations
Instances For
Compose progress.
Equations
- (ParseIt.Progress.stay h₁).trans (ParseIt.Progress.stay h₂) = ParseIt.Progress.stay ⋯
- (ParseIt.Progress.stay h₁).trans (ParseIt.Progress.moved h₂) = ParseIt.Progress.moved ⋯
- (ParseIt.Progress.moved h₁).trans (ParseIt.Progress.stay h₂) = ParseIt.Progress.moved ⋯
- (ParseIt.Progress.moved h₁).trans (ParseIt.Progress.moved h₂) = ParseIt.Progress.moved ⋯
Instances For
Parser result for a parse starting at iterator it.
- ok
{ι τ : Type u}
[Std.Iterator ι Id τ]
{ε : Type u}
{it : Std.Iter τ}
{α : Type u}
(rest : Std.Iter τ)
(progress : Progress rest it)
(value : α)
: Result ε it α
Success, with the remaining iterator and the progress made.
- error
{ι τ : Type u}
[Std.Iterator ι Id τ]
{ε : Type u}
{it : Std.Iter τ}
{α : Type u}
(err : ε)
: Result ε it α
Failure. No input is consumed on failure.
Instances For
ParserT ε ι τ m is a monad transformer to parse tokens of type τ from an iterator with internal
state ι, with error type ε.
Equations
- ParseIt.ParserT ε ι τ m α = ((it : Std.Iter τ) → m (ParseIt.Result ε it α))
Instances For
Parser ε ι τ is the parser monad without an underlying monad.
Equations
- ParseIt.Parser ε ι τ = ParseIt.ParserT ε ι τ Id
Instances For
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
p <|> q runs q from the same starting point when p fails, merging both errors.
Equations
- One or more equations did not get rendered due to their size.
Run parser p on iterator it.
Instances For
Run parser p on iterator it, returning the parsed value or the error.
Equations
- p.parse it = match p.run it with | ParseIt.Result.ok rest progress x => Except.ok x | ParseIt.Result.error e => Except.error e
Instances For
Positions and errors #
getPos returns the current input position. Consumes no input and never fails.
Equations
- ParseIt.getPos it = pure (ParseIt.Result.ok it (ParseIt.Progress.refl it) (ParseIt.HasPos.pos it))
Instances For
withSpan p parses p and returns its result together with the input positions where p started
and stopped.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Throw an unexpected input error at the current position.
Equations
- ParseIt.throwUnexpected input it = pure (ParseIt.Result.error (ParseIt.Error.unexpected it input))
Instances For
Throw an unexpected input error with an additional message.
Equations
- ParseIt.throwUnexpectedWithMessage input msg it = pure (ParseIt.Result.error (ParseIt.Error.addMessage (ParseIt.Error.unexpected it input) it msg))
Instances For
Add a message to errors reported by p.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Basic combinators #
lookAhead p parses p without consuming any input. If p fails then lookAhead p fails with the
same error.
Equations
- One or more equations did not get rendered due to their size.
Instances For
notFollowedBy p succeeds only if p fails. Consumes no input.
Equations
- One or more equations did not get rendered due to their size.
Instances For
option family #
optionD p default tries to parse p and returns x if p returns x, otherwise returns the
monadic value default. This parser never fails.
Equations
- ParseIt.optionD p default = do let __do_lift ← ParseIt.eoption✝ p match __do_lift with | Sum.inl x => pure x | Sum.inr val => liftM default
Instances For
option! p tries to parse p and returns x if p returns x, otherwise returns
Inhabited.default. This parser never fails.
Equations
Instances For
option? p tries to parse p and returns some x if p returns x, otherwise returns none.
This parser never fails.
Equations
- ParseIt.option? p = ParseIt.optionD (some <$> p) (pure none)
Instances For
optional p tries to parse p, ignoring its output and errors. This parser never fails.
Equations
- ParseIt.optional p = ParseIt.optionD (p *> pure PUnit.unit) (pure PUnit.unit)
Instances For
test p returns true if p succeeds and false otherwise. This parser never fails.
Equations
Instances For
first ps tries the parsers from the list ps in order until one succeeds and returns its result.
When all parsers fail, their errors are folded with combine, from left to right. The default is
Error.merge. When ps is empty, an unexpected input error at the current position is reported.
Equations
- One or more equations did not get rendered due to their size.
- ParseIt.first [] combine it = pure (ParseIt.Result.error (ParseIt.Error.unexpected it none))