guess I'm done with re-org
This commit is contained in:
parent
4e33d595cc
commit
3082f3409f
6 changed files with 126 additions and 109 deletions
24
src/RTLILParser/AST.hs
Normal file
24
src/RTLILParser/AST.hs
Normal file
|
@ -0,0 +1,24 @@
|
|||
module RTLILParser.AST(
|
||||
PublicId(..),
|
||||
AutogenId(..),
|
||||
AutoIdxStmt(..),
|
||||
Id(..),
|
||||
Value(..)
|
||||
) where
|
||||
import Text.Read (Lexeme(Ident))
|
||||
|
||||
data PublicId = PublicId String deriving (Show)
|
||||
data AutogenId = AutogenId String deriving (Show)
|
||||
data Id = Public PublicId
|
||||
| Autogen AutogenId
|
||||
deriving (Show)
|
||||
data AutoIdxStmt = AutoIdxStmt Int deriving (Show)
|
||||
data Value = Value
|
||||
{ width :: Int
|
||||
, value :: Int
|
||||
}
|
||||
deriving (Show)
|
||||
data Constant = ConstantValue Value
|
||||
| ConstantInteger Int
|
||||
| ConstantString String
|
||||
deriving (Show)
|
85
src/RTLILParser/Parser.hs
Normal file
85
src/RTLILParser/Parser.hs
Normal file
|
@ -0,0 +1,85 @@
|
|||
-- this parser largely references:
|
||||
-- https://github.com/YosysHQ/yosys/blob/111b747d2797238eadf541879848492a9d34909a/docs/source/yosys_internals/formats/rtlil_text.rst
|
||||
module RTLILParser.Parser(a, val) where
|
||||
|
||||
|
||||
import Control.Monad (void)
|
||||
import Text.Parsec
|
||||
import Text.Parsec.String (Parser)
|
||||
import RTLILParser.AST(
|
||||
PublicId(..),
|
||||
Id(..),
|
||||
AutogenId(..),
|
||||
AutoIdxStmt(..),
|
||||
Value(..)
|
||||
)
|
||||
import Util(binaryStringToInt)
|
||||
import RTLILParser.Primitives(pEscapedChar)
|
||||
|
||||
-- https://github.com/YosysHQ/yosys/blob/111b747d2797238eadf541879848492a9d34909a/frontends/rtlil/rtlil_lexer.l#L88C1-L88C17
|
||||
nonws :: Parser Char
|
||||
nonws = noneOf " \t\r\n"
|
||||
|
||||
pWs :: Parser String
|
||||
pWs = many1 (oneOf " \t")
|
||||
|
||||
pEol :: Parser String
|
||||
pEol = many1 (oneOf "\r\n")
|
||||
|
||||
pPublicId :: Parser PublicId
|
||||
pPublicId = PublicId <$> (char '\\' *> many1 nonws)
|
||||
|
||||
pAutogenId :: Parser AutogenId
|
||||
pAutogenId = AutogenId <$> (char '$' *> many1 nonws)
|
||||
|
||||
pId :: Parser Id
|
||||
pId = Public <$> pPublicId
|
||||
<|> Autogen <$> pAutogenId
|
||||
|
||||
decimalDigit :: Parser Char
|
||||
decimalDigit = oneOf "0123456789"
|
||||
|
||||
-- update in the future to support 4 state logic
|
||||
-- by converting x and z to 0 and warning about it.
|
||||
pBinaryDigit :: Parser Char
|
||||
pBinaryDigit = oneOf "01"
|
||||
|
||||
pString :: Parser String
|
||||
pString =
|
||||
between delimiter delimiter parseString
|
||||
where
|
||||
delimiter = char '"'
|
||||
parseString = many (pEscapedChar <|> noneOf "\\\"")
|
||||
|
||||
|
||||
pValue :: Parser Value
|
||||
pValue = Value <$> pInteger
|
||||
<*> (binaryStringToInt <$> many1 pBinaryDigit)
|
||||
|
||||
pInteger :: Parser Int
|
||||
pInteger = do
|
||||
sign <- optionMaybe (char '-')
|
||||
digits <- many1 digit
|
||||
let value = read digits
|
||||
return $ case sign of
|
||||
Just _ -> -value
|
||||
Nothing -> value
|
||||
|
||||
|
||||
pAutoIdxStmt :: Parser AutoIdxStmt
|
||||
pAutoIdxStmt = AutoIdxStmt <$> (string "autoidx" *> pWs *> pInteger <* pEol)
|
||||
|
||||
pModuleStmt :: Parser Id
|
||||
pModuleStmt = string "module" *> pWs *> pId <* pEol
|
||||
|
||||
pModuleEndStmt :: Parser ()
|
||||
pModuleEndStmt = void (string "end")
|
||||
|
||||
-- pModuleStmt :: Parser ()
|
||||
-- pModuleStmt =
|
||||
|
||||
-- val = parse pInteger "pInteger" "721"
|
||||
val = parse pModuleStmt "pModuleStmt" "module \\top\n"
|
||||
|
||||
a :: Int
|
||||
a = 3
|
29
src/RTLILParser/Primitives.hs
Normal file
29
src/RTLILParser/Primitives.hs
Normal file
|
@ -0,0 +1,29 @@
|
|||
module RTLILParser.Primitives(
|
||||
pOctal,
|
||||
pEscapedChar
|
||||
) where
|
||||
|
||||
import Control.Monad (void)
|
||||
import Text.Parsec
|
||||
import Text.Parsec.String (Parser)
|
||||
import Data.Char (digitToInt)
|
||||
import Util(binaryStringToInt, octalStringToInt)
|
||||
|
||||
|
||||
pOctal :: Parser Char
|
||||
pOctal = do
|
||||
digits <- count 1 digit -- At least 1 digit
|
||||
moreDigits <- option "" (count 2 digit) -- Up to 3 digits total
|
||||
case octalStringToInt (digits ++ moreDigits) of
|
||||
Just value -> return $ toEnum value -- Convert integer to a Char
|
||||
Nothing -> fail "Invalid octal escape sequence"
|
||||
|
||||
pEscapedChar :: Parser Char
|
||||
pEscapedChar = do
|
||||
char '\\' -- Match the backslash
|
||||
choice
|
||||
[ char 'n' >> return '\n'
|
||||
, char 't' >> return '\t'
|
||||
, try pOctal
|
||||
, anyChar
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue