WIP: starting to fix this criminally inefficient parser for PDF's postfix-operator instructions

This commit is contained in:
Tissevert 2019-11-29 17:42:57 +01:00
parent f9f799c59b
commit bac08446dd

View file

@ -43,6 +43,7 @@ stateOperator Ri = (,) Ri <$> count 1 argument <* string "ri"
stateOperator I = (,) I <$> count 1 argument <* string "i"
stateOperator Gs = (,) Gs <$> count 1 nameArg <* string "gs"
{-
textOperator :: MonadParser m => TextOperator -> m (Call TextOperator)
textOperator Td = (,) Td <$> count 2 argument <* string "Td"
textOperator TD = (,) TD <$> count 2 argument <* string "TD"
@ -59,6 +60,17 @@ textOperator TL = (,) TL <$> count 1 argument <* string "TL"
textOperator Tf = (,) Tf <$> sequence [nameArg, argument] <* string "Tf"
textOperator Tr = (,) Tr <$> count 1 argument <* string "Tr"
textOperator Ts = (,) Ts <$> count 1 argument <* string "Ts"
-}
textOperator :: MonadParser m => TextOperator -> m TextOperator
textOperator textOp = string (show textOp) *> return textOp
textChunk :: MonadParser m => m (Either TextOperator Argument)
textChunk =
choice $ Left . textOperator <$> [minBound .. maxBound]
<|> choice $ Right <$> [stringArg, nameArg, arrayArg, argument]
aTextOperator :: MonadParser m => m (Call TextOperator)
a :: (Bounded o, Enum o, MonadParser m) => (o -> m (Call o)) -> m (Call o)
a parserGenerator = choice $ parserGenerator <$> [minBound .. maxBound]
@ -95,7 +107,7 @@ text :: ParserWithFont [ByteString]
text =
string "BT" *> blank *> commands <* blank <* string "ET" <?> "Text operators"
where
commands = concat <$> (a textOperator >>= runOperator) `sepBy` blank
commands = concat <$> (aTextOperator >>= runOperator) `sepBy` blank
runOperator :: Call TextOperator -> ParserWithFont [ByteString]
runOperator (Tf, [Typed (NameObject fontName), _]) =