1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
------------------------------------------------------------------------------
--- A Regex Parser
---
--- @author Jasper Sikorra
--- @version July 2017
------------------------------------------------------------------------------

{-# OPTIONS_FRONTEND -Wno-missing-signatures -Wno-incomplete-patterns #-}

module RegexParser(parse) where

import Parser
import Data.Char
import Data.List
import Numeric

import ParseTypes

--- The parse function allows the translation of extended regular expression
--- to normal Curry code.
--- @param po - The position of the ERE code
--- @param st - The ERE code
--- @return A string containg normal curry code with the same semantics as the
---         original ERE code
parse :: LangParser
parse po st = return (liftPM (\p -> "(" ++ showRegex p ++ ")")
                             (parsen po (lex st)))

--- The function showRegex is used to generate a string containing the
--- target code
showRegex :: Regex -> String
showRegex []     = "[]"
showRegex re@(_:_) = "[" ++ (init $ showRegexHelper re) ++ "]"

showRegexHelper :: Regex -> String
showRegexHelper []     = ""
showRegexHelper (r:rs) = case r of
    Literal s     -> "Literal (" ++ s ++ ")," ++ showRegexHelper rs
    Xor x1 x2     -> "Xor (" ++ showRegex x1 ++ ") (" ++ showRegex x2 ++ "),"
                      ++ showRegexHelper rs
    Star x        -> "Star (" ++ showRegex x ++ "),"  ++ showRegexHelper rs
    Plus x        -> "Plus (" ++ showRegex x ++ "),"  ++ showRegexHelper rs
    AnyLiteral    -> "AnyLiteral,"  ++ showRegexHelper rs
    Bracket x     -> "Bracket " ++ showEither x ++ "," ++ showRegexHelper rs
    NegBracket x  -> "NegBracket " ++ showEither x ++ "," ++ showRegexHelper rs
    Start x       -> "Start (" ++ showRegex x ++ "),"  ++ showRegexHelper rs
    End x         -> "End (" ++ showRegex x ++ ")," ++ showRegexHelper rs
    Times (i,j) x -> "Times (" ++ show i ++ "," ++ show j ++ ") ("
                     ++ showRegex x ++ ")," ++ showRegexHelper rs
    _             -> show r ++ "," ++ showRegexHelper rs
  where
    showEither :: [Either String (String,String)] -> String
    showEither []     = ""
    showEither (x:xs) = showE "" (x:xs)
        where
          showE acc []     = "[" ++ (init acc) ++ "]"
          showE acc (y:ys) = case y of
            (Left s)        -> showE (acc ++ "Left (" ++ s ++ "),") ys
            (Right (s1,s2)) -> showE (acc ++ "Right " ++ "((" ++ s1 ++ "),("
                                     ++ s2 ++ "))" ++ ",") ys

--- An intermediate data type which can be used for code generation
type Regex = [ORegex]

data ORegex = Nil
           | Literal String
           | Xor Regex Regex
           | Star Regex
           | Plus Regex
           | AnyLiteral
           | Bracket [Either String (String,String)]
           | NegBracket [Either String (String,String)]
           | Start Regex
           | End Regex
           | Times (Int,Int) Regex
 deriving Show

--- Possible regex operators
operators       = ['|','*','.','[',']','^','$','{','}','(',')','?','+']
--- Characters which can be used to escape other characters
escapers        = ['\\']
--- Characters which aren't operators but still escapable
non_op_escp     = ['<','>','\n','\t','\\','-']
--- All characters which are escapable
escapable       = flip elem (non_op_escp ++ operators)
--- POSIX classes describing ranges
posixclasses    = [":alnum:",":alpha:",":blank:",":cntrl:",":digit:",
                   ":graph:",":lower:",":print:",":punct:",":space:",
                   ":upper:",":xdigit:"]

--- Converting POSIX classes to square bracket expressions
posixclassconv :: String -> [Either String (String,String)]
posixclassconv =  map eccToEss . posixclasscon
  where
    eccToEss :: Either Char (Char,Char) -> Either String (String,String)
    eccToEss (Left c) = Left (show c)
    eccToEss (Right (c1,c2)) = Right (show c1,show c2)

posixclasscon :: String -> [Either Char (Char,Char)]
posixclasscon str = case str of
  ":alnum:"   ->  [Right ('A','Z'), Right ('a','z'), Right ('0','9')]
  ":alpha:"   ->  [Right ('A','Z'), Right ('a','z')]
  ":blank:"   ->  [Left ' ', Left '\t']
  ":cntrl:"   ->  [Right ('\NUL','\US' ), Left '\DEL']
  ":digit:"   ->  [Right ('0','9')]
  ":graph:"   ->  [Right ('!'   ,'~'   )]
  ":lower:"   ->  [Right ('a','z')]
  ":print:"   ->  [Right (' '   ,'~'   )]
  ":punct:"   ->  [Left ']', Left '[', Left '!', Left '"',  Left '#',
                   Left '$', Left '%', Left '&', Left '\'', Left '(',
                   Left ')', Left '*', Left '+', Left ',' , Left '.',
                   Left '/', Left ':', Left ';', Left '<' , Left '=',
                   Left '>', Left '?', Left '@', Left '\\', Left '^',
                   Left '_', Left '`', Left '{', Left '|',  Left '}',
                   Left '~', Left '-']
  ":space:"   ->  [Left ' ', Left '\t', Left '\r', Left '\n',
                             Left '\v', Left '\f']
  ":upper:"   ->  [Right ('A','Z')]
  ":xdigit:"  ->  [Right ('A','F'), Right ('a','f'), Right ('0','9')]

--- Tokens for the lexer
data Token = TokenStar
           | TokenBar
           | TokenPoint
           | TokenDash
           | TokenDollar
           | TokenPlus
           | TokenOSBracket
           | TokenCSBracket
           | TokenORBracket
           | TokenCRBracket
           | TokenOCBracket
           | TokenCCBracket
           | TokenOABracket
           | TokenCABracket
           | TokenLiteral Char
 deriving Eq

--- Assigning Tokens to Chars
tokenToChar :: Token -> Char
tokenToChar t = case t of
  TokenStar       -> '*'
  TokenBar        -> '|'
  TokenPoint      -> '.'
  TokenDash       -> '^'
  TokenDollar     -> '$'
  TokenPlus       -> '+'
  TokenOSBracket  -> '['
  TokenCSBracket  -> ']'
  TokenORBracket  -> '('
  TokenCRBracket  -> ')'
  TokenOCBracket  -> '{'
  TokenCCBracket  -> '}'
  TokenOABracket  -> '<'
  TokenCABracket  -> '>'
  TokenLiteral c  ->  c

--- Lexer
lex :: String -> [Token]
lex ""  = []
lex str@(c:cs) = case str of
  ('*':_)    -> (TokenStar         :lex cs)
  ('|':_)    -> (TokenBar          :lex cs)
  ('.':_)    -> (TokenPoint        :lex cs)
  ('^':_)    -> (TokenDash         :lex cs)
  ('$':_)    -> (TokenDollar       :lex cs)
  ('?':_)    -> lex ('{':'0':',':'1':'}':cs)
  ('+':_)    -> (TokenPlus         :lex cs)
  ('[':_)    -> (TokenOSBracket    :lex cs)
  (']':_)    -> (TokenCSBracket    :lex cs)
  ('(':_)    -> (TokenORBracket    :lex cs)
  (')':_)    -> (TokenCRBracket    :lex cs)
  ('{':_)    -> (TokenOCBracket    :lex cs)
  ('}':_)    -> (TokenCCBracket    :lex cs)
  ('<':_)    -> (TokenOABracket    :lex cs)
  ('>':_)    -> (TokenCABracket    :lex cs)
  ('-':_)    -> (TokenLiteral '-'  :lex cs)
  ('\\':d:ds) -> if (escapable d) then
                    case d of
                      'n'       -> TokenLiteral '\n':lex ds
                      't'       -> TokenLiteral '\t':lex ds
                      _         -> TokenLiteral d   :lex ds
                  else (TokenLiteral '\\':lex cs)
  ('\n':_)   -> lex cs
  _           -> (TokenLiteral c    :lex cs)

--- Parser
parsen :: Pos -> [Token] -> PM Regex
parsen p tks = pars p (cleanPM []) tks

pars :: Pos -> PM Regex -> [Token] -> PM Regex
pars _ prr []     = prr
pars p prr (t:ts) = bindPM prr (\r ->
  case t of
      TokenStar        -> liftPM ((:) (Star r)) (parsen p ts)
      TokenBar         -> parseBar p r ts
      TokenPoint       -> liftPM ((++) r) (pars p (cleanPM [AnyLiteral]) ts)
      TokenDash        -> liftPM ((++) r) (parseDash p ts)
      TokenDollar      -> liftPM ((++) r) (parseDollar p ts)
      TokenPlus        -> liftPM ((:) (Plus r)) (parsen p ts)
      TokenOSBracket   -> liftPM ((++) r) (parseOSBracket p ts)
      TokenCSBracket   -> throwPM p "No '[' for ']' found"
      TokenORBracket   -> liftPM ((++) r) (parseRBracket p ts)
      TokenCRBracket   -> throwPM p "No '(' for ')' found"
      TokenOCBracket   -> parseCBracket p r ts
      TokenCCBracket   -> throwPM p "No '{' for '}' found"
      TokenOABracket   -> let prsrs = parseABracket p ts
                              prs   = fstPM prsrs
                              prrs  = sndPM prsrs
                           in bindPM prs (\s -> bindPM prrs (\rs -> liftPM
                                  ((++) r) (pars p (cleanPM [Literal s]) rs)))
      TokenCABracket   -> throwPM p "No '<' for '>' found"
      TokenLiteral c   -> liftPM ((++) r)
                                 (pars p (cleanPM [Literal (show c)]) ts))

--- Alternative
parseBar :: Pos -> Regex -> [Token] -> PM Regex
parseBar p r ts = liftPM (\x -> [Xor r x]) (parsen p ts)

--- Start
parseDash :: Pos -> [Token] -> PM Regex
parseDash p ts = liftPM (\li -> Start [head li]:tail li) (parsen p ts)

--- End
parseDollar :: Pos -> [Token] -> PM Regex
parseDollar p ts = liftPM (\li -> End [head li]:tail li) (parsen p ts)

--- Square Bracket (Range)
parseOSBracket :: Pos -> [Token] -> PM Regex
parseOSBracket p []     = throwPM p "Missing ']'"
parseOSBracket p (t:ts) = case t of
  TokenDash -> let (cont,rst) = case ts of
                    -- Handle closing square bracket directly after opening,
                    -- meaning the ']' char is an option
                    (TokenCSBracket:tss) ->
                      ((\(x,y) -> ((TokenCSBracket:x),y))
                       (span (\x -> x /= TokenCSBracket) tss))
                    _            -> span (\x -> x /= TokenCSBracket) ts
               in pars p (liftPM (\x -> [NegBracket x]) (squareParser p cont))
                         (tail rst)
  _         -> let (cont,rst) = case (t:ts) of
                    -- Handle closing square bracket directly after opening,
                    -- meaning the ']' char is an option
                    (TokenCSBracket:tss) ->
                      (\(x,y) -> ((TokenCSBracket:x),y))
                        (span (\x -> x /= TokenCSBracket) tss)
                    _           -> span (\x -> x /= TokenCSBracket)
                                                (t:ts)
               in pars p (liftPM (\x -> [Bracket x]) (squareParser p cont))
                         (tail rst)

squareParser :: Pos -> [Token] -> PM [Either String (String,String)]
squareParser pos toks =
      let chars  = extractChars toks
      in if (elem chars posixclasses)
          then cleanPM (posixclassconv chars)
          else rangeAndCharParser pos toks
  where
    rangeAndCharParser p tks = case tks of
      []
        -> cleanPM []
      (TokenLiteral a:TokenLiteral '-':TokenLiteral b:ts)
        -> liftPM (\rc -> ((:) (Right (show a,show b))) rc)
                  (rangeAndCharParser p ts)
      (TokenOABracket:ts)
        -> let prsrs = parseABracket p ts
               prs   = fstPM prsrs
               prrs  = sndPM prsrs
            in (bindPM prs $  \s  ->
                bindPM prrs $ \rs ->
              if (head rs == TokenLiteral '-')
                then
                  if (head (tail rs) == TokenOABracket)
                    then
                      let prs2rs2 = parseABracket p (tail (tail rs))
                          prs2    = fstPM prs2rs2
                          prrs2   = sndPM prs2rs2
                      in (bindPM prs2 $  \s2  ->
                          bindPM prrs2 $ \rs2 ->
                          liftPM ((:) (Right (s,s2))) $
                            rangeAndCharParser p rs2)
                    else liftPM ((:)
                            (Right (s,(\(TokenLiteral c) -> (show c))
                                          (head (tail rs)))))
                            (rangeAndCharParser p (tail (tail rs)))
                else liftPM ((:) (Left s)) (rangeAndCharParser p rs))
      _
        -> liftPM ((:) (Left (show $ tokenToChar $ head tks)))
                  (rangeAndCharParser p (tail tks))

--- Round bracket (Paranthesis)
parseRBracket :: Pos -> [Token] -> PM Regex
parseRBracket p ts = pars p (parsen p (init cont)) rst
  where
    (cont,rst) = splitAt (cntUntilClosed TokenORBracket TokenCRBracket ts) ts

--- Curly bracket (Multiple times)
parseCBracket :: Pos -> Regex -> [Token] -> PM Regex
parseCBracket p r ts = pars p (cleanPM [(Times (curlyParser (init cont)) r)])
                              rst
  where
    (cont,rst) = splitAt (cntUntilClosed TokenOCBracket TokenCCBracket ts) ts

curlyParser :: [Token] -> (Int,Int)
curlyParser tks = (fst fir,fst sec)
  where
    fir = case readNat (extractChars tks) of
      [v] -> v
      _   -> failed
    sec = case readNat (tail (snd fir)) of
      [v] -> v
      _   -> failed

--- Arrow bracket (Variable)
parseABracket :: Pos -> [Token] -> PM (String,[Token])
parseABracket = pOAB []
  where
    pOAB _ p []       = throwPM p "Missing '>'"
    pOAB acc p (t:ts) = case t of
      TokenCABracket   -> cleanPM (reverse acc,ts)
      _                -> pOAB (tokenToChar t:acc) p ts

--- Extract a char from a token (necessary for variables)
extractChars :: [Token] -> String
extractChars []     = ""
extractChars (t:ts) = case t of
  (TokenLiteral c) -> (c:extractChars ts)
  _                -> ""

-- | Helper
cntUntilClosed :: Eq a => a -> a -> [a] -> Int
cntUntilClosed c1 c2 li = cUCB 0 0 li
  where
    cUCB n c l =
      if (c < 0) then n else
        if (l == []) then failed else
          if ((head l) == c1) then cUCB (n+1) (c+1) (tail l) else
            if ((head l) == c2) then cUCB (n+1) (c-1) (tail l)
              else cUCB (n+1) c (tail l)