Documentation

Parser_.Common

structure ParseError (σ : Type u_1) (τ : Type u_2) [Parser.Stream σ τ] :
Type (max u_2 u_3)

A megaparsec-shape parse error, ε for every Parser_ lexer/parser: the token that failed to match (none at end of input) and every spelling that would have matched there instead. ErrorCombine unions two same-position errors' expected; Parser_.withErrorMessage replaces one outright, only when its own parser failed without consuming (megaparsec's <?>/label) — never appends, which is what made the old chain-of-messages design repeat "expected expression" once per level of parseExpression's recursive descent.

posOverride, unset in the ordinary case, is for a sub-parser running over a stream this error's own Stream.Position σ cannot describe on its own (Annotations.tryParseAnnotations', over the flat concatenated comment text): it resolves its own position eagerly, against comment boundaries only it has in scope, and hands the result over pre-resolved.

Instances For
    @[implicit_reducible]
    instance instReprParseErrorOfPosition {σ : Type u_1} {τ : Type u_2} [Parser.Stream σ τ] [Repr (Parser.Stream.Position σ)] [Repr τ] :
    Equations
    • One or more equations did not get rendered due to their size.
    @[implicit_reducible]
    Equations
    @[implicit_reducible]
    instance instErrorParseError {σ : Type u_1} {τ : Type u_2} [Parser.Stream σ τ] :
    Parser.Error (ParseError σ τ) σ τ
    Equations
    • One or more equations did not get rendered due to their size.
    class ErrorCombine (ε : Type u_1) :
    Type u_1

    How two errors alt/first found at the same position (their job to establish that) combine into the one actually reported.

    • combine : εεε
    Instances
      @[implicit_reducible]
      instance instErrorCombineParseError {σ : Type u_1} {τ : Type u_2} [Parser.Stream σ τ] :
      Equations
      • One or more equations did not get rendered due to their size.
      @[implicit_reducible]
      instance instErrorCombineSimple {σ : Type u_1} {τ : Type u_2} [Parser.Stream σ τ] :
      Equations
      def ParseError.expectedHints {σ : Type u_1} {τ : Type u_2} [Parser.Stream σ τ] (e : ParseError σ τ) :

      Render a ParseError's expected as the Unexpected hint it becomes: nothing when empty; several spellings become "expected one of: …"; one spelling gets "expected " prepended if it reads as a bare noun phrase (token's toString tk, "identifier", …, all lowercase-led), or is shown verbatim if it already reads as a full sentence — either the library's own char/chars/ Unicode.* (withErrorMessage s!"expected {repr tk}", already "expected "-led) or fugue's own direct, capitalized messages ("Operator conflict …", checkConflicts) — the one place these three uses of the same expected : List String field are told apart.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        def throwExpected {σ : Type (max u_1 u_2)} {τ : Type u_1} {m : Type (max u_1 u_2) → Type u_3} {α : Type (max u_2 u_1)} [Parser.Stream σ τ] [Monad m] (tok : Option τ) (items : List String) :
        ParserT (ParseError σ τ) σ τ m α

        Fail with tok unexpected and items (bare noun phrases) as the full expected set — a peek-dispatch catch-all's way to name every alternative a match itself cannot state, since Parser.Error.addMessage only ever replaces expected with one string at a time.

        Equations
        Instances For
          def ParserT.mapStream {τ₁ τ₂ α : Type u_1} {m : Type u_1 → Type u_2} [Monad m] (f : τ₂Option τ₁) (g : τ₁τ₂) (p : ParserT (ParseError (TokenStream τ₁) τ₁) (TokenStream τ₁) τ₁ m α) :
          ParserT (ParseError (TokenStream τ₂) τ₂) (TokenStream τ₂) τ₂ m α

          Run a parser written against one token type inside a parser over another. f maps an outer token to an inner one (returning none at the first token the inner parser has no business seeing); g maps back. The inner parser sees the maximal f-mapped prefix of the tokens not yet consumed, indexed from 0, so mapError shifts its reported positions back by the number of tokens already consumed. Only the cursor moves in the outer stream — its token array is untouched, so the outer tokens g would have reconstructed are the originals.

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            def Parser.Result.isOk {ε σ α : Type u_1} :
            Parser.Result ε σ αBool
            Equations
            Instances For
              @[unbox]
              Instances For
                @[implicit_reducible]
                Equations
                @[implicit_reducible]
                Equations
                @[implicit_reducible]
                Equations
                @[implicit_reducible]
                Equations
                • One or more equations did not get rendered due to their size.
                structure Located (α : Type u_1) :
                Type u_1
                Instances For
                  @[implicit_reducible]
                  Equations
                  • One or more equations did not get rendered due to their size.
                  @[implicit_reducible]
                  instance instInhabitedLocated {α : Type u_1} [Inhabited α] :
                  Equations
                  @[unbox]
                  structure Located' (α : Type u_1) :
                  Type u_1

                  A piece of data is located if its span in the stream is fully known.

                  Instances For
                    def instReprLocated'.repr {α✝ : Type u_1} [Repr α✝] :
                    Located' α✝Std.Format
                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For
                      @[implicit_reducible]
                      instance instReprLocated' {α✝ : Type u_1} [Repr α✝] :
                      Repr (Located' α✝)
                      Equations
                      @[implicit_reducible]
                      instance instInhabitedLocated' {a✝ : Type u_1} [Inhabited a✝] :
                      Equations
                      def instDecidableEqLocated'.decEq {α✝ : Type u_1} [DecidableEq α✝] (x✝ x✝¹ : Located' α✝) :
                      Decidable (x✝ = x✝¹)
                      Equations
                      Instances For
                        @[implicit_reducible]
                        instance instBEqLocated' {α✝ : Type u_1} [BEq α✝] :
                        BEq (Located' α✝)
                        Equations
                        def instBEqLocated'.beq {α✝ : Type u_1} [BEq α✝] :
                        Located' α✝Located' α✝Bool
                        Equations
                        Instances For
                          @[implicit_reducible]
                          instance instToStringLocated' {α : Type u_1} [ToString α] :

                          Describes the payload, position dropped — a located value reads the same as its data wherever only a human-facing description is wanted (Parser_.token's "expected" hints).

                          Equations
                          structure Unexpected (α : Type u_1) :
                          Type u_1
                          Instances For
                            def instReprUnexpected.repr {α✝ : Type u_1} [Repr α✝] :
                            Unexpected α✝Std.Format
                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              @[implicit_reducible]
                              instance instReprUnexpected {α✝ : Type u_1} [Repr α✝] :
                              Equations
                              @[implicit_reducible]
                              instance instInhabitedUnexpected {a✝ : Type u_1} :
                              Equations
                              @[implicit_reducible]
                              instance instToStringUnexpected {α : Type u_1} [ToString α] :
                              Equations
                              • One or more equations did not get rendered due to their size.
                              @[implicit_reducible]
                              Equations
                              • One or more equations did not get rendered due to their size.
                              inductive ParserWarning :

                              Warnings raised by the parser itself, as opposed to hard errors (Unexpected). Collected out-of-band during parsing (ParserWarningM, below) rather than emitted immediately, and filtered/printed by the compiler driver once parsing returns.

                              • fairIgnored (pos : SourceSpan) : ParserWarning

                                fair process/fair+ was parsed and round-tripped, but is never acted on.

                              • unusedAnnotation (pos : SourceSpan) : ParserWarning

                                A comment parses as a well-formed annotation (@type/@mailbox/@parameter) but sits where no call site consumes it, so it is silently ignored. Distinct from a misplaced annotation (captured, but attached to the wrong role), which is a hard error.

                              Instances For
                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For

                                  The -W<name>/-Wno-<name> name a given warning is filtered under.

                                  Equations
                                  Instances For
                                    @[implicit_reducible]
                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    @[reducible, inline]
                                    abbrev ParserWarningM (α : Type) :

                                    The base monad every parser in Parser_ runs against: Id plus a List ParserWarning accumulator.

                                    Equations
                                    Instances For
                                      class MonadParserBacktrack (m : TypeType u_1) :
                                      Type (max 1 u_1)

                                      The base-monad state a backtrack has to undo, alongside the stream position. For a parser over ParserWarningM that is the accumulated ParserWarning list: a warning emitted inside an alternative that is then abandoned must not survive. Id has no such state, so its instance is a no-op and the lexer pays nothing.

                                      • Saved : Type

                                        The part of m's state to snapshot and restore.

                                      • save : m (Saved m)

                                        Take a snapshot.

                                      • restore : Saved mm PUnit.{1}

                                        Restore a snapshot.

                                      Instances
                                        @[implicit_reducible]
                                        Equations
                                        @[never_extract, macro_inline, specialize #[]]
                                        def debug {ε σ : Type u_1} {τ : Type u_2} {m : Type u_1 → Type u_3} {α : Type u_1} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] [Repr ε] [Repr α] [Repr (Parser.Stream.Position σ)] (_name : String) (p : ParserT ε σ τ m α) :
                                        ParserT ε σ τ m α

                                        debug name p runs p unchanged. It is the single seam parser tracing is inserted at, and name is the label such a trace would carry; call sites tag themselves once and stay tagged.

                                        Equations
                                        Instances For
                                          @[specialize #[]]
                                          def eoption {ε σ : Type} {τ : Type u_1} {m : TypeType u_2} {α : Type} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] [MonadParserBacktrack m] [BEq (Parser.Stream.Position σ)] (p : ParserT ε σ τ m α) :
                                          ParserT ε σ τ m (Option α)

                                          Tries to execute a parser p and returns its result. If p fails without consuming tokens, returns none and rolls back any warnings p emitted before failing; if p fails after consuming input, that failure propagates unchanged.

                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For
                                            def takeMany1 {ε σ : Type} {τ : Type u_1} {m : TypeType u_2} {α : Type} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] [MonadParserBacktrack m] [BEq (Parser.Stream.Position σ)] (p : ParserT ε σ τ m α) :
                                            ParserT ε σ τ m (Array α)

                                            takeMany1 p applies p at least once, collecting the results. Fails if p cannot be applied at least once, or if p fails while consuming input. Warnings emitted by the final, failed (non-consuming) application of p are rolled back; warnings from the applications that succeeded are kept.

                                            Equations
                                            • One or more equations did not get rendered due to their size.
                                            Instances For
                                              def takeMany {ε σ : Type} {τ : Type u_1} {m : TypeType u_2} {α : Type} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] [MonadParserBacktrack m] [BEq (Parser.Stream.Position σ)] (p : ParserT ε σ τ m α) :
                                              ParserT ε σ τ m (Array α)

                                              takeMany p tries to repeatedly apply p until it does not parse, collecting its results. Fails if p fails while consuming input at some point. Warnings from the final, failed (non-consuming) application are rolled back.

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For

                                                Strict (megaparsec) choice combinators #

                                                fgdorais/Parser's OrElse, first, sepBy* reset the stream position on every failure, so p <|> q is really try p <|> q and every first alternative is implicitly atomic. These shadow them with megaparsec semantics: a failure that consumed input propagates, and only a failure at the entry position falls through to the next alternative. withBacktracking (a.k.a. try) is the one sanctioned opt-out, and it now also rolls back base-monad warnings.

                                                Kept in namespace Parser_ so a parser namespace opts in with open Parser_ while the lexer, which still wants the library's backtracking first/<|> for its symbol trie, simply does not.

                                                @[specialize #[]]
                                                def Parser_.withBacktracking {ε σ : Type} {τ : Type u_1} {m : TypeType u_2} {α : Type} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] [MonadParserBacktrack m] (p : ParserT ε σ τ m α) :
                                                ParserT ε σ τ m α

                                                withBacktracking p runs p and, if p fails, rewinds the stream position and any warnings p emitted back to the entry point, so a failed p leaves no trace. Shadows the library's, which rewinds the position only. This is megaparsec try.

                                                Equations
                                                • One or more equations did not get rendered due to their size.
                                                Instances For
                                                  @[specialize #[]]
                                                  def Parser_.tokenMap {ε σ : Type u_1} {τ : Type u_2} {m : Type u_1 → Type u_3} {α : Type u_1} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] (test : τOption α) :
                                                  ParserT ε σ τ m α

                                                  tokenMap test reads one token and maps it through test. On none it fails without consuming — the library's tokenCore advances the stream before the test runs, which turns every rejected token into a phantom consumption and makes strict alt/first commit to the wrong branch. Shadows Parser.tokenMap; tokenFilter/token/anyToken sit on top of it.

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

                                                    tokenFilter test accepts and returns a token satisfying test, failing without consuming otherwise.

                                                    Equations
                                                    Instances For
                                                      @[inline]
                                                      def Parser_.token {ε σ τ : Type u_1} {m : Type u_1 → Type u_2} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] [DecidableEq τ] (tk : τ) :
                                                      ParserT ε σ τ m τ

                                                      token tk accepts and returns tk, failing without consuming otherwise.

                                                      Equations
                                                      Instances For
                                                        @[inline]
                                                        def Parser_.anyToken {ε σ τ : Type u_1} {m : Type u_1 → Type u_2} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] :
                                                        ParserT ε σ τ m τ

                                                        anyToken consumes and returns one token, failing (without consuming) only at end of input.

                                                        Equations
                                                        Instances For
                                                          @[specialize #[]]
                                                          def Parser_.alt {ε σ : Type} {τ : Type u_1} {m : TypeType u_2} {α : Type} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] [MonadParserBacktrack m] [BEq (Parser.Stream.Position σ)] [ErrorCombine ε] (p q : ParserT ε σ τ m α) :
                                                          ParserT ε σ τ m α

                                                          alt p q: run p; if it fails without consuming input, roll its warnings back and run q; if p consumed input before failing, that failure propagates and q is never tried. This is megaparsec <|> — wrap p in withBacktracking to get the retry-after-consumption the library's <|> always gives. When q also fails at that same entry position, the two errors combine (ErrorCombine) rather than p's being silently dropped — p <||> q failing means "neither matched", and the report should say what either would have accepted.

                                                          Equations
                                                          • One or more equations did not get rendered due to their size.
                                                          Instances For

                                                            alt p q: run p; if it fails without consuming input, roll its warnings back and run q; if p consumed input before failing, that failure propagates and q is never tried. This is megaparsec <|> — wrap p in withBacktracking to get the retry-after-consumption the library's <|> always gives. When q also fails at that same entry position, the two errors combine (ErrorCombine) rather than p's being silently dropped — p <||> q failing means "neither matched", and the report should say what either would have accepted.

                                                            Equations
                                                            Instances For
                                                              @[specialize #[]]
                                                              def Parser_.first {ε σ : Type} {τ : Type u_1} {α : Type} {m : TypeType u_2} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] [MonadParserBacktrack m] [BEq (Parser.Stream.Position σ)] [ErrorCombine ε] (ps : List (ParserT ε σ τ m α)) :
                                                              ParserT ε σ τ m α

                                                              first ps tries the alternatives in order with megaparsec semantics (see alt): the first that consumes input commits, and only same-position failures fall through. On total failure every alternative's expected is merged into the one reported error.

                                                              Equations
                                                              Instances For
                                                                @[specialize #[]]
                                                                def Parser_.withErrorMessage {ε σ : Type u_1} {τ : Type u_2} {m : Type u_1 → Type u_3} {α : Type u_1} [Parser.Stream σ τ] [Parser.Error ε σ τ] [Monad m] [BEq (Parser.Stream.Position σ)] (msg : String) (p : ParserT ε σ τ m α) :
                                                                ParserT ε σ τ m α

                                                                withErrorMessage msg p: if p fails without consuming input, replace whatever it expected with msg (megaparsec's <?>/label); a failure that consumed input propagates untouched. Shadows the library's withErrorMessage, which wraps unconditionally: used directly on a recursive-descent parser like parseExpression, every nested call would re-wrap whatever the previous level had already wrapped, repeating "expected expression" once per level.

                                                                Equations
                                                                • One or more equations did not get rendered due to their size.
                                                                Instances For
                                                                  @[specialize #[]]
                                                                  def Parser_.sepAccBy1 {ε σ : Type} {τ : Type u_1} {α β : Type} {m : TypeType u_2} [Monad m] [Parser.Stream σ τ] [Parser.Error ε σ τ] [MonadParserBacktrack m] [BEq (Parser.Stream.Position σ)] (sep : ParserT ε σ τ m α) (p : ParserT ε σ τ m β) :
                                                                  ParserT ε σ τ m (List β × List α)

                                                                  A variant of sepBy1 sep p which also returns the collected results of the parser sep.

                                                                  Equations
                                                                  • One or more equations did not get rendered due to their size.
                                                                  Instances For
                                                                    @[specialize #[]]
                                                                    def Parser_.sepBy1 {ε σ : Type} {τ : Type u_1} {α β : Type} {m : TypeType u_2} [Monad m] [Parser.Stream σ τ] [Parser.Error ε σ τ] [MonadParserBacktrack m] [BEq (Parser.Stream.Position σ)] (sep : ParserT ε σ τ m β) (p : ParserT ε σ τ m α) :
                                                                    ParserT ε σ τ m (Array α)

                                                                    sepBy1 sep p parses one or more p separated by sep, megaparsec-style: sep is a probe (a sep that consumes then fails — e.g. its whitespace/comment skip runs before the token check misses — ends the list cleanly), but once sep is in, a p that fails after consuming propagates its error rather than being silently dropped. No trailing sep is consumed.

                                                                    Equations
                                                                    Instances For
                                                                      @[specialize #[]]
                                                                      def Parser_.sepBy {ε σ : Type} {τ : Type u_1} {α β : Type} {m : TypeType u_2} [Monad m] [Parser.Stream σ τ] [Parser.Error ε σ τ] [MonadParserBacktrack m] [BEq (Parser.Stream.Position σ)] (sep : ParserT ε σ τ m β) (p : ParserT ε σ τ m α) :
                                                                      ParserT ε σ τ m (Array α)

                                                                      sepBy sep p parses zero or more p separated by sep. Same strictness as sepBy1 once the first p is in.

                                                                      Equations
                                                                      • One or more equations did not get rendered due to their size.
                                                                      Instances For
                                                                        @[specialize #[]]
                                                                        def Parser_.sepNoEndBy1 {ε σ : Type} {τ : Type u_1} {α β : Type} {m : TypeType u_2} [Monad m] [Parser.Stream σ τ] [Parser.Error ε σ τ] [MonadParserBacktrack m] [BEq (Parser.Stream.Position σ)] (sep : ParserT ε σ τ m β) (p : ParserT ε σ τ m α) :
                                                                        ParserT ε σ τ m (Array α)

                                                                        Alias: under strict semantics sepBy1 already refuses a trailing sep.

                                                                        Equations
                                                                        Instances For
                                                                          def Parser_.sepEndBy1 {ε σ : Type} {τ : Type u_1} {α β : Type} {m : TypeType u_2} [Monad m] [Parser.Stream σ τ] [Parser.Error ε σ τ] [MonadParserBacktrack m] [BEq (Parser.Stream.Position σ)] (sep : ParserT ε σ τ m β) (p : ParserT ε σ τ m α) :
                                                                          ParserT ε σ τ m (Array α)

                                                                          sepEndBy1 sep p parses one or more p separated by sep with an optional trailing sep — the case where sep doubles as a terminator (PlusCal's statement ;). A sep with a non-p after it ends the list; a p that fails after consuming still propagates its error.

                                                                          Equations
                                                                          Instances For