diff options
author | Yann Herklotz <git@yannherklotz.com> | 2019-09-04 20:15:51 +1000 |
---|---|---|
committer | Yann Herklotz <git@yannherklotz.com> | 2019-09-04 20:15:51 +1000 |
commit | a2b01b92612a098673ff03890e6e8aef4ceb28ea (patch) | |
tree | 15cafe6ba47981938552a4b342a56795251aadc5 /src/VeriSmith/Verilog/Quote.hs | |
parent | cccb665ebac6e916c4f961eacbe11a9af7d7ceb3 (diff) | |
download | verismith-a2b01b92612a098673ff03890e6e8aef4ceb28ea.tar.gz verismith-a2b01b92612a098673ff03890e6e8aef4ceb28ea.zip |
Renaming to VeriSmith
Diffstat (limited to 'src/VeriSmith/Verilog/Quote.hs')
-rw-r--r-- | src/VeriSmith/Verilog/Quote.hs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/VeriSmith/Verilog/Quote.hs b/src/VeriSmith/Verilog/Quote.hs new file mode 100644 index 0000000..3815fe6 --- /dev/null +++ b/src/VeriSmith/Verilog/Quote.hs @@ -0,0 +1,50 @@ +{-| +Module : VeriSmith.Verilog.Quote +Description : QuasiQuotation for verilog code in Haskell. +Copyright : (c) 2019, Yann Herklotz Grave +License : GPL-3 +Maintainer : yann [at] yannherklotz [dot] com +Stability : experimental +Portability : POSIX + +QuasiQuotation for verilog code in Haskell. +-} + +{-# LANGUAGE TemplateHaskell #-} + +module VeriSmith.Verilog.Quote + ( verilog + ) +where + +import Data.Data +import qualified Data.Text as T +import qualified Language.Haskell.TH as TH +import Language.Haskell.TH.Quote +import Language.Haskell.TH.Syntax +import VeriSmith.Verilog.Parser + +liftDataWithText :: Data a => a -> Q Exp +liftDataWithText = dataToExpQ $ fmap liftText . cast + +liftText :: T.Text -> Q Exp +liftText txt = AppE (VarE 'T.pack) <$> lift (T.unpack txt) + +-- | Quasiquoter for verilog, so that verilog can be written inline and be +-- parsed to an AST at compile time. +verilog :: QuasiQuoter +verilog = QuasiQuoter + { quoteExp = quoteVerilog + , quotePat = undefined + , quoteType = undefined + , quoteDec = undefined + } + +quoteVerilog :: String -> TH.Q TH.Exp +quoteVerilog s = do + loc <- TH.location + let pos = T.pack $ TH.loc_filename loc + v <- case parseVerilog pos (T.pack s) of + Right e -> return e + Left e -> fail $ show e + liftDataWithText v |