Reflect the distinction between eval and run from State monad into the Parser module

This commit is contained in:
Tissevert 2019-09-24 18:32:23 +02:00
parent 51db57ec67
commit 66d315b7fe
3 changed files with 11 additions and 8 deletions

View File

@ -21,7 +21,7 @@ import PDF.Object (
) )
import qualified PDF.Output as Output (render, line) import qualified PDF.Output as Output (render, line)
import PDF.Output (Output(..)) import PDF.Output (Output(..))
import PDF.Parser (Parser, runParser, string, takeAll) import PDF.Parser (Parser, evalParser, string, takeAll)
import Text.Printf (printf) import Text.Printf (printf)
data Document = Document { data Document = Document {
@ -83,7 +83,7 @@ findNextSection offset input =
readStructures :: Int -> ByteString -> Either String [InputStructure] readStructures :: Int -> ByteString -> Either String [InputStructure]
readStructures startXref input = readStructures startXref input =
runParser structure () (BS.drop startXref input) >>= stopOrFollow evalParser structure () (BS.drop startXref input) >>= stopOrFollow
where where
stopOrFollow s@(Structure {trailer}) = stopOrFollow s@(Structure {trailer}) =
case Map.lookup (Name "Prev") trailer of case Map.lookup (Name "Prev") trailer of
@ -96,7 +96,7 @@ readStructures startXref input =
parseDocument :: ByteString -> Either String Document parseDocument :: ByteString -> Either String Document
parseDocument input = do parseDocument input = do
(pdfVersion, eolStyle) <- runParser ((,) <$> version <*> EOL.parser) () input (pdfVersion, eolStyle) <- evalParser ((,) <$> version <*> EOL.parser) () input
startXref <- readStartXref eolStyle input startXref <- readStartXref eolStyle input
structuresRead <- readStructures startXref input structuresRead <- readStructures startXref input
let updates = populate input <$> structuresRead let updates = populate input <$> structuresRead

View File

@ -18,7 +18,7 @@ import PDF.Object (
, blank, dictionary, directObject, integer, line , blank, dictionary, directObject, integer, line
) )
import PDF.Output (ObjectId(..), Offset(..)) import PDF.Output (ObjectId(..), Offset(..))
import PDF.Parser (Parser, (<?>), block, char, on, option, runParser, takeAll) import PDF.Parser (Parser, (<?>), block, char, evalParser, on, option, takeAll)
data UserState = UserState { data UserState = UserState {
input :: ByteString input :: ByteString
@ -109,7 +109,7 @@ occurrence =
populate :: ByteString -> InputStructure -> Content populate :: ByteString -> InputStructure -> Content
populate input structure = populate input structure =
let bodyInput = BS.drop (startOffset structure) input in let bodyInput = BS.drop (startOffset structure) input in
case runParser recurseOnOccurrences initialState bodyInput of case evalParser recurseOnOccurrences initialState bodyInput of
Left _ -> Content {occurrences = [], objects = Map.empty, docStructure} Left _ -> Content {occurrences = [], objects = Map.empty, docStructure}
Right finalState -> Right finalState ->
let Flow {occurrencesStack, tmpObjects} = flow finalState in let Flow {occurrencesStack, tmpObjects} = flow finalState in

View File

@ -13,6 +13,7 @@ module PDF.Parser (
, oneOf , oneOf
, option , option
, runParser , runParser
, evalParser
, sepBy , sepBy
, string , string
, takeAll , takeAll
@ -80,9 +81,11 @@ oneOf charSet = lift $ Atto.satisfy (`elem` charSet)
option :: a -> Parser s a -> Parser s a option :: a -> Parser s a -> Parser s a
option defaultValue p = p <|> pure defaultValue option defaultValue p = p <|> pure defaultValue
runParser :: Parser s a -> s -> ByteString -> Either String a runParser :: Parser s a -> s -> ByteString -> Either String (a, s)
runParser parser initState = runParser parser initState = Atto.parseOnly (runStateT parser initState)
Atto.parseOnly (evalStateT parser initState)
evalParser :: Parser s a -> s -> ByteString -> Either String a
evalParser parser initState = Atto.parseOnly (evalStateT parser initState)
sepBy :: Parser s a -> Parser s b -> Parser s [a] sepBy :: Parser s a -> Parser s b -> Parser s [a]
sepBy parser separator = sepBy parser separator =