2015-07-28 16:32:19 +03:00
|
|
|
|
-- |
|
2015-08-01 19:24:45 +03:00
|
|
|
|
-- Module : Text.Megaparsec
|
2016-01-09 15:56:33 +03:00
|
|
|
|
-- Copyright : © 2015–2016 Megaparsec contributors
|
2015-07-30 19:20:37 +03:00
|
|
|
|
-- © 2007 Paolo Martini
|
|
|
|
|
-- © 1999–2001 Daan Leijen
|
2015-10-30 14:26:45 +03:00
|
|
|
|
-- License : FreeBSD
|
2015-07-28 16:32:19 +03:00
|
|
|
|
--
|
|
|
|
|
-- 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
|
2015-09-14 19:44:34 +03:00
|
|
|
|
-- 'token' 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
|
2015-10-22 12:28:15 +03:00
|
|
|
|
-- input stream. It just defines useful type-synonym @Parser@.
|
2015-08-03 21:15:16 +03:00
|
|
|
|
--
|
|
|
|
|
-- Megaparsec is capable of a lot. Apart from this standard functionality
|
|
|
|
|
-- you can parse permutation phrases with "Text.Megaparsec.Perm" and even
|
2015-08-30 13:00:07 +03:00
|
|
|
|
-- entire languages with "Text.Megaparsec.Lexer". These modules should be
|
|
|
|
|
-- imported explicitly along with the two modules mentioned above.
|
2014-03-24 05:07:39 +04:00
|
|
|
|
|
2015-08-01 19:24:45 +03:00
|
|
|
|
module Text.Megaparsec
|
2015-08-20 11:05:41 +03:00
|
|
|
|
( -- * Running parser
|
|
|
|
|
Parsec
|
|
|
|
|
, ParsecT
|
2015-08-12 20:51:06 +03:00
|
|
|
|
, runParser
|
2015-10-25 22:24:48 +03:00
|
|
|
|
, runParser'
|
2015-08-17 18:58:59 +03:00
|
|
|
|
, runParserT
|
2015-10-25 22:24:48 +03:00
|
|
|
|
, runParserT'
|
2015-08-12 20:51:06 +03:00
|
|
|
|
, parse
|
2015-09-24 14:13:06 +03:00
|
|
|
|
, parseMaybe
|
2015-08-12 20:51:06 +03:00
|
|
|
|
, parseTest
|
2015-10-22 12:28:15 +03:00
|
|
|
|
, parseFromFile
|
2015-08-12 20:51:06 +03:00
|
|
|
|
-- * Combinators
|
|
|
|
|
, (A.<|>)
|
|
|
|
|
-- $assocbo
|
|
|
|
|
, A.many
|
|
|
|
|
-- $many
|
|
|
|
|
, A.some
|
|
|
|
|
-- $some
|
|
|
|
|
, A.optional
|
|
|
|
|
-- $optional
|
2015-08-17 18:58:59 +03:00
|
|
|
|
, unexpected
|
2015-10-26 10:52:21 +03:00
|
|
|
|
, failure
|
2015-08-12 20:51:06 +03:00
|
|
|
|
, (<?>)
|
|
|
|
|
, label
|
2015-08-19 22:08:20 +03:00
|
|
|
|
, hidden
|
2015-08-12 20:51:06 +03:00
|
|
|
|
, try
|
2015-08-12 21:40:32 +03:00
|
|
|
|
, lookAhead
|
2015-08-17 18:58:59 +03:00
|
|
|
|
, notFollowedBy
|
2016-02-09 10:55:53 +03:00
|
|
|
|
, withRecovery
|
2015-08-17 18:58:59 +03:00
|
|
|
|
, eof
|
|
|
|
|
, token
|
|
|
|
|
, tokens
|
2015-08-12 21:40:32 +03:00
|
|
|
|
, between
|
2015-08-12 20:51:06 +03:00
|
|
|
|
, choice
|
|
|
|
|
, count
|
2015-08-12 21:40:32 +03:00
|
|
|
|
, count'
|
|
|
|
|
, endBy
|
|
|
|
|
, endBy1
|
|
|
|
|
, manyTill
|
2015-08-23 19:58:42 +03:00
|
|
|
|
, someTill
|
2015-08-12 20:51:06 +03:00
|
|
|
|
, option
|
|
|
|
|
, sepBy
|
|
|
|
|
, sepBy1
|
2015-10-20 13:37:24 +03:00
|
|
|
|
, sepEndBy
|
|
|
|
|
, sepEndBy1
|
2015-08-12 21:40:32 +03:00
|
|
|
|
, skipMany
|
|
|
|
|
, skipSome
|
2015-08-12 20:51:06 +03:00
|
|
|
|
-- * Character parsing
|
|
|
|
|
, newline
|
|
|
|
|
, crlf
|
|
|
|
|
, eol
|
|
|
|
|
, tab
|
|
|
|
|
, space
|
|
|
|
|
, controlChar
|
|
|
|
|
, spaceChar
|
|
|
|
|
, upperChar
|
|
|
|
|
, lowerChar
|
|
|
|
|
, letterChar
|
|
|
|
|
, alphaNumChar
|
|
|
|
|
, printChar
|
|
|
|
|
, digitChar
|
|
|
|
|
, octDigitChar
|
|
|
|
|
, hexDigitChar
|
|
|
|
|
, markChar
|
|
|
|
|
, numberChar
|
|
|
|
|
, punctuationChar
|
|
|
|
|
, symbolChar
|
|
|
|
|
, separatorChar
|
|
|
|
|
, asciiChar
|
|
|
|
|
, latin1Char
|
|
|
|
|
, charCategory
|
|
|
|
|
, char
|
2015-09-06 12:11:18 +03:00
|
|
|
|
, char'
|
2015-08-12 20:51:06 +03:00
|
|
|
|
, anyChar
|
|
|
|
|
, oneOf
|
2015-09-06 12:11:18 +03:00
|
|
|
|
, oneOf'
|
2015-08-12 20:51:06 +03:00
|
|
|
|
, noneOf
|
2015-09-06 12:11:18 +03:00
|
|
|
|
, noneOf'
|
2015-08-12 20:51:06 +03:00
|
|
|
|
, satisfy
|
|
|
|
|
, string
|
2015-09-04 15:12:59 +03:00
|
|
|
|
, string'
|
2015-08-12 20:51:06 +03:00
|
|
|
|
-- * Error messages
|
|
|
|
|
, Message (..)
|
|
|
|
|
, messageString
|
|
|
|
|
, badMessage
|
|
|
|
|
, ParseError
|
|
|
|
|
, errorPos
|
|
|
|
|
, errorMessages
|
|
|
|
|
, errorIsUnknown
|
2015-09-22 20:19:56 +03:00
|
|
|
|
-- * Textual source position
|
2015-08-12 20:51:06 +03:00
|
|
|
|
, SourcePos
|
|
|
|
|
, sourceName
|
|
|
|
|
, sourceLine
|
|
|
|
|
, sourceColumn
|
|
|
|
|
-- * Low-level operations
|
|
|
|
|
, Stream (..)
|
2015-10-22 12:28:15 +03:00
|
|
|
|
, StorableStream (..)
|
2015-08-12 20:51:06 +03:00
|
|
|
|
, State (..)
|
2015-08-20 11:05:41 +03:00
|
|
|
|
, getInput
|
|
|
|
|
, setInput
|
2015-09-23 13:47:17 +03:00
|
|
|
|
, getPosition
|
|
|
|
|
, setPosition
|
|
|
|
|
, getTabWidth
|
|
|
|
|
, setTabWidth
|
2015-08-12 20:51:06 +03:00
|
|
|
|
, getParserState
|
|
|
|
|
, setParserState
|
2015-08-20 11:05:41 +03:00
|
|
|
|
, updateParserState )
|
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.
|