{-# LANGUAGE LambdaCase #-} module Main where import Data.Text.Lazy (Text) import qualified Data.Text.Lazy as T import qualified Data.Text.Lazy.IO as T import System.Environment import System.Exit import Pattern import Parser import Pretty import SourceFile apply :: Pattern -> SourceFile -> SourceFile apply pat (SourceFile chunks) = SourceFile $ flip map chunks $ \case CSkip s -> CSkip s COcc occ -> COcc (apply' pat occ) apply' :: Pattern -> Occurrence -> Occurrence apply' (Pattern name items) origocc@(Occurrence name' args) | name == name' = Occurrence name' (travOrigsArgs items args) | otherwise = origocc travOrigsArgs :: [Origin] -> [(Text, Text)] -> [(Text, Text)] travOrigsArgs [] args = args travOrigsArgs _ [] = [] travOrigsArgs (OCopy : items) (arg:args) = arg : travOrigsArgs items args travOrigsArgs (ONew name : items) args = (T.pack " ", name) : travOrigsArgs items args main :: IO () main = do getArgs >>= \case [fname, pat] -> do pat' <- case parsePattern pat of Left err -> die err Right pat' -> return pat' source <- readFile fname parsed <- case parseSourceFile fname source pat' of Left err -> die (show err) Right res -> return res print parsed T.putStrLn (pretty parsed) putStrLn (replicate 80 '-') T.putStrLn (pretty (apply pat' parsed)) _ -> do die "Usage: refactor-type-parameters "