Minor but strict improvement : remove the general implementation of <?> for Alternative

This commit is contained in:
Tissevert 2020-02-12 17:48:26 +01:00
parent 919f640443
commit 1a25307c8c
4 changed files with 17 additions and 24 deletions

View file

@ -19,7 +19,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, evalParser, on, takeAll) import PDF.Parser (Parser, block, char, evalParser, on, takeAll)
data UserState = UserState { data UserState = UserState {
input :: ByteString input :: ByteString
@ -105,7 +105,7 @@ indirectObjCoordinates = do
occurrence :: SParser Occurrence occurrence :: SParser Occurrence
occurrence = occurrence =
Comment <$> comment <|> Indirect <$> indirectObjCoordinates <?> "comment or object" Comment <$> comment <|> Indirect <$> indirectObjCoordinates
populate :: ByteString -> InputStructure -> Content populate :: ByteString -> InputStructure -> Content
populate input structure = populate input structure =

View file

@ -15,7 +15,7 @@ import Data.ByteString (ByteString)
import PDF.Content.Operator (Instruction, operator) import PDF.Content.Operator (Instruction, operator)
import PDF.Object (blank, directObject) import PDF.Object (blank, directObject)
import PDF.Output (Output(..), line) import PDF.Output (Output(..), line)
import PDF.Parser (MonadParser, (<?>), evalParser, string) import PDF.Parser (MonadParser, evalParser, string)
data GraphicContextUnit = data GraphicContextUnit =
GraphicInstruction Instruction GraphicInstruction Instruction
@ -29,7 +29,7 @@ data ContentUnit =
newtype Content = Content [ContentUnit] deriving Show newtype Content = Content [ContentUnit] deriving Show
content :: MonadParser m => m Content content :: MonadParser m => m Content
content = Content <$> contentUnit `sepBy` blank <?> "content" content = Content <$> contentUnit `sepBy` blank
contentUnit :: MonadParser m => m ContentUnit contentUnit :: MonadParser m => m ContentUnit
contentUnit = contentUnit =

View file

@ -50,11 +50,11 @@ import PDF.Output (
, byteString, getObjectId, getOffset, getOffsets, join, newLine , byteString, getObjectId, getOffset, getOffsets, join, newLine
, saveOffset , saveOffset
) )
import PDF.Parser (MonadParser(..), Parser, (<?>), octDigit, oneOf) import PDF.Parser (MonadParser(..), Parser, octDigit, oneOf)
import Text.Printf (printf) import Text.Printf (printf)
line :: MonadParser m => String -> m () line :: MonadParser m => String -> m ()
line l = (string (Char8.pack l) *> blank *> return ()) <?> printf "line «%s»" l line l = (string (Char8.pack l) *> blank *> return ())
magicNumber :: ByteString magicNumber :: ByteString
magicNumber = "%PDF-" magicNumber = "%PDF-"
@ -75,7 +75,7 @@ regular :: Char -> Bool
regular = not . (`elem` (EOL.charset ++ whiteSpaceCharset ++ delimiterCharset)) regular = not . (`elem` (EOL.charset ++ whiteSpaceCharset ++ delimiterCharset))
integer :: (Read a, Num a, MonadParser m) => m a integer :: (Read a, Num a, MonadParser m) => m a
integer = read . Char8.unpack <$> decNumber <* blank <?> "decimal integer" integer = read . Char8.unpack <$> decNumber <* blank
------------------------------------- -------------------------------------
-- OBJECTS -- OBJECTS
@ -88,7 +88,7 @@ type IndexedObjects = Map ObjectId Object
-- --
boolean :: MonadParser m => m Bool boolean :: MonadParser m => m Bool
boolean = boolean =
(string "true" *> return True) <|> (string "false" *> return False) <?> "boolean" (string "true" *> return True) <|> (string "false" *> return False)
-- --
-- Number -- Number
@ -104,7 +104,6 @@ instance Output Number where
number :: MonadParser m => m Number number :: MonadParser m => m Number
number = Number . read . Char8.unpack <$> number = Number . read . Char8.unpack <$>
(mappend <$> sign <*> (integerPart <|> Char8.cons '0' <$> floatPart)) (mappend <$> sign <*> (integerPart <|> Char8.cons '0' <$> floatPart))
<?> "number"
where where
sign = string "-" <|> option "" (char '+' >> return "") sign = string "-" <|> option "" (char '+' >> return "")
integerPart = mappend <$> decNumber <*> option "" floatPart integerPart = mappend <$> decNumber <*> option "" floatPart
@ -123,7 +122,6 @@ stringObject :: MonadParser m => m StringObject
stringObject = stringObject =
Literal <$> (char '(' *> (BS.concat <$> literalString) <* char ')') Literal <$> (char '(' *> (BS.concat <$> literalString) <* char ')')
<|> Hexadecimal <$> (char '<' *> hexNumber <* char '>') <|> Hexadecimal <$> (char '<' *> hexNumber <* char '>')
<?> "string object (literal or hexadecimal)"
where where
literalString = many literalStringBlock literalString = many literalStringBlock
literalStringBlock = takeAll1 normalChar <|> matchingParenthesis <|> escapedChar literalStringBlock = takeAll1 normalChar <|> matchingParenthesis <|> escapedChar
@ -147,14 +145,14 @@ instance Output Name where
output (Name n) = Output.string ('/':n) output (Name n) = Output.string ('/':n)
name :: MonadParser m => m Name name :: MonadParser m => m Name
name = Name . Char8.unpack <$> (char '/' *> takeAll regular) <?> "name" name = Name . Char8.unpack <$> (char '/' *> takeAll regular)
-- --
-- Array -- Array
-- --
array :: MonadParser m => m [DirectObject] array :: MonadParser m => m [DirectObject]
array = array =
char '[' *> blank *> directObject `sepBy` blank <* blank <* char ']' <?> "array" char '[' *> blank *> directObject `sepBy` blank <* blank <* char ']'
-- --
-- Dictionary -- Dictionary
@ -171,7 +169,7 @@ instance Output Dictionary where
dictionary :: MonadParser m => m Dictionary dictionary :: MonadParser m => m Dictionary
dictionary = dictionary =
string "<<" *> blank *> keyValPairs <* string ">>" <?> "dictionary" string "<<" *> blank *> keyValPairs <* string ">>"
where where
keyVal = (,) <$> name <* blank <*> directObject keyVal = (,) <$> name <* blank <*> directObject
keyValPairs = Map.fromList <$> keyVal `sepBy` blank <* blank keyValPairs = Map.fromList <$> keyVal `sepBy` blank <* blank
@ -180,7 +178,7 @@ dictionary =
-- Null -- Null
-- --
nullObject :: MonadParser m => m () nullObject :: MonadParser m => m ()
nullObject = string "null" *> return () <?> "null object" nullObject = string "null" *> return ()
-- --
-- Reference -- Reference
@ -192,7 +190,7 @@ data IndirectObjCoordinates = IndirectObjCoordinates {
reference :: MonadParser m => m IndirectObjCoordinates reference :: MonadParser m => m IndirectObjCoordinates
reference = IndirectObjCoordinates reference = IndirectObjCoordinates
<$> (fmap ObjectId integer) <*> integer <* char 'R' <?> "reference to an object" <$> (fmap ObjectId integer) <*> integer <* char 'R'
-- --
-- DirectObject -- DirectObject
@ -220,7 +218,7 @@ instance Output DirectObject where
Output.string (printf "%d %d R" (getObjectId objectId) versionNumber) Output.string (printf "%d %d R" (getObjectId objectId) versionNumber)
directObject :: MonadParser m => m DirectObject directObject :: MonadParser m => m DirectObject
directObject = (peek >>= dispatch) <?> "direct object" directObject = peek >>= dispatch
where where
dispatch 't' = Boolean <$> boolean dispatch 't' = Boolean <$> boolean
dispatch 'f' = Boolean <$> boolean dispatch 'f' = Boolean <$> boolean
@ -291,7 +289,7 @@ instance Output XRefEntry where
entry :: Parser u XRefEntry entry :: Parser u XRefEntry
entry = do entry = do
(big, small) <- (,) <$> integer <*> integer (big, small) <- (,) <$> integer <*> integer
(inUse big small <|> free big small <?> "XRef entry") <* blank (inUse big small <|> free big small) <* blank
where where
inUse :: Int -> Int -> Parser u XRefEntry inUse :: Int -> Int -> Parser u XRefEntry
inUse big generation = inUse big generation =
@ -315,7 +313,7 @@ instance Output XRefSubSection where
xRefSubSection :: Parser u XRefSubSection xRefSubSection :: Parser u XRefSubSection
xRefSubSection = do xRefSubSection = do
(firstId, entriesNumber) <- (,) <$> integer <*> integer <?> "XRef subsection" (firstId, entriesNumber) <- (,) <$> integer <*> integer
entries <- count entriesNumber entry entries <- count entriesNumber entry
return $ XRefSubSection {firstObjectId = ObjectId firstId, entries} return $ XRefSubSection {firstObjectId = ObjectId firstId, entries}

View file

@ -4,14 +4,12 @@
module PDF.Parser ( module PDF.Parser (
MonadParser(..) MonadParser(..)
, Parser , Parser
, (<?>)
, octDigit , octDigit
, on , on
, runParser , runParser
, evalParser , evalParser
) where ) where
import Control.Applicative (Alternative, (<|>))
import Control.Monad (MonadPlus) import Control.Monad (MonadPlus)
import Control.Monad.Fail (MonadFail(..)) import Control.Monad.Fail (MonadFail(..))
import Control.Monad.State (StateT(..), evalStateT) import Control.Monad.State (StateT(..), evalStateT)
@ -49,7 +47,7 @@ instance MonadParser Atto.Parser where
hexNumber = B16Int <$> Atto.takeWhile1 (`Set.member` hexDigits) hexNumber = B16Int <$> Atto.takeWhile1 (`Set.member` hexDigits)
oneOf charSet = Atto.satisfy (`elem` charSet) oneOf charSet = Atto.satisfy (`elem` charSet)
peek = Atto.peekChar' peek = Atto.peekChar'
string s = Atto.string s <?> show s string s = Atto.string s
takeAll = Atto.takeWhile takeAll = Atto.takeWhile
takeAll1 = Atto.takeWhile1 takeAll1 = Atto.takeWhile1
@ -67,9 +65,6 @@ instance (MonadParser m, MonadTrans t, MonadDeps (t m)) => MonadParser (t m) whe
type Parser s = StateT s Atto.Parser type Parser s = StateT s Atto.Parser
(<?>) :: (Alternative m, MonadFail m) => m a -> String -> m a
(<?>) parser debugMessage = parser <|> fail debugMessage
digits :: Set Char digits :: Set Char
digits = Set.fromList ['0'..'9'] digits = Set.fromList ['0'..'9']