Commit bceec393 by Zachary Snow

standardize input handle logic

parent 40f66e02
...@@ -21,6 +21,7 @@ import Data.List (tails, isPrefixOf, findIndex, intercalate) ...@@ -21,6 +21,7 @@ import Data.List (tails, isPrefixOf, findIndex, intercalate)
import Data.Maybe (isJust, fromJust) import Data.Maybe (isJust, fromJust)
import System.Directory (findFile) import System.Directory (findFile)
import System.FilePath (dropFileName) import System.FilePath (dropFileName)
import System.IO (hGetContents, openFile, stdin, IOMode(ReadMode))
import qualified Data.Map.Strict as Map import qualified Data.Map.Strict as Map
import Language.SystemVerilog.Parser.Tokens (Position(..)) import Language.SystemVerilog.Parser.Tokens (Position(..))
...@@ -71,10 +72,7 @@ elsifCond defined c = ...@@ -71,10 +72,7 @@ elsifCond defined c =
-- preprocessor entrypoint -- preprocessor entrypoint
preprocess :: [String] -> Env -> FilePath -> ExceptT String IO (Env, Contents) preprocess :: [String] -> Env -> FilePath -> ExceptT String IO (Env, Contents)
preprocess includePaths env path = do preprocess includePaths env path = do
contents <- liftIO $ contents <- liftIO $ loadFile path
if path == "-"
then getContents
else loadFile path
let initialState = PP let initialState = PP
{ ppInput = contents { ppInput = contents
, ppOutput = [] , ppOutput = []
...@@ -98,24 +96,25 @@ preprocess includePaths env path = do ...@@ -98,24 +96,25 @@ preprocess includePaths env path = do
-- preprocessing -- preprocessing
annotate :: FilePath -> ExceptT String IO Contents annotate :: FilePath -> ExceptT String IO Contents
annotate path = do annotate path = do
contents <- liftIO $ contents <- liftIO $ loadFile path
if path == "-"
then getContents
else loadFile path
let positions = scanl advance (Position path 1 1) contents let positions = scanl advance (Position path 1 1) contents
return $ zip contents positions return $ zip contents positions
-- read in the given file -- read in the given file
loadFile :: FilePath -> IO String loadFile :: FilePath -> IO String
loadFile path = do loadFile path = do
contents <- readFile path handle <-
if path == "-"
then return stdin
else openFile path ReadMode
contents <- hGetContents handle
return $ normalize contents return $ normalize contents
where
-- removes carriage returns before newlines -- removes carriage returns before newlines
normalize :: String -> String normalize :: String -> String
normalize ('\r' : '\n' : rest) = '\n' : (normalize rest) normalize ('\r' : '\n' : rest) = '\n' : (normalize rest)
normalize (ch : chs) = ch : (normalize chs) normalize (ch : chs) = ch : (normalize chs)
normalize [] = [] normalize [] = []
-- find the given file for inclusion -- find the given file for inclusion
includeSearch :: FilePath -> PPS FilePath includeSearch :: FilePath -> PPS FilePath
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment