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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
module CIParser(parse) where
import Data.List
import Data.Char
import ParseTypes
s_ident :: Char
s_ident = '`'
e_ident :: Char
e_ident = '\''
min_number :: Int
min_number = 2
s_block_comment :: String
= "{-"
e_block_comment :: String
= "-}"
s_line_comment :: String
= "--"
err_missing_quote :: String
err_missing_quote = "Missing corresponding closing quote!"
err_missing_block :: String
err_missing_block = "Missing corresponding closing comment block delimiter "
++ e_block_comment ++ " !"
err_missing_integ :: String
err_missing_integ = "Integrated code not terminated with correct number " ++
"of " ++ [e_ident] ++ " chars"
err_no_langtag :: String
err_no_langtag = "Missing language tag for integrated code!"
err_layout_code :: String
err_layout_code = "Bad layout. Check amount of white spaces!"
parse :: Filename -> String -> IO (PM [StandardToken])
parse fn input = return $ bindPM (parserL1 (initPos fn) input) parserL2
data L1Token = Normal Pos String
| Exp Pos
Int
String
parserL1 :: Pos -> String -> PM [L1Token]
parserL1 p s = parserL1Iter "" p p s
parserL1Iter :: String -> Pos -> Pos -> String -> PM [L1Token]
parserL1Iter acc accP _ "" =
cleanPM (ifThenElse (null acc) [] [Normal accP (reverse acc)])
parserL1Iter acc accP p s@(c:cs)
| c == '"' = passThrough parseQuotation (movePosByChar p '"' ) cs
| c == '\'' = passThrough parseSingleQuote (movePosByChar p '\'') cs
| otherwise =
let (isprefco,rstco) = isPrefixAndDrop s_line_comment s
in if isprefco
then passThrough parseLineComment (movePosByString p s_line_comment)
rstco
else
let (isprefbc,rstbc) = isPrefixAndDrop s_block_comment s
in if isprefbc
then passThrough parseBlockComment
(movePosByString p s_block_comment) rstbc
else
let (n,r) = countAndDrop s_ident s
in if (min_number <= n)
then passThroughInt parseIntegrated
(movePosByString p (replicate n s_ident)) n r
else parserL1Iter (c:acc) accP (movePosByChar p c) cs
where
passThrough f np st =
if (null acc) then f p np st
else liftPM ((:) (Normal accP (reverse acc))) $ f p np st
passThroughInt f np i st =
if (null acc) then f p np i st
else liftPM ((:) (Normal accP (reverse acc))) $ f p np i st
isPrefixAndDrop :: Eq a => [a] -> [a] -> (Bool,[a])
isPrefixAndDrop [] [] = (True,[])
isPrefixAndDrop (_:_) [] = (False,[])
isPrefixAndDrop [] (c:cs) = (True,(c:cs))
isPrefixAndDrop (c:cs) (d:ds) | c == d = isPrefixAndDrop cs ds
| otherwise = (False,(d:ds))
countAndDrop :: Eq a => a -> [a] -> (Int,[a])
countAndDrop c s = countAndDropIter 0 s
where
countAndDropIter n [] = (n,[])
countAndDropIter n (c1:cs) | c == c1 = countAndDropIter (n+1) cs
| otherwise = (n,(c1:cs))
parseQuotation :: Pos -> Pos -> String -> PM [L1Token]
parseQuotation accP p s =
let (qu,rst,fnd,pos) = findUnescapedChar p '"' '\\' s
in if fnd then liftPM ((:) (Normal accP ('"' :qu))) $ parserL1 pos rst
else throwPM accP err_missing_quote
findUnescapedChar :: Pos -> Char -> Char -> String -> (String,String,Bool,Pos)
findUnescapedChar pos ch escaper s = findUnescapedCharHelper pos "" s
where
findUnescapedCharHelper p acc "" = (reverse acc,"",False,p)
findUnescapedCharHelper p acc (c:[])
| c == ch = (reverse (c:acc),[],True,movePosByChar p c)
| c /= ch = (reverse (c:acc),[],False,movePosByChar p c)
findUnescapedCharHelper p acc (c:d:ds)
| c == ch =
(reverse (c:acc),(d:ds),True,movePosByChar p c)
| c /= ch && c == escaper =
findUnescapedCharHelper (movePosByString p [d,c]) (d:c:acc) ds
| otherwise =
findUnescapedCharHelper (movePosByChar p c) (c:acc) (d:ds)
parseSingleQuote :: Pos -> Pos -> String -> PM [L1Token]
parseSingleQuote accP _ "" = cleanPM [Normal accP "'" ]
parseSingleQuote accP p s@(c:_) =
case s of
('\\':d:'\'':rst) ->
liftPM ((:) (Normal accP ['\'','\\',d,'\'']))
$ parserL1 (moveAbs (moveCol p 4) 4) rst
(_:'\'':rst) ->
liftPM ((:) (Normal accP ['\'',c,'\'']))
$ parserL1 (moveAbs (moveCol p 3) 3) rst
_ -> liftPM ((:) (Normal accP "'" )) $ parserL1 p s
parseLineComment :: Pos -> Pos -> String -> PM [L1Token]
accP p s =
let (co,rst,fnd,pos) = findSequence p "\n" s
in if fnd
then liftPM ((:) (Normal accP (s_line_comment ++ co)))
$ parserL1 pos rst
else cleanPM [Normal accP (s_line_comment ++ co)]
parseBlockComment :: Pos -> Pos -> String -> PM [L1Token]
accP p s =
let (blc,rst,fnd,pos) = findSequence p e_block_comment s
in if fnd
then liftPM ((:) (Normal accP (s_block_comment ++ blc)))
$ parserL1 pos rst
else throwPM accP err_missing_block
findSequence :: Pos -> String -> String -> (String,String,Bool,Pos)
findSequence p1 s1 s2 = findSequenceIter p1 [] s1 s2
where
findSequenceIter p acc [] [] = (reverse acc,[],True,p)
findSequenceIter p acc [] (c:cs) =
(reverse acc,(c:cs),True,p)
findSequenceIter p acc (_:_) [] = (reverse acc,[],False,p)
findSequenceIter p acc (c:cs) (d:ds)
| c == d = findSequenceIter (movePosByChar p d) (d:acc) cs ds
| otherwise = findSequenceIter (movePosByChar p d) (d:acc) s1 ds
parseIntegrated :: Pos -> Pos -> Int -> String -> PM [L1Token]
parseIntegrated accP p n s =
let (exp,rst,fnd,pos) = findReplicate p n e_ident s_ident s
in if fnd then liftPM ((:) (Exp accP n exp))$ parserL1 pos rst
else throwPM accP err_missing_integ
findReplicate :: Pos -> Int -> Char -> Char -> String -> (String,String,Bool,Pos)
findReplicate p1 n e_i s_i s = findReplicateIter p1 [] 0 s
where
findReplicateIter p accS accN [] =
if (accN == (-n)) then (s,s,False,p1)
else (reverse (drop accN accS),[],(accN == n),p)
findReplicateIter p accS accN (c2:cs) =
if (accN == n) then (reverse (drop accN accS),(c2:cs),True,p)
else
if (accN == (-n))
then (s,s,False,p1)
else
if (e_i == c2)
then
if (accN < 0)
then findReplicateIter (movePosByChar p c2)(c2:accS) 1 cs
else
findReplicateIter (movePosByChar p c2)(c2:accS) (accN+1) cs
else
if (s_i == c2)
then
if (accN < 0)
then findReplicateIter
(movePosByChar p c2) (c2:accS) (accN-1) cs
else findReplicateIter
(movePosByChar p c2) (c2:accS) (-1) cs
else findReplicateIter (movePosByChar p c2) (c2:accS) 0 cs
parserL2 :: [L1Token] -> PM [StandardToken]
parserL2 [] = cleanPM []
parserL2 (t:tks) | isNormal t = liftPM ((:) $ normalToStTk t) $ parserL2 tks
| otherwise =
bindPM (disassembleIntExp t) (\ptk -> liftPM ((:) ptk) $ parserL2 tks)
isNormal :: L1Token -> Bool
isNormal t =
case t of
(Normal _ _) -> True
_ -> False
normalToStTk :: L1Token -> StandardToken
normalToStTk (Normal p s) = StTk p p Nothing s
normalToStTk (Exp _ _ _) = failed
disassembleIntExp :: L1Token -> PM StandardToken
disassembleIntExp (Exp p i s) =
let
(langtag,rest1) = break isSpace s
(spaces,dsl) = span isSpace rest1
in
if (null langtag) then throwPM p err_no_langtag
else
let
posBeforeDSL = movePosByString p
((replicate i s_ident) ++ langtag ++ spaces)
offset = getCol posBeforeDSL
cleanDSL = removeOffset posBeforeDSL offset dsl
in bindPM cleanDSL
(\cDSL -> cleanPM (StTk p posBeforeDSL (Just langtag) cDSL))
disassembleIntExp (Normal _ _) = failed
removeOffset :: Pos -> Int -> String -> PM String
removeOffset p n s =
let linesOfDSL = lines s
in case linesOfDSL of
[] -> cleanPM ""
[st] -> cleanPM st
(x:xs) -> liftPM ((++) x) $ removeOffsetFromLines
(movePosByString p (x ++ "\n")) n xs
removeOffsetFromLines :: Pos -> Int -> [String] -> PM String
removeOffsetFromLines _ _ [] = cleanPM ""
removeOffsetFromLines p n (l:ls) =
let rmoffl = removeOffsetFromLine p n l
in bindPM rmoffl (\(np,s) -> liftPM ((++) ('\n':s)) $
removeOffsetFromLines
(movePosByChar np '\n') n ls)
removeOffsetFromLine :: Pos -> Int -> String -> PM (Pos,String)
removeOffsetFromLine pos i st = removeOffsetFromLineIter pos st
where
removeOffsetFromLineIter p s =
if (getCol p >= i) then cleanPM (p,s)
else case s of
[] -> cleanPM (p,"")
(c:cs) -> if (isWhiteSpace c)
then removeOffsetFromLineIter (movePosByChar p c) cs
else throwPM pos err_layout_code
isWhiteSpace :: Char -> Bool
isWhiteSpace c = c == ' ' || c == '\t'
|