startGhc :: BChan (E.TokenizedEvent [Located Token]) -> MVar String -> IO ()
startGhc eventChannel lexerChannel = do
  _ <- forkIO $ runGhc (Just libdir) $ do
    flags <- getSessionDynFlags
    let lexLoc = mkRealSrcLoc (mkFastString "<interactive>") 1 1
    GMU.liftIO $ forever $ do
        src <- takeMVar lexerChannel
        let sb = stringToStringBuffer src
        let pResult = lexTokenStream sb lexLoc flags
        case pResult of

          POk _ toks -> GMU.liftIO $ do
            hPutStrLn stderr $ "TOKENS\n" ++ concatMap showToken toks
            writeBChan eventChannel $ E.Tokens toks

          PFailed srcspan msg -> do
            GMU.liftIO $ print $ show srcspan
            GMU.liftIO $
              do putStrLn "Lexer Error:"
                 print $ mkPlainErrMsg flags srcspan msg
  return ()
startGhc :: BChan (E.TokenizedEvent [Located Token]) -> MVar String -> IO ()
startGhc eventChannel lexerChannel = do
  _ <- forkIO $ runGhc (Just libdir) $ do
    flags <- getSessionDynFlags
    let lexLoc = mkRealSrcLoc (mkFastString "<interactive>") 1 1
    GMU.liftIO $ forever $ do
        src <- takeMVar lexerChannel
        let sb = stringToStringBuffer src
        let pResult = lexTokenStream sb lexLoc flags
        case pResult of

          POk _ toks -> GMU.liftIO $ do
            hPutStrLn stderr $ "TOKENS\n" ++ concatMap showToken toks
            writeBChan eventChannel $ E.Tokens toks

          PFailed srcspan msg -> do
            GMU.liftIO $ print $ show srcspan
            GMU.liftIO $
              do putStrLn "Lexer Error:"
                 print $ mkPlainErrMsg flags srcspan msg
  return ()
startGhc eventChannel lexerChannel = do
  _ <- forkIO $ runGhc (Just libdir) $ do
    flags <- getSessionDynFlags
    let lexLoc = mkRealSrcLoc (mkFastString "<interactive>") 1 1
    GMU.liftIO $ forever $ do
        src <- takeMVar lexerChannel
        let sb = stringToStringBuffer src
        let pResult = lexTokenStream sb lexLoc flags
        case pResult of

          POk _ toks -> GMU.liftIO $ do
            hPutStrLn stderr $ "TOKENS\n" ++ concatMap showToken toks
            writeBChan eventChannel $ E.Tokens toks

          PFailed srcspan msg -> do
            GMU.liftIO $ print $ show srcspan
            GMU.liftIO $
              do putStrLn "Lexer Error:"
                 print $ mkPlainErrMsg flags srcspan msg
  return ()