mirror of
https://github.com/mrkkrp/megaparsec.git
synced 2024-12-25 17:22:33 +03:00
cosmetic changes (indentation, etc)
This commit is contained in:
parent
77a54394b5
commit
287a777e6c
@ -40,8 +40,7 @@
|
|||||||
-- imported explicitly along with two modules mentioned above.
|
-- imported explicitly along with two modules mentioned above.
|
||||||
|
|
||||||
module Text.Megaparsec
|
module Text.Megaparsec
|
||||||
(
|
( -- * Parsers
|
||||||
-- * Parsers
|
|
||||||
ParsecT
|
ParsecT
|
||||||
, Parsec
|
, Parsec
|
||||||
, token
|
, token
|
||||||
|
@ -64,13 +64,6 @@ option x p = p <|> return x
|
|||||||
optionMaybe :: Stream s m t => ParsecT s u m a -> ParsecT s u m (Maybe a)
|
optionMaybe :: Stream s m t => ParsecT s u m a -> ParsecT s u m (Maybe a)
|
||||||
optionMaybe p = option Nothing (Just <$> p)
|
optionMaybe p = option Nothing (Just <$> p)
|
||||||
|
|
||||||
-- -- | @optional p@ tries to apply parser @p@. It will parse @p@ or nothing.
|
|
||||||
-- -- It only fails if @p@ fails after consuming input. It discards the result
|
|
||||||
-- -- of @p@.
|
|
||||||
|
|
||||||
-- optional :: Stream s m t => ParsecT s u m a -> ParsecT s u m ()
|
|
||||||
-- optional p = (p *> return ()) <|> return ()
|
|
||||||
|
|
||||||
-- | @between open close p@ parses @open@, followed by @p@ and @close@.
|
-- | @between open close p@ parses @open@, followed by @p@ and @close@.
|
||||||
-- Returns the value returned by @p@.
|
-- Returns the value returned by @p@.
|
||||||
--
|
--
|
||||||
|
@ -43,7 +43,8 @@ import Text.Megaparsec.Pos
|
|||||||
-- The fine distinction between different kinds of parse errors allows the
|
-- The fine distinction between different kinds of parse errors allows the
|
||||||
-- system to generate quite good error messages for the user.
|
-- system to generate quite good error messages for the user.
|
||||||
|
|
||||||
data Message = Unexpected !String
|
data Message
|
||||||
|
= Unexpected !String
|
||||||
| Expected !String
|
| Expected !String
|
||||||
| Message !String
|
| Message !String
|
||||||
deriving Show
|
deriving Show
|
||||||
@ -55,8 +56,7 @@ instance Enum Message where
|
|||||||
toEnum _ = error "Text.Megaparsec.Error: toEnum is undefined for Message"
|
toEnum _ = error "Text.Megaparsec.Error: toEnum is undefined for Message"
|
||||||
|
|
||||||
instance Eq Message where
|
instance Eq Message where
|
||||||
m1 == m2 =
|
m1 == m2 = fromEnum m1 == fromEnum m2 && messageString m1 == messageString m2
|
||||||
fromEnum m1 == fromEnum m2 && messageString m1 == messageString m2
|
|
||||||
|
|
||||||
instance Ord Message where
|
instance Ord Message where
|
||||||
compare m1 m2 =
|
compare m1 m2 =
|
||||||
@ -157,8 +157,8 @@ mergeError e1@(ParseError pos1 ms1) e2@(ParseError pos2 ms2) =
|
|||||||
|
|
||||||
showMessages :: [Message] -> String
|
showMessages :: [Message] -> String
|
||||||
showMessages [] = "unknown parse error"
|
showMessages [] = "unknown parse error"
|
||||||
showMessages ms = intercalate "\n" $
|
showMessages ms =
|
||||||
filter (not . null) [unexpected', expected', messages']
|
intercalate "\n" $ filter (not . null) [unexpected', expected', messages']
|
||||||
where (unexpected, ms') = span ((== 0) . fromEnum) ms
|
where (unexpected, ms') = span ((== 0) . fromEnum) ms
|
||||||
(expected, messages) = span ((== 1) . fromEnum) ms'
|
(expected, messages) = span ((== 1) . fromEnum) ms'
|
||||||
|
|
||||||
|
@ -123,8 +123,7 @@ add perm@(Perm _mf fs) p = Perm Nothing (first : fmap insert fs)
|
|||||||
|
|
||||||
addopt :: Stream s Identity tok => StreamPermParser s st (a -> b) ->
|
addopt :: Stream s Identity tok => StreamPermParser s st (a -> b) ->
|
||||||
a -> Parsec s st a -> StreamPermParser s st b
|
a -> Parsec s st a -> StreamPermParser s st b
|
||||||
addopt perm@(Perm mf fs) x p
|
addopt perm@(Perm mf fs) x p = Perm (fmap ($ x) mf) (first : map insert fs)
|
||||||
= Perm (fmap ($ x) mf) (first:map insert fs)
|
|
||||||
where first = Branch perm p
|
where first = Branch perm p
|
||||||
insert (Branch perm' p') = Branch (addopt (mapPerms flip perm') x p) p'
|
insert (Branch perm' p') = Branch (addopt (mapPerms flip perm') x p) p'
|
||||||
|
|
||||||
|
@ -120,8 +120,7 @@ instance Monad m => Stream TL.Text m Char where
|
|||||||
--
|
--
|
||||||
-- You shouldn't really need to know this. See also: 'Reply'.
|
-- You shouldn't really need to know this. See also: 'Reply'.
|
||||||
|
|
||||||
data Consumed a = Consumed a
|
data Consumed a = Consumed a | Empty !a
|
||||||
| Empty !a
|
|
||||||
|
|
||||||
-- | This data structure represents an aspect of result of parser's
|
-- | This data structure represents an aspect of result of parser's
|
||||||
-- work. The two constructors have the following meaning:
|
-- work. The two constructors have the following meaning:
|
||||||
@ -131,8 +130,7 @@ data Consumed a = Consumed a
|
|||||||
--
|
--
|
||||||
-- You shouldn't really need to know this. See also 'Consumed'.
|
-- You shouldn't really need to know this. See also 'Consumed'.
|
||||||
|
|
||||||
data Reply s u a = Ok a !(State s u) ParseError
|
data Reply s u a = Ok a !(State s u) ParseError | Error ParseError
|
||||||
| Error ParseError
|
|
||||||
|
|
||||||
-- | @ParsecT s u m a@ is a parser with stream type @s@, user state type @u@,
|
-- | @ParsecT s u m a@ is a parser with stream type @s@, user state type @u@,
|
||||||
-- underlying monad @m@ and return type @a@. Parsec is strict in the user
|
-- underlying monad @m@ and return type @a@. Parsec is strict in the user
|
||||||
@ -189,8 +187,8 @@ parserBind m k = ParsecT $ \s cok cerr eok eerr ->
|
|||||||
-- if (k x) consumes, those go straight up
|
-- if (k x) consumes, those go straight up
|
||||||
pcok = cok
|
pcok = cok
|
||||||
pcerr = cerr
|
pcerr = cerr
|
||||||
-- if (k x) doesn't consume input, but is okay, we still
|
-- if (k x) doesn't consume input, but is okay, we still return in
|
||||||
-- return in the consumed continuation
|
-- the consumed continuation
|
||||||
peok x' s' err' = cok x' s' (mergeError err err')
|
peok x' s' err' = cok x' s' (mergeError err err')
|
||||||
-- if (k x) doesn't consume input, but errors, we return the
|
-- if (k x) doesn't consume input, but errors, we return the
|
||||||
-- error in the 'consumed-error' continuation
|
-- error in the 'consumed-error' continuation
|
||||||
@ -282,12 +280,9 @@ parserZero = ParsecT $ \s _ _ _ eerr -> eerr $ unknownError s
|
|||||||
|
|
||||||
parserPlus :: ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
|
parserPlus :: ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
|
||||||
{-# INLINE parserPlus #-}
|
{-# INLINE parserPlus #-}
|
||||||
parserPlus m n
|
parserPlus m n = ParsecT $ \s cok cerr eok eerr ->
|
||||||
= ParsecT $ \s cok cerr eok eerr ->
|
let meerr err =
|
||||||
let
|
let neok y s' err' = eok y s' (mergeError err err')
|
||||||
meerr err =
|
|
||||||
let
|
|
||||||
neok y s' err' = eok y s' (mergeError err err')
|
|
||||||
neerr err' = eerr $ mergeError err err'
|
neerr err' = eerr $ mergeError err err'
|
||||||
in unParser n s cok cerr neok neerr
|
in unParser n s cok cerr neok neerr
|
||||||
in unParser m s cok cerr eok meerr
|
in unParser m s cok cerr eok meerr
|
||||||
@ -295,7 +290,7 @@ parserPlus m n
|
|||||||
instance MonadTrans (ParsecT s u) where
|
instance MonadTrans (ParsecT s u) where
|
||||||
lift amb = ParsecT $ \s _ _ eok _ -> do
|
lift amb = ParsecT $ \s _ _ eok _ -> do
|
||||||
a <- amb
|
a <- amb
|
||||||
eok a s $ unknownError s
|
eok a s (unknownError s)
|
||||||
|
|
||||||
-- Errors
|
-- Errors
|
||||||
|
|
||||||
@ -371,14 +366,13 @@ labels p msgs = ParsecT $ \s cok cerr eok eerr ->
|
|||||||
|
|
||||||
runParserT :: Stream s m t =>
|
runParserT :: Stream s m t =>
|
||||||
ParsecT s u m a -> u -> SourceName -> s -> m (Either ParseError a)
|
ParsecT s u m a -> u -> SourceName -> s -> m (Either ParseError a)
|
||||||
runParserT p u name s
|
runParserT p u name s = do
|
||||||
= do res <- runParsecT p (State s (initialPos name) u)
|
res <- runParsecT p (State s (initialPos name) u)
|
||||||
r <- parserReply res
|
r <- parserReply res
|
||||||
case r of
|
case r of
|
||||||
Ok x _ _ -> return (Right x)
|
Ok x _ _ -> return (Right x)
|
||||||
Error err -> return (Left err)
|
Error err -> return (Left err)
|
||||||
where parserReply res
|
where parserReply res = case res of
|
||||||
= case res of
|
|
||||||
Consumed r -> r
|
Consumed r -> r
|
||||||
Empty r -> r
|
Empty r -> r
|
||||||
|
|
||||||
@ -510,8 +504,7 @@ tokens :: (Stream s m t, Eq t, ShowToken [t]) =>
|
|||||||
-> ParsecT s u m [t]
|
-> ParsecT s u m [t]
|
||||||
{-# INLINE tokens #-}
|
{-# INLINE tokens #-}
|
||||||
tokens _ [] = ParsecT $ \s _ _ eok _ -> eok [] s $ unknownError s
|
tokens _ [] = ParsecT $ \s _ _ eok _ -> eok [] s $ unknownError s
|
||||||
tokens nextposs tts
|
tokens nextposs tts = ParsecT $ \(State input pos u) cok cerr _ eerr ->
|
||||||
= ParsecT $ \(State input pos u) cok cerr _ eerr ->
|
|
||||||
let errExpect x = setErrorMessage (Expected $ showToken tts)
|
let errExpect x = setErrorMessage (Expected $ showToken tts)
|
||||||
(newErrorMessage (Unexpected x) pos)
|
(newErrorMessage (Unexpected x) pos)
|
||||||
|
|
||||||
@ -589,8 +582,7 @@ unexpectError :: String -> SourcePos -> ParseError
|
|||||||
unexpectError msg = newErrorMessage (Unexpected msg)
|
unexpectError msg = newErrorMessage (Unexpected msg)
|
||||||
|
|
||||||
manyAccum :: (a -> [a] -> [a]) -> ParsecT s u m a -> ParsecT s u m [a]
|
manyAccum :: (a -> [a] -> [a]) -> ParsecT s u m a -> ParsecT s u m [a]
|
||||||
manyAccum acc p =
|
manyAccum acc p = ParsecT $ \s cok cerr eok _ ->
|
||||||
ParsecT $ \s cok cerr eok _ ->
|
|
||||||
let walk xs x s' _ =
|
let walk xs x s' _ =
|
||||||
unParser p s'
|
unParser p s'
|
||||||
(seq xs $ walk $ acc x xs) -- consumed-ok
|
(seq xs $ walk $ acc x xs) -- consumed-ok
|
||||||
@ -600,8 +592,7 @@ manyAccum acc p =
|
|||||||
in unParser p s (walk []) cerr manyErr (eok [] s)
|
in unParser p s (walk []) cerr manyErr (eok [] s)
|
||||||
|
|
||||||
manyErr :: forall t . t
|
manyErr :: forall t . t
|
||||||
manyErr =
|
manyErr = error
|
||||||
error
|
|
||||||
"Text.Megaparsec.Prim.many: combinator 'many' is applied to a parser \
|
"Text.Megaparsec.Prim.many: combinator 'many' is applied to a parser \
|
||||||
\that accepts an empty string."
|
\that accepts an empty string."
|
||||||
|
|
||||||
@ -641,8 +632,8 @@ setParserState st = updateParserState (const st)
|
|||||||
-- | @updateParserState f@ applies function @f@ to the parser state.
|
-- | @updateParserState f@ applies function @f@ to the parser state.
|
||||||
|
|
||||||
updateParserState :: (State s u -> State s u) -> ParsecT s u m (State s u)
|
updateParserState :: (State s u -> State s u) -> ParsecT s u m (State s u)
|
||||||
updateParserState f =
|
updateParserState f = ParsecT $ \s _ _ eok _ ->
|
||||||
ParsecT $ \s _ _ eok _ -> let s' = f s in eok s' s' $ unknownError s'
|
let s' = f s in eok s' s' $ unknownError s'
|
||||||
|
|
||||||
-- User state combinators
|
-- User state combinators
|
||||||
|
|
||||||
|
@ -113,9 +113,8 @@ data TokenParser s u m =
|
|||||||
|
|
||||||
-- | The lexeme parser parses a legal operator. Returns the name of the
|
-- | The lexeme parser parses a legal operator. Returns the name of the
|
||||||
-- operator. This parser will fail on any operators that are reserved
|
-- operator. This parser will fail on any operators that are reserved
|
||||||
-- operators. Legal operator (start) characters and reserved operators
|
-- operators. Legal operator (start) characters and reserved operators are
|
||||||
-- are defined in the 'LanguageDef' that is passed to
|
-- defined in the 'LanguageDef' that is passed to 'makeTokenParser'.
|
||||||
-- 'makeTokenParser'.
|
|
||||||
|
|
||||||
, operator :: ParsecT s u m String
|
, operator :: ParsecT s u m String
|
||||||
|
|
||||||
@ -135,15 +134,15 @@ data TokenParser s u m =
|
|||||||
-- | The lexeme parser parses a literal string. Returns the literal
|
-- | The lexeme parser parses a literal string. Returns the literal
|
||||||
-- string value. This parsers deals correctly with escape sequences and
|
-- string value. This parsers deals correctly with escape sequences and
|
||||||
-- gaps. The literal string is parsed according to the grammar rules
|
-- gaps. The literal string is parsed according to the grammar rules
|
||||||
-- defined in the Haskell report (which matches most programming
|
-- defined in the Haskell report (which matches most programming languages
|
||||||
-- languages quite closely).
|
-- quite closely).
|
||||||
|
|
||||||
, stringLiteral :: ParsecT s u m String
|
, stringLiteral :: ParsecT s u m String
|
||||||
|
|
||||||
-- | The lexeme parser parses an integer (a whole number). This parser
|
-- | The lexeme parser parses an integer (a whole number). This parser
|
||||||
-- /does not/ parse sign. Returns the value of the number. The number
|
-- /does not/ parse sign. Returns the value of the number. The number can
|
||||||
-- can be specified in 'decimal', 'hexadecimal' or 'octal'. The number
|
-- be specified in 'decimal', 'hexadecimal' or 'octal'. The number is
|
||||||
-- is parsed according to the grammar rules in the Haskell report.
|
-- parsed according to the grammar rules in the Haskell report.
|
||||||
|
|
||||||
, integer :: ParsecT s u m Integer
|
, integer :: ParsecT s u m Integer
|
||||||
|
|
||||||
@ -176,7 +175,7 @@ data TokenParser s u m =
|
|||||||
|
|
||||||
-- | The lexeme parser parses a floating point value. Returns the value
|
-- | The lexeme parser parses a floating point value. Returns the value
|
||||||
-- of the number. The number is parsed according to the grammar rules
|
-- of the number. The number is parsed according to the grammar rules
|
||||||
-- defined in the Haskell report, sign is /not/ parsed, use 'float\'' to
|
-- defined in the Haskell report, sign is /not/ parsed, use 'float'' to
|
||||||
-- achieve parsing of signed floating point values.
|
-- achieve parsing of signed floating point values.
|
||||||
|
|
||||||
, float :: ParsecT s u m Double
|
, float :: ParsecT s u m Double
|
||||||
@ -186,8 +185,8 @@ data TokenParser s u m =
|
|||||||
, float' :: ParsecT s u m Double
|
, float' :: ParsecT s u m Double
|
||||||
|
|
||||||
-- | The lexeme parser parses either 'integer' or a 'float'.
|
-- | The lexeme parser parses either 'integer' or a 'float'.
|
||||||
-- Returns the value of the number. This parser deals with any overlap
|
-- Returns the value of the number. This parser deals with any overlap in
|
||||||
-- in the grammar rules for integers and floats. The number is parsed
|
-- the grammar rules for integers and floats. The number is parsed
|
||||||
-- according to the grammar rules defined in the Haskell report.
|
-- according to the grammar rules defined in the Haskell report.
|
||||||
|
|
||||||
, number :: ParsecT s u m (Either Integer Double)
|
, number :: ParsecT s u m (Either Integer Double)
|
||||||
@ -203,13 +202,13 @@ data TokenParser s u m =
|
|||||||
|
|
||||||
-- | @lexeme p@ first applies parser @p@ and than the 'whiteSpace'
|
-- | @lexeme p@ first applies parser @p@ and than the 'whiteSpace'
|
||||||
-- parser, returning the value of @p@. Every lexical token (lexeme) is
|
-- parser, returning the value of @p@. Every lexical token (lexeme) is
|
||||||
-- defined using @lexeme@, this way every parse starts at a point
|
-- defined using @lexeme@, this way every parse starts at a point without
|
||||||
-- without white space. Parsers that use @lexeme@ are called /lexeme/
|
-- white space. Parsers that use @lexeme@ are called /lexeme/ parsers in
|
||||||
-- parsers in this document.
|
-- this document.
|
||||||
--
|
--
|
||||||
-- The only point where the 'whiteSpace' parser should be called
|
-- The only point where the 'whiteSpace' parser should be called
|
||||||
-- explicitly is the start of the main parser in order to skip any
|
-- explicitly is the start of the main parser in order to skip any leading
|
||||||
-- leading white space.
|
-- white space.
|
||||||
|
|
||||||
, lexeme :: forall a. ParsecT s u m a -> ParsecT s u m a
|
, lexeme :: forall a. ParsecT s u m a -> ParsecT s u m a
|
||||||
|
|
||||||
|
@ -148,8 +148,8 @@ prop_letterChar :: String -> Property
|
|||||||
prop_letterChar = checkChar letterChar isAlpha (Just "letter")
|
prop_letterChar = checkChar letterChar isAlpha (Just "letter")
|
||||||
|
|
||||||
prop_alphaNumChar :: String -> Property
|
prop_alphaNumChar :: String -> Property
|
||||||
prop_alphaNumChar = checkChar alphaNumChar isAlphaNum
|
prop_alphaNumChar =
|
||||||
(Just "alphanumeric character")
|
checkChar alphaNumChar isAlphaNum (Just "alphanumeric character")
|
||||||
|
|
||||||
prop_printChar :: String -> Property
|
prop_printChar :: String -> Property
|
||||||
prop_printChar = checkChar printChar isPrint (Just "printable character")
|
prop_printChar = checkChar printChar isPrint (Just "printable character")
|
||||||
@ -170,8 +170,8 @@ prop_numberChar :: String -> Property
|
|||||||
prop_numberChar = checkChar numberChar isNumber (Just "numeric character")
|
prop_numberChar = checkChar numberChar isNumber (Just "numeric character")
|
||||||
|
|
||||||
prop_punctuationChar :: String -> Property
|
prop_punctuationChar :: String -> Property
|
||||||
prop_punctuationChar = checkChar punctuationChar isPunctuation
|
prop_punctuationChar =
|
||||||
(Just "punctuation")
|
checkChar punctuationChar isPunctuation (Just "punctuation")
|
||||||
|
|
||||||
prop_symbolChar :: String -> Property
|
prop_symbolChar :: String -> Property
|
||||||
prop_symbolChar = checkChar symbolChar isSymbol (Just "symbol")
|
prop_symbolChar = checkChar symbolChar isSymbol (Just "symbol")
|
||||||
|
@ -82,9 +82,9 @@ prop_wellFormedMessages err = wellFormed $ errorMessages err
|
|||||||
|
|
||||||
prop_parseErrorCopy :: ParseError -> Bool
|
prop_parseErrorCopy :: ParseError -> Bool
|
||||||
prop_parseErrorCopy err =
|
prop_parseErrorCopy err =
|
||||||
foldr addErrorMessage (newErrorUnknown pos) messages == err
|
foldr addErrorMessage (newErrorUnknown pos) msgs == err
|
||||||
where pos = errorPos err
|
where pos = errorPos err
|
||||||
messages = errorMessages err
|
msgs = errorMessages err
|
||||||
|
|
||||||
prop_setErrorPos :: SourcePos -> ParseError -> Bool
|
prop_setErrorPos :: SourcePos -> ParseError -> Bool
|
||||||
prop_setErrorPos pos err =
|
prop_setErrorPos pos err =
|
||||||
|
@ -71,7 +71,8 @@ prop_components pos = pos == copy
|
|||||||
where copy = newPos (sourceName pos) (sourceLine pos) (sourceColumn pos)
|
where copy = newPos (sourceName pos) (sourceLine pos) (sourceColumn pos)
|
||||||
|
|
||||||
prop_showFileName :: SourcePos -> Bool
|
prop_showFileName :: SourcePos -> Bool
|
||||||
prop_showFileName pos = if null name
|
prop_showFileName pos =
|
||||||
|
if null name
|
||||||
then '"'`notElem` shown
|
then '"'`notElem` shown
|
||||||
else ("\"" ++ name ++ "\"") `isInfixOf` shown
|
else ("\"" ++ name ++ "\"") `isInfixOf` shown
|
||||||
where name = sourceName pos
|
where name = sourceName pos
|
||||||
|
Loading…
Reference in New Issue
Block a user