git.iko.soy/typeable/pandoc
on master help sign in
Added readerTrace to ReaderOptions, --trace command line opt.

This is to debug backtracking-related parsing bugs. So far it is only implemented for markdown, but it would be good to extend it to latex and html readers.

commit69f7b1dbf3423960b148d6d11ad9f25fa6cc81a9 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 25 Feb 2014, 22:43 (13 years ago) parent19b127b
3 files changed+22−1
pandoc.hs+9 src/Text/Pandoc/Options.hs+2 src/Text/Pandoc/Readers/Markdown.hs+11−1
pandoc.hs+9
@@ -162,6 +162,7 @@ data Opt = Opt
162162 , optAscii :: Bool -- ^ Use ascii characters only in html
163163 , optTeXLigatures :: Bool -- ^ Use TeX ligatures for quotes/dashes
164164 , optDefaultImageExtension :: String -- ^ Default image extension
165+ , optTrace :: Bool -- ^ Print debug information
165166 }
166167
167168-- | Defaults for command-line options.
@@ -217,6 +218,7 @@ defaultOpts = Opt
217218 , optAscii = False
218219 , optTeXLigatures = True
219220 , optDefaultImageExtension = ""
221+ , optTrace = False
220222 }
221223
222224-- | A list of functions, each transforming the options data structure
@@ -758,6 +760,11 @@ options =
758760 (\opt -> return opt { optHTMLMathMethod = GladTeX }))
759761 "" -- "Use gladtex for HTML math"
760762
763+ , Option "" ["trace"]
764+ (NoArg
765+ (\opt -> return opt { optTrace = True }))
766+ "" -- "Turn on diagnostic tracing in readers."
767+
761768 , Option "" ["dump-args"]
762769 (NoArg
763770 (\opt -> return opt { optDumpArgs = True }))
@@ -952,6 +959,7 @@ main = do
952959 , optAscii = ascii
953960 , optTeXLigatures = texLigatures
954961 , optDefaultImageExtension = defaultImageExtension
962+ , optTrace = trace
955963 } = opts
956964
957965 when dumpArgs $
@@ -1074,6 +1082,7 @@ main = do
10741082 , readerIndentedCodeClasses = codeBlockClasses
10751083 , readerApplyMacros = not laTeXOutput
10761084 , readerDefaultImageExtension = defaultImageExtension
1085+ , readerTrace = trace
10771086 }
10781087
10791088 let writerOptions = def { writerStandalone = standalone',
src/Text/Pandoc/Options.hs+2
@@ -210,6 +210,7 @@ data ReaderOptions = ReaderOptions{
210210 , readerIndentedCodeClasses :: [String] -- ^ Default classes for
211211 -- indented code blocks
212212 , readerDefaultImageExtension :: String -- ^ Default extension for images
213+ , readerTrace :: Bool -- ^ Print debugging info
213214} deriving (Show, Read)
214215
215216instance Default ReaderOptions
@@ -225,6 +226,7 @@ instance Default ReaderOptions
225226 , readerApplyMacros = True
226227 , readerIndentedCodeClasses = []
227228 , readerDefaultImageExtension = ""
229+ , readerTrace = False
228230 }
229231
230232--
src/Text/Pandoc/Readers/Markdown.hs+11−1
@@ -60,6 +60,8 @@ import System.FilePath (takeExtension, addExtension)
6060import Text.HTML.TagSoup
6161import Text.HTML.TagSoup.Match (tagOpen)
6262import qualified Data.Set as Set
63+import Text.Printf (printf)
64+import Debug.Trace (trace)
6365
6466type MarkdownParser = Parser [Char] ParserState
6567
@@ -440,7 +442,10 @@ parseBlocks :: MarkdownParser (F Blocks)
440442parseBlocks = mconcat <$> manyTill block eof
441443
442444block :: MarkdownParser (F Blocks)
443−block = choice [ mempty <$ blanklines
445+block = do
446+ tr <- getOption readerTrace
447+ pos <- getPosition
448+ res <- choice [ mempty <$ blanklines
444449 , codeBlockFenced
445450 , yamlMetaBlock
446451 , guardEnabled Ext_latex_macros *> (macro >>= return . return)
@@ -465,6 +470,11 @@ block = choice [ mempty <$ blanklines
465470 , para
466471 , plain
467472 ] <?> "block"
473+ when tr $ do
474+ st <- getState
475+ trace (printf "line %d: %s" (sourceLine pos)
476+ (take 60 $ show $ B.toList $ runF res st)) (return ())
477+ return res
468478
469479--
470480-- header blocks