git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader efficiency improvements.

In conjunction with other changes this makes the reader almost twice as fast on our benchmark as it was on Feb. 10.

commitf6cf03857b59776f4f44ea831787231f7f93da96 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 28 Feb 2021, 12:52 (6 years ago) parent564c39b
1 file changed+42−31
src/Text/Pandoc/Readers/LaTeX.hs+42−31
src/Text/Pandoc/Readers/LaTeX.hs+42−31
@@ -191,12 +191,6 @@ inlineCommand = do
191191word :: PandocMonad m => LP m Inlines
192192word = str . untoken <$> satisfyTok isWordTok
193193
194−regularSymbol :: PandocMonad m => LP m Inlines
195−regularSymbol = str . untoken <$> satisfyTok isRegularSymbol
196− where isRegularSymbol (Tok _ Symbol t) = not $ T.any isSpecial t
197− isRegularSymbol _ = False
198− isSpecial c = c `Set.member` specialChars
199−
200194inlineGroup :: PandocMonad m => LP m Inlines
201195inlineGroup = do
202196 ils <- grouped inline
@@ -961,31 +955,48 @@ lookupListDefault d = (fromMaybe d .) . lookupList
961955 where lookupList l m = msum $ map (`M.lookup` m) l
962956
963957inline :: PandocMonad m => LP m Inlines
964−inline = (mempty <$ comment)
965− <|> (space <$ whitespace)
966− <|> (softbreak <$ endline)
967− <|> word
968− <|> macroDef (rawInline "latex")
969− <|> inlineCommand'
970− <|> inlineEnvironment
971− <|> inlineGroup
972− <|> (symbol '-' *>
973− option (str "-") (symbol '-' *>
974− option (str "–") (str "—" <$ symbol '-')))
975− <|> doubleQuote
976− <|> singleQuote
977− <|> (str "”" <$ try (symbol '\'' >> symbol '\''))
978− <|> (str "’" <$ symbol '\'')
979− <|> (str "\160" <$ symbol '~')
980− <|> dollarsMath
981− <|> (guardEnabled Ext_literate_haskell *> symbol '|' *> doLHSverb)
982− <|> (str . T.singleton <$> primEscape)
983− <|> regularSymbol
984− <|> (do res <- symbolIn "#^'`\"[]&"
985− pos <- getPosition
986− let s = untoken res
987− report $ ParsingUnescaped s pos
988− return $ str s)
958+inline = do
959+ Tok pos toktype t <- lookAhead anyTok
960+ let symbolAsString = str . untoken <$> anySymbol
961+ let unescapedSymbolAsString =
962+ do s <- untoken <$> anySymbol
963+ report $ ParsingUnescaped s pos
964+ return $ str s
965+ case toktype of
966+ Comment -> mempty <$ comment
967+ Spaces -> space <$ whitespace
968+ Newline -> softbreak <$ endline
969+ Word -> word
970+ Esc1 -> str . T.singleton <$> primEscape
971+ Esc2 -> str . T.singleton <$> primEscape
972+ Symbol ->
973+ case t of
974+ "-" -> symbol '-' *>
975+ option (str "-") (symbol '-' *>
976+ option (str "–") (str "—" <$ symbol '-'))
977+ "'" -> symbol '\'' *>
978+ option (str "’") (str "”" <$ symbol '\'')
979+ "~" -> str "\160" <$ symbol '~'
980+ "`" -> doubleQuote <|> singleQuote <|> symbolAsString
981+ "\"" -> doubleQuote <|> singleQuote <|> symbolAsString
982+ "“" -> doubleQuote <|> symbolAsString
983+ "‘" -> singleQuote <|> symbolAsString
984+ "$" -> dollarsMath <|> unescapedSymbolAsString
985+ "|" -> (guardEnabled Ext_literate_haskell *>
986+ symbol '|' *> doLHSverb) <|> symbolAsString
987+ "{" -> inlineGroup
988+ "#" -> unescapedSymbolAsString
989+ "&" -> unescapedSymbolAsString
990+ "_" -> unescapedSymbolAsString
991+ "^" -> unescapedSymbolAsString
992+ "\\" -> mzero
993+ "}" -> mzero
994+ _ -> symbolAsString
995+ CtrlSeq _ -> macroDef (rawInline "latex")
996+ <|> inlineCommand'
997+ <|> inlineEnvironment
998+ <|> inlineGroup
999+ _ -> mzero
9891000
9901001inlines :: PandocMonad m => LP m Inlines
9911002inlines = mconcat <$> many inline