diff --git a/Language/SystemVerilog/Parser/Preprocess.hs b/Language/SystemVerilog/Parser/Preprocess.hs index f0fed29..29c1b5c 100644 --- a/Language/SystemVerilog/Parser/Preprocess.hs +++ b/Language/SystemVerilog/Parser/Preprocess.hs @@ -5,18 +5,18 @@ module Language.SystemVerilog.Parser.Preprocess -- | Remove comments from code. uncomment :: FilePath -> String -> String -uncomment file a = uncomment a +uncomment file str = uncomment' str where - uncomment a = case a of + uncomment' a = case a of "" -> "" '/' : '/' : rest -> " " ++ removeEOL rest '/' : '*' : rest -> " " ++ remove rest '"' : rest -> '"' : ignoreString rest - a : rest -> a : uncomment rest + ch : rest -> ch : uncomment' rest removeEOL a = case a of "" -> "" - '\n' : rest -> '\n' : uncomment rest + '\n' : rest -> '\n' : uncomment' rest '\t' : rest -> '\t' : removeEOL rest _ : rest -> ' ' : removeEOL rest @@ -25,7 +25,7 @@ uncomment file a = uncomment a '"' : rest -> removeString rest '\n' : rest -> '\n' : remove rest '\t' : rest -> '\t' : remove rest - '*' : '/' : rest -> " " ++ uncomment rest + '*' : '/' : rest -> " " ++ uncomment' rest _ : rest -> " " ++ remove rest removeString a = case a of @@ -38,17 +38,18 @@ uncomment file a = uncomment a ignoreString a = case a of "" -> error $ "File ended without closing string: " ++ file - '"' : rest -> '"' : uncomment rest + '"' : rest -> '"' : uncomment' rest '\\' : '"' : rest -> "\\\"" ++ ignoreString rest - a : rest -> a : ignoreString rest + ch : rest -> ch : ignoreString rest -- | A simple `define preprocessor. preprocess :: [(String, String)] -> FilePath -> String -> String -preprocess env file content = unlines $ pp True [] env $ lines $ uncomment file content +preprocess _env file content = unlines $ pp True [] _env $ lines $ uncomment file content where pp :: Bool -> [Bool] -> [(String, String)] -> [String] -> [String] pp _ _ _ [] = [] pp on stack env (a : rest) = + -- handle macros with escaped newlines if a /= "" && last a == '\\' && head a == '`' then "" : (pp on stack env $ ((init a) ++ " " ++ (head rest)) : (tail rest)) else case words a of