-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.hs
783 lines (706 loc) · 26.6 KB
/
parser.hs
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
module Parser where
import Control.Applicative as Applicative
import Control.Monad
import Data.Char
import Nodes
import Data.List
import Data.Void
import EitherUtility
import Text.Megaparsec as P
import Text.Megaparsec.Char
import MergeDefs
import qualified Text.Megaparsec.Char.Lexer as L
import Debug.Trace
lower = oneOf "abcdefghijklmnopqrstuvwxyz" :: Parser Char
upper = oneOf "ABCDEFGHIJKLMNOPQRSTUVWXYZ" :: Parser Char
digit = oneOf "1234567890" :: Parser Char
newline = oneOf "\n;" :: Parser Char
newlines = P.many Parser.newline
space = char ' '
spaces = P.many Parser.space
mspaces = Parser.space *> Parser.spaces
keyword k = Text.Megaparsec.Char.string (showL k) :: Parser String
notKeyword = try $ notFollowedBy $ choice keywords *> Text.Megaparsec.Char.string " " where
keywords = map (Text.Megaparsec.Char.string . showL) [Parser.If ..]
showL k = map toLower (show k)
data Keyword =
If
| Then
| Else
| True
| False
| Class
| End
| Where
| Include
| Open
| Mod
| Lib
| Def
| External
deriving(Show, Eq, Enum)
type Parser = Parsec Void String
eofString :: Parser String
eofString = "" <$ eof
string :: Char -> Parser Node
string c =
do
pos <- getSourcePos
str <- (char c *> manyTill L.charLiteral (char c)) :: Parser String
return $ StringNode str pos
number :: Parser Node
number =
do
pos <- getSourcePos
fs <- digit :: Parser Char
str <- P.many digit :: Parser String
return $ NumNode (fs : str) pos
fractional :: Parser Node
fractional =
do
pos <- getSourcePos
dec <- number
Text.Megaparsec.Char.string "." :: Parser String
frac <- number
return $ NumNode (extractString dec ++ "." ++ extractString frac) pos
identifier :: Bool -> Parser Node
identifier sQuote =
do
pos <- getSourcePos
notKeyword
a <- Text.Megaparsec.Char.string "$" <|> Text.Megaparsec.Char.string ""
fc <- lower
l <- if sQuote then P.many allowedPs <* char '\'' else P.many allowedPs
let el = (a ++ [fc]) ++ l
return $ IdentifierNode el pos
<|> (
(\pos str -> IdentifierNode ("{" ++ str ++ "}") pos)
<$> getSourcePos
<*> if sQuote then allSyms <* char '\'' else allSyms
)
<|> nsAccess
where
allSyms = Text.Megaparsec.Char.string "{" *> manyTill L.charLiteral (char '}')
allowedPs = lower <|> upper <|> digit <|> undersore
undersore = (
(char '_' :: Parser Char)
<* notFollowedBy (char '_' :: Parser Char)
) :: Parser Char
nsAccess =
do
t1 <- dataName
termSuffix t1
termSuffix t1 = try $ unTrySuffix t1
unTrySuffix t1 =
do
s <- singleSuffix t1
loop s
singleSuffix t1 =
do
pos <- getSourcePos
op <- Text.Megaparsec.Char.string "::"
t2 <- dataName <|> identifier Prelude.False
loop $ IdentifierNode (extractString t1 ++ "__" ++ extractString t2) pos
loop t = (unTrySuffix t <|> return t) :: Parser Node
undersoreIdentifier :: Parser Node
undersoreIdentifier = (flip IdentifierNode <$> getSourcePos <*> Text.Megaparsec.Char.string "_") <|> identifier Prelude.False
dataName :: Parser Node
dataName =
try nsDataAccess <|> dataNameFormula
where
dataNameFormula =
do
pos <- getSourcePos
a <- Text.Megaparsec.Char.string "$" <|> Text.Megaparsec.Char.string ""
fc <- upper
l <- P.many (lower <|> upper <|> digit)
return $ DataNode ((a ++ [fc]) ++ l) pos
nsDataAccess =
do
t1 <- dataNameFormula
termSuffix Nothing t1
where
termSuffix stp t1 = try $ unTrySuffix stp t1
unTrySuffix stp t1 =
do
s <- singleSuffix stp t1
loop stp s
singleSuffix stp t1 =
do
pos <- getSourcePos
op <- Text.Megaparsec.Char.string "::"
t2 <- dataNameFormula
x stp t1 t2 pos
loop :: Maybe Node -> Node -> Parser Node
loop stp t = case stp of
Just a -> return a
Nothing -> unTrySuffix Nothing t <|> return t :: Parser Node
x stp t1 t2 pos =
(
lookAhead (Text.Megaparsec.Char.string "::" :: Parser String) *>
loop stp v
) <|> loop (Just v) v where
v = DataNode (extractString t1 ++ "__" ++ extractString t2) pos
structInstanceExpr :: Parser Node
structInstanceExpr =
do
pos <- getSourcePos
id <- dataName
try (
do
Text.Megaparsec.Char.string "{"
ls <- spaces *> seqInstance <* spaces
Text.Megaparsec.Char.string "}"
return $ StructInstanceNode id ls Prelude.False pos
) <|> return (StructInstanceNode id [] Prelude.False pos)
where
seqInstance :: Parser [Node]
seqInstance = commaSep seqPair
seqPair :: Parser Node
seqPair =
do
id <- identifier Prelude.False
spaces
Text.Megaparsec.Char.string "::"
spaces
pos <- getSourcePos
thing <- expr
return $ DeclNode id thing pos
commaSep p = p `sepBy` (try (spaces *> Text.Megaparsec.Char.string ", " <* spaces) :: Parser String)
list :: Parser Node
list =
do
pos <- getSourcePos
Text.Megaparsec.Char.string "["
spaces
ls <- commaSep Parser.expr
spaces
Text.Megaparsec.Char.string "]"
return $ ListNode ls pos
tuple :: Parser Node
tuple =
do
pos <- getSourcePos
Text.Megaparsec.Char.string "("
spaces
ls <- commaSep Parser.expr
spaces
Text.Megaparsec.Char.string ")"
return $ TupleNode ls pos
where
commaSep p =
try (p `endBy` (spaces *> Text.Megaparsec.Char.string "," <* spaces :: Parser String))
<|> p `sepBy` (spaces *> Text.Megaparsec.Char.string "," <* spaces :: Parser String)
prefix = choice $ map try [
Parser.ifExpr,
Parser.lambdaExpr,
Parser.parens,
accessFuncExpr,
Parser.negExpr,
Parser.boolean,
Parser.tuple,
Parser.caseExpr,
Parser.list,
Parser.string '"',
Parser.fractional,
Parser.number,
Parser.containerFunction "(" ")" "," TupleNode,
Parser.containerFunction "[" "]" "," ListNode,
Parser.structInstanceExpr <* notFollowedBy (Text.Megaparsec.Char.string "::"),
Parser.identifier Prelude.False
]
atom =
do
pos <- getSourcePos
pre <- prefix
try
$ do
xId <- identifier Prelude.False
return $ CallNode xId [pre] pos
<|> (pre <$ Text.Megaparsec.Char.string "")
containerFunction :: String -> String -> String -> ([Node] -> P.SourcePos -> Node) -> Parser Node
containerFunction strt end sep f =
do
pos <- getSourcePos
Text.Megaparsec.Char.string strt
fstComma <- comma
commas <- P.many comma
Text.Megaparsec.Char.string end
let args = map (flip IdentifierNode pos . (\a -> "x" ++ show a)) [1 .. length commas + 2]
return $ FuncDefNode Nothing args (f args pos) Prelude.False pos
where comma = spaces *> Text.Megaparsec.Char.string sep <* spaces
typeRef =
do
pos <- getSourcePos
spaces
Text.Megaparsec.Char.string "&"
spaces
e <- dataName
spaces
return $ TypeRefNode e pos
lambdaExpr =
do
pos <- getSourcePos
hashes <- explicitHash Prelude.True
Text.Megaparsec.Char.string "\\"
spaces
try (fullLamba hashes pos) <|> basicLambda hashes pos
where
fullLamba h pos =
do
args <- undersoreIdentifier `sepBy1` (Text.Megaparsec.Char.string "," <* spaces)
spaces
Text.Megaparsec.Char.string "->"
spaces
e <- logicalExpr
return $ FuncDefNode Nothing args e h pos
basicLambda h pos =
do
e <- logicalExpr
return $ FuncDefNode Nothing [IdentifierNode "x" pos] e h pos
boolean =
do
pos <- getSourcePos
b <- keyword Parser.True <|> keyword Parser.False
return $ BoolNode b pos
parens =
do
Text.Megaparsec.Char.string "("
newlines
spaces
newlines
spaces
e <- whereExpr
newlines
spaces
newlines
spaces
char ')'
return e
rBinOp :: Parser Node -> Parser String -> Parser Node -> (Node -> String -> Node -> SourcePos -> Node) -> Parser Node
rBinOp fa ops fb ret =
do
pos <- getSourcePos
spaces
a <- fa
try (
do
spaces
op <- ops
spaces
b <- fb
return $ ret a op b pos
) <|> return a
binOp f ops ret = do
t1 <- f
loop t1
where termSuffix t1 = try (do
pos <- getSourcePos
spaces
op <- ops
spaces
t2 <- f
loop (ret t1 op t2 pos))
loop t = termSuffix t <|> return t
lhs =
do
id <- mainLhs
spaces
Text.Megaparsec.Char.string "<-"
return id
where
mainLhs = try fDef
<|> identifier Prelude.False
<|> flip TupleNode <$> getSourcePos <*> (
Text.Megaparsec.Char.string "(" *> spaces *>
undersoreIdentifier `sepBy` (Text.Megaparsec.Char.string "," <* spaces)
<* spaces <* Text.Megaparsec.Char.string ")"
)
<|> destructureExpr
destructureExpr =
do
pos <- getSourcePos
ls <- Text.Megaparsec.Char.string "{" *> spaces *>
undersoreIdentifier `sepBy1` (Text.Megaparsec.Char.string "," <* spaces)
<* spaces <* Text.Megaparsec.Char.string "}"
return $ DeStructure ls pos
fDef =
do
pos <- getSourcePos
callee <- identifier Prelude.False
spaces *> Text.Megaparsec.Char.string ":" <* spaces
args <- undersoreIdentifier `sepBy1` (Text.Megaparsec.Char.string "," <* spaces)
spaces
return $ CallNode callee args pos
structDef =
do
pos <- getSourcePos
id <- dataName
spaces
Text.Megaparsec.Char.string "<-"
spaces
try (structs id) <|> fields id pos
where
structs overarch =
do
pos <- getSourcePos
sts <- structInstanceExpr `sepBy1`
try (spaces *> newlines *> spaces *> Text.Megaparsec.Char.string "|" <* spaces <* newlines <* spaces)
let ls = zip3 (map extractMId sts) (map extractIds sts) (map extractStrict sts)
let defs = map (\(id, xs, stct) -> StructDefNode id xs stct (Just overarch) pos) ls
let fdefs = map makeFun defs
return $ MultipleDefinitionNode $ SumTypeNode defs pos : fdefs
lowId (DataNode id pos) = IdentifierNode (toLower (head id) : tail id) pos
lowId (IdentifierNode id pos) = IdentifierNode (map toLower id) pos
makeFun strct@(StructDefNode id xs _ _ pos) =
if null xs then
FromStruct $ DeclNode (lowId id) (instantiate xs strct) pos
else
FromStruct $ DeclNode (lowId id) (FuncDefNode (Just $ lowId id) xs (instantiate xs strct) Prelude.False pos) pos
instantiate rhss (StructDefNode id lhss b _ pos) = StructInstanceNode id (zipWith (\a b -> DeclNode a b pos) rhss lhss) b pos
extractIds strct@(StructInstanceNode _ ls _ _) = snd (extractStructInstance strct)
extractMId strct@(StructInstanceNode id _ _ _) = id
extractStrict strct@(StructInstanceNode _ _ stct _) = stct
fields id pos =
do
a <- (Text.Megaparsec.Char.string "!" <* spaces) <|> Text.Megaparsec.Char.string ""
ls <- Text.Megaparsec.Char.string "{" *> commaSep (identifier Prelude.False) <* Text.Megaparsec.Char.string "}"
let stDef = StructDefNode id ls (a /= "") Nothing pos
let fDef = makeFun stDef
return $ MultipleDefinitionNode $ stDef : [fDef]
structInstanceExpr =
se <|> e where
se =
do
pos <- getSourcePos
Text.Megaparsec.Char.string "!" <* spaces
ins <- structInstanceExpr
return $ StructInstanceNode (extractMId ins) (extractIds ins) Prelude.True pos
e =
do
pos <- getSourcePos
id <- dataName
try (
do
Text.Megaparsec.Char.string "{"
ls <- seqInstance
Text.Megaparsec.Char.string "}"
return $ StructInstanceNode id ls Prelude.False pos
) <|> return (StructInstanceNode id [] Prelude.False pos)
seqInstance = commaSep seqPair
seqPair = do
id <- identifier Prelude.False
IdentifierNode (extractString id) <$> getSourcePos
explicitHash :: Bool -> Parser Bool
explicitHash def = f <$> (Text.Megaparsec.Char.string "!" <|> Text.Megaparsec.Char.string "~" <|> Text.Megaparsec.Char.string "") where
f "!" = Prelude.True
f "~" = Prelude.False
f "" = def
decl =
do
pos <- getSourcePos
hashes <- explicitHash Prelude.False
id <- lhs
new_id <-
case id of
(CallNode c arg _) -> return c
_ -> return id
spaces
e <- whereExpr
new_e <-
case id of
(CallNode c arg _) -> return $ FuncDefNode (Just c) arg e hashes pos
_ -> return e
return $ DeclNode new_id new_e pos
includes iType =
do
pos <- getSourcePos
P.many Parser.newline
is <- include `sepBy` try (spaces *> newlines *> spaces *> lookAhead include <* spaces <* newlines <* spaces)
return $ ListNode is pos
where
include :: Parser Node
include =
do
pos <- getSourcePos
keyword iType
mspaces
Parser.string '"'
modStmnt =
do
pos <- getSourcePos
mname <- keyword Mod *> spaces *> dataName <* newlines <* spaces
ds <- (spaces *> newlines *> spaces *> (try mewMethod <|> try methodDecl <|> try classStmnt <|> try structDef <|> decl ))
`sepBy1` notFollowedBy (newlines *> spaces *> newlines *> (keyword End <|> eofString))
newlines *> spaces *> newlines *> (keyword End <|> eofString)
let tds = map (differLhs mname) ds
let flist = map (getDollar $ extractString mname) tds
return $ MultipleDefinitionNode flist
where
differLhs :: Node -> Node -> Node
differLhs mn (IdentifierNode id pos) = IdentifierNode (extractString mn ++ "__" ++ id) pos
differLhs mn (DataNode id pos) = DataNode (extractString mn ++ "__" ++ id) pos
differLhs mn (TupleNode ts pos) = TupleNode (map (differLhs mn) ts) pos
differLhs mn (DeclNode lhs rhs pos) = DeclNode (differLhs mn lhs) (changeFun mn rhs) pos
differLhs mn (StructDefNode id x st (Just o) pos) = StructDefNode (differLhs mn id) x st (Just $ differLhs mn o) pos
differLhs mn (StructDefNode id x stct Nothing pos) = StructDefNode (differLhs mn id) x stct Nothing pos
differLhs mn (DeStructure ids pos) = DeStructure (map (differLhs mn) ids) pos
differLhs mn (SumTypeNode ds pos) = SumTypeNode (map (differLhs mn) ds) pos
differLhs mn (MethodNode id args pos) = MethodNode (differLhs mn id) args pos
differLhs mn (MultipleDefinitionNode ds) = MultipleDefinitionNode $ map (differLhs mn) ds
differLhs mn (FromStruct (DeclNode lhs (FuncDefNode (Just id) args (StructInstanceNode sid sargs b spos) h pos) dpos)) =
FromStruct $
DeclNode (differLhs mn lhs)
(FuncDefNode (Just $ differLhs mn id) args (StructInstanceNode (differLhs mn sid) sargs b spos) h pos)
dpos
differLhs mn (FromStruct (DeclNode lhs (StructInstanceNode sid sargs b spos) pos)) =
FromStruct $
DeclNode (differLhs mn lhs)
(StructInstanceNode (differLhs mn sid) sargs b spos)
pos
differLhs mn nm@NewMethodNode{} = nm
differLhs _ a = error(show a ++ "\n")
changeFun mn (FuncDefNode (Just id) args e h pos) = FuncDefNode (Just $ differLhs mn id) args e h pos
changeFun mn n = n
externals =
do
pos <- getSourcePos
keyword External
libName <- spaces *> Parser.string '"' <* spaces
exts <- Text.Megaparsec.Char.string "{" *> spaces *> commaSep (identifier Prelude.False) <* spaces <* Text.Megaparsec.Char.string "}"
return $ ExternalNode libName exts pos
decls xs =
do
pos <- getSourcePos
P.many Parser.newline
a <- includes Lib
P.many Parser.newline
b <- includes Mod
exts <- newlines *> spaces *> P.many (externals <* spaces <* newlines)
P.many Parser.newline
spaces
dcs <-
(++ exts) <$>
(pref *>
(try mewMethod <|> try methodDecl <|> try classStmnt <|> try structDef <|> decl <|> modStmnt))
`endBy` (eofString <|> (spaces *> Parser.newline *> P.many Parser.newline <* spaces :: Parser String))
return $ ProgramNode (concatLists dcs $ getLists xs) pos
where
pref =
P.many (Text.Megaparsec.Char.string "--" *> manyTill (L.charLiteral <|> char '\\') (char '\n' :: Parser Char) *> P.many (Text.Megaparsec.Char.string "\n"))
<|> ((\a -> [[a]]) <$> Text.Megaparsec.Char.string "")
getLists ns = map extractList ns
concatLists dcs [] = dcs
concatLists dcs xs = Data.List.concat xs ++ dcs
whereExpr =
do
rns <- expr
try (
do
pos <- getSourcePos
mspaces
keyword Where
spaces
ds <- (newlines *> spaces *> (try classStmnt <|> try decl)) `sepBy1` notFollowedBy (spacificSpaces *> keyword End)
spacificSpaces *> keyword End
return $ WhereNode rns ds pos
) <|> return rns
where
spacificSpaces = (spaces *> newlines *> spaces) <|> (spaces *> Parser.newline *> newlines *> spaces)
expr =
do
pos <- getSourcePos
xs pos
where
spacificSpaces = (spaces *> newlines *> spaces) <|> (Parser.newline *> newlines *> spaces)
xs :: SourcePos -> Parser Node
xs pos = backExpr `sepBy1` try (spaces *> Text.Megaparsec.Char.string "|>" <* spaces) >>= \l -> return $ foldr (\a b -> CallNode a [b] pos) (head l) (reverse $ tail l)
backExpr =
do
pos <- getSourcePos
bs pos
where
bs :: SourcePos -> Parser Node
bs pos =
do
logicalExpr `sepBy1` try (spaces *> Text.Megaparsec.Char.string "<|" <* spaces) >>=
\xs -> let (l:ls) = reverse xs in return $ foldl (\a b -> CallNode b [a] pos) l ls
logicalExpr = binOp compExpr (Text.Megaparsec.Char.string "&" <|> Text.Megaparsec.Char.string "|") BinOpNode
compExpr = binOp typeExpr ops BinOpNode where
ops =
(
Text.Megaparsec.Char.string "="
<|> Text.Megaparsec.Char.string "/="
<|> Text.Megaparsec.Char.string ">="
<|> Text.Megaparsec.Char.string ">"
<|> Text.Megaparsec.Char.string "<="
<|> Text.Megaparsec.Char.string "<"
) :: Parser String
typeExpr = rBinOp arithExpr (Text.Megaparsec.Char.string "@") (dataName <|> arithExpr) BinOpNode
arithExpr = binOp term (Text.Megaparsec.Char.string "+" <|> Text.Megaparsec.Char.string "-") BinOpNode
term = binOp Parser.concat (Text.Megaparsec.Char.string "*" <|> Text.Megaparsec.Char.string "/") BinOpNode
concat = binOp infixOp (Text.Megaparsec.Char.string "..") BinOpNode
infixOp = rBinOp infixLOp op infixOp (\a op b pos -> CallNode (IdentifierNode op pos) [a, b] pos) where
op = do extractString <$> identifier Prelude.False
infixLOp = binOp application op (\a op b pos -> CallNode (IdentifierNode op pos) [a, b] pos) where
op = do extractString <$> identifier Prelude.True
application =
do
pos <- getSourcePos
callee <- index
try (
do
m <- mid
let args = arr m where arr(ListNode arr _) = arr
return $ CallNode callee args pos
) <|> return callee
where
mid =
do
pos <- getSourcePos
spaces *> Text.Megaparsec.Char.string ":" <* spaces
args <- (typeRef <|> logicalExpr) `sepEndBy1` (Text.Megaparsec.Char.string "," <* spaces)
return $ ListNode args pos
index =
do
pos <- getSourcePos
og <- access
ls <- P.many tIndex
return $ case last $ Just og : ls of
Just _ -> folded og ls pos
Nothing -> FuncDefNode Nothing [IdentifierNode "elwegot" pos] (folded og ls pos) Prelude.True pos
where
folded og ls pos = foldr (makeCall pos) og (reverse ls)
makeCall pos fa b = case fa of
Just a -> CallNode (IdentifierNode "access" pos) [a, b] pos
Nothing -> CallNode (IdentifierNode "access" pos) [b, IdentifierNode "elwegot" pos] pos
tIndex =
try (do
Text.Megaparsec.Char.string "["
e <- expr
Text.Megaparsec.Char.string "]"
return $ Just e)
<|>
do
Text.Megaparsec.Char.string "["
Text.Megaparsec.Char.string "]"
return Nothing
access =
do
pos <- getSourcePos
l <- atom `sepBy1` try (spaces *> Text.Megaparsec.Char.string "." <* notFollowedBy (Text.Megaparsec.Char.string ".") <* spaces)
let mpl = map makeBin (tail l)
return $ foldl' (\a b -> BinOpNode a "." b pos) (head l) mpl
where
makeBin :: Node -> Node
makeBin n@(BinOpNode a op b pos) = BinOpNode a op (makeBin b) pos
makeBin n@(IdentifierNode s pos) = StringNode s pos
makeBin n = n
methodDecl =
do
pos <- getSourcePos
keyword Open
spaces
id <- identifier Prelude.False
spaces *> Text.Megaparsec.Char.string ":" <* spaces
args <- identifier Prelude.False `sepBy1` (Text.Megaparsec.Char.string "," <* spaces)
return $ MethodNode id args pos
mewMethod =
do
pos <- getSourcePos
id <- identifier Prelude.False
spaces *> Text.Megaparsec.Char.string "?" <* spaces
cond <- (IdentifierNode "def" pos <$ try (keyword Def)) <|> expr
spaces
Text.Megaparsec.Char.string "->"
spaces
exp <- expr
return $ NewMethodNode id cond exp pos
classStmnt =
do
pos <- getSourcePos
keyword Class
spaces
id <- identifier Prelude.False
spaces *> Text.Megaparsec.Char.string ":" <* spaces
args <- identifier Prelude.False `sepBy1` (Text.Megaparsec.Char.string "," <* spaces)
newlines
seqPos <- getSourcePos
allCases <- cases `sepBy1`
notFollowedBy (try
(P.many Parser.newline *> spaces *> (
eof *> (StringNode "" <$> getSourcePos)
<|> keyword Class *> (StringNode "" <$> getSourcePos)
<|> keyword Open *> (StringNode "" <$> getSourcePos)
<|> keyword End *> (StringNode "" <$> getSourcePos)
<|> try decl
<|> structDef
<|> mewMethod)))
return $ DeclNode id (FuncDefNode (Just id) args (SequenceIfNode allCases seqPos) Prelude.True seqPos) pos
where
cases = do
newlines
spaces
pos <- getSourcePos
spaces
cond <- expr
spaces
Text.Megaparsec.Char.string "->"
spaces
thenExpr <- expr
return $ IfNode cond thenExpr Nothing pos
mnewlines = Parser.newline *> newlines
prefixExpr pref expT resf construct =
do
pos <- getSourcePos
prefT <- pref
Parser.spaces
expr <- expT
return $ construct (resf prefT) expr pos
negExpr = prefixExpr (Text.Megaparsec.Char.string "-") compExpr (const "-") UnaryExpr
accessFuncExpr :: Parser Node
accessFuncExpr = prefixExpr
(Text.Megaparsec.Char.string ".")
(identifier Prelude.False)
id
(\_ (IdentifierNode id ipos) pos ->
FuncDefNode
Nothing
[IdentifierNode "x" ipos]
(BinOpNode (IdentifierNode "x" ipos) "." (StringNode id ipos) pos)
Prelude.False
pos
)
ifExpr =
do
pos <- getSourcePos
keyword If
mspaces
c <- expr
mspaces
keyword Then
mspaces
te <- expr
mspaces
keyword Else
mspaces
ee <- expr
return $ IfNode c te (Just ee) pos
caseExpr =
do
pos <- getSourcePos
Text.Megaparsec.Char.string "|"
spaces
fls <- p `sepBy` (spaces *> Text.Megaparsec.Char.string "|" <* spaces :: Parser String)
return $ SequenceIfNode fls pos
where
p =
do
pos <- getSourcePos
ce <- expr
spaces
Text.Megaparsec.Char.string "->"
spaces
te <- expr
return $ IfNode ce te Nothing pos
parse xs = decls xs <* eof