diff options
| author | John MacFarlane <jgm@berkeley.edu> | 2022-10-16 19:16:12 -0700 |
|---|---|---|
| committer | John MacFarlane <jgm@berkeley.edu> | 2022-10-16 20:01:01 -0700 |
| commit | c64ac35ae23dfbc3e6d6ab5a034e03c7e9c2d7db (patch) | |
| tree | 7c4fd1a2c640facf239d5a8ff4ce65f0eae38236 /src/Text/Pandoc/Parsing | |
| parent | 276335bcea515061250b894bbc78da222fb3fdf6 (diff) | |
T.P.Parsing: Remove gratuitious renaming of Parsec types.
We were exporting Parser, ParserT as synonyms of Parsec, ParsecT.
There is no good reason for this and it can cause confusion.
Also, when possible, we replace imports of Text.Parsec with
T.P.Parsing. The idea is to make it easier, at some point,
to switch to megaparsec or another parsing engine if we want to.
T.P.Parsing new exports: Stream(..), updatePosString, SourceName,
Parsec, ParsecT [API change].
Removed exports: Parser, ParserT [API change].
Diffstat (limited to 'src/Text/Pandoc/Parsing')
| -rw-r--r-- | src/Text/Pandoc/Parsing/Capabilities.hs | 20 | ||||
| -rw-r--r-- | src/Text/Pandoc/Parsing/Citations.hs | 6 | ||||
| -rw-r--r-- | src/Text/Pandoc/Parsing/Future.hs (renamed from src/Text/Pandoc/Parsing/Types.hs) | 17 | ||||
| -rw-r--r-- | src/Text/Pandoc/Parsing/General.hs | 142 | ||||
| -rw-r--r-- | src/Text/Pandoc/Parsing/GridTable.hs | 31 | ||||
| -rw-r--r-- | src/Text/Pandoc/Parsing/Lists.hs | 36 | ||||
| -rw-r--r-- | src/Text/Pandoc/Parsing/Math.hs | 15 | ||||
| -rw-r--r-- | src/Text/Pandoc/Parsing/Smart.hs | 36 | ||||
| -rw-r--r-- | src/Text/Pandoc/Parsing/State.hs | 2 |
9 files changed, 147 insertions, 158 deletions
diff --git a/src/Text/Pandoc/Parsing/Capabilities.hs b/src/Text/Pandoc/Parsing/Capabilities.hs index 0b50576de..4a2b9d2ca 100644 --- a/src/Text/Pandoc/Parsing/Capabilities.hs +++ b/src/Text/Pandoc/Parsing/Capabilities.hs @@ -43,7 +43,8 @@ where import Control.Monad (guard, when) import Data.Text (Text) -import Text.Parsec (ParsecT, SourcePos, Stream, getPosition, getState, updateState) +import Text.Parsec (SourcePos, Stream, ParsecT, + getPosition, getState, updateState) import Text.Pandoc.Class.PandocMonad (PandocMonad, report) import Text.Pandoc.Logging (LogMessage) import Text.Pandoc.Options @@ -51,7 +52,6 @@ import Text.Pandoc.Options , ReaderOptions(readerExtensions) , extensionEnabled ) -import Text.Pandoc.Parsing.Types import Text.Pandoc.TeX (Macro) import qualified Data.Map as M @@ -59,7 +59,7 @@ import qualified Data.Set as Set class HasReaderOptions st where extractReaderOptions :: st -> ReaderOptions - getOption :: (Stream s m t) => (ReaderOptions -> b) -> ParserT s st m b + getOption :: (Stream s m t) => (ReaderOptions -> b) -> ParsecT s st m b -- default getOption f = f . extractReaderOptions <$> getState @@ -69,7 +69,7 @@ class HasQuoteContext st m where failIfInQuoteContext :: (HasQuoteContext st m, Stream s m t) => QuoteContext - -> ParserT s st m () + -> ParsecT s st m () failIfInQuoteContext context = do context' <- getQuoteContext when (context' == context) $ Prelude.fail "already inside quotes" @@ -97,34 +97,34 @@ class HasIncludeFiles st where -- | Add a log message. logMessage :: (Stream s m a, HasLogMessages st) - => LogMessage -> ParserT s st m () + => LogMessage -> ParsecT s st m () logMessage msg = updateState (addLogMessage msg) -- | Report all the accumulated log messages, according to verbosity level. -reportLogMessages :: (PandocMonad m, HasLogMessages st) => ParserT s st m () +reportLogMessages :: (PandocMonad m, HasLogMessages st) => ParsecT s st m () reportLogMessages = do msgs <- getLogMessages <$> getState mapM_ report msgs -- | Succeed only if the extension is enabled. guardEnabled :: (Stream s m a, HasReaderOptions st) - => Extension -> ParserT s st m () + => Extension -> ParsecT s st m () guardEnabled ext = getOption readerExtensions >>= guard . extensionEnabled ext -- | Succeed only if the extension is disabled. guardDisabled :: (Stream s m a, HasReaderOptions st) - => Extension -> ParserT s st m () + => Extension -> ParsecT s st m () guardDisabled ext = getOption readerExtensions >>= guard . not . extensionEnabled ext -- | Update the position on which the last string ended. updateLastStrPos :: (Stream s m a, HasLastStrPosition st) - => ParserT s st m () + => ParsecT s st m () updateLastStrPos = getPosition >>= updateState . setLastStrPos . Just -- | Whether we are right after the end of a string. -notAfterString :: (Stream s m a, HasLastStrPosition st) => ParserT s st m Bool +notAfterString :: (Stream s m a, HasLastStrPosition st) => ParsecT s st m Bool notAfterString = do pos <- getPosition st <- getState diff --git a/src/Text/Pandoc/Parsing/Citations.hs b/src/Text/Pandoc/Parsing/Citations.hs index 923cd7dd0..a086951aa 100644 --- a/src/Text/Pandoc/Parsing/Citations.hs +++ b/src/Text/Pandoc/Parsing/Citations.hs @@ -21,6 +21,7 @@ import Text.Pandoc.Sources import Text.Parsec ( (<|>) , Stream(..) + , ParsecT , lookAhead , many , option @@ -28,13 +29,12 @@ import Text.Parsec ) import Text.Pandoc.Parsing.Capabilities (HasLastStrPosition, notAfterString) import Text.Pandoc.Parsing.General -import Text.Pandoc.Parsing.Types (ParserT) import qualified Data.Text as T citeKey :: (Stream s m Char, UpdateSourcePos s Char, HasLastStrPosition st) => Bool -- ^ If True, allow expanded @{..} syntax. - -> ParserT s st m (Bool, Text) + -> ParsecT s st m (Bool, Text) citeKey allowBraced = try $ do guard =<< notAfterString suppress_author <- option False (True <$ char '-') @@ -46,7 +46,7 @@ citeKey allowBraced = try $ do return (suppress_author, key) simpleCiteIdentifier :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Text + => ParsecT s st m Text simpleCiteIdentifier = do firstChar <- alphaNum <|> char '_' <|> char '*' -- @* for wildcard in nocite let regchar = satisfy (\c -> isAlphaNum c || c == '_') diff --git a/src/Text/Pandoc/Parsing/Types.hs b/src/Text/Pandoc/Parsing/Future.hs index f3745270b..041ee0a78 100644 --- a/src/Text/Pandoc/Parsing/Types.hs +++ b/src/Text/Pandoc/Parsing/Future.hs @@ -1,17 +1,15 @@ {-# LANGUAGE GeneralizedNewtypeDeriving #-} {- | - Module : Text.Pandoc.Parsing + Module : Text.Pandoc.Parsing.Future Copyright : Copyright (C) 2006-2022 John MacFarlane License : GPL-2.0-or-later Maintainer : John MacFarlane <jgm@berkeley.edu> -Types and type-related functions for parsers. +Future type for parsing. -} -module Text.Pandoc.Parsing.Types - ( Parser - , ParserT - , Future (..) +module Text.Pandoc.Parsing.Future + ( Future (..) , runF , askF , asksF @@ -23,13 +21,6 @@ import Prelude hiding (Applicative(..)) import Control.Applicative (Applicative(..)) import Control.Monad.Reader ( asks, runReader, MonadReader(ask), Reader, ReaderT(ReaderT) ) -import Text.Parsec ( Parsec , ParsecT ) - --- | Generic parser type used by many pandoc readers. -type Parser t s = Parsec t s - --- | Generic parser transformer used by many pandoc readers. -type ParserT = ParsecT -- | Reader monad wrapping the parser state. This is used to possibly -- delay evaluation until all relevant information has been parsed and diff --git a/src/Text/Pandoc/Parsing/General.hs b/src/Text/Pandoc/Parsing/General.hs index f5f8136d2..d7c724694 100644 --- a/src/Text/Pandoc/Parsing/General.hs +++ b/src/Text/Pandoc/Parsing/General.hs @@ -128,12 +128,12 @@ import Text.Parsec , updateState ) import Text.Parsec.Pos (initialPos, newPos) +import Text.Parsec (Parsec) import Text.Pandoc.Error ( PandocError(PandocParseError, PandocParsecError) ) import Text.Pandoc.Parsing.Capabilities import Text.Pandoc.Parsing.State -import Text.Pandoc.Parsing.Types ( Parser, ParserT, Future (..)) - +import Text.Pandoc.Parsing.Future (Future (..)) import qualified Data.Set as Set import qualified Data.Text as T import qualified Text.Pandoc.Builder as B @@ -159,7 +159,7 @@ textStr t = string (T.unpack t) $> t -- | Parse any line of text, returning the contents without the -- final newline. -anyLine :: Monad m => ParserT Sources st m Text +anyLine :: Monad m => ParsecT Sources st m Text anyLine = do -- This is much faster than: -- manyTill anyChar newline @@ -182,13 +182,13 @@ anyLine = do return this -- | Parse any line, include the final newline in the output -anyLineNewline :: Monad m => ParserT Sources st m Text +anyLineNewline :: Monad m => ParsecT Sources st m Text anyLineNewline = (<> "\n") <$> anyLine -- | Parse indent by specified number of spaces (or equiv. tabs) indentWith :: (Stream s m Char, UpdateSourcePos s Char) => HasReaderOptions st - => Int -> ParserT s st m Text + => Int -> ParsecT s st m Text indentWith num = do tabStop <- getOption readerTabStop if num < tabStop @@ -198,28 +198,28 @@ indentWith num = do -- | Like @many@, but packs its result. manyChar :: Stream s m t - => ParserT s st m Char - -> ParserT s st m Text + => ParsecT s st m Char + -> ParsecT s st m Text manyChar = fmap T.pack . many -- | Like @many1@, but packs its result. many1Char :: Stream s m t - => ParserT s st m Char - -> ParserT s st m Text + => ParsecT s st m Char + -> ParsecT s st m Text many1Char = fmap T.pack . many1 -- | Like @manyTill@, but packs its result. manyTillChar :: Stream s m t - => ParserT s st m Char - -> ParserT s st m a - -> ParserT s st m Text + => ParsecT s st m Char + -> ParsecT s st m a + -> ParsecT s st m Text manyTillChar p = fmap T.pack . manyTill p -- | Like @manyTill@, but reads at least one item. many1Till :: (Show end, Stream s m t) - => ParserT s st m a - -> ParserT s st m end - -> ParserT s st m [a] + => ParsecT s st m a + -> ParsecT s st m end + -> ParsecT s st m [a] many1Till p end = do notFollowedBy' end first <- p @@ -228,15 +228,15 @@ many1Till p end = do -- | Like @many1Till@, but packs its result many1TillChar :: (Show end, Stream s m t) - => ParserT s st m Char - -> ParserT s st m end - -> ParserT s st m Text + => ParsecT s st m Char + -> ParsecT s st m end + -> ParsecT s st m Text many1TillChar p = fmap T.pack . many1Till p -- | Like @manyTill@, but also returns the result of end parser. -manyUntil :: ParserT s u m a - -> ParserT s u m b - -> ParserT s u m ([a], b) +manyUntil :: ParsecT s u m a + -> ParsecT s u m b + -> ParsecT s u m ([a], b) manyUntil p end = scan where scan = (do e <- end @@ -247,9 +247,9 @@ manyUntil p end = scan return (x:xs, e)) -- | Like @manyUntil@, but also packs its result. -manyUntilChar :: ParserT s u m Char - -> ParserT s u m b - -> ParserT s u m (Text, b) +manyUntilChar :: ParsecT s u m Char + -> ParsecT s u m b + -> ParsecT s u m (Text, b) manyUntilChar p = fmap go . manyUntil p where go (x, y) = (T.pack x, y) @@ -264,7 +264,7 @@ sepBy1' p sep = (:) <$> p <*> many (try $ sep >> p) -- | A more general form of @notFollowedBy@. This one allows any -- type of parser to be specified, and succeeds only if that parser fails. -- It does not consume any input. -notFollowedBy' :: (Show b, Stream s m a) => ParserT s st m b -> ParserT s st m () +notFollowedBy' :: (Show b, Stream s m a) => ParsecT s st m b -> ParsecT s st m () notFollowedBy' p = try $ join $ do a <- try p return (unexpected (show a)) <|> @@ -272,12 +272,12 @@ notFollowedBy' p = try $ join $ do a <- try p -- (This version due to Andrew Pimlott on the Haskell mailing list.) oneOfStrings' :: (Stream s m Char, UpdateSourcePos s Char) - => (Char -> Char -> Bool) -> [Text] -> ParserT s st m Text + => (Char -> Char -> Bool) -> [Text] -> ParsecT s st m Text oneOfStrings' f = fmap T.pack . oneOfStrings'' f . fmap T.unpack -- TODO: This should be re-implemented in a Text-aware way oneOfStrings'' :: (Stream s m Char, UpdateSourcePos s Char) - => (Char -> Char -> Bool) -> [String] -> ParserT s st m String + => (Char -> Char -> Bool) -> [String] -> ParsecT s st m String oneOfStrings'' _ [] = Prelude.fail "no strings" oneOfStrings'' matches strs = try $ do c <- anyChar @@ -293,7 +293,7 @@ oneOfStrings'' matches strs = try $ do -- two strings one of which is a prefix of the other, the longer -- string will be matched if possible. oneOfStrings :: (Stream s m Char, UpdateSourcePos s Char) - => [Text] -> ParserT s st m Text + => [Text] -> ParsecT s st m Text oneOfStrings = oneOfStrings' (==) -- | Parses one of a list of strings (tried in order), case insensitive. @@ -301,7 +301,7 @@ oneOfStrings = oneOfStrings' (==) -- TODO: This will not be accurate with general Unicode (neither -- Text.toLower nor Text.toCaseFold can be implemented with a map) oneOfStringsCI :: (Stream s m Char, UpdateSourcePos s Char) - => [Text] -> ParserT s st m Text + => [Text] -> ParsecT s st m Text oneOfStringsCI = oneOfStrings' ciMatch where ciMatch x y = toLower' x == toLower' y -- this optimizes toLower by checking common ASCII case @@ -313,12 +313,12 @@ oneOfStringsCI = oneOfStrings' ciMatch -- | Parses a space or tab. spaceChar :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Char + => ParsecT s st m Char spaceChar = satisfy $ \c -> c == ' ' || c == '\t' -- | Parses a nonspace, nonnewline character. nonspaceChar :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Char + => ParsecT s st m Char nonspaceChar = satisfy (not . isSpaceChar) isSpaceChar :: Char -> Bool @@ -330,23 +330,23 @@ isSpaceChar _ = False -- | Skips zero or more spaces or tabs. skipSpaces :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m () + => ParsecT s st m () skipSpaces = skipMany spaceChar -- | Skips zero or more spaces or tabs, then reads a newline. blankline :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Char + => ParsecT s st m Char blankline = try $ skipSpaces >> newline -- | Parses one or more blank lines and returns a string of newlines. blanklines :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Text + => ParsecT s st m Text blanklines = T.pack <$> many1 blankline -- | Gobble n spaces; if tabs are encountered, expand them -- and gobble some or all of their spaces, leaving the rest. gobbleSpaces :: (HasReaderOptions st, Monad m) - => Int -> ParserT Sources st m () + => Int -> ParsecT Sources st m () gobbleSpaces 0 = return () gobbleSpaces n | n < 0 = error "gobbleSpaces called with negative number" @@ -354,7 +354,7 @@ gobbleSpaces n char ' ' <|> eatOneSpaceOfTab gobbleSpaces (n - 1) -eatOneSpaceOfTab :: (HasReaderOptions st, Monad m) => ParserT Sources st m Char +eatOneSpaceOfTab :: (HasReaderOptions st, Monad m) => ParsecT Sources st m Char eatOneSpaceOfTab = do lookAhead (char '\t') pos <- getPosition @@ -373,7 +373,7 @@ eatOneSpaceOfTab = do -- | Gobble up to n spaces; if tabs are encountered, expand them -- and gobble some or all of their spaces, leaving the rest. gobbleAtMostSpaces :: (HasReaderOptions st, Monad m) - => Int -> ParserT Sources st m Int + => Int -> ParsecT Sources st m Int gobbleAtMostSpaces 0 = return 0 gobbleAtMostSpaces n | n < 0 = error "gobbleAtMostSpaces called with negative number" @@ -383,20 +383,20 @@ gobbleAtMostSpaces n -- | Parses material enclosed between start and end parsers. enclosed :: (Show end, Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m t -- ^ start parser - -> ParserT s st m end -- ^ end parser - -> ParserT s st m a -- ^ content parser (to be used repeatedly) - -> ParserT s st m [a] + => ParsecT s st m t -- ^ start parser + -> ParsecT s st m end -- ^ end parser + -> ParsecT s st m a -- ^ content parser (to be used repeatedly) + -> ParsecT s st m [a] enclosed start end parser = try $ start >> notFollowedBy space >> many1Till parser end -- | Parse string, case insensitive. stringAnyCase :: (Stream s m Char, UpdateSourcePos s Char) - => Text -> ParserT s st m Text + => Text -> ParsecT s st m Text stringAnyCase = fmap T.pack . stringAnyCase' . T.unpack stringAnyCase' :: (Stream s m Char, UpdateSourcePos s Char) - => String -> ParserT s st m String + => String -> ParsecT s st m String stringAnyCase' [] = string "" stringAnyCase' (x:xs) = do firstChar <- char (toUpper x) <|> char (toLower x) @@ -406,9 +406,9 @@ stringAnyCase' (x:xs) = do -- TODO rewrite by just adding to Sources stream? -- | Parse contents of 'str' using 'parser' and return result. parseFromString :: Monad m - => ParserT Sources st m r + => ParsecT Sources st m r -> Text - -> ParserT Sources st m r + -> ParsecT Sources st m r parseFromString parser str = do oldPos <- getPosition oldInput <- getInput @@ -423,9 +423,9 @@ parseFromString parser str = do -- | Like 'parseFromString' but specialized for 'ParserState'. -- This resets 'stateLastStrPos', which is almost always what we want. parseFromString' :: (Monad m, HasLastStrPosition u) - => ParserT Sources u m a + => ParsecT Sources u m a -> Text - -> ParserT Sources u m a + -> ParsecT Sources u m a parseFromString' parser str = do oldLastStrPos <- getLastStrPos <$> getState updateState $ setLastStrPos Nothing @@ -434,7 +434,7 @@ parseFromString' parser str = do return res -- | Parse raw line block up to and including blank lines. -lineClump :: Monad m => ParserT Sources st m Text +lineClump :: Monad m => ParsecT Sources st m Text lineClump = blanklines <|> (T.unlines <$> many1 (notFollowedBy blankline >> anyLine)) @@ -443,8 +443,8 @@ lineClump = blanklines -- pairs of open and close, which must be different. For example, -- @charsInBalanced '(' ')' anyChar@ will parse "(hello (there))" -- and return "hello (there)". -charsInBalanced :: (Stream s m Char, UpdateSourcePos s Char) => Char -> Char -> ParserT s st m Char - -> ParserT s st m Text +charsInBalanced :: (Stream s m Char, UpdateSourcePos s Char) => Char -> Char -> ParsecT s st m Char + -> ParsecT s st m Text charsInBalanced open close parser = try $ do char open let isDelim c = c == open || c == close @@ -463,7 +463,7 @@ charsInBalanced open close parser = try $ do -- | Parses an email address; returns original and corresponding -- escaped mailto: URI. -emailAddress :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (Text, Text) +emailAddress :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s st m (Text, Text) emailAddress = try $ toResult <$> mailbox <*> (char '@' *> domain) where toResult mbox dom = let full = fromEntities $ T.pack $ mbox ++ '@':dom in (full, escapeURI $ "mailto:" <> full) @@ -486,11 +486,11 @@ emailAddress = try $ toResult <$> mailbox <*> (char '@' *> domain) isEmailPunct c = T.any (== c) "!\"#$%&'*+-/=?^_{|}~;" -uriScheme :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m Text +uriScheme :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s st m Text uriScheme = oneOfStringsCI (Set.toList schemes) -- | Parses a URI. Returns pair of original and URI-escaped version. -uri :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (Text, Text) +uri :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s st m (Text, Text) uri = try $ do scheme <- uriScheme char ':' @@ -538,8 +538,8 @@ uri = try $ do -- and the source column at the beginning). Vertical displacement -- (source row) is ignored. withHorizDisplacement :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m a -- ^ Parser to apply - -> ParserT s st m (a, Int) -- ^ (result, displacement) + => ParsecT s st m a -- ^ Parsec to apply + -> ParsecT s st m (a, Int) -- ^ (result, displacement) withHorizDisplacement parser = do pos1 <- getPosition result <- parser @@ -574,12 +574,12 @@ sourcesDifference (Sources is1) (Sources is2) = go is1 is2 -- | Parses backslash, then applies character parser. escaped :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Char -- ^ Parser for character to escape - -> ParserT s st m Char + => ParsecT s st m Char -- ^ Parsec for character to escape + -> ParsecT s st m Char escaped parser = try $ char '\\' >> parser -- | Parse character entity. -characterReference :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m Char +characterReference :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s st m Char characterReference = try $ do char '&' ent <- many1Till nonspaceChar (char ';') @@ -592,10 +592,10 @@ characterReference = try $ do _ -> Prelude.fail "entity not found" -- | Parses a character reference and returns a Str element. -charRef :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m Inline +charRef :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s st m Inline charRef = Str . T.singleton <$> characterReference -lineBlockLine :: Monad m => ParserT Sources st m Text +lineBlockLine :: Monad m => ParsecT Sources st m Text lineBlockLine = try $ do char '|' char ' ' @@ -605,11 +605,11 @@ lineBlockLine = try $ do continuations <- many (try $ char ' ' >> anyLine) return $ white <> T.unwords (line : continuations) -blankLineBlockLine :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m Char +blankLineBlockLine :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s st m Char blankLineBlockLine = try (char '|' >> blankline) -- | Parses an RST-style line block and returns a list of strings. -lineBlockLines :: Monad m => ParserT Sources st m [Text] +lineBlockLines :: Monad m => ParsecT Sources st m [Text] lineBlockLines = try $ do lines' <- many1 (lineBlockLine <|> (T.singleton <$> blankLineBlockLine)) skipMany blankline @@ -618,7 +618,7 @@ lineBlockLines = try $ do -- | Removes the ParsecT layer from the monad transformer stack readWithM :: (Monad m, ToSources t) - => ParserT Sources st m a -- ^ parser + => ParsecT Sources st m a -- ^ parser -> st -- ^ initial state -> t -- ^ input -> m (Either PandocError a) @@ -630,7 +630,7 @@ readWithM parser state input = -- | Parse a string with a given parser and state readWith :: ToSources t - => Parser Sources st a + => Parsec Sources st a -> st -> t -> Either PandocError a @@ -638,7 +638,7 @@ readWith p t inp = runIdentity $ readWithM p t inp -- | Parse a string with @parser@ (for testing). testStringWith :: Show a - => ParserT Sources ParserState Identity a + => ParsecT Sources ParserState Identity a -> Text -> IO () testStringWith parser str = UTF8.putStrLn $ tshow $ @@ -653,7 +653,7 @@ testStringWith parser str = UTF8.putStrLn $ tshow $ -- (explicit or automatically generated). registerHeader :: (Stream s m a, HasReaderOptions st, HasLogMessages st, HasIdentifierList st) - => Attr -> Inlines -> ParserT s st m Attr + => Attr -> Inlines -> ParsecT s st m Attr registerHeader (ident,classes,kvs) header' = do ids <- extractIdentifierList <$> getState exts <- getOption readerExtensions @@ -677,8 +677,8 @@ registerHeader (ident,classes,kvs) header' = do -- This is used to prevent exponential blowups for things like: -- a**a*a**a*a**a*a**a*a**a*a**a*a** nested :: Stream s m a - => ParserT s ParserState m a - -> ParserT s ParserState m a + => ParsecT s ParserState m a + -> ParsecT s ParserState m a nested p = do nestlevel <- stateMaxNestingLevel <$> getState guard $ nestlevel > 0 @@ -695,7 +695,7 @@ token :: (Stream s m t) token pp pos match = tokenPrim (T.unpack . pp) (\_ t _ -> pos t) match infixr 5 <+?> -(<+?>) :: (Monoid a) => ParserT s st m a -> ParserT s st m a -> ParserT s st m a +(<+?>) :: (Monoid a) => ParsecT s st m a -> ParsecT s st m a -> ParsecT s st m a a <+?> b = a >>= flip fmap (try b <|> return mempty) . mappend extractIdClass :: Attr -> Attr @@ -706,13 +706,13 @@ extractIdClass (ident, cls, kvs) = (ident', cls', kvs') kvs' = filter (\(k,_) -> k /= "id" || k /= "class") kvs insertIncludedFile :: (PandocMonad m, HasIncludeFiles st) - => ParserT a st m b -- ^ parser to apply + => ParsecT a st m b -- ^ parser to apply -> (Text -> a) -- ^ convert Text to stream type -> [FilePath] -- ^ search path (directories) -> FilePath -- ^ path of file to include -> Maybe Int -- ^ start line (negative counts from end) -> Maybe Int -- ^ end line (negative counts from end) - -> ParserT a st m b + -> ParsecT a st m b insertIncludedFile parser toStream dirs f mbstartline mbendline = do oldPos <- getPosition oldInput <- getInput diff --git a/src/Text/Pandoc/Parsing/GridTable.hs b/src/Text/Pandoc/Parsing/GridTable.hs index 88c592477..cec8653f7 100644 --- a/src/Text/Pandoc/Parsing/GridTable.hs +++ b/src/Text/Pandoc/Parsing/GridTable.hs @@ -31,9 +31,8 @@ import Text.Pandoc.Builder (Blocks) import Text.Pandoc.Definition import Text.Pandoc.Parsing.Capabilities import Text.Pandoc.Parsing.General -import Text.Pandoc.Parsing.Types import Text.Pandoc.Sources -import Text.Parsec (Stream (..), optional, sepEndBy1, try) +import Text.Parsec (Stream (..), ParsecT, optional, sepEndBy1, try) import qualified Data.Text as T import qualified Text.GridTable as GT @@ -103,8 +102,8 @@ data TableNormalization -- blank lines, and ending with a footer (dashed line followed by blank -- line). gridTableWith :: (Monad m, Monad mf, HasLastStrPosition st, HasReaderOptions st) - => ParserT Sources st m (mf Blocks) -- ^ Block list parser - -> ParserT Sources st m (mf Blocks) + => ParsecT Sources st m (mf Blocks) -- ^ Block list parser + -> ParsecT Sources st m (mf Blocks) gridTableWith blocks = fmap tableFromComponents <$> gridTableWith' NoNormalization blocks @@ -113,8 +112,8 @@ gridTableWith blocks = fmap tableFromComponents <$> gridTableWith' :: (Monad m, Monad mf, HasReaderOptions st, HasLastStrPosition st) => TableNormalization - -> ParserT Sources st m (mf Blocks) -- ^ Block list parser - -> ParserT Sources st m (mf TableComponents) + -> ParsecT Sources st m (mf Blocks) -- ^ Block list parser + -> ParsecT Sources st m (mf TableComponents) gridTableWith' normalization blocks = do tbl <- GT.gridTable <* optional blanklines let blkTbl = GT.mapCells @@ -192,22 +191,22 @@ fractionalColumnWidths gt charColumns = -- 'lineParser', and 'footerParser'. tableWith :: (Stream s m Char, UpdateSourcePos s Char, HasReaderOptions st, Monad mf) - => ParserT s st m (mf [Blocks], [Alignment], [Int]) -- ^ header parser - -> ([Int] -> ParserT s st m (mf [Blocks])) -- ^ row parser - -> ParserT s st m sep -- ^ line parser - -> ParserT s st m end -- ^ footer parser - -> ParserT s st m (mf Blocks) + => ParsecT s st m (mf [Blocks], [Alignment], [Int]) -- ^ header parser + -> ([Int] -> ParsecT s st m (mf [Blocks])) -- ^ row parser + -> ParsecT s st m sep -- ^ line parser + -> ParsecT s st m end -- ^ footer parser + -> ParsecT s st m (mf Blocks) tableWith hp rp lp fp = fmap tableFromComponents <$> tableWith' NoNormalization hp rp lp fp tableWith' :: (Stream s m Char, UpdateSourcePos s Char, HasReaderOptions st, Monad mf) => TableNormalization - -> ParserT s st m (mf [Blocks], [Alignment], [Int]) -- ^ header parser - -> ([Int] -> ParserT s st m (mf [Blocks])) -- ^ row parser - -> ParserT s st m sep -- ^ line parser - -> ParserT s st m end -- ^ footer parser - -> ParserT s st m (mf TableComponents) + -> ParsecT s st m (mf [Blocks], [Alignment], [Int]) -- ^ header parser + -> ([Int] -> ParsecT s st m (mf [Blocks])) -- ^ row parser + -> ParsecT s st m sep -- ^ line parser + -> ParsecT s st m end -- ^ footer parser + -> ParsecT s st m (mf TableComponents) tableWith' n11n headerParser rowParser lineParser footerParser = try $ do (heads, aligns, indices) <- headerParser lines' <- sequence <$> rowParser indices `sepEndBy1` lineParser diff --git a/src/Text/Pandoc/Parsing/Lists.hs b/src/Text/Pandoc/Parsing/Lists.hs index 6f0c47ce2..bd12c6ac2 100644 --- a/src/Text/Pandoc/Parsing/Lists.hs +++ b/src/Text/Pandoc/Parsing/Lists.hs @@ -37,6 +37,7 @@ import Text.Pandoc.Shared (safeRead) import Text.Pandoc.Sources import Text.Parsec ( (<|>) + , ParsecT , Stream(..) , choice , getState @@ -48,7 +49,6 @@ import Text.Parsec , updateState ) import Text.Pandoc.Parsing.State -import Text.Pandoc.Parsing.Types (ParserT) import qualified Data.Map as M import qualified Data.Text as T @@ -56,7 +56,7 @@ import qualified Data.Text as T -- | Parses a roman numeral (uppercase or lowercase), returns number. romanNumeral :: (Stream s m Char, UpdateSourcePos s Char) => Bool -- ^ Uppercase if true - -> ParserT s st m Int + -> ParsecT s st m Int romanNumeral upperCase = do let rchar uc = char $ if upperCase then uc else toLower uc let one = rchar 'I' @@ -88,19 +88,19 @@ romanNumeral upperCase = do else return total -- | Parses an uppercase roman numeral and returns (UpperRoman, number). -upperRoman :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int) +upperRoman :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s st m (ListNumberStyle, Int) upperRoman = do num <- romanNumeral True return (UpperRoman, num) -- | Parses a lowercase roman numeral and returns (LowerRoman, number). -lowerRoman :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int) +lowerRoman :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s st m (ListNumberStyle, Int) lowerRoman = do num <- romanNumeral False return (LowerRoman, num) -- | Parses a decimal numeral and returns (Decimal, number). -decimal :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int) +decimal :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s st m (ListNumberStyle, Int) decimal = do num <- many1 digit return (Decimal, fromMaybe 1 $ safeRead $ T.pack num) @@ -110,7 +110,7 @@ decimal = do -- example number is incremented in parser state, and the label -- (if present) is added to the label table. exampleNum :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s ParserState m (ListNumberStyle, Int) + => ParsecT s ParserState m (ListNumberStyle, Int) exampleNum = do char '@' lab <- mconcat . map T.pack <$> @@ -128,30 +128,30 @@ exampleNum = do return (Example, num) -- | Parses a '#' returns (DefaultStyle, 1). -defaultNum :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int) +defaultNum :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s st m (ListNumberStyle, Int) defaultNum = do char '#' return (DefaultStyle, 1) -- | Parses a lowercase letter and returns (LowerAlpha, number). -lowerAlpha :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int) +lowerAlpha :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s st m (ListNumberStyle, Int) lowerAlpha = do ch <- satisfy isAsciiLower return (LowerAlpha, ord ch - ord 'a' + 1) -- | Parses an uppercase letter and returns (UpperAlpha, number). -upperAlpha :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int) +upperAlpha :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s st m (ListNumberStyle, Int) upperAlpha = do ch <- satisfy isAsciiUpper return (UpperAlpha, ord ch - ord 'A' + 1) -- | Parses a roman numeral i or I -romanOne :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s st m (ListNumberStyle, Int) +romanOne :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s st m (ListNumberStyle, Int) romanOne = (char 'i' >> return (LowerRoman, 1)) <|> (char 'I' >> return (UpperRoman, 1)) -- | Parses an ordered list marker and returns list attributes. -anyOrderedListMarker :: (Stream s m Char, UpdateSourcePos s Char) => ParserT s ParserState m ListAttributes +anyOrderedListMarker :: (Stream s m Char, UpdateSourcePos s Char) => ParsecT s ParserState m ListAttributes anyOrderedListMarker = choice [delimParser numParser | delimParser <- [inPeriod, inOneParen, inTwoParens], numParser <- [decimal, exampleNum, defaultNum, romanOne, @@ -159,8 +159,8 @@ anyOrderedListMarker = choice -- | Parses a list number (num) followed by a period, returns list attributes. inPeriod :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m (ListNumberStyle, Int) - -> ParserT s st m ListAttributes + => ParsecT s st m (ListNumberStyle, Int) + -> ParsecT s st m ListAttributes inPeriod num = try $ do (style, start) <- num char '.' @@ -171,8 +171,8 @@ inPeriod num = try $ do -- | Parses a list number (num) followed by a paren, returns list attributes. inOneParen :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m (ListNumberStyle, Int) - -> ParserT s st m ListAttributes + => ParsecT s st m (ListNumberStyle, Int) + -> ParsecT s st m ListAttributes inOneParen num = try $ do (style, start) <- num char ')' @@ -180,8 +180,8 @@ inOneParen num = try $ do -- | Parses a list number (num) enclosed in parens, returns list attributes. inTwoParens :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m (ListNumberStyle, Int) - -> ParserT s st m ListAttributes + => ParsecT s st m (ListNumberStyle, Int) + -> ParsecT s st m ListAttributes inTwoParens num = try $ do char '(' (style, start) <- num @@ -193,7 +193,7 @@ inTwoParens num = try $ do orderedListMarker :: (Stream s m Char, UpdateSourcePos s Char) => ListNumberStyle -> ListNumberDelim - -> ParserT s ParserState m Int + -> ParsecT s ParserState m Int orderedListMarker style delim = do let num = defaultNum <|> -- # can continue any kind of list case style of diff --git a/src/Text/Pandoc/Parsing/Math.hs b/src/Text/Pandoc/Parsing/Math.hs index a2cfa1a07..d001dc82a 100644 --- a/src/Text/Pandoc/Parsing/Math.hs +++ b/src/Text/Pandoc/Parsing/Math.hs @@ -17,20 +17,19 @@ where import Control.Monad (mzero, when) import Data.Text (Text) -import Text.Parsec ((<|>), Stream(..), notFollowedBy, skipMany, try) +import Text.Parsec ((<|>), ParsecT, Stream(..), notFollowedBy, skipMany, try) import Text.Pandoc.Options ( Extension(Ext_tex_math_dollars, Ext_tex_math_single_backslash, Ext_tex_math_double_backslash) ) import Text.Pandoc.Parsing.Capabilities (HasReaderOptions, guardEnabled) import Text.Pandoc.Parsing.General -import Text.Pandoc.Parsing.Types (ParserT) import Text.Pandoc.Shared (trimMath) import Text.Pandoc.Sources (UpdateSourcePos, anyChar, char, digit, newline, satisfy, space, string) import qualified Data.Text as T -mathInlineWith :: (Stream s m Char, UpdateSourcePos s Char) => Text -> Text -> ParserT s st m Text +mathInlineWith :: (Stream s m Char, UpdateSourcePos s Char) => Text -> Text -> ParsecT s st m Text mathInlineWith op cl = try $ do textStr op when (op == "$") $ notFollowedBy space @@ -51,10 +50,10 @@ mathInlineWith op cl = try $ do notFollowedBy digit -- to prevent capture of $5 return $ trimMath $ T.concat words' where - inBalancedBraces :: (Stream s m Char, UpdateSourcePos s Char) => Int -> Text -> ParserT s st m Text + inBalancedBraces :: (Stream s m Char, UpdateSourcePos s Char) => Int -> Text -> ParsecT s st m Text inBalancedBraces n = fmap T.pack . inBalancedBraces' n . T.unpack - inBalancedBraces' :: (Stream s m Char, UpdateSourcePos s Char) => Int -> String -> ParserT s st m String + inBalancedBraces' :: (Stream s m Char, UpdateSourcePos s Char) => Int -> String -> ParsecT s st m String inBalancedBraces' 0 "" = do c <- anyChar if c == '{' @@ -71,14 +70,14 @@ mathInlineWith op cl = try $ do '{' -> inBalancedBraces' (numOpen + 1) (c:xs) _ -> inBalancedBraces' numOpen (c:xs) -mathDisplayWith :: (Stream s m Char, UpdateSourcePos s Char) => Text -> Text -> ParserT s st m Text +mathDisplayWith :: (Stream s m Char, UpdateSourcePos s Char) => Text -> Text -> ParsecT s st m Text mathDisplayWith op cl = try $ fmap T.pack $ do textStr op many1Till (satisfy (/= '\n') <|> (newline <* notFollowedBy' blankline)) (try $ textStr cl) mathDisplay :: (HasReaderOptions st, Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Text + => ParsecT s st m Text mathDisplay = (guardEnabled Ext_tex_math_dollars >> mathDisplayWith "$$" "$$") <|> (guardEnabled Ext_tex_math_single_backslash >> @@ -87,7 +86,7 @@ mathDisplay = mathDisplayWith "\\\\[" "\\\\]") mathInline :: (HasReaderOptions st, Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Text + => ParsecT s st m Text mathInline = (guardEnabled Ext_tex_math_dollars >> mathInlineWith "$" "$") <|> (guardEnabled Ext_tex_math_single_backslash >> diff --git a/src/Text/Pandoc/Parsing/Smart.hs b/src/Text/Pandoc/Parsing/Smart.hs index 52ad27119..fdc824e2c 100644 --- a/src/Text/Pandoc/Parsing/Smart.hs +++ b/src/Text/Pandoc/Parsing/Smart.hs @@ -33,10 +33,10 @@ import Text.Pandoc.Options import Text.Pandoc.Sources import Text.Pandoc.Parsing.Capabilities import Text.Pandoc.Parsing.General -import Text.Pandoc.Parsing.Types (ParserT) import Text.Parsec ( (<|>) , Stream(..) + , ParsecT , choice , lookAhead , manyTill @@ -53,8 +53,8 @@ import qualified Text.Pandoc.Builder as B smartPunctuation :: (HasReaderOptions st, HasLastStrPosition st, HasQuoteContext st m, Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Inlines - -> ParserT s st m Inlines + => ParsecT s st m Inlines + -> ParsecT s st m Inlines smartPunctuation inlineParser = do guardEnabled Ext_smart choice [ quoted inlineParser, apostrophe, doubleCloseQuote, dash, ellipses ] @@ -63,16 +63,16 @@ smartPunctuation inlineParser = do -- quoting conventions. quoted :: (HasLastStrPosition st, HasQuoteContext st m, Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Inlines - -> ParserT s st m Inlines + => ParsecT s st m Inlines + -> ParsecT s st m Inlines quoted inlineParser = doubleQuoted inlineParser <|> singleQuoted inlineParser -- | Parses inline text in single quotes, assumes English quoting -- conventions. singleQuoted :: (HasLastStrPosition st, HasQuoteContext st m, Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Inlines - -> ParserT s st m Inlines + => ParsecT s st m Inlines + -> ParsecT s st m Inlines singleQuoted inlineParser = do singleQuoteStart (B.singleQuoted . mconcat <$> @@ -84,8 +84,8 @@ singleQuoted inlineParser = do -- conventions. doubleQuoted :: (HasQuoteContext st m, HasLastStrPosition st, Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Inlines - -> ParserT s st m Inlines + => ParsecT s st m Inlines + -> ParsecT s st m Inlines doubleQuoted inlineParser = do doubleQuoteStart (B.doubleQuoted . mconcat <$> @@ -93,7 +93,7 @@ doubleQuoted inlineParser = do (withQuoteContext InDoubleQuote (manyTill inlineParser doubleQuoteEnd))) <|> pure (B.str "\8220") -charOrRef :: (Stream s m Char, UpdateSourcePos s Char) => [Char] -> ParserT s st m Char +charOrRef :: (Stream s m Char, UpdateSourcePos s Char) => [Char] -> ParsecT s st m Char charOrRef cs = oneOf cs <|> try (do c <- characterReference guard (c `elem` cs) @@ -109,7 +109,7 @@ charOrRef cs = -- Gobbles the quote character on success. singleQuoteStart :: (HasLastStrPosition st, HasQuoteContext st m, Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m () + => ParsecT s st m () singleQuoteStart = do failIfInQuoteContext InSingleQuote -- single quote start can't be right after str @@ -119,7 +119,7 @@ singleQuoteStart = do void $ lookAhead (satisfy (not . isSpaceChar)) singleQuoteEnd :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m () + => ParsecT s st m () singleQuoteEnd = try $ do charOrRef "'\8217\146" notFollowedBy alphaNum @@ -137,7 +137,7 @@ singleQuoteEnd = try $ do doubleQuoteStart :: (HasLastStrPosition st, HasQuoteContext st m, Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m () + => ParsecT s st m () doubleQuoteStart = do failIfInQuoteContext InDoubleQuote guard =<< notAfterString @@ -146,24 +146,24 @@ doubleQuoteStart = do -- | Parses a closing quote character. doubleQuoteEnd :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m () + => ParsecT s st m () doubleQuoteEnd = void (charOrRef "\"\8221\148") -- | Parses an ASCII apostrophe (@'@) or right single quotation mark and -- returns a RIGHT SINGLE QUOtatiON MARK character. apostrophe :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Inlines + => ParsecT s st m Inlines apostrophe = (char '\'' <|> char '\8217') >> return (B.str "\8217") -- | Parses an ASCII quotation mark character and returns a RIGHT DOUBLE -- QUOTATION MARK. doubleCloseQuote :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Inlines + => ParsecT s st m Inlines doubleCloseQuote = B.str "\8221" <$ char '"' -- | Parses three dots as HORIZONTAL ELLIPSIS. ellipses :: (Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Inlines + => ParsecT s st m Inlines ellipses = try (string "..." >> return (B.str "\8230")) -- | Parses two hyphens as EN DASH and three as EM DASH. @@ -172,7 +172,7 @@ ellipses = try (string "..." >> return (B.str "\8230")) -- parsed as EM DASH, and one hyphen is parsed as EN DASH if it is -- followed by a digit. dash :: (HasReaderOptions st, Stream s m Char, UpdateSourcePos s Char) - => ParserT s st m Inlines + => ParsecT s st m Inlines dash = try $ do oldDashes <- extensionEnabled Ext_old_dashes <$> getOption readerExtensions if oldDashes diff --git a/src/Text/Pandoc/Parsing/State.hs b/src/Text/Pandoc/Parsing/State.hs index 9e59b4c7e..1efb90b73 100644 --- a/src/Text/Pandoc/Parsing/State.hs +++ b/src/Text/Pandoc/Parsing/State.hs @@ -32,7 +32,7 @@ import Text.Pandoc.Definition (Attr, Meta, Target, nullMeta) import Text.Pandoc.Logging (LogMessage) import Text.Pandoc.Options (ReaderOptions) import Text.Pandoc.Parsing.Capabilities -import Text.Pandoc.Parsing.Types +import Text.Pandoc.Parsing.Future import Text.Pandoc.TeX (Macro) import qualified Data.Map as M |
