2015-07-28 16:32:19 +03:00
|
|
|
|
-- |
|
2015-08-01 19:24:45 +03:00
|
|
|
|
-- Module : Text.Megaparsec
|
|
|
|
|
-- Copyright : © 2015 Megaparsec contributors
|
2015-07-30 19:20:37 +03:00
|
|
|
|
-- © 2007 Paolo Martini
|
|
|
|
|
-- © 1999–2001 Daan Leijen
|
2015-07-28 16:32:19 +03:00
|
|
|
|
-- License : BSD3
|
|
|
|
|
--
|
|
|
|
|
-- Maintainer : Mark Karpov <markkarpov@opmbx.org>
|
2015-07-29 11:38:32 +03:00
|
|
|
|
-- Stability : experimental
|
2015-07-28 16:32:19 +03:00
|
|
|
|
-- Portability : portable
|
|
|
|
|
--
|
|
|
|
|
-- This module includes everything you need to get started writing a parser.
|
|
|
|
|
--
|
|
|
|
|
-- By default this module is set up to parse character data. If you'd like to
|
|
|
|
|
-- parse the result of your own tokenizer you should start with the following
|
|
|
|
|
-- imports:
|
|
|
|
|
--
|
2015-08-01 19:24:45 +03:00
|
|
|
|
-- > import Text.Megaparsec.Prim
|
|
|
|
|
-- > import Text.Megaparsec.Combinator
|
2015-07-28 16:32:19 +03:00
|
|
|
|
--
|
|
|
|
|
-- Then you can implement your own version of 'satisfy' on top of the
|
|
|
|
|
-- 'tokenPrim' primitive.
|
2015-08-03 21:15:16 +03:00
|
|
|
|
--
|
|
|
|
|
-- Typical import section looks like this:
|
|
|
|
|
--
|
|
|
|
|
-- > import Text.Megaparsec
|
|
|
|
|
-- > import Text.Megaparsec.String
|
|
|
|
|
-- > -- import Text.Megaparsec.ByteString
|
|
|
|
|
-- > -- import Text.Megaparsec.ByteString.Lazy
|
|
|
|
|
-- > -- import Text.Megaparsec.Text
|
|
|
|
|
-- > -- import Text.Megaparsec.Text.Lazy
|
|
|
|
|
--
|
|
|
|
|
-- As you can see the second import depends on data type you want to use as
|
|
|
|
|
-- input stream. It just defines useful type-synonyms @Parser@ and
|
|
|
|
|
-- @GenParser@ and @parseFromFile@ function.
|
|
|
|
|
--
|
|
|
|
|
-- Megaparsec is capable of a lot. Apart from this standard functionality
|
|
|
|
|
-- you can parse permutation phrases with "Text.Megaparsec.Perm" and even
|
|
|
|
|
-- entire languages with "Text.Megaparsec.Token". These modules should be
|
|
|
|
|
-- imported explicitly along with two modules mentioned above.
|
2014-03-24 05:07:39 +04:00
|
|
|
|
|
2015-08-01 19:24:45 +03:00
|
|
|
|
module Text.Megaparsec
|
2015-07-28 16:32:19 +03:00
|
|
|
|
(
|
|
|
|
|
-- * Parsers
|
2014-03-24 05:07:39 +04:00
|
|
|
|
ParsecT
|
|
|
|
|
, Parsec
|
|
|
|
|
, token
|
|
|
|
|
, tokens
|
|
|
|
|
, runParserT
|
|
|
|
|
, runParser
|
|
|
|
|
, parse
|
2015-07-30 18:45:06 +03:00
|
|
|
|
, parseMaybe
|
2014-03-24 05:07:39 +04:00
|
|
|
|
, parseTest
|
|
|
|
|
, getPosition
|
|
|
|
|
, getInput
|
|
|
|
|
, getState
|
|
|
|
|
, putState
|
|
|
|
|
, modifyState
|
2015-07-28 16:32:19 +03:00
|
|
|
|
-- * Combinators
|
2015-08-01 17:39:20 +03:00
|
|
|
|
, (A.<|>)
|
|
|
|
|
-- $assocbo
|
|
|
|
|
, A.many
|
|
|
|
|
-- $many
|
|
|
|
|
, A.some
|
|
|
|
|
-- $some
|
|
|
|
|
, A.optional
|
|
|
|
|
-- $optional
|
2014-03-24 05:07:39 +04:00
|
|
|
|
, (<?>)
|
|
|
|
|
, label
|
|
|
|
|
, try
|
|
|
|
|
, unexpected
|
|
|
|
|
, choice
|
|
|
|
|
, skipMany
|
2015-08-01 17:39:20 +03:00
|
|
|
|
, skipSome
|
2014-03-24 05:07:39 +04:00
|
|
|
|
, count
|
|
|
|
|
, between
|
|
|
|
|
, option
|
|
|
|
|
, optionMaybe
|
|
|
|
|
, sepBy
|
|
|
|
|
, sepBy1
|
|
|
|
|
, endBy
|
|
|
|
|
, endBy1
|
|
|
|
|
, sepEndBy
|
|
|
|
|
, sepEndBy1
|
|
|
|
|
, chainl
|
|
|
|
|
, chainl1
|
|
|
|
|
, chainr
|
|
|
|
|
, chainr1
|
|
|
|
|
, eof
|
|
|
|
|
, notFollowedBy
|
|
|
|
|
, manyTill
|
|
|
|
|
, lookAhead
|
|
|
|
|
, anyToken
|
2015-07-28 16:32:19 +03:00
|
|
|
|
-- * Character parsing
|
|
|
|
|
, oneOf
|
|
|
|
|
, noneOf
|
|
|
|
|
, spaces
|
|
|
|
|
, space
|
|
|
|
|
, newline
|
|
|
|
|
, crlf
|
|
|
|
|
, endOfLine
|
|
|
|
|
, tab
|
|
|
|
|
, upper
|
|
|
|
|
, lower
|
|
|
|
|
, alphaNum
|
|
|
|
|
, letter
|
|
|
|
|
, digit
|
|
|
|
|
, hexDigit
|
|
|
|
|
, octDigit
|
|
|
|
|
, char
|
|
|
|
|
, anyChar
|
|
|
|
|
, satisfy
|
|
|
|
|
, string
|
|
|
|
|
-- * Error messages
|
2008-01-13 20:53:15 +03:00
|
|
|
|
, ParseError
|
|
|
|
|
, errorPos
|
2015-07-28 16:32:19 +03:00
|
|
|
|
-- * Position
|
2008-01-13 20:53:15 +03:00
|
|
|
|
, SourcePos
|
2015-08-03 21:02:23 +03:00
|
|
|
|
, SourceName
|
|
|
|
|
, Line
|
|
|
|
|
, Column
|
|
|
|
|
, sourceName
|
|
|
|
|
, sourceLine
|
|
|
|
|
, sourceColumn
|
2015-07-28 16:32:19 +03:00
|
|
|
|
-- * Low-level operations
|
2015-01-10 05:33:05 +03:00
|
|
|
|
, Stream (..)
|
|
|
|
|
, Consumed (..)
|
|
|
|
|
, Reply (..)
|
|
|
|
|
, State (..)
|
2015-08-03 21:02:23 +03:00
|
|
|
|
, tokenPrim
|
|
|
|
|
, getParserState
|
|
|
|
|
, setParserState
|
|
|
|
|
, updateParserState
|
2014-03-24 05:07:39 +04:00
|
|
|
|
, setPosition
|
2015-07-30 18:45:06 +03:00
|
|
|
|
, setInput )
|
2015-07-28 16:32:19 +03:00
|
|
|
|
where
|
2008-01-13 20:53:15 +03:00
|
|
|
|
|
2015-08-01 17:39:20 +03:00
|
|
|
|
import qualified Control.Applicative as A
|
|
|
|
|
|
2015-08-01 19:24:45 +03:00
|
|
|
|
import Text.Megaparsec.Char
|
|
|
|
|
import Text.Megaparsec.Combinator
|
|
|
|
|
import Text.Megaparsec.Error
|
|
|
|
|
import Text.Megaparsec.Pos
|
|
|
|
|
import Text.Megaparsec.Prim
|
2015-08-01 17:39:20 +03:00
|
|
|
|
|
|
|
|
|
-- $assocbo
|
|
|
|
|
--
|
|
|
|
|
-- This combinator implements choice. The parser @p \<|> q@ first applies
|
|
|
|
|
-- @p@. If it succeeds, the value of @p@ is returned. If @p@ fails
|
|
|
|
|
-- /without consuming any input/, parser @q@ is tried.
|
|
|
|
|
--
|
|
|
|
|
-- The parser is called /predictive/ since @q@ is only tried when parser @p@
|
|
|
|
|
-- didn't consume any input (i.e. the look ahead is 1). This
|
|
|
|
|
-- non-backtracking behaviour allows for both an efficient implementation of
|
|
|
|
|
-- the parser combinators and the generation of good error messages.
|
|
|
|
|
|
|
|
|
|
-- $many
|
|
|
|
|
--
|
|
|
|
|
-- @many p@ applies the parser @p@ /zero/ or more times. Returns a list of
|
|
|
|
|
-- the returned values of @p@.
|
|
|
|
|
--
|
|
|
|
|
-- > identifier = (:) <$> letter <*> many (alphaNum <|> char '_')
|
|
|
|
|
|
|
|
|
|
-- $some
|
|
|
|
|
--
|
|
|
|
|
-- @some p@ applies the parser @p@ /one/ or more times. Returns a list of
|
|
|
|
|
-- the returned values of @p@.
|
|
|
|
|
--
|
|
|
|
|
-- > word = some letter
|
|
|
|
|
|
|
|
|
|
-- $optional
|
|
|
|
|
--
|
|
|
|
|
-- @optional p@ tries to apply parser @p@. It will parse @p@ or nothing. It
|
|
|
|
|
-- only fails if @p@ fails after consuming input. On success result of @p@
|
|
|
|
|
-- is returned inside of 'Just', on failure 'Nothing' is returned.
|