forked from RefactoringTools/HaRe
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
load files with TH and QuasiQuotation
- Loading branch information
Alan Zimmerman
committed
Mar 9, 2014
1 parent
4c23cb2
commit 9dda4b6
Showing
6 changed files
with
132 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,27 @@ | |||
{-# LANGUAGE TemplateHaskell #-} | |||
{-# LANGUGE QuasiQuotes #-} | |||
|
|||
{- Main.hs -} | |||
module TH.Main where | |||
|
|||
-- Import our template "pr" | |||
import TH.Printf | |||
|
|||
-- The splice operator $ takes the Haskell source code | |||
-- generated at compile time by "pr" and splices it into | |||
-- the argument of "putStrLn". | |||
main = putStrLn ( $(pr "Hello") ) | |||
|
|||
-- import Control.Lens | |||
-- data Foo a = Foo { _fooArgs :: [String], _fooValue :: a } | |||
-- makeLenses ''Foo | |||
|
|||
-- main = putStrLn "hello" | |||
|
|||
-- longString = [str| hello |] | |||
|
|||
|
|||
baz = 'a' | |||
|
|||
sillyString = [e|baz|] | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,50 @@ | |||
{-# LANGUAGE TemplateHaskell #-} | |||
{-# LANGUAGE QuasiQuotes #-} | |||
{- Printf.hs -} | |||
module TH.Printf where | |||
|
|||
-- Skeletal printf from the paper. | |||
-- It needs to be in a separate module to the one where | |||
-- you intend to use it. | |||
|
|||
-- Import some Template Haskell syntax | |||
-- import Language.Haskell.THSyntax | |||
import Language.Haskell.TH | |||
import Language.Haskell.TH.Quote | |||
import qualified Language.Haskell.TH as TH | |||
|
|||
-- Describe a format string | |||
data Format = D | S | L String | |||
|
|||
-- Parse a format string. This is left largely to you | |||
-- as we are here interested in building our first ever | |||
-- Template Haskell program and not in building printf. | |||
parse :: String -> [Format] | |||
parse s = [ L s ] | |||
|
|||
-- Generate Haskell source code from a parsed representation | |||
-- of the format string. This code will be spliced into | |||
-- the module which calls "pr", at compile time. | |||
gen :: [Format] -> ExpQ | |||
gen [D] = [| \n -> show n |] | |||
gen [S] = [| \s -> s |] | |||
gen [L s] = stringE s | |||
|
|||
-- Here we generate the Haskell code for the splice | |||
-- from an input format string. | |||
pr :: String -> ExpQ | |||
pr s = gen (parse s) | |||
|
|||
-- str :: QuasiQuoter | |||
-- str = QuasiQuoter { quoteExp = stringE } | |||
|
|||
silly :: QuasiQuoter | |||
silly = QuasiQuoter { quoteExp = \_ -> [| "yeah!!!" |] } | |||
|
|||
silly2 :: QuasiQuoter | |||
silly2 = QuasiQuoter { quoteExp = \_ -> stringE "yeah!!!" | |||
, quotePat = undefined | |||
, quoteType = undefined | |||
, quoteDec = undefined | |||
} | |||
|