Merge pull request #1 from ThePerfectComputer/re_org_directories
guess I'm done with re-org
This commit is contained in:
commit
3573642131
|
@ -57,9 +57,14 @@ common warnings
|
||||||
library
|
library
|
||||||
exposed-modules:
|
exposed-modules:
|
||||||
Haskellator,
|
Haskellator,
|
||||||
RtlilAstTypes
|
-- RtlilAstTypes
|
||||||
other-modules:
|
other-modules:
|
||||||
hs-source-dirs: src
|
RTLILParser.AST,
|
||||||
|
RTLILParser.Primitives,
|
||||||
|
RTLILParser.Parser,
|
||||||
|
Util
|
||||||
|
hs-source-dirs:
|
||||||
|
src,
|
||||||
build-depends:
|
build-depends:
|
||||||
base ^>=4.17.2.1,
|
base ^>=4.17.2.1,
|
||||||
parsec >=3.1
|
parsec >=3.1
|
||||||
|
|
|
@ -1,86 +1,3 @@
|
||||||
-- this parser largely references:
|
module Haskellator(val) where
|
||||||
-- https://github.com/YosysHQ/yosys/blob/111b747d2797238eadf541879848492a9d34909a/docs/source/yosys_internals/formats/rtlil_text.rst
|
|
||||||
module Haskellator(a, val) where
|
|
||||||
|
|
||||||
|
import RTLILParser.Parser(val)
|
||||||
import Control.Monad (void)
|
|
||||||
import Text.Parsec
|
|
||||||
import Text.Parsec.String (Parser)
|
|
||||||
import RtlilAstTypes(
|
|
||||||
PublicId(..),
|
|
||||||
Id(..),
|
|
||||||
AutogenId(..),
|
|
||||||
AutoIdxStmt(..),
|
|
||||||
Value(..)
|
|
||||||
)
|
|
||||||
import Util(
|
|
||||||
binaryStringToInt,
|
|
||||||
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
|
|
|
@ -1,4 +1,4 @@
|
||||||
module RtlilAstTypes(
|
module RTLILParser.AST(
|
||||||
PublicId(..),
|
PublicId(..),
|
||||||
AutogenId(..),
|
AutogenId(..),
|
||||||
AutoIdxStmt(..),
|
AutoIdxStmt(..),
|
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
|
||||||
|
]
|
21
src/Util.hs
21
src/Util.hs
|
@ -1,7 +1,6 @@
|
||||||
module Util(
|
module Util(
|
||||||
binaryStringToInt,
|
binaryStringToInt,
|
||||||
pEscapedChar,
|
octalStringToInt,
|
||||||
pEscapedChar
|
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Monad (void)
|
import Control.Monad (void)
|
||||||
|
@ -17,21 +16,3 @@ octalStringToInt :: String -> Maybe Int
|
||||||
octalStringToInt str
|
octalStringToInt str
|
||||||
| all (`elem` ['0'..'7']) str = Just $ foldl' (\acc x -> acc * 8 + digitToInt x) 0 str
|
| all (`elem` ['0'..'7']) str = Just $ foldl' (\acc x -> acc * 8 + digitToInt x) 0 str
|
||||||
| otherwise = Nothing
|
| otherwise = Nothing
|
||||||
|
|
||||||
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' -- \n → newline
|
|
||||||
, char 't' >> return '\t' -- \t → tab
|
|
||||||
, try pOctal -- \123 → octal escape
|
|
||||||
, anyChar -- Any other escaped char (e.g., \\)
|
|
||||||
]
|
|
||||||
|
|
Loading…
Reference in a new issue