Parser.hs 1.43 KB
Newer Older
1 2 3
{- sv2v
 - Author: Zachary Snow <zach@zachjs.com>
 -}
4
module Language.SystemVerilog.Parser
5
    ( parseFiles
6
    ) where
7

8
import Control.Monad.Except
9
import qualified Data.Map.Strict as Map
10
import Language.SystemVerilog.AST (AST)
11
import Language.SystemVerilog.Parser.Lex (lexFile, Env)
12
import Language.SystemVerilog.Parser.Parse (parse)
13

14
-- parses a compilation unit given include search paths and predefined macros
15 16
parseFiles :: [FilePath] -> [(String, String)] -> Bool -> [FilePath] -> IO (Either String [AST])
parseFiles includePaths defines siloed paths = do
17
    let env = Map.map (\a -> (a, [])) $ Map.fromList defines
18
    runExceptT (parseFiles' includePaths env siloed paths)
19 20

-- parses a compilation unit given include search paths and predefined macros
21 22 23 24 25 26
parseFiles' :: [FilePath] -> Env -> Bool -> [FilePath] -> ExceptT String IO [AST]
parseFiles' _ _ _ [] = return []
parseFiles' includePaths env siloed (path : paths) = do
    (ast, envEnd) <- parseFile' includePaths env path
    let envNext = if siloed then env else envEnd
    asts <- parseFiles' includePaths envNext siloed paths
27 28
    return $ ast : asts

29 30
-- parses a file given include search paths, a table of predefined macros, and
-- the file path
31
parseFile' :: [String] -> Env -> FilePath -> ExceptT String IO (AST, Env)
32
parseFile' includePaths env path = do
33 34 35
    result <- liftIO $ lexFile includePaths env path
    (tokens, env') <- liftEither result
    ast <- parse tokens
36
    return (ast, env')