<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<pre><font color=Blue>{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}</font>
<font color=Blue>{-|
Module : Text.Regex.PCRE.QQ
Copyright : (c) Matt Morrow 2008
License : BSD3
Maintainer : mjm2002@gmail.com
Stability : unstable
Portability : non-portable (GHC QuasiQuotes)
A quasiquoter for Text.Regex.PCRE regexes.
This makes use of a new GHC extension known as QuasiQuotes.
See the README for the temporary location of the docs for
Language.Haskell.TH.Quote. See the EXAMPLES file for examples.
> ghci> [$rx|([aeiou]).*(er|ing|tion)([\.,\?]*)$|] "helloing.!?!?!"
> Just ["elloing.!?!?!","e","ing",".!?!?!"]
-}</font>
<font color=Green><u>module</u></font> Text<font color=Cyan>.</font>Regex<font color=Cyan>.</font>PCRE<font color=Cyan>.</font>QQ <font color=Cyan>(</font>
rx <font color=Blue>-- the regex QuasiQuoter</font>
<font color=Cyan>,</font> regexToExpQ <font color=Blue>-- String -> ExpQ</font>
<font color=Cyan>,</font> regexToPatQ <font color=Blue>-- String -> PatQ</font>
<font color=Blue>-- should these be being exported here??</font>
<font color=Cyan>,</font> <font color=Green><u>module</u></font> Language<font color=Cyan>.</font>Haskell<font color=Cyan>.</font>TH<font color=Cyan>.</font>Quote
<font color=Cyan>,</font> <font color=Green><u>module</u></font> Language<font color=Cyan>.</font>Haskell<font color=Cyan>.</font>TH<font color=Cyan>.</font>Syntax
<font color=Cyan>,</font> <font color=Green><u>module</u></font> Language<font color=Cyan>.</font>Haskell<font color=Cyan>.</font>TH<font color=Cyan>.</font>Lib
<font color=Cyan>,</font> <font color=Green><u>module</u></font> Language<font color=Cyan>.</font>Haskell<font color=Cyan>.</font>TH<font color=Cyan>.</font>Ppr
<font color=Cyan>)</font> <font color=Green><u>where</u></font>
<font color=Green><u>import</u></font> Language<font color=Cyan>.</font>Haskell<font color=Cyan>.</font>TH<font color=Cyan>.</font>Quote
<font color=Green><u>import</u></font> Language<font color=Cyan>.</font>Haskell<font color=Cyan>.</font>TH<font color=Cyan>.</font>Syntax
<font color=Green><u>import</u></font> Language<font color=Cyan>.</font>Haskell<font color=Cyan>.</font>TH<font color=Cyan>.</font>Lib
hiding <font color=Cyan>(</font>match<font color=Cyan>)</font>
<font color=Green><u>import</u></font> Language<font color=Cyan>.</font>Haskell<font color=Cyan>.</font>TH<font color=Cyan>.</font>Ppr
<font color=Green><u>import</u></font> Text<font color=Cyan>.</font>Regex<font color=Cyan>.</font>PCRE<font color=Cyan>.</font>Light
<font color=Cyan>(</font>Regex<font color=Cyan>,</font>PCREOption<font color=Cyan>,</font>PCREExecOption<font color=Cyan>)</font>
<font color=Green><u>import</u></font> <font color=Green><u>qualified</u></font>
Text<font color=Cyan>.</font>Regex<font color=Cyan>.</font>PCRE<font color=Cyan>.</font>Light <font color=Green><u>as</u></font> PCRE
<font color=Green><u>import</u></font> Data<font color=Cyan>.</font>ByteString <font color=Cyan>(</font>ByteString<font color=Cyan>)</font>
<font color=Green><u>import</u></font> <font color=Green><u>qualified</u></font> Data<font color=Cyan>.</font>ByteString <font color=Green><u>as</u></font> B
<font color=Green><u>import</u></font> Data<font color=Cyan>.</font>ByteString<font color=Cyan>.</font>Internal <font color=Cyan>(</font>c2w<font color=Cyan>,</font>w2c<font color=Cyan>)</font>
<font color=Blue>-----------------------------------------------------------------------------</font>
<font color=Blue>{- data QuasiQuoter
= QuasiQuoter { quoteExp :: String -> Q Exp,
quotePat :: String -> Q Pat}
-- Defined in Language.Haskell.TH.Quote -}</font>
<font color=Blue>{- |
> ghci> maybe [] tail $ [$rx|^([+-])?([0-9]+)\.([0-9]+)|] (show $ negate pi)
> ["-","3","141592653589793"]
-}</font>
<font color=Blue>rx</font> <font color=Red>::</font> QuasiQuoter
<font color=Blue>rx</font> <font color=Red>=</font> QuasiQuoter
regexToExpQ
regexToPatQ
<font color=Blue>-- | Transform a string rep</font>
<font color=Blue>-- of a regex to an ExpQ. The</font>
<font color=Blue>-- resulting ExpQ, when spliced,</font>
<font color=Blue>-- results in a function of type</font>
<font color=Blue>-- @String -> Maybe [String]@,</font>
<font color=Blue>-- where the input is the String</font>
<font color=Blue>-- to match on. The result is</font>
<font color=Blue>-- Nothing on error, and Just</font>
<font color=Blue>-- a list of results on success.</font>
<font color=Blue>-- Note: I'm packing\/unpacking\/...</font>
<font color=Blue>-- the ByteString unnecessarily</font>
<font color=Blue>-- for convenience in testing</font>
<font color=Blue>-- out the first go at this.</font>
<font color=Blue>-- This will be dealt with in</font>
<font color=Blue>-- the future.</font>
<font color=Blue>regexToExpQ</font> <font color=Red>::</font> String <font color=Red>-></font> ExpQ
<font color=Blue>regexToExpQ</font> s <font color=Red>=</font> <font color=Green><u>case</u></font> PCRE<font color=Cyan>.</font>compileM <font color=Cyan>(</font>pack s<font color=Cyan>)</font> pcreOpts <font color=Green><u>of</u></font>
Left err <font color=Red>-></font> error err
<font color=Blue>-- Give an error at compile time</font>
<font color=Blue>-- if the regex string is invalid</font>
Right <font color=Green><u>_</u></font> <font color=Red>-></font> <font color=Red>[</font><font color=Red>|</font>
<font color=Cyan>(</font>return <font color=Cyan>.</font> fmap unpack <font color=Cyan>=<<</font><font color=Cyan>)</font>
<font color=Cyan>.</font> match <font color=Cyan>(</font>regex s<font color=Cyan>)</font> <font color=Cyan>.</font> pack
<font color=Red>|</font><font color=Red>]</font>
<font color=Blue>-- | Transform a string (presumably)</font>
<font color=Blue>-- containing a regex to a PatQ.</font>
<font color=Blue>-- NOTE: Given a regex, a pattern</font>
<font color=Blue>-- is constructed which matches</font>
<font color=Blue>-- a literal string containing</font>
<font color=Blue>-- the verbatim regex. It does</font>
<font color=Blue>-- this because I couldn't think</font>
<font color=Blue>-- of anything better for it to</font>
<font color=Blue>-- do off the cuff. This needs</font>
<font color=Blue>-- thought.</font>
<font color=Blue>regexToPatQ</font> <font color=Red>::</font> String <font color=Red>-></font> PatQ
<font color=Blue>regexToPatQ</font> <font color=Red>=</font>
litP <font color=Cyan>.</font> stringL
<font color=Cyan>.</font> dropWhile <font color=Cyan>(</font><font color=Cyan>/=</font><font color=Magenta>'"'</font><font color=Cyan>)</font>
<font color=Cyan>.</font> show <font color=Cyan>.</font> regex
<font color=Blue>-- | Regex compilation. Temporarily uses</font>
<font color=Blue>-- hardcoded options @extended@ and</font>
<font color=Blue>-- @multiline@.</font>
<font color=Blue>regex</font> <font color=Red>::</font> String <font color=Red>-></font> Regex
<font color=Blue>regex</font> <font color=Red>=</font> flip PCRE<font color=Cyan>.</font>compile pcreOpts <font color=Cyan>.</font> pack
<font color=Blue>-- | Regex matching.</font>
<font color=Blue>match</font> <font color=Red>::</font> Regex <font color=Red>-></font> B<font color=Cyan>.</font>ByteString <font color=Red>-></font> Maybe <font color=Red>[</font>B<font color=Cyan>.</font>ByteString<font color=Red>]</font>
<font color=Blue>match</font> rx <font color=Red>=</font> flip <font color=Cyan>(</font>PCRE<font color=Cyan>.</font>match rx<font color=Cyan>)</font> pcreExecOpts
<font color=Blue>-- | Adjust these to your tastes. An</font>
<font color=Blue>-- interface to options seems to be</font>
<font color=Blue>-- the next logical (and practical)</font>
<font color=Blue>-- step.</font>
<font color=Blue>pcreOpts</font> <font color=Red>::</font> <font color=Red>[</font>PCREOption<font color=Red>]</font>
<font color=Blue>pcreOpts</font> <font color=Red>=</font>
<font color=Red>[</font> PCRE<font color=Cyan>.</font>extended
<font color=Cyan>,</font> PCRE<font color=Cyan>.</font>multiline <font color=Red>]</font>
<font color=Blue>-- , dotall, caseless, utf8</font>
<font color=Blue>-- , newline_any, PCRE.newline_crlf ]</font>
<font color=Blue>-- | Adjust these to your tastes. An</font>
<font color=Blue>-- interface to options seems to be</font>
<font color=Blue>-- the next logical (and practical)</font>
<font color=Blue>-- step.</font>
<font color=Blue>pcreExecOpts</font> <font color=Red>::</font> <font color=Red>[</font>PCREExecOption<font color=Red>]</font>
<font color=Blue>pcreExecOpts</font> <font color=Red>=</font> []
<font color=Blue>-- [ PCRE.exec_newline_crlf</font>
<font color=Blue>-- , exec_newline_any, PCRE.exec_notempty</font>
<font color=Blue>-- , PCRE.exec_notbol, PCRE.exec_noteol ]</font>
<font color=Blue>pack</font> <font color=Red>::</font> String <font color=Red>-></font> ByteString
<font color=Blue>pack</font> <font color=Red>=</font> B<font color=Cyan>.</font>pack <font color=Cyan>.</font> fmap c2w
<font color=Blue>unpack</font> <font color=Red>::</font> ByteString <font color=Red>-></font> String
<font color=Blue>unpack</font> <font color=Red>=</font> fmap w2c <font color=Cyan>.</font> B<font color=Cyan>.</font>unpack
<font color=Blue>-----------------------------------------------------------------------------</font>
</pre>
</html>