Refactor the "str" code

This commit is contained in:
Harendra Kumar 2024-01-17 00:14:34 +05:30
parent 207f948d22
commit b39bed0669

View File

@ -39,14 +39,14 @@ module Streamly.Internal.Unicode.String
-- --
-- $setup -- $setup
str str
) where ) where
import Control.Applicative (Alternative(..)) import Control.Applicative (Alternative(..))
import Control.Exception (displayException) import Control.Exception (displayException)
import Data.Functor.Identity (runIdentity) import Data.Functor.Identity (runIdentity)
import Streamly.Internal.Data.Parser (Parser) import Streamly.Internal.Data.Parser (Parser, ParseError)
import Language.Haskell.TH import Language.Haskell.TH
import Language.Haskell.TH.Quote import Language.Haskell.TH.Quote
@ -106,9 +106,12 @@ strSegmentExp (StrVar name) = do
strExp :: [StrSegment] -> Q Exp strExp :: [StrSegment] -> Q Exp
strExp xs = appE [| concat |] $ listE $ map strSegmentExp xs strExp xs = appE [| concat |] $ listE $ map strSegmentExp xs
parseStr :: String -> Either ParseError [StrSegment]
parseStr = runIdentity . Stream.parse strParser . Stream.fromList
expandVars :: String -> Q Exp expandVars :: String -> Q Exp
expandVars ln = expandVars input =
case runIdentity $ Stream.parse strParser (Stream.fromList ln) of case parseStr input of
Left e -> Left e ->
fail $ "str QuasiQuoter parse error: " ++ displayException e fail $ "str QuasiQuoter parse error: " ++ displayException e
Right x -> Right x ->