From 8e0073a1e60325507c9776ba41b469ed29918f46 Mon Sep 17 00:00:00 2001 From: Yehowshua Immanuel Date: Mon, 9 Dec 2024 11:34:26 -0500 Subject: [PATCH] RTLIL parser presumably finished except for comment parser --- README.md | 7 +++++- TODO.md | 1 + src/RTLILParser/AST.hs | 30 +++++++++++++++-------- src/RTLILParser/Parser.hs | 50 +++++++++++++++++++++++++++++++++------ 4 files changed, 70 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index fb802d3..1ec2597 100644 --- a/README.md +++ b/README.md @@ -70,4 +70,9 @@ $ rtlil-parse I have yet to evaluate the implications of how this affects multi-clock domain circuits and their associated primitives such as asynchronous FIFOs, but I plan to make sure simulation of such - circuits is possible and correct. \ No newline at end of file + circuits is possible and correct. + +# Lessons Learned + - Should have written parser to be token based where after consuming + and capturing token, we consume and discard all following whitespaces + as well as comments... \ No newline at end of file diff --git a/TODO.md b/TODO.md index 656df2a..16cf4e7 100644 --- a/TODO.md +++ b/TODO.md @@ -42,6 +42,7 @@ - [x] Remove all instances of `_ <-` - [ ] Module - [ ] Remove weird GHC imports + - [x] Are the `try` statements in `pWireOption` correctly constructed? - [ ] Consider the very weird case where the process body has nothing, thus, `pEolAndAdvanceToNextNonWs` may never get invoked in any of the sub-parsers encapsulated in `pProcessBody`. Do we need to diff --git a/src/RTLILParser/AST.hs b/src/RTLILParser/AST.hs index 8a6d3a7..56d0461 100644 --- a/src/RTLILParser/AST.hs +++ b/src/RTLILParser/AST.hs @@ -5,11 +5,15 @@ module RTLILParser.AST ( -- Values Value(..), + -- File + File(..), + -- Autoindex statements AutoIdxStmt(..), -- Module - ParamStmt(..), Constant(..), + Module(..), ModuleStmt(..), ModuleBody(..), + ModuleBodyVariant(..), ParamStmt(..), Constant(..), -- Attribute statements AttrStmt(..), @@ -68,20 +72,26 @@ data Value = Value -- strings -- comments -- file +data File = File AutoIdxStmt [Module] deriving (Show) -- Autoindex statements data AutoIdxStmt = AutoIdxStmt Int deriving (Show) -- Module -data ParamStmt = ParamStmt - { paramId :: Id - , paramConstant :: Maybe Constant - } - deriving (Show) -data Constant = ConstantValue Value - | ConstantInteger Int - | ConstantString String - deriving (Show) +data Module = Module ModuleStmt [AttrStmt] ModuleBody deriving (Show) +data ModuleStmt = ModuleStmt Id deriving (Show) +data ModuleBody = ModuleBody [ModuleBodyVariant] deriving (Show) +data ModuleBodyVariant = ModuleBodyParamStmt ParamStmt + | ModuleBodyWire Wire + | ModuleBodyMemory Memory + | ModuleBodyCell Cell + | ModuleBodyProcess Process + deriving (Show) +data ParamStmt = ParamStmt Id (Maybe Constant) deriving (Show) +data Constant = ConstantValue Value + | ConstantInteger Int + | ConstantString String + deriving (Show) -- Attribute statements data AttrStmt = AttrStmt Id Constant deriving (Show) diff --git a/src/RTLILParser/Parser.hs b/src/RTLILParser/Parser.hs index 9a1e318..94f6dcd 100644 --- a/src/RTLILParser/Parser.hs +++ b/src/RTLILParser/Parser.hs @@ -11,11 +11,15 @@ import RTLILParser.AST ( -- Values Value(..), + -- File + File(..), + -- Autoindex statements AutoIdxStmt(..), -- Module - ParamStmt(..), Constant(..), + Module(..), ModuleStmt(..), ModuleBody(..), + ModuleBodyVariant(..), ParamStmt(..), Constant(..), -- Attribute statements AttrStmt(..), @@ -106,6 +110,10 @@ pString = -- comments -- file +pFile :: Parser File +pFile = File + <$> pAutoIdxStmt + <*> many pModule -- Autoindex statements pAutoIdxStmt :: Parser AutoIdxStmt @@ -114,9 +122,37 @@ pAutoIdxStmt = AutoIdxStmt pInteger <* pEolAndAdvanceToNextNonWs) -- Module -pModuleStmt :: Parser Id -pModuleStmt = string "module" *> pWs *> pId <* - pEolAndAdvanceToNextNonWs +pModule :: Parser Module +pModule = do + attrs <- many pAttrStmt + moduleStmt <- pModuleStmt + moduleBody <- pModuleBody + pModuleEndStmt + return $ Module moduleStmt attrs moduleBody + +pModuleStmt :: Parser ModuleStmt +pModuleStmt = ModuleStmt + <$> (string "module" *> pWs *> + pId <* pEolAndAdvanceToNextNonWs) + +pModuleBody :: Parser ModuleBody +pModuleBody = ModuleBody + <$> many pModuleBodyVariant + +pModuleBodyVariant :: Parser ModuleBodyVariant +pModuleBodyVariant = + -- `pWire`, `pMemory`, `pCell`, `pProcess` all + -- start by parsing attribute statements, so we + -- need backtracking since we can't determin which + -- parser will succeed based on the first character + -- we encounter alone. `pParamStmt` technically doesn't + -- need to be prefixed by `try`, so that is a stylistic + -- choice. + try (ModuleBodyParamStmt <$> pParamStmt) <|> + try (ModuleBodyWire <$> pWire ) <|> + try (ModuleBodyMemory <$> pMemory ) <|> + try (ModuleBodyCell <$> pCell ) <|> + (ModuleBodyProcess <$> pProcess ) pParamStmt :: Parser ParamStmt pParamStmt = ParamStmt @@ -126,9 +162,9 @@ pParamStmt = ParamStmt pConstant :: Parser Constant pConstant = - try (ConstantValue <$> pValue) - <|> (ConstantInteger <$> pInteger) - <|> (ConstantString <$> pString) + try (ConstantValue <$> pValue ) + <|> (ConstantInteger <$> pInteger) + <|> (ConstantString <$> pString ) pModuleEndStmt :: Parser () pModuleEndStmt = void (string "end")