git.iko.soy/typeable/pandoc
on master help sign in
Introduce class HasLastStrPosition, generalize functions

Both `ParserState` and `OrgParserState` keep track of the parser position at which the last string ended. This patch introduces a new class `HasLastStrPosition` and makes the above types instances of that class. This enables the generalization of functions updating the state or checking if one is right after a string.

commit9df589b9c5a4f2dcb19445239dfae41b54625330 authorAlbert Krewinkel <tarleb@moltkeplatz.de> dateWed 14 May 2014, 14:45 (12 years ago) parenta8319d1
3 files changed+30−24
src/Text/Pandoc/Parsing.hs+23−9 src/Text/Pandoc/Readers/Markdown.hs+3−8 src/Text/Pandoc/Readers/Org.hs+4−7
src/Text/Pandoc/Parsing.hs+23−9
@@ -54,7 +54,6 @@ module Text.Pandoc.Parsing ( (>>~),
5454 withRaw,
5555 escaped,
5656 characterReference,
57− updateLastStrPos,
5857 anyOrderedListMarker,
5958 orderedListMarker,
6059 charRef,
@@ -66,11 +65,14 @@ module Text.Pandoc.Parsing ( (>>~),
6665 testStringWith,
6766 guardEnabled,
6867 guardDisabled,
68+ updateLastStrPos,
69+ notAfterString,
6970 ParserState (..),
7071 HasReaderOptions (..),
7172 HasHeaderMap (..),
7273 HasIdentifierList (..),
7374 HasMacros (..),
75+ HasLastStrPosition (..),
7476 defaultParserState,
7577 HeaderType (..),
7678 ParserContext (..),
@@ -904,6 +906,14 @@ instance HasMacros ParserState where
904906 extractMacros = stateMacros
905907 updateMacros f st = st{ stateMacros = f $ stateMacros st }
906908
909+class HasLastStrPosition st where
910+ setLastStrPos :: SourcePos -> st -> st
911+ getLastStrPos :: st -> Maybe SourcePos
912+
913+instance HasLastStrPosition ParserState where
914+ setLastStrPos pos st = st{ stateLastStrPos = Just pos }
915+ getLastStrPos st = stateLastStrPos st
916+
907917defaultParserState :: ParserState
908918defaultParserState =
909919 ParserState { stateOptions = def,
@@ -938,6 +948,17 @@ guardEnabled ext = getOption readerExtensions >>= guard . Set.member ext
938948guardDisabled :: HasReaderOptions st => Extension -> Parser s st ()
939949guardDisabled ext = getOption readerExtensions >>= guard . not . Set.member ext
940950
951+-- | Update the position on which the last string ended.
952+updateLastStrPos :: HasLastStrPosition st => Parser s st ()
953+updateLastStrPos = getPosition >>= updateState . setLastStrPos
954+
955+-- | Whether we are right after the end of a string.
956+notAfterString :: HasLastStrPosition st => Parser s st Bool
957+notAfterString = do
958+ pos <- getPosition
959+ st <- getState
960+ return $ getLastStrPos st /= Just pos
961+
941962data HeaderType
942963 = SingleHeader Char -- ^ Single line of characters underneath
943964 | DoubleHeader Char -- ^ Lines of characters above and below
@@ -1049,17 +1070,11 @@ charOrRef cs =
10491070 guard (c `elem` cs)
10501071 return c)
10511072
1052−updateLastStrPos :: Parser [Char] ParserState ()
1053−updateLastStrPos = getPosition >>= \p ->
1054− updateState $ \s -> s{ stateLastStrPos = Just p }
1055−
10561073singleQuoteStart :: Parser [Char] ParserState ()
10571074singleQuoteStart = do
10581075 failIfInQuoteContext InSingleQuote
1059− pos <- getPosition
1060− st <- getState
10611076 -- single quote start can't be right after str
1062− guard $ stateLastStrPos st /= Just pos
1077+ guard =<< notAfterString
10631078 () <$ charOrRef "'\8216\145"
10641079
10651080singleQuoteEnd :: Parser [Char] st ()
@@ -1156,4 +1171,3 @@ applyMacros' target = do
11561171 then do macros <- extractMacros `fmap` getState
11571172 return $ applyMacros macros target
11581173 else return target
1159−
src/Text/Pandoc/Readers/Markdown.hs+3−8
@@ -1474,9 +1474,7 @@ strongOrEmph = enclosure '*' <|> (checkIntraword >> enclosure '_')
14741474 where checkIntraword = do
14751475 exts <- getOption readerExtensions
14761476 when (Ext_intraword_underscores `Set.member` exts) $ do
1477− pos <- getPosition
1478− lastStrPos <- stateLastStrPos <$> getState
1479− guard $ lastStrPos /= Just pos
1477+ guard =<< notAfterString
14801478
14811479-- | Parses a list of inlines between start and end delimiters.
14821480inlinesBetween :: (Show b)
@@ -1518,8 +1516,7 @@ nonEndline = satisfy (/='\n')
15181516str :: MarkdownParser (F Inlines)
15191517str = do
15201518 result <- many1 alphaNum
1521− pos <- getPosition
1522− updateState $ \s -> s{ stateLastStrPos = Just pos }
1519+ updateLastStrPos
15231520 let spacesToNbr = map (\c -> if c == ' ' then '\160' else c)
15241521 isSmart <- getOption readerSmart
15251522 if isSmart
@@ -1821,9 +1818,7 @@ citeKey :: MarkdownParser (Bool, String)
18211818citeKey = try $ do
18221819 -- make sure we're not right after an alphanumeric,
18231820 -- since foo@bar.baz is probably an email address
1824− lastStrPos <- stateLastStrPos <$> getState
1825− pos <- getPosition
1826− guard $ lastStrPos /= Just pos
1821+ guard =<< notAfterString
18271822 suppress_author <- option False (char '-' >> return True)
18281823 char '@'
18291824 first <- letter <|> char '_'
src/Text/Pandoc/Readers/Org.hs+4−7
@@ -105,6 +105,10 @@ instance HasMeta OrgParserState where
105105 deleteMeta field st =
106106 st{ orgStateMeta = deleteMeta field $ orgStateMeta st }
107107
108+instance HasLastStrPosition OrgParserState where
109+ getLastStrPos = orgStateLastStrPos
110+ setLastStrPos pos st = st{ orgStateLastStrPos = Just pos }
111+
108112instance Default OrgParserState where
109113 def = defaultOrgParserState
110114
@@ -1274,13 +1278,6 @@ afterEmphasisPreChar = do
12741278 lastPrePos <- orgStateLastPreCharPos <$> getState
12751279 return . fromMaybe True $ (== pos) <$> lastPrePos
12761280
1277−-- | Whether we are right after the end of a string
1278−notAfterString :: OrgParser Bool
1279−notAfterString = do
1280− pos <- getPosition
1281− lastStrPos <- orgStateLastStrPos <$> getState
1282− return $ lastStrPos /= Just pos
1283−
12841281-- | Whether the parser is right after a forbidden border char
12851282notAfterForbiddenBorderChar :: OrgParser Bool
12861283notAfterForbiddenBorderChar = do