Documentation

Parser.Parser

inductive Parser.Result (ε σ α : Type u) :

Parser result type.

Instances For
    instance Parser.instInhabitedResult {a✝ a✝¹ : Type u_1} [Inhabited a✝¹] {a✝² : Type u_1} [Inhabited a✝²] :
    Inhabited (Parser.Result a✝ a✝¹ a✝²)
    Equations
    instance Parser.instReprResult {ε✝ σ✝ α✝ : Type u_1} [Repr ε✝] [Repr σ✝] [Repr α✝] :
    Repr (Parser.Result ε✝ σ✝ α✝)
    Equations
    def ParserT (ε σ : Type u_1) (τ : Type u_2) [Parser.Stream σ τ] [Parser.Error ε σ τ] (m : Type u_1 → Type u_4) (α : Type u_1) :
    Type (max u_4 u_1)

    ParserT ε σ τ is a monad transformer to parse tokens of type τ from the stream type σ with error type ε.

    Equations
    Instances For
      @[inline]
      def ParserT.run {σ : Type u_1} {τ : Type u_2} {ε : Type u_1} {m : Type u_1 → Type u_3} {α : Type u_1} [Parser.Stream σ τ] [Parser.Error ε σ τ] (p : ParserT ε σ τ m α) (s : σ) :
      m (Parser.Result ε σ α)

      Run the monadic parser p on input stream s.

      Equations
      Instances For
        instance instMonadParserT (σ ε : Type u_1) (τ : Type u_2) (m : Type u_1 → Type u_3) [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] :
        Monad (ParserT ε σ τ m)
        Equations
        instance instMonadExceptOfParserTOfMonad (σ ε : Type u_1) (τ : Type u_2) (m : Type u_1 → Type u_3) [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] :
        MonadExceptOf ε (ParserT ε σ τ m)
        Equations
        • One or more equations did not get rendered due to their size.
        instance instOrElseParserTOfMonad {α : Type u_1} (σ ε : Type u_1) (τ : Type u_2) (m : Type u_1 → Type u_3) [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] :
        OrElse (ParserT ε σ τ m α)
        Equations
        • One or more equations did not get rendered due to their size.
        instance instMonadLiftParserTOfMonad (σ ε : Type u_1) (τ : Type u_2) (m : Type u_1 → Type u_3) [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] :
        MonadLift m (ParserT ε σ τ m)
        Equations
        @[reducible, inline]
        abbrev Parser (ε σ : Type u_1) (τ : Type u_2) [Parser.Stream σ τ] [Parser.Error ε σ τ] (α : Type u_1) :
        Type u_1

        Parser ε σ τ monad to parse tokens of type τ from the stream type σ with error type ε.

        Equations
        Instances For
          @[inline]
          def Parser.run {ε σ : Type u_1} {τ : Type u_2} {α : Type u_1} [Parser.Stream σ τ] [Parser.Error ε σ τ] (p : Parser ε σ τ α) (s : σ) :
          Parser.Result ε σ α

          Run parser p on input stream s.

          Equations
          Instances For
            @[reducible, inline]
            abbrev TrivialParserT (σ : Type) (τ : Type u_1) [Parser.Stream σ τ] (m : TypeType u_3) (α : Type) :
            Type u_3

            TrivialParserT σ τ monad transformer to parse tokens of type τ from the stream σ with trivial error handling.

            Equations
            Instances For
              @[reducible, inline]
              abbrev TrivialParser (σ : Type) (τ : Type u_1) [Parser.Stream σ τ] (α : Type) :

              TrivialParser σ τ monad to parse tokens of type τ from the stream σ with trivial error handling.

              Equations
              Instances For
                @[reducible, inline]
                abbrev BasicParserT (σ : Type (max u_1 u_2)) (τ : Type u_2) [Parser.Stream σ τ] (m : Type (max u_1 u_2) → Type u_3) (α : Type (max u_1 u_2)) :
                Type (max u_3 u_1 u_2)

                BasicParserT σ τ monad transformer to parse tokens of type τ from the stream σ with basic error handling.

                Equations
                Instances For
                  @[reducible, inline]
                  abbrev BasicParser (σ : Type (max u_1 u_2)) (τ : Type u_2) [Parser.Stream σ τ] (α : Type (max u_1 u_2)) :
                  Type (max u_1 u_2)

                  BasicParser σ τ monad to parse tokens of type τ from the stream σ with basic error handling.

                  Equations
                  Instances For
                    @[reducible, inline]
                    abbrev SimpleParserT (σ : Type (max u_1 u_2)) (τ : Type u_2) [Parser.Stream σ τ] (m : Type (max u_1 u_2) → Type u_3) (α : Type (max u_1 u_2)) :
                    Type (max u_3 u_1 u_2)

                    SimpleParserT σ τ monad transformer to parse tokens of type τ from the stream σ with simple error handling.

                    Equations
                    Instances For
                      @[reducible, inline]
                      abbrev SimpleParser (σ : Type (max u_1 u_2)) (τ : Type u_2) [Parser.Stream σ τ] (α : Type (max u_1 u_2)) :
                      Type (max u_1 u_2)

                      SimpleParser σ τ monad to parse tokens of type τ from the stream σ with simple error handling.

                      Equations
                      Instances For

                        Stream Functions #

                        @[inline]
                        def Parser.getStream {τ : Type u_1} {m : Type u → Type u_2} {ε σ : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] :
                        ParserT ε σ τ m σ

                        Get parser stream.

                        Equations
                        Instances For
                          @[inline]
                          def Parser.setStream {τ : Type u_1} {m : Type u → Type u_2} {ε σ : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (s : σ) :
                          ParserT ε σ τ m PUnit

                          Set parser stream.

                          Equations
                          Instances For
                            @[inline]
                            def Parser.getPosition {τ : Type u_1} {m : Type u → Type u_2} {ε σ : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] :
                            ParserT ε σ τ m (Stream.Position σ)

                            Get stream position from parser.

                            Equations
                            Instances For
                              @[inline]
                              def Parser.setPosition {τ : Type u_1} {m : Type u → Type u_2} {ε σ : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (pos : Stream.Position σ) :
                              ParserT ε σ τ m PUnit

                              Set stream position from parser.

                              Equations
                              Instances For
                                @[inline]
                                def Parser.withBacktracking {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (p : ParserT ε σ τ m α) :
                                ParserT ε σ τ m α

                                withBacktracking p parses p but does not consume any input on error.

                                Equations
                                Instances For
                                  def Parser.withCapture {τ : Type u_1} {m : Type (max u_2 u_3) → Type u_4} [Monad m] {ε σ α : Type (max u_2 u_3)} [Parser.Stream σ τ] [Parser.Error ε σ τ] (p : ParserT ε σ τ m α) :
                                  ParserT ε σ τ m (α × Stream.Segment σ)

                                  withCapture p parses p and returns the output of p with the corresponding stream segment.

                                  Equations
                                  Instances For

                                    Error Functions #

                                    @[inline]
                                    def Parser.throwUnexpected {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (input : Option τ := none) :
                                    ParserT ε σ τ m α

                                    Throw error on unexpected token.

                                    Equations
                                    Instances For
                                      @[inline]
                                      def Parser.throwErrorWithMessage {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (e : ε) (msg : String) :
                                      ParserT ε σ τ m α

                                      Throw error with additional message.

                                      Equations
                                      Instances For
                                        @[inline]
                                        def Parser.throwUnexpectedWithMessage {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (input : Option τ := none) (msg : String) :
                                        ParserT ε σ τ m α

                                        Throw error on unexpected token with error message.

                                        Equations
                                        Instances For
                                          @[inline]
                                          def Parser.withErrorMessage {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (msg : String) (p : ParserT ε σ τ m α) :
                                          ParserT ε σ τ m α

                                          Add message on parser error.

                                          Equations
                                          Instances For

                                            Low-Level Combinators #

                                            foldl family #

                                            @[inline]
                                            def Parser.efoldlP {τ : Type u_1} {m : Type u → Type u_2} {ε σ α β : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (f : βαParserT ε σ τ m β) (init : β) (p : ParserT ε σ τ m α) :
                                            ParserT ε σ τ m (β × ε × Bool)

                                            foldlP f init p folds the parser function f from left to right using init as an intitial value and the parser p to generate inputs of type α. The folding ends as soon as the update parser function (p >>= f ⬝) fails. Then the final folding result is returned along with the pair:

                                            • (e, true) if the final p succeeds but then f fails reporting error e.
                                            • (e, false) if the final p fails reporting error e.

                                            In either case, the final p is not consumed. This parser never fails.

                                            Equations
                                            Instances For
                                              @[inline]
                                              def Parser.efoldlM {τ : Type u_1} {m : Type u → Type u_2} {ε σ α β : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (f : βαm β) (init : β) (p : ParserT ε σ τ m α) :
                                              ParserT ε σ τ m (β × ε)

                                              foldlM f init p folds the monadic function f from left to right using init as an intitial value and the parser p to generate inputs of type α. The folding ends as soon as p fails and the error reported by p is returned along with the result of folding. This parser never fails.

                                              Equations
                                              Instances For
                                                @[inline]
                                                def Parser.efoldl {τ : Type u_1} {m : Type u → Type u_2} {ε σ α β : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (f : βαβ) (init : β) (p : ParserT ε σ τ m α) :
                                                ParserT ε σ τ m (β × ε)

                                                foldl f init p folds the function f from left to right using init as an intitial value and the parser p to generate inputs of type α. The folding ends as soon as p fails and the error reported by p is returned along with the result of folding. This parser never fails.

                                                Equations
                                                Instances For
                                                  @[inline]
                                                  def Parser.foldlP {τ : Type u_1} {m : Type u → Type u_2} {ε σ α β : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (f : βαParserT ε σ τ m β) (init : β) (p : ParserT ε σ τ m α) :
                                                  ParserT ε σ τ m β

                                                  foldlP f init p folds the parser function f from left to right using init as an intitial value and the parser p to generate inputs of type α. The folding ends as soon as the update function (p >>= f ·) fails. This parser never fails.

                                                  Equations
                                                  Instances For
                                                    @[inline]
                                                    def Parser.foldlM {τ : Type u_1} {m : Type u → Type u_2} {ε σ α β : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (f : βαm β) (init : β) (p : ParserT ε σ τ m α) :
                                                    ParserT ε σ τ m β

                                                    foldlM f init p folds the monadic function f from left to right using init as an intitial value and the parser p to generate inputs of type α. The folding ends as soon as p fails. This parser never fails.

                                                    Equations
                                                    Instances For
                                                      @[inline]
                                                      def Parser.foldl {τ : Type u_1} {m : Type u → Type u_2} {ε σ α β : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (f : βαβ) (init : β) (p : ParserT ε σ τ m α) :
                                                      ParserT ε σ τ m β

                                                      foldl f init p folds the function f from left to right using init as an intitial value and the parser p to generate inputs of type α. The folding ends as soon as p fails. This parser never fails.

                                                      Equations
                                                      Instances For

                                                        option family #

                                                        @[specialize #[]]
                                                        def Parser.eoption {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (p : ParserT ε σ τ m α) :
                                                        ParserT ε σ τ m (α ε)

                                                        eoption p tries to parse p (with backtracking) and returns:

                                                        This parser never fails.

                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        Instances For
                                                          @[inline]
                                                          def Parser.optionM {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (p : ParserT ε σ τ m α) (default : m α) :
                                                          ParserT ε σ τ m α

                                                          optionM p tries to parse p (with backtracking) and returns x if p returns x, returns the monadic value default if p fails. This parser never fails.

                                                          Equations
                                                          Instances For
                                                            @[inline]
                                                            def Parser.optionD {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (p : ParserT ε σ τ m α) (default : α) :
                                                            ParserT ε σ τ m α

                                                            optionD p tries to parse p (with backtracking) and returns x if p returns x, returns default if p fails. This parser never fails.

                                                            Equations
                                                            Instances For
                                                              @[inline]
                                                              def Parser.option! {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] [Inhabited α] (p : ParserT ε σ τ m α) :
                                                              ParserT ε σ τ m α

                                                              option! p tries to parse p (with backtracking) and returns x if p returns x, returns Inhabited.default if p fails. This parser never fails.

                                                              Equations
                                                              Instances For
                                                                @[inline]
                                                                def Parser.option? {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (p : ParserT ε σ τ m α) :
                                                                ParserT ε σ τ m (Option α)

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

                                                                Equations
                                                                Instances For
                                                                  @[inline]
                                                                  def Parser.optional {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (p : ParserT ε σ τ m α) :
                                                                  ParserT ε σ τ m PUnit

                                                                  optional p tries to parse p (with backtracking) ignoring output or errors. This parser never fails.

                                                                  Equations
                                                                  Instances For

                                                                    first family #

                                                                    def Parser.efirst {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (ps : List (ParserT ε σ τ m α)) :
                                                                    ParserT ε σ τ m (Option α × List ε)

                                                                    efirst ps tries parsers from the list ps in order (with backtracking) until one succeeds:

                                                                    • Once a parser p succeeds with value x then some x is returne along with the list of errors from all previous parsers.
                                                                    • If none succeed then none is returned along with the list of errors of all parsers.

                                                                    This parser never fails.

                                                                    Equations
                                                                    Instances For
                                                                      def Parser.efirst.go {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] :
                                                                      List (ParserT ε σ τ m α)List εParserT ε σ τ m (Option α × List ε)
                                                                      Equations
                                                                      Instances For
                                                                        def Parser.first {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (ps : List (ParserT ε σ τ m α)) (combine : εεε := fun (x : ε) => id) :
                                                                        ParserT ε σ τ m α

                                                                        first ps tries parsers from the list ps in order (with backtracking) until one succeeds and returns the result of that parser.

                                                                        The optional parameter combine can be used to control the error reported when all parsers fail. The default is to only report the error from the last parser.

                                                                        Equations
                                                                        Instances For
                                                                          def Parser.first.go {τ : Type u_1} {m : Type u → Type u_2} {ε σ α : Type u} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (combine : εεε := fun (x : ε) => id) :
                                                                          List (ParserT ε σ τ m α)εσm (Parser.Result ε σ α)
                                                                          Equations
                                                                          Instances For