HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleheaded-megaparsec-0.2.1.3Haskell2010

HeadedMegaparsec

  • 1 type
  • 8 values

Types

1 declaration
newtypenewtype HeadedParsec err strm a
#

Headed parser.

Abstracts over explicit composition between consecutive megaparsec try blocks, providing for better error messages.

With headed parser you don't need to use try at all.

Examples

Example7 expressions
import Preludeimport Control.Applicativeimport Data.Voidimport qualified Text.Megaparsec as Mimport qualified Text.Megaparsec.Char as Mimport qualified Text.Megaparsec.Char.Lexer as ML:{  let    select :: HeadedParsec Void String (Maybe [Either Char Int], Maybe Int)    select = do      string' "select"      endHead      _targets <- optional (space1 *> targets)      _limit <- optional (space1 *> limit)      return (_targets, _limit)      where        -- Lifted versions of basic parsers:        char = parse . M.char        space = parse M.space        space1 = parse M.space1        decimal = parse ML.decimal        string' = parse . M.string'        -- Syntax parsers:        targets = M.sepBy1 target commaSeparator        target = Left <$> char '*' <|> Right <$> decimal        commaSeparator = space *> char ',' *> endHead *> space        limit = string' "limit" *> endHead *> space1 *> decimal    test :: String -> IO ()    test = M.parseTest (toParsec select <* M.eof):}
Example1 expression
test "select 1, "1:11:  |1 | select 1,  |           ^unexpected end of inputexpecting '*', integer, or white space
Example1 expression
test "select limit "...unexpected end of inputexpecting integer or white space
Example1 expression
test "select 1, 2 limit 2"(Just [Right 1,Right 2],Just 2)
Instances7Monad, Functor, MonadFail, Applicative, Alternative, MonadPlus, …

Execution

1 declaration

Transformation

5 declarations
valuewrapToHead
  1. :: (Ord err, Stream strm)
  2. => HeadedParsec err strm a
  3. -> HeadedParsec err strm a
#

Wrap a parser to be usable as a whole in a head block, allowing it in effect to be composed with the following parsers into a single try when executed, no matter whether it contains endHead or not.

Construction

2 declarations