haskell

Haskell regular expression error "parse error on input ‘2’ [re|^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,64}$|]"


I'm using the PCRE library to validate the email in haskell.

Below is my code -


import ClassyPrelude
import Domain.Validation
import Text.Regex.PCRE.Heavy
import Control.Monad.Except

type Validation e a = a -> Maybe e

validate :: (a -> b) -> [Validation e a] -> a -> Either [e] b
validate constructor validations val = 
  case concatMap (\f -> maybeToList $ f val) validations of
    []    -> Right $ constructor val
    errs  -> Left errs
    
newtype Email = Email { emailRaw :: Text } deriving (Show, Eq, Ord)

rawEmail :: Email -> Text
rawEmail = emailRaw

mkEmail :: Text -> Either [Text] Email
mkEmail =
  validate Email
    [ regexMatches
        [re|^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,64}$|]
        "Not a valid email"
    ]

Below are my cabal settings -


    default-extensions: TemplateHaskell
                      , ConstraintKinds
                      , FlexibleContexts
                      , NoImplicitPrelude
                      , OverloadedStrings
                      , TemplateHaskell
    
    build-depends:    base ^>=4.21.0.0
                    , katip >= 0.8.8.2
                    , string-random == 0.1.4.4
                    , mtl
                    , data-has
                    , classy-prelude
                    , pcre-heavy
                    , time
                    , time-lens
                    
    hs-source-dirs:   src
    default-language: GHC2024

When I do cabal build, I get the below error -

cabal build
Resolving dependencies...
Build profile: -w ghc-9.12.2 -O1
In order, the following will be built (use -v for more details):
 - practical-web-dev-ghc-0.1.0.0 (lib) (first run)
 - practical-web-dev-ghc-0.1.0.0 (exe:practical-web-dev-ghc) (first run)
Configuring library for practical-web-dev-ghc-0.1.0.0...
Preprocessing library for practical-web-dev-ghc-0.1.0.0...
Building library for practical-web-dev-ghc-0.1.0.0...
[1 of 5] Compiling Domain.Validation ( src/Domain/Validation.hs, dist-newstyle/build/aarch64-osx/ghc-9.12.2/practical-web-dev-ghc-0.1.0.0/build/Domain/Validation.o, dist-newstyle/build/aarch64-osx/ghc-9.12.2/practical-web-dev-ghc-0.1.0.0/build/Domain/Validation.dyn_o )
[2 of 5] Compiling Domain.Auth      ( src/Domain/Auth.hs, dist-newstyle/build/aarch64-osx/ghc-9.12.2/practical-web-dev-ghc-0.1.0.0/build/Domain/Auth.o, dist-newstyle/build/aarch64-osx/ghc-9.12.2/practical-web-dev-ghc-0.1.0.0/build/Domain/Auth.dyn_o )
src/Domain/Auth.hs:42:57: error: [GHC-58481]
    parse error on input ‘2’
   |
42 |         [re|^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,64}$|]
   |                                                         ^

[4 of 5] Compiling Logger           ( src/Logger.hs, dist-newstyle/build/aarch64-osx/ghc-9.12.2/practical-web-dev-ghc-0.1.0.0/build/Logger.o, dist-newstyle/build/aarch64-osx/ghc-9.12.2/practical-web-dev-ghc-0.1.0.0/build/Logger.dyn_o )
[5 of 5] Compiling MyLib            ( src/MyLib.hs, dist-newstyle/build/aarch64-osx/ghc-9.12.2/practical-web-dev-ghc-0.1.0.0/build/MyLib.o, dist-newstyle/build/aarch64-osx/ghc-9.12.2/practical-web-dev-ghc-0.1.0.0/build/MyLib.dyn_o )
Error: [Cabal-7125]
Failed to build practical-web-dev-ghc-0.1.0.0 (which is required by exe:practical-web-dev-ghc from practical-web-dev-ghc-0.1.0.0).

Note: The haskell version I'm using is

$ ghc --version
  The Glorious Glasgow Haskell Compilation System, version 9.12.2

This is example from Practical Web Development with Haskell and the project is in github here


Solution

  • You need the QuasiQuotes extension. You can turn it on by adding it to the default-extensions field of your cabal file or by adding {-# Language QuasiQuotes #-} to the top of your Haskell file.