Documentation

ParseIt.Basic

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.

def ParseIt.Advanced {ι τ : Type u} [Std.Iterator ι Id τ] (it' it : Std.Iter τ) :

it' is reachable from it by one or more plausible successor steps.

Equations
Instances For
    theorem ParseIt.Advanced.trans {ι τ : Type u} [Std.Iterator ι Id τ] {it'' it' it : Std.Iter τ} :
    Advanced it'' it' → Advanced it' it → Advanced it'' it
    theorem ParseIt.Advanced.of_yield {ι τ : Type u} [Std.Iterator ι Id τ] {it' it : Std.Iter τ} {out : τ} (h : it.IsPlausibleStep (Std.IterStep.yield it' out)) :
    Advanced it' it
    theorem ParseIt.Advanced.of_skip {ι τ : Type u} [Std.Iterator ι Id τ] {it' it : Std.Iter τ} (h : it.IsPlausibleStep (Std.IterStep.skip it')) :
    Advanced it' it
    inductive ParseIt.Progress {ι τ : Type u} [Std.Iterator ι Id τ] (it' it : Std.Iter τ) :

    How far a parser moved the iterator from it to it'.

    Instances For
      @[inline]
      def ParseIt.Progress.isMoved {ι τ : Type u} [Std.Iterator ι Id τ] {it' it : Std.Iter τ} :
      Progress it' it → Bool

      Whether the iterator moved.

      Equations
      Instances For
        @[inline]
        def ParseIt.Progress.refl {ι τ : Type u} [Std.Iterator ι Id τ] (it : Std.Iter τ) :
        Progress it it

        No progress.

        Equations
        Instances For
          inductive ParseIt.Result {ι τ : Type u} [Std.Iterator ι Id τ] (ε : Type u) (it : Std.Iter τ) (α : Type u) :

          Parser result for a parse starting at iterator it.

          Instances For
            def ParseIt.ParserT (ε ι τ : Type u) [Std.Iterator ι Id τ] (m : Type u → Type v) (α : Type u) :
            Type (max u v)

            ParserT ε ι τ m is a monad transformer to parse tokens of type τ from an iterator with internal state ι, with error type ε.

            Equations
            Instances For
              @[reducible, inline]
              abbrev ParseIt.Parser (ε ι τ : Type u) [Std.Iterator ι Id τ] (α : Type u) :

              Parser ε ι τ is the parser monad without an underlying monad.

              Equations
              Instances For
                @[inline]
                def ParseIt.ParserT.run {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] (p : ParserT ε ι τ m α) (it : Std.Iter τ) :
                m (Result ε it α)

                Run parser p on iterator it.

                Equations
                Instances For
                  @[instance_reducible]
                  instance ParseIt.ParserT.instMonad {ε ι τ : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] :
                  Monad (ParserT ε ι τ m)
                  Equations
                  • One or more equations did not get rendered due to their size.
                  @[instance_reducible]
                  instance ParseIt.ParserT.instMonadExceptOfOfMonad {ε ι τ : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] :
                  MonadExceptOf ε (ParserT ε ι τ m)
                  Equations
                  • One or more equations did not get rendered due to their size.
                  @[instance_reducible]
                  instance ParseIt.ParserT.instMonadLiftOfMonad {ε ι τ : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] :
                  MonadLift m (ParserT ε ι τ m)
                  Equations
                  • One or more equations did not get rendered due to their size.
                  @[instance_reducible]
                  instance ParseIt.ParserT.instOrElseOfMonadOfError {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] [Error ε ι τ] :
                  OrElse (ParserT ε ι τ m α)

                  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.
                  @[inline]
                  def ParseIt.Parser.run {ε ι τ α : Type u} [Std.Iterator ι Id τ] (p : Parser ε ι τ α) (it : Std.Iter τ) :
                  Result ε it α

                  Run parser p on iterator it.

                  Equations
                  Instances For
                    @[inline]
                    def ParseIt.Parser.parse {ε ι τ α : Type u} [Std.Iterator ι Id τ] (p : Parser ε ι τ α) (it : Std.Iter τ) :
                    Except ε α

                    Run parser p on iterator it, returning the parsed value or the error.

                    Equations
                    Instances For

                      Positions and errors #

                      @[inline]
                      def ParseIt.getPos {ε ι τ : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Pure m] [HasPos ι τ] :
                      ParserT ε ι τ m (HasPos.Pos ι)

                      getPos returns the current input position. Consumes no input and never fails.

                      Equations
                      Instances For
                        @[inline]
                        def ParseIt.withSpan {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] [HasPos ι τ] (p : ParserT ε ι τ m α) :
                        ParserT ε ι τ m (α × HasPos.Pos ι × HasPos.Pos ι)

                        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
                          @[inline]
                          def ParseIt.throwUnexpected {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Pure m] [Error ε ι τ] (input : Option τ := none) :
                          ParserT ε ι τ m α

                          Throw an unexpected input error at the current position.

                          Equations
                          Instances For
                            @[inline]
                            def ParseIt.throwUnexpectedWithMessage {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Pure m] [Error ε ι τ] (input : Option τ := none) (msg : String) :
                            ParserT ε ι τ m α

                            Throw an unexpected input error with an additional message.

                            Equations
                            Instances For
                              @[inline]
                              def ParseIt.withErrorMessage {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] [Error ε ι τ] (msg : String) (p : ParserT ε ι τ m α) :
                              ParserT ε ι τ m α

                              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 #

                                @[inline]
                                def ParseIt.lookAhead {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] (p : ParserT ε ι τ m α) :
                                ParserT ε ι τ m α

                                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
                                  @[inline]
                                  def ParseIt.notFollowedBy {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] [Error ε ι τ] (p : ParserT ε ι τ m α) :
                                  ParserT ε ι τ m Unitᵤ

                                  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 #

                                    @[inline]
                                    def ParseIt.optionD {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] (p : ParserT ε ι τ m α) (default : m α) :
                                    ParserT ε ι τ m α

                                    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
                                    Instances For
                                      @[inline]
                                      def ParseIt.option! {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] [Inhabited α] (p : ParserT ε ι τ m α) :
                                      ParserT ε ι τ m α

                                      option! p tries to parse p and returns x if p returns x, otherwise returns Inhabited.default. This parser never fails.

                                      Equations
                                      Instances For
                                        @[inline]
                                        def ParseIt.option? {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] (p : ParserT ε ι τ m α) :
                                        ParserT ε ι τ m (Option α)

                                        option? p tries to parse p and returns some x if p returns x, otherwise returns none. This parser never fails.

                                        Equations
                                        Instances For
                                          @[inline]
                                          def ParseIt.optional {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] (p : ParserT ε ι τ m α) :
                                          ParserT ε ι τ m Unitᵤ

                                          optional p tries to parse p, ignoring its output and errors. This parser never fails.

                                          Equations
                                          Instances For
                                            @[inline]
                                            def ParseIt.test {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] (p : ParserT ε ι τ m α) :
                                            ParserT ε ι τ m Boolᵤ

                                            test p returns true if p succeeds and false otherwise. This parser never fails.

                                            Equations
                                            Instances For

                                              first family #

                                              def ParseIt.first {ε ι τ α : Type u} {m : Type u → Type v} [Std.Iterator ι Id τ] [Monad m] [Error ε ι τ] (ps : List (ParserT ε ι τ m α)) (combine : ε → ε → ε := Error.merge ι) :
                                              ParserT ε ι τ m α

                                              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
                                              Instances For