aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYann Herklotz Grave <git@yannherklotzgrave.com>2019-02-16 11:30:11 +0000
committerYann Herklotz Grave <git@yannherklotzgrave.com>2019-02-16 11:30:11 +0000
commit01f9fe39f3e6fefbafb878f95d23802f8f368b51 (patch)
treedbfb35c4fa373334c748f4643f2cff78cff868db /src
parent7453b4e8ed3a23f16822b32a100bd36e813910d4 (diff)
downloadverismith-01f9fe39f3e6fefbafb878f95d23802f8f368b51.tar.gz
verismith-01f9fe39f3e6fefbafb878f95d23802f8f368b51.zip
Add number parsing
Diffstat (limited to 'src')
-rw-r--r--src/VeriFuzz/Parser.hs45
1 files changed, 37 insertions, 8 deletions
diff --git a/src/VeriFuzz/Parser.hs b/src/VeriFuzz/Parser.hs
index 28a475d..667cab1 100644
--- a/src/VeriFuzz/Parser.hs
+++ b/src/VeriFuzz/Parser.hs
@@ -11,7 +11,14 @@ Minimal Verilog parser to reconstruct the AST. This parser does not support the
whole Verilog syntax, as the AST does not support it either.
-}
-module VeriFuzz.Parser where
+module VeriFuzz.Parser
+ ( -- * Parsers
+ parseVerilogSrc
+ , parseDescription
+ , parseModDecl
+ , parseContAssign
+ , parseExpr
+ ) where
import Control.Applicative ((<|>))
import Data.Attoparsec.Expr
@@ -44,9 +51,7 @@ constP p t = case parseOnly p t of
Right a -> return a
parseOf :: Parser Text -> Parser a -> Parser a
-parseOf ptxt pa = bothParse
- where
- bothParse = ptxt >>= constP pa
+parseOf ptxt pa = ptxt >>= constP pa
ignoreWS :: Parser a -> Parser a
ignoreWS a = do
@@ -55,11 +60,34 @@ ignoreWS a = do
skipSpace
return t
+matchHex :: Char -> Bool
+matchHex c = c == 'h' || c == 'H'
+
+--matchBin :: Char -> Bool
+--matchBin c = c == 'b' || c == 'B'
+
+matchDec :: Char -> Bool
+matchDec c = c == 'd' || c == 'D'
+
+--matchOct :: Char -> Bool
+--matchOct c = c == 'o' || c == 'O'
+
+-- | Parse a Number depending on if it is in a hex or decimal form. Octal and
+-- binary are not supported yet.
+parseNum :: Parser Expr
+parseNum = ignoreWS $ do
+ size <- decimal
+ _ <- "'"
+ matchNum size
+ where
+ matchNum size = (satisfy matchHex >> Number size <$> hexadecimal)
+ <|> (satisfy matchDec >> Number size <$> decimal)
+
parseTerm :: Parser Expr
parseTerm = (Concat <$> aroundList "{" "}" parseExpr)
<|> parseCond
<|> parseParens parseExpr
- <|> ignoreWS (Number 32 <$> decimal)
+ <|> parseNum
<?> "simple expr"
takeUntil :: Char -> Parser Text
@@ -68,12 +96,16 @@ takeUntil c = do
_ <- char c
return t
+-- | Parses the ternary conditional operator. It will behave in a right
+-- associative way.
parseCond :: Parser Expr
parseCond = do
x <- parseOf (takeUntil '?') parseExpr
y <- parseOf (takeUntil ':') parseExpr
Cond x y <$> parseExpr
+-- | Table of binary and unary operators that encode the right precedence for
+-- each.
parseTable :: [[Operator Text Expr]]
parseTable =
[ [ prefix "!" (UnOp UnLNot), prefix "~" (UnOp UnNot) ]
@@ -109,9 +141,6 @@ binary name fun = Infix ((string name <?> "binary") >> return fun)
prefix :: Text -> (a -> a) -> Operator Text a
prefix name fun = Prefix ((string name <?> "prefix") >> return fun)
-postfix :: Text -> (a -> a) -> Operator Text a
-postfix name fun = Postfix ((string name <?> "postfix") >> return fun)
-
commaSep :: Parser a -> Parser [a]
commaSep f = sepBy f (skipSpace *> char ',' *> skipSpace)