Skip to content
Snippets Groups Projects
detokenizer.hs 609 B
Newer Older
#!/usr/bin/env -S runhaskell --ghc-arg="-Wall"
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Data.Text (Text, lines, unsnoc)
import Data.Text.IO (interact)
import Prelude hiding (interact, lines)

detokenize :: [Text] -> Text
detokenize [] = "\n"
detokenize [w] = w <> "\n"
detokenize (w:c:ws)
  | c `elem` [",", "."] = w <> c <> " " <> detokenize ws
detokenize (w:ws) = finish $ unsnoc w
  where
    finish Nothing = detokenize ws
    finish (Just (_, c))
      | c == '\'' = w <> detokenize ws
      | otherwise = w <> " " <> detokenize ws

main :: IO ()
main = interact $ detokenize . lines