From 51a8adf27311ab4c4201b909af0a7b727858393f Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 5 Dec 2012 23:49:50 +0000 Subject: [PATCH] regexSplit, ptrace (parsec trace) utilities --- hledger-lib/Hledger/Utils.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/hledger-lib/Hledger/Utils.hs b/hledger-lib/Hledger/Utils.hs index 90028771f..29f44f6ca 100644 --- a/hledger-lib/Hledger/Utils.hs +++ b/hledger-lib/Hledger/Utils.hs @@ -228,6 +228,9 @@ regexReplaceBy r replfn s = gsubRegexPRBy r replfn s regexToCaseInsensitive :: String -> String regexToCaseInsensitive r = "(?i)"++ r +regexSplit :: String -> String -> [String] +regexSplit = splitRegexPR + -- lists splitAtElement :: Eq a => a -> [a] -> [[a]] @@ -340,6 +343,26 @@ mtrace a = strace a `seq` return a tracewith :: (a -> String) -> a -> a tracewith f e = trace (f e) e +-- | Parsec trace - show the current parsec position and next input, +-- prefixed by the specified label if it's non-null. +ptrace :: String -> GenParser Char st () +ptrace label = do + let label' = if null label then "" else label ++ ": " + pos <- getPosition + let (line,col) = (sourceLine pos, sourceColumn pos) + next <- take 20 `fmap` getInput + mtrace (printf "%-10sat line %2d col %2d looking at >>>%s<<<" label' line col next :: String) + return () + +ptrace' :: (Show a) => String -> GenParser a st () +ptrace' label = do + let label' = if null label then "" else label ++ ": " + pos <- getPosition + let (line,col) = (sourceLine pos, sourceColumn pos) + next <- take 20 `fmap` getInput + mtrace (printf "%-10sat line %2d col %2d looking at %s" label' line col (show next) :: String) + return () + -- parsing -- | Backtracking choice, use this when alternatives share a prefix.