Skip to content

Commit

Permalink
Use the ReaderT to enable/disable debug printing
Browse files Browse the repository at this point in the history
  • Loading branch information
anka-213 committed Oct 18, 2021
1 parent f7c53a7 commit 2c0ca93
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions mengwong/mp/src/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Debug.Trace

import Types
import Error
import Control.Monad.Reader (ReaderT(runReaderT))
import Control.Monad.Reader (ReaderT(runReaderT), asks)

-- our task: to parse an input CSV into a collection of Rules.
-- example "real-world" input can be found at https://docs.google.com/spreadsheets/d/1qMGwFhgPYLm-bmoN2es2orGkTaTN382pG2z3RjZ_s-4/edit
Expand All @@ -49,9 +49,17 @@ someFunc = do

-- TODO: integrate debugging and callstack depth with the runConfig, thread it through all the functions here
-- printf debugging infrastructure
debuggery = False
myTraceM x = when debuggery (traceM x)
debugPrint depth str = when debuggery $ do

whenDebug :: Parser () -> Parser ()
whenDebug act = do
isDebug <- asks debug
when isDebug act

myTraceM x = whenDebug (traceM x)

debugPrint :: Int -> String -> ReaderT RunConfig (Parsec Void MyStream) ()

debugPrint depth str = whenDebug $ do
lookingAt <- lookAhead (getToken :: Parser MyToken)
myTraceM $ indent <> str <> " running. depth=" <> show depth <> "; looking at: " <> show lookingAt
where
Expand Down

0 comments on commit 2c0ca93

Please sign in to comment.