@@ -40,6 +40,9 @@ module Streamly.Internal.Unicode.String
4040 -- $setup
4141
4242 str
43+
44+ -- * Internals
45+ , strWith
4346 ) where
4447
4548
@@ -92,30 +95,42 @@ strParser = Parser.many content Fold.toList
9295 -- order is important
9396 content = plainText <|> escHash <|> lineCont <|> var <|> plainHash
9497
95- strSegmentExp :: StrSegment -> Q Exp
96- strSegmentExp (StrText text) = stringE text
97- strSegmentExp (StrVar name) = do
98+ strSegmentExp ::
99+ (Q Exp -> Q Exp ) -> (Q Exp -> Q Exp ) -> StrSegment -> Q Exp
100+ strSegmentExp f _ (StrText text) = f (stringE text)
101+ strSegmentExp _ f (StrVar name) = do
98102 valueName <- lookupValueName name
99103 case valueName of
100- Just vn -> varE vn
104+ Just vn -> f ( varE vn)
101105 Nothing ->
102106 fail
103107 $ " str quote: Haskell symbol `" ++ name
104108 ++ " ` is not in scope"
105109
106- strExp :: [StrSegment ] -> Q Exp
107- strExp xs = appE [ | concat | ] $ listE $ map strSegmentExp xs
110+ strExp :: Q Exp -> ( Q Exp -> Q Exp ) -> ( Q Exp -> Q Exp ) -> [StrSegment ] -> Q Exp
111+ strExp c f g xs = appE c $ listE $ map ( strSegmentExp f g) xs
108112
109113parseStr :: String -> Either ParseError [StrSegment ]
110114parseStr = runIdentity . Stream. parse strParser . Stream. fromList
111115
112- expandVars :: String -> Q Exp
113- expandVars input =
116+ expandVars :: Q Exp -> ( Q Exp -> Q Exp ) -> ( Q Exp -> Q Exp ) -> String -> Q Exp
117+ expandVars c f g input =
114118 case parseStr input of
115- Left e ->
116- fail $ " str QuasiQuoter parse error: " ++ displayException e
117- Right x ->
118- strExp x
119+ Left e -> fail $ " str QuasiQuoter parse error: " ++ displayException e
120+ Right x -> strExp c f g x
121+
122+ strWith :: Q Exp -> (Q Exp -> Q Exp ) -> (Q Exp -> Q Exp ) -> QuasiQuoter
123+ strWith c f g =
124+ QuasiQuoter
125+ { quoteExp = expandVars c f g
126+ , quotePat = notSupported
127+ , quoteType = notSupported
128+ , quoteDec = notSupported
129+ }
130+
131+ where
132+
133+ notSupported = error " str: Not supported."
119134
120135-- | A QuasiQuoter that treats the input as a string literal:
121136--
@@ -145,14 +160,4 @@ expandVars input =
145160-- "hello world!"
146161--
147162str :: QuasiQuoter
148- str =
149- QuasiQuoter
150- { quoteExp = expandVars
151- , quotePat = notSupported
152- , quoteType = notSupported
153- , quoteDec = notSupported
154- }
155-
156- where
157-
158- notSupported = error " str: Not supported."
163+ str = strWith [| concat | ] id id
0 commit comments