parse :: Opt -> FilePath -> String -> IO HsModule
parse opt fp s = case scanner opt s of
    Left s -> fail s
    Right s -> do
        wdump FD.Tokens $ do
            putStrLn "-- scanned"
            putStrLn $ unwords [ s | L _ _ s <- s ]
            let pp = preprocessLexemes opt fp s
            putStrLn "-- preprocessed"
            forM_ pp $ \c -> case c of
                Token (L _ _ s) -> putStr s >> putStr " "
                TokenNL n -> putStr ('\n':replicate (n - 1) ' ')
                TokenVLCurly _ i -> putStr $ "{" ++ show (i - 1) ++ " "
            putStrLn ""
        laidOut <- doLayout opt fp s
        wdump FD.Tokens $ do
            putStrLn "-- after layout"
            putStrLn $ unwords [ s | L _ _ s <- laidOut ]
        case runP (withSrcLoc bogusASrcLoc { srcLocFileName = packString fp } $ P.parseModule laidOut) opt of
            (ws, ~(Just p)) -> do
                processErrors ws
                return p { hsModuleOpt = opt }
parse :: Opt -> FilePath -> String -> IO HsModule
parse opt fp s = case scanner opt s of
    Left s -> fail s
    Right s -> do
        wdump FD.Tokens $ do
            putStrLn "-- scanned"
            putStrLn $ unwords [ s | L _ _ s <- s ]
            let pp = preprocessLexemes opt fp s
            putStrLn "-- preprocessed"
            forM_ pp $ \c -> case c of
                Token (L _ _ s) -> putStr s >> putStr " "
                TokenNL n -> putStr ('\n':replicate (n - 1) ' ')
                TokenVLCurly _ i -> putStr $ "{" ++ show (i - 1) ++ " "
            putStrLn ""
        laidOut <- doLayout opt fp s
        wdump FD.Tokens $ do
            putStrLn "-- after layout"
            putStrLn $ unwords [ s | L _ _ s <- laidOut ]
        case runP (withSrcLoc bogusASrcLoc { srcLocFileName = packString fp } $ P.parseModule laidOut) opt of
            (ws, ~(Just p)) -> do
                processErrors ws
                return p { hsModuleOpt = opt }
parse opt fp s = case scanner opt s of
    Left s -> fail s
    Right s -> do
        wdump FD.Tokens $ do
            putStrLn "-- scanned"
            putStrLn $ unwords [ s | L _ _ s <- s ]
            let pp = preprocessLexemes opt fp s
            putStrLn "-- preprocessed"
            forM_ pp $ \c -> case c of
                Token (L _ _ s) -> putStr s >> putStr " "
                TokenNL n -> putStr ('\n':replicate (n - 1) ' ')
                TokenVLCurly _ i -> putStr $ "{" ++ show (i - 1) ++ " "
            putStrLn ""
        laidOut <- doLayout opt fp s
        wdump FD.Tokens $ do
            putStrLn "-- after layout"
            putStrLn $ unwords [ s | L _ _ s <- laidOut ]
        case runP (withSrcLoc bogusASrcLoc { srcLocFileName = packString fp } $ P.parseModule laidOut) opt of
            (ws, ~(Just p)) -> do
                processErrors ws
                return p { hsModuleOpt = opt }