2019-03-18 10:00:23 +01:00
|
|
|
{- sv2v
|
|
|
|
|
- Author: Zachary Snow <zach@zachjs.com>
|
|
|
|
|
-}
|
2019-02-08 06:19:39 +01:00
|
|
|
module Language.SystemVerilog.Parser
|
2019-08-06 04:00:04 +02:00
|
|
|
( parseFiles
|
2019-04-04 02:24:09 +02:00
|
|
|
) where
|
2019-02-08 05:49:12 +01:00
|
|
|
|
2019-09-18 01:34:53 +02:00
|
|
|
import Control.Monad.Except
|
2020-08-12 01:14:18 +02:00
|
|
|
import Control.Monad.State.Strict
|
2019-08-06 04:00:04 +02:00
|
|
|
import qualified Data.Map.Strict as Map
|
2019-04-04 02:24:09 +02:00
|
|
|
import Language.SystemVerilog.AST (AST)
|
2020-02-07 05:27:51 +01:00
|
|
|
import Language.SystemVerilog.Parser.Lex (lexStr)
|
2019-04-04 02:24:09 +02:00
|
|
|
import Language.SystemVerilog.Parser.Parse (parse)
|
2020-03-19 01:02:20 +01:00
|
|
|
import Language.SystemVerilog.Parser.Preprocess (preprocess, annotate, Env)
|
2020-01-31 04:17:17 +01:00
|
|
|
import Language.SystemVerilog.Parser.Tokens (Position(..), tokenPosition)
|
2019-02-08 05:49:12 +01:00
|
|
|
|
2019-08-06 04:00:04 +02:00
|
|
|
-- parses a compilation unit given include search paths and predefined macros
|
2020-03-19 01:02:20 +01:00
|
|
|
parseFiles :: [FilePath] -> [(String, String)] -> Bool -> Bool -> [FilePath] -> IO (Either String [AST])
|
|
|
|
|
parseFiles includePaths defines siloed skipPreprocessor paths = do
|
2019-08-06 04:00:04 +02:00
|
|
|
let env = Map.map (\a -> (a, [])) $ Map.fromList defines
|
2020-03-19 01:02:20 +01:00
|
|
|
runExceptT (parseFiles' includePaths env siloed skipPreprocessor paths)
|
2019-08-06 04:00:04 +02:00
|
|
|
|
|
|
|
|
-- parses a compilation unit given include search paths and predefined macros
|
2020-03-19 01:02:20 +01:00
|
|
|
parseFiles' :: [FilePath] -> Env -> Bool -> Bool -> [FilePath] -> ExceptT String IO [AST]
|
|
|
|
|
parseFiles' _ _ _ _ [] = return []
|
|
|
|
|
parseFiles' includePaths env siloed skipPreprocessor (path : paths) = do
|
|
|
|
|
(ast, envEnd) <- parseFile' includePaths env skipPreprocessor path
|
2019-09-18 01:34:53 +02:00
|
|
|
let envNext = if siloed then env else envEnd
|
2020-03-19 01:02:20 +01:00
|
|
|
asts <- parseFiles' includePaths envNext siloed skipPreprocessor paths
|
2019-08-06 04:00:04 +02:00
|
|
|
return $ ast : asts
|
|
|
|
|
|
2019-04-04 02:24:09 +02:00
|
|
|
-- parses a file given include search paths, a table of predefined macros, and
|
|
|
|
|
-- the file path
|
2020-03-19 01:02:20 +01:00
|
|
|
parseFile' :: [String] -> Env -> Bool -> FilePath -> ExceptT String IO (AST, Env)
|
|
|
|
|
parseFile' includePaths env skipPreprocessor path = do
|
|
|
|
|
let runner = if skipPreprocessor then annotate else preprocess
|
|
|
|
|
preResult <- liftIO $ runner includePaths env path
|
2020-02-07 05:27:51 +01:00
|
|
|
(contents, env') <- liftEither preResult
|
2020-12-10 21:33:06 +01:00
|
|
|
tokens <- liftEither $ uncurry lexStr $ unzip contents
|
2020-01-31 04:17:17 +01:00
|
|
|
let position =
|
|
|
|
|
if null tokens
|
|
|
|
|
then Position path 1 1
|
|
|
|
|
else tokenPosition $ head tokens
|
|
|
|
|
ast <- evalStateT parse (position, tokens)
|
2019-08-06 04:00:04 +02:00
|
|
|
return (ast, env')
|