Debian Codegraph @

Text/RE/ZeInternals/Tools/Lex.lhs

raw
1 \begin{code}
2 {-# LANGUAGE NoImplicitPrelude #-}
3
4 module Text.RE.ZeInternals.Tools.Lex
5 ( alex
6 , alex'
7 ) where
8
9 import Prelude.Compat
10 import Text.RE.Replace
11 import Text.RE.ZeInternals.Types.IsRegex
12 \end{code}
13
14
15 \begin{code}
16 -- | a simple regex-based scanner interpreter for prototyping
17 -- scanners
18 alex :: IsRegex re s => [(re,Match s->Maybe t)] -> t -> s -> [t]
19 alex = alex' matchOnce
20
21 -- | a higher order version of 'alex' parameterised over the @matchOnce@
22 -- function
23 alex' :: Replace s
24 => (re->s->Match s)
25 -> [(re,Match s->Maybe t)]
26 -> t
27 -> s
28 -> [t]
29 alex' mo al t_err = loop
30 where
31 loop s = case lengthR s == 0 of
32 True -> []
33 False -> choose al s
34
35 choose [] _ = [t_err]
36 choose ((re,f):al') s = case mb_p of
37 Just (s',t) -> t : loop s'
38 _ -> choose al' s
39 where
40 mb_p = do
41 cap <- matchCapture mtch
42 case captureOffset cap == 0 of
43 True -> (,) (captureSuffix cap) <$> f mtch
44 False -> Nothing
45
46 mtch = mo re s
47 \end{code}