-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathParser.hs
More file actions
1096 lines (902 loc) · 35.9 KB
/
Copy pathParser.hs
File metadata and controls
1096 lines (902 loc) · 35.9 KB
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- A lot of this is copied basically verbatim from the full python grammar
-- located at http://docs.python.org/3/reference/grammar.html
-- For example, the function `fileInput` is actually just a rule in the Python
-- grammar. Nearly everything also appears in the order it does in the grammar.
module Parser (parseFile) where
import Prelude hiding (break, exp, exponent, id, lines, not, otherwise, return)
import Data.String.Utils (join)
import Data.Set (Set)
import Text.Derp
import DerpInterface
import qualified Lexer
-- PUBLIC-FACING PARSER API FUNCTIONS
parseFile :: [Lexer.Tkn] -> Set String
parseFile tkns = runParse fileInput $ derpTkns tkns
-- PRIVATE "HELPER" FUNCTIONS FOR THE PARSER
-- These functions mostly correspond to the grammar rules in the Python
-- grammar, but are effectively useless outside of this module. Thus they are
-- private.
-- corresponds to `file_input` in grammar
fileInput :: Parser String
fileInput = lines <~> endOfFile ==> emitProgram
where lines = lineCfg
endOfFile = ter "ENDMARKER"
lineCfg :: Parser String
lineCfg = emptyLine
<|> newline <~> moreLines ==> emitNl
<|> stmt <~> moreLines ==> emitLine
where emptyLine = eps ""
newline = ter "NEWLINE"
moreLines = lineCfg
-- corresponds to `funcdef` in grammar
funcdef :: Parser String
funcdef = def <~> id <~> parameters <~> colon <~> body ==> emitFuncdef
where def = ter "def"
id = ter "ID"
colon = ter ":"
body = suite
-- corresponds to `parameters` in grammar
parameters :: Parser String
parameters = openParen <~> zeroPlusParams <~> closeParen ==> emitParams
where openParen = ter "("
closeParen = ter ")"
zeroPlusParams :: Parser String
zeroPlusParams = noParams
<|> id <~> restOfIds ==> emitParamList
<|> comma
where noParams = eps ""
id = ter "ID"
comma = ter ","
restOfIds = noParams
<|> comma <~> id <~> restOfIds ==> emitRestOfParams
-- corresponds to `stmt` in grammar
stmt :: Parser String
stmt = simpleStmt <|> compoundStmt
-- corresponds to `simple_stmt` in grammar
-- simple statements are either terminated with newline or semicolon
simpleStmt :: Parser String
simpleStmt = smallStmt <~> zeroPlusSmallStmts <~> endOfStmts ==> emitSimpleStmt
where noMoreStmts = eps ""
semicolon = ter ";"
newline = ter "NEWLINE"
endOfStmts = (noMoreStmts <~> newline)
<|> (semicolon <~> newline)
zeroPlusSmallStmts :: Parser String
zeroPlusSmallStmts = noMoreStmts
<|> moreSmallStmts
where noMoreStmts = eps ""
semicolon = ter ";"
moreSmallStmts = semicolon <~> smallStmt <~> moreSmallStmts
==> emitSmallStmts
-- corresponds to `small_stmt` in grammar
smallStmt :: Parser String
smallStmt = exprStmt
<|> delStmt
<|> passStmt
<|> flowStmt
<|> globalStmt
<|> nonlocalStmt
<|> assertStmt
-- corresponds to `expr_stmt` in grammar
-- augmented assignment is stuff like `+=` and `*=`. A "test" is an assignment
-- with a conditional, eg, `x = 1 if 1 == y else 2`.
-- TODO: REFACTOR THIS TO USE DIFFERENT FUNCTIONS AND STUFF
exprStmt :: Parser String
exprStmt = manyTests <~> augassign <~> manyTests ==> emitAugAssignStmt
<|> manyTests <~> equals <~> testOrTests ==> emitAssignStmt
<|> testOrTests ==> emitExprStmt
where equals = ter "="
-- corresponds to `augassign` in grammar
augassign :: Parser String
augassign = ter "+="
<|> ter "-="
<|> ter "*="
<|> ter "/="
<|> ter "%="
<|> ter "&="
<|> ter "|="
<|> ter "^="
<|> ter "<<="
<|> ter ">>="
<|> ter "**="
<|> ter "//="
-- corresponds to `del_stmt` in grammar
delStmt :: Parser String
delStmt = del <~> starExpr ==> emitDelStmt
where del = ter "del"
-- corresponds to `pass_stmt` in grammar
passStmt :: Parser String
passStmt = pass ==> emitPassStmt
where pass = ter "pass"
-- corresponds to `flow_stmt` in grammar
flowStmt :: Parser String
flowStmt = breakStmt <|> continueStmt <|> returnStmt <|> raiseStmt
-- corresponds to `break_stmt` in grammar
breakStmt :: Parser String
breakStmt = break ==> emitBreakStmt
where break = ter "break"
-- corresponds to `continue_stmt` in grammar
continueStmt :: Parser String
continueStmt = continue ==> emitContinueStmt
where continue = ter "continue"
-- corresponds to `return_stmt` in grammar
returnStmt :: Parser String
returnStmt = return <~> returnExpr ==> emitReturnStmt
where return = ter "return"
-- whatever comes after `return`, eg `return "cows"`
returnExpr :: Parser String
returnExpr = none
<|> exp
where none = eps ""
exp = manyTests
-- corresponds to `raise_stmt` in grammar
raiseStmt :: Parser String
raiseStmt = raise <~> exceptionTypesAndVars ==> emitRaiseStmt
where raise = ter "raise"
exceptionTypesAndVars = raiseClause
-- matches whatever comes after `raise`, eg `raise ValueError("too many cows")`
-- `raise` with no args re-raises an exception in the except suite.
raiseClause :: Parser String
raiseClause = reRaiseException
<|> raiseNewException
where reRaiseException = eps ""
raiseNewException = test <~> raiseFrom ==> emitRaiseNewException
-- eg, `raise ValueError from msg`
raiseFrom :: Parser String
raiseFrom = fromNowhere
<|> from <~> test ==> emitFromStmt
where fromNowhere = eps ""
from = ter "from"
-- corresponds to `global_stmt` in grammar
globalStmt :: Parser String
globalStmt = global <~> id <~> zeroPlusIds ==> emitGlobalStmt
where global = ter "global"
id = ter "ID"
zeroPlusIds :: Parser String
zeroPlusIds = noMoreIds
<|> comma <~> id <~> zeroPlusIds ==> emitRestOfIds
where noMoreIds = eps ""
comma = ter ","
id = ter "ID"
-- corresponds to `nonlocal_stmt` in grammar
nonlocalStmt :: Parser String
nonlocalStmt = nonlocal <~> id <~> zeroPlusIds ==> emitNonlocalStmt
where nonlocal = ter "nonlocal"
id = ter "ID"
-- corresponds to `assert_stmt` in grammar
assertStmt :: Parser String
assertStmt = assert <~> test <~> zeroPlusTests ==> emitAssertStmt
where assert = ter "assert"
zeroPlusTests :: Parser String
zeroPlusTests = noMoreTests
<|> comma <~> test ==> emitRestTests
where noMoreTests = eps ""
comma = ter ","
-- corresponds to `compound_stmt` in grammar
compoundStmt :: Parser String
compoundStmt = ifStmt
<|> whileStmt
<|> forStmt
<|> tryStmt
<|> funcdef
-- corresponds to `if_stmt` in grammar
ifStmt :: Parser String
ifStmt = ifKeyword <~> test <~> colon <~> block <~> otherwise ==> emitIfStmt
where ifKeyword = ter "if"
colon = ter ":"
block = suite
otherwise = zeroOrMoreElifs <~> elseClause
elseClause :: Parser String
elseClause = noElseClause
<|> elseKeyword <~> colon <~> block ==> emitElseClause
where noElseClause = eps ""
elseKeyword = ter "else"
colon = ter ":"
block = suite
zeroOrMoreElifs :: Parser String
zeroOrMoreElifs = noMoreElifs
<|> elif <~> test <~> colon <~> block <~> zeroOrMoreElifs
==> emitElifs
where noMoreElifs = eps ""
elif = ter "elif"
colon = ter ":"
block = suite
-- corresponds to `while_stmt` in grammar
whileStmt :: Parser String
whileStmt = while <~> test <~> colon <~> block <~> whileElseClause
==> emitWhileStmt
where while = ter "while"
colon = ter ":"
block = suite
whileElseClause :: Parser String
whileElseClause = noElseClause
<|> elseKeyword <~> colon <~> block ==> emitWhileElseClause
where noElseClause = eps ""
elseKeyword = ter "else"
colon = ter ":"
block = suite
-- corresponds to `for_stmt` in grammar
forStmt :: Parser String
forStmt = for <~> id <~> inKeywrd <~> test <~> colon <~> block <~> forElseClause
==> emitForStmt
where for = ter "for"
id = ter "ID"
inKeywrd = ter "in"
colon = ter ":"
block = suite
forElseClause :: Parser String
forElseClause = noElseClause
<|> elseKeyword <~> colon <~> block ==> emitForElseClause
where noElseClause = eps ""
elseKeyword = ter "else"
colon = ter ":"
block = suite
-- corresponds to `try_stmt` in grammar
tryStmt :: Parser String
tryStmt = try <~> colon <~> block <~> exceptionHandlers ==> emitTryStmt
where try = ter "try"
colon = ter ":"
block = suite
onlyAFinallyBlock = finallyTryBlock
exceptionHandlers = catchAndFinallyBlocks <|> onlyAFinallyBlock
-- used to emit finally block occurring right after a try block
finallyTryBlock :: Parser String
finallyTryBlock = finally <~> colon <~> block ==> emitFinallyTryBlock
where finally = ter "finally"
colon = ter ":"
block = suite
finallyCatchBlock :: Parser String
finallyCatchBlock = noFinallyBlock
<|> finally <~> colon <~> block ==> emitFinallyCatchBlock
where noFinallyBlock = eps ""
finally = ter "finally"
colon = ter ":"
block = suite
catchAndFinallyBlocks :: Parser String
catchAndFinallyBlocks = exceptClause <~> colon <~> block <~> zeroPlusExcepts
<~> elseBlock <~> finallyBlock ==> emitExceptElseFinally
where -- no extra excepts
colon = ter ":"
block = suite
elseBlock = exceptElseClause ==> emitFailIfNotParsed
finallyBlock = finallyCatchBlock ==> emitFailIfNotParsed
exceptElseClause :: Parser String
exceptElseClause = noElseClause
<|> elseKeyword <~> colon <~> block ==> emitExceptElseClause
where noElseClause = eps ""
elseKeyword = ter "else"
colon = ter ":"
block = suite
zeroPlusExcepts :: Parser String
zeroPlusExcepts = noMoreExcepts
<|> exceptClause <~> colon <~> block <~> zeroPlusExcepts
==> emitExceptClauses
where noMoreExcepts = eps ""
colon = ter ":"
block = suite
-- corresponds to `except_clause` in grammar
exceptClause :: Parser String
exceptClause = except <~> exceptType ==> emitExceptClause
where except = ter "except"
-- matches what sort of exception we're catching, eg, in `except ValueError:`,
-- we'd matech `ValueError`.
exceptType :: Parser String
exceptType = catchAllExceptions
<|> catchSpecificExceptions ==> emitExceptType
where catchAllExceptions = eps "" -- `except:` will catch all excptns
catchSpecificExceptions = test <~> exceptVars
-- matches vars in exception declaration, eg, in `except ValueError as e:` we
-- we would match `as e`
exceptVars :: Parser String
exceptVars = noVars
<|> as <~> id ==> emitExceptVars
where noVars = eps ""
as = ter "as"
id = ter "ID"
-- corresponds to `suite` in grammar
suite :: Parser String
suite = simpleStmt
<|> newline <~> indent <~> onePlusStmts <~> dedent ==> emitSuite
where newline = ter "NEWLINE"
indent = ter "INDENT"
dedent = ter "DEDENT"
onePlusStmts :: Parser String
onePlusStmts = stmt <~> zeroPlusStmts ==> emitStmts
zeroPlusStmts :: Parser String
zeroPlusStmts = noMoreStmts
<|> stmt <~> zeroPlusStmts ==> emitStmts
where noMoreStmts = eps ""
-- corresponds to `test` in grammar
-- generates all possible conditionals... I think
test :: Parser String
test = orTest
<|> orTest <~> ifKeyword <~> orTest <~> elseKeyword <~> test ==> emitTest
<|> lambdef
where ifKeyword = ter "if"
elseKeyword = ter "else"
-- corresponds to `lambdef` in grammar
lambdef :: Parser String
lambdef = lambda <~> zeroOrMoreParams <~> colon <~> body ==> emitLambdef
where lambda = ter "lambda"
zeroOrMoreParams = zeroPlusParams
colon = ter ":"
body = test
-- corresponds to `or_test` in grammar
orTest :: Parser String
orTest = andTest <~> zeroPlusOrs ==> emitOrTest
zeroPlusOrs :: Parser String
zeroPlusOrs = noMoreTests
<|> orKeyword <~> andTest <~> zeroPlusOrs ==> emitZeroPlusOrs
where noMoreTests = eps ""
orKeyword = ter "or"
-- corresponds to `and_test` in grammar
andTest :: Parser String
andTest = notTest <~> zeroPlusNots ==> emitAndTest
zeroPlusNots :: Parser String
zeroPlusNots = noMoreNots
<|> andKeyword <~> notTest <~> zeroPlusNots ==> emitZeroPlusNots
where noMoreNots = eps ""
andKeyword = ter "and"
-- corresponds to `not_test` in grammar
notTest :: Parser String
notTest = notKeyword <~> notTest ==> emitNotTest
<|> comparison
where notKeyword = ter "not"
-- corresponds to `comparison` in grammar
comparison :: Parser String
comparison = starExpr <~> zeroPlusComps ==> emitComparison
zeroPlusComps :: Parser String
zeroPlusComps = noMoreComps
<|> comparisonType <~> starExpr <~> zeroPlusComps
==> emitZeroPlusComps
where noMoreComps = eps ""
comparisonType :: Parser String
comparisonType = comparisonOperator ==> emitComparisonOperator
<|> inKeyword
<|> is
<|> (not <~> inKeyword ==> emitNotIn)
<|> (is <~> not ==> emitIsNot)
where inKeyword = ter "in"
not = ter "not"
is = ter "is"
emitNotIn _ = "not-in"
emitIsNot _ = "is-not"
comparisonOperator :: Parser String
comparisonOperator = ter "<"
<|> ter ">"
<|> ter "=="
<|> ter ">="
<|> ter "<="
<|> ter "<>"
<|> ter "!="
-- corresponds to `star_expr` in grammar
-- matches optional star before the expression, eg `myfunc(*cows)`
starExpr :: Parser String
starExpr = maybeHasStar <~> expr ==> emitStarExpr
where star = ter "*"
noStar = eps ""
maybeHasStar = noStar <|> star
-- corresponds to `expr` in grammar
expr :: Parser String
expr = xorExpr <~> zeroPlusXors ==> emitExpr
-- corresponds to `xor_expr` in grammar
xorExpr :: Parser String
xorExpr = andExpr <~> zeroPlusAnds ==> emitXorExpr
zeroPlusXors :: Parser String
zeroPlusXors = noMoreXors
<|> bitOr <~> xorExpr <~> zeroPlusXors ==> emitZeroPlusXors
where noMoreXors = eps ""
bitOr = ter "|"
-- corresponds to `and_expr` in grammar
andExpr :: Parser String
andExpr = shiftExpr <~> zeroPlusShifts ==> emitAndExpr
zeroPlusAnds :: Parser String
zeroPlusAnds = noMoreAnds
<|> xor <~> andExpr <~> zeroPlusAnds ==> emitZeroPlusAnds
where noMoreAnds = eps ""
xor = ter "^"
-- corresponds to `shift_expr` in grammar
shiftExpr :: Parser String
shiftExpr = arithExpr <~> zeroPlusArithExprs ==> emitShiftExpr
zeroPlusShifts :: Parser String
zeroPlusShifts = noMoreShifts
<|> bitAnd <~> shiftExpr <~> zeroPlusShifts
==> emitZeroPlusShifts
where noMoreShifts = eps ""
bitAnd = ter "&"
-- corresponds to `arith_expr` in grammar
arithExpr :: Parser String
arithExpr = term <~> zeroPlusAdds ==> emitArithExpr
emitArithExpr :: (String,String) -> String
emitArithExpr (termExp, restOfAdds) = case restOfAdds of
[] -> termExp
_ -> joinStrs [header, body, footer]
where header = "(arith "
body = join " " [termExp, restOfAdds]
footer = ")"
zeroPlusArithExprs :: Parser String
zeroPlusArithExprs = noMoreArithExprs
<|> leftOrRightShift <~> arithExpr <~> zeroPlusArithExprs
==> emitZeroPlusArithExprs
where noMoreArithExprs = eps ""
leftOrRightShift = ter "<<" <|> ter ">>"
zeroPlusAdds :: Parser String
zeroPlusAdds = noMoreAdds
<|> minusOrPlus <~> term <~> zeroPlusAdds ==> emitZeroPlusAdds
where noMoreAdds = eps ""
minusOrPlus = ter "+" <|> ter "-"
-- corresponds to `term` in grammar
term :: Parser String
term = factor <~> zeroPlusMults ==> emitTerm
zeroPlusMults :: Parser String
zeroPlusMults = noMoreMults
<|> operator <~> factor <~> zeroPlusMults ==> emitZeroPlusMults
where noMoreMults = eps ""
mult = ter "*"
divis = ter "/"
modu = ter "%"
intdiv = ter "//"
operator = mult <|> divis <|> modu <|> intdiv
-- corresponds to `factor` in grammar
factor :: Parser String
factor = power
<|> operatorChoice <~> factor ==> emitFactor
where plus = ter "+"
minus = ter "-"
bitNot = ter "~"
operatorChoice = plus <|> minus <|> bitNot
indexed :: Parser String
indexed = atom <~> zeroPlusTrailers ==> emitIndexed
-- corresponds to `power` in grammar
power :: Parser String
power = indexed <~> exponent ==> emitPower
where noPower = eps ""
raisedTo = ter "**"
emitExponent = (\(_, fctr) -> fctr)
exponent = noPower <|> ((raisedTo <~> factor) ==> emitExponent)
-- corresponds to `atom` in grammar
atom :: Parser String
atom = tuple
<|> list
<|> dict
<|> id ==> emitId
<|> lit ==> emitLit
<|> str ==> emitString
<|> dots ==> emitDots
<|> noneType ==> emitNoneType
<|> trueType ==> emitTrueType
<|> falseType ==> emitFalseType
where id = ter "ID"
lit = ter "LIT"
dots = ter "..."
noneType = ter "None"
trueType = ter "True"
falseType = ter "False"
emitId x = x
emitLit x = x
emitString x = joinStrs ["\"", x, "\""]
emitDots _ = ""
emitNoneType _ = "None"
emitTrueType _ = "True"
emitFalseType _ = "False"
tuple :: Parser String
tuple = lparen <~> optionalExps <~> rparen ==> emitTuple
where lparen = ter "("
rparen = ter ")"
none = eps ""
optionalExps = (none <|> testOrTests)
list :: Parser String
list = lbracket <~> optionalExps <~> rbracket ==> emitList
where lbracket = ter "["
rbracket = ter "]"
none = eps ""
optionalExps = none <|> manyTests
dict :: Parser String
dict = lbrace <~> optDictOrSetMaker <~> rbrace ==> emitDict
where lbrace = ter "{"
rbrace = ter "}"
none = eps ""
optDictOrSetMaker = none <|> dictorsetmaker
str :: Parser String
str = string <~> zeroPlusStrs ==> emitStr
where string = ter "STRING"
zeroPlusStrs :: Parser String
zeroPlusStrs = noMoreStrs
<|> string <~> zeroPlusStrs ==> emitStr
where noMoreStrs = eps ""
string = ter "STRING"
-- corresponds to `trailer` in grammar
trailer :: Parser String
trailer = leftParen <~> optionalArglist <~> rightParen ==> emitTrailerTuple
<|> subscript ==> emitSubscript
<|> methodCall ==> emitMethodCall
where leftParen = ter "("
rightParen = ter ")"
leftBracket = ter "["
rightBracket = ter "]"
noArglist = eps ""
optionalArglist = noArglist <|> arglist
subscript = leftBracket <~> testOrTests <~> rightBracket
methodCall = period <~> id
period = ter "."
id = ter "ID"
zeroPlusTrailers :: Parser String
zeroPlusTrailers = noMoreTrailers
<|> trailer <~> zeroPlusTrailers ==> emitZeroPlusTrailers
where noMoreTrailers = eps ""
manyTests :: Parser String
manyTests = test <~> testTuple <~> optionalComma ==> emitManyTests
where nothing = eps ""
comma = ter ","
optionalComma = nothing <|> comma
-- a test exp can either be just one expr or a tuple eg `ex1, ex2 = blah()`
testOrTests :: Parser String
testOrTests = exp <~> zeroPlusExps <~> optionalComma ==> emitTestOrTests
where exp = test
zeroPlusExps = zeroPlusArgs
noComma = eps ""
comma = ter ","
optionalComma = noComma <|> comma
-- corresponds to `dictorsetmaker` in grammar
dictorsetmaker :: Parser String
dictorsetmaker = dictMaker <|> setMaker
dictMaker :: Parser String
dictMaker = test <~> colon <~> test <~> testTuple' <~> optionalComma
==> emitDictMaker
where colon = ter ":"
nothing = eps ""
comma = ter ","
optionalComma = nothing <|> comma
setMaker :: Parser String
setMaker = test <~> testTuple <~> optionalComma ==> emitSetMaker
where nothing = eps ""
comma = ter ","
optionalComma = nothing <|> comma
testTuple :: Parser String
testTuple = noMoreTests
<|> comma <~> test <~> testTuple ==> emitTestTuple
where noMoreTests = eps ""
comma = ter ","
testTuple' :: Parser String
testTuple' = nothing
<|> comma <~> test <~> colon <~> test <~> testTuple'
==> emitTestTuple'
where nothing = eps ""
comma = ter ","
colon = ter ":"
-- corresponds to `arglist` in grammar
arglist :: Parser String
arglist = arg <~> zeroPlusArgs <~> optionalComma ==> emitArglist
where arg = test
noComma = eps ""
comma = ter ","
optionalComma = noComma <|> comma
zeroPlusArgs :: Parser String
zeroPlusArgs = noMoreArgs
<|> comma <~> arg <~> zeroPlusArgs ==> emitZeroPlusArgs
where noMoreArgs = eps ""
comma = ter ","
arg = test
-- EMISSION FUNCTIONS
-- Functions designed to take the output of a reduction from Derp lib,
-- destructure it using pattern matching, and output a string that is a like
-- a lispy AST.
--
-- Each is accompanied by an emit function that gives hints about how and why
-- the destructuring is happening. For example, `emitFuncdef` has `emitFuncdef'`
-- as a helper, and it takes `id`, `params`, and `body` as parameters, which
-- tells you a lot about how the destructuring works.
emitProgram :: (String,String) -> String
emitProgram (p, _) = emitProgram' p
emitProgram' :: String -> String
emitProgram' prog = joinStrs [header, prog, footer]
where header = "(program "
footer = ")"
emitFuncdef :: (String,(String,(String,(String,String)))) -> String
emitFuncdef (_, (id, (params, (_, body)))) = emitFuncdef' id params body
emitFuncdef' :: String -> String -> String -> String
emitFuncdef' id params body = joinStrs [header, wrappedBody, footer]
where header = join " " ["(def (" ++ id, params] ++ ") "
wrappedBody = "(" ++ body
footer = "))"
emitParams :: (String, (String, String)) -> String
emitParams (_, (ps, _)) = ps
emitParamList :: (String, String) -> String
emitParamList (s1, s2) = join " " [s1, s2]
emitRestOfParams :: (String,(String,String)) -> String
emitRestOfParams (_, (name, _)) = name
emitNl :: (String,String) -> String
emitNl (_, ln) = ln
emitLine :: (String,String) -> String
emitLine (sp, exp) = join " " [sp, exp]
emitSmallStmts :: (String, (String, String)) -> String
emitSmallStmts (_, (stmts, end)) = joinStrs [wrappedStmt, end]
where wrappedStmt = "(" ++ stmts ++ ") "
emitSimpleStmt :: (String, (String, (String, String))) -> String
emitSimpleStmt (st1, (st2, _)) = case (null st2) of
True -> "(" ++ st1 ++ ")"
False -> joinStrs [header, body, footer]
where header = "(begin (" ++ st1 ++ ") "
body = st2
footer = ")"
emitAssignStmt :: (String, (String, String)) -> String
emitAssignStmt (id, (_, rhs)) = joinStrs [lhs, rhs]
where lhs = "= (" ++ id ++ ")"
emitExprStmt :: String -> String
emitExprStmt st = joinStrs ["expr ", st]
emitAugAssignStmt :: (String, (String, String)) -> String
emitAugAssignStmt (exp,(aug,rhs)) = joinStrs [operator, lhs, rhs]
where operator = "\"" ++ aug ++ "\" "
lhs = "(" ++ exp ++ ") "
emitDelStmt :: (String,String) -> String
emitDelStmt (_, exp) = joinStrs ["del ", exp]
emitPassStmt :: String -> String
emitPassStmt _ = "pass"
emitBreakStmt :: String -> String
emitBreakStmt _ = "break"
emitContinueStmt :: String -> String
emitContinueStmt _ = "continue"
emitReturnStmt :: (String,String) -> String
emitReturnStmt (_, exp) = join " " ["return", exp]
emitGlobalStmt :: (String,(String,String)) -> String
emitGlobalStmt (_, (x, xs)) = joinStrs [global, exps]
where global = "global "
exps = join " " [x,xs]
emitRestOfIds :: (String,(String,String)) -> String
emitRestOfIds (_, (x, xs)) = join " " [x, xs]
emitNonlocalStmt :: (String,(String,String)) -> String
emitNonlocalStmt (_, (x, xs)) = joinStrs [nonlocal, exps]
where nonlocal = "nonlocal "
exps = join " " [x, xs]
emitRestTests :: (String,String) -> String
emitRestTests (_, tst) = tst
emitAssertStmt :: (String,(String,String)) -> String
emitAssertStmt (_, (exp1, exp2)) = joinStrs [assert, exps]
where assert = "assert "
exps = join " " [exp1,exp2]
emitIfStmt :: (String,(String,(String,(String,(String,String))))) -> String
emitIfStmt (_,(t,(_,(s,(elif,els))))) = (join " " [(join " " [("(cond (" ++ t ++ " (" ++ s ++ "))"), elif]) , els]) ++ ")"
emitElseClause :: (String,(String,String)) -> String
emitElseClause (_, (_, body)) = joinStrs [header, body, footer]
where header = "(else ("
footer = "))"
emitElifs :: (String,(String,(String,(String,String)))) -> String
emitElifs (_, (tk, (_, (cond, block)))) = join " " [header, block]
where wrappedCond = " (" ++ cond ++ ")"
header = "(" ++ tk ++ wrappedCond ++ ")"
emitWhileElseClause :: (String,(String,String)) -> String
emitWhileElseClause (_, (_, s)) = "(" ++ s ++ ")"
emitWhileStmt :: (String,(String,(String,(String,(String))))) -> String
emitWhileStmt (_,(t,(_,(s,(block))))) = body ++ footer
where header = "(while " ++ t ++ " (" ++ s ++ ")"
body = join " " [header, block]
footer = ")"
emitForElseClause :: (String,(String,String)) -> String
emitForElseClause (_, (_, block)) = "(" ++ block ++ ")"
emitForStmt :: (String,(String,(String,(String,(String,(String,String))))))
-> String
emitForStmt (_, (id, (_, (tst, (_, (stmts, els)))))) = joinStrs [forOpen,
forLoop,
forClose]
where forOpen = "(for "
forLoop = join " " [id, tst, "(", stmts, ")", els]
forClose = ")"
emitTryStmt :: (String,(String,(String,String))) -> String
emitTryStmt (_, (_, (exp, except))) = joinStrs [header, exp', except, footer]
where header = "(try ("
exp' = exp ++ ") "
footer = ")"
emitExceptClauses :: (String,(String,(String,String))) -> String
emitExceptClauses (exc, (_, (exp, bl))) = join " " [clause, bl]
where clause = "(" ++ exc ++ " (" ++ exp ++ "))"
emitExceptClause :: (String,String) -> String
emitExceptClause (_,bl) = joinStrs [except, bl, footer]
where except = "(except "
footer = ")"
emitSuite :: (String,(String,(String,String))) -> String
emitSuite (_, (_, (stm, _))) = joinStrs [suitek, stm]
where suitek = "suite "
emitStmts :: (String,String) -> String
emitStmts (st1, st2) = join " " [st1,st2]
emitExceptType :: (String,String) -> String
emitExceptType (tst, exps) = join " " [tst, exps]
emitExceptVars :: (String,String) -> String
emitExceptVars (_, v) = v
emitRaiseStmt :: (String,String) -> String
emitRaiseStmt (s1, s2) = join " " [s1, s2]
emitRaiseNewException :: (String,String) -> String
emitRaiseNewException (tp, frm) = join " " [tp, frm]
emitFromStmt :: (String,String) -> String
emitFromStmt (_, fr) = fr
emitLambdef :: (String,(String,(String,String))) -> String
emitLambdef (_, (params, (_, body))) = joinStrs [header, middle, footer]
where header = "(expr (lambda ("
middle = params ++ ") (" ++ body
footer = ")))"
emitStarExpr :: (String,String) -> String
emitStarExpr (star, exp) = case star of
[] -> exp
_ -> "(star " ++ exp ++ ")"
emitArglist :: (String,(String,String)) -> String
emitArglist (arg, (restOfArgs, _)) = join " " [arg, restOfArgs]
emitZeroPlusArgs :: (String,(String,String)) -> String
emitZeroPlusArgs (_,(arg, restOfArgs)) = join " " [arg, restOfArgs]
emitFinallyTryBlock :: (String,(String,String)) -> String
emitFinallyTryBlock (_, (_, block)) = "() #f (" ++ block ++ ")"
emitFailIfNotParsed :: String -> String
emitFailIfNotParsed parsed = case parsed of
[] -> "#f"
_ -> parsed
emitExceptElseClause :: (String,(String,String)) -> String
emitExceptElseClause (_, (_, block)) = "(" ++ block ++ ")"
emitFinallyCatchBlock :: (String,(String,String)) -> String
emitFinallyCatchBlock (_, (_, block)) = "(" ++ block ++ ")"
emitExceptElseFinally :: (String,(String,(String,(String,(String,String))))) ->
String
emitExceptElseFinally (except, (_, (block, ([], (elseBlock, finally))))) =
joinStrs [exceptBlock, elseBlock, " ", finally]
where exceptBlock = "((" ++ except ++ " (" ++ block ++ "))) "
emitExceptElseFinally (except, (_, (block, (moreExcepts, (elseBlock,
finally))))) =
joinStrs [exceptBlock, moreExcepts, ") ", elseBlock, " ", finally]
where exceptBlock = "((" ++ except ++ " (" ++ block ++ ")) "
emitTest :: (String,(String,(String,(String,String)))) -> String
emitTest (ortest1, (_, (ortest2, (_, testExp)))) =
join " " [header, ortest1, ortest2, testExp, footer]
where header = "(expr (if "
footer = "))"
emitZeroPlusOrs :: (String,(String,String)) -> String
emitZeroPlusOrs (_, (andTestExp, restOfOrs)) = join " " [andTestExp, restOfOrs]
emitOrTest :: (String,String) -> String
emitOrTest (andTestExp, orExps) = case orExps of
[] -> andTestExp
_ -> joinStrs [header, body, footer]
where header = "(or "
body = join " " [andTestExp, orExps]
footer = ")"
emitAndTest :: (String,String) -> String
emitAndTest (notTestExp, restOfNotTests) = case restOfNotTests of
[] -> notTestExp
_ -> joinStrs [header, notTestExp, " ", restOfNotTests, footer]
where header = "(and "
footer = ")"
emitZeroPlusNots :: (String,(String,String)) -> String
emitZeroPlusNots (_, (notTestExp, restOfNots)) = join " " [notTestExp,restOfNots]
emitNotTest :: (String,String) -> String
emitNotTest (_, notTestExp) = joinStrs [header, notTestExp, footer]
where header = "(not "
footer = ")"
emitComparison :: (String,String) -> String
emitComparison (starExp, restOfComps) = case restOfComps of
[] -> starExp
_ -> joinStrs [header, starExp, " ", restOfComps, footer]
where header = "(comparison "
footer = ")"
emitZeroPlusComps :: (String,(String,String)) -> String
emitZeroPlusComps (cop,(e,r)) = join " " [("(" ++ cop ++ " " ++ e ++ ")"),r]
emitComparisonOperator :: String -> String
emitComparisonOperator x = joinStrs ["\"", x, "\""]
emitZeroPlusXors :: (String,(String,String)) -> String
emitZeroPlusXors (_, (xorExp, restXors)) = join " " [xorExp, restXors]
emitExpr :: (String,String) -> String
emitExpr (xorExp, restXors) = case restXors of
[] -> xorExp
_ -> joinStrs [header, xorExp, " ", restXors, footer]
where header = "(bitwise-or "
footer = ")"
emitXorExpr :: (String,String) -> String
emitXorExpr (andExp, restOfAnds) = case restOfAnds of
[] -> andExp
_ -> joinStrs [header, andExp, " ", restOfAnds, footer]
where header = "(bitwise-xor "
footer = ")"
emitZeroPlusAnds :: (String,(String,String)) -> String
emitZeroPlusAnds (_, (andExp, restOfAnds)) = join " " [andExp, restOfAnds]
emitZeroPlusShifts :: (String,(String,String)) -> String
emitZeroPlusShifts (_,(shiftExp, restOfShifts)) =
join " " [shiftExp, restOfShifts]
emitAndExpr :: (String,String) -> String
emitAndExpr (shiftExp, restOfShiftExps) = case restOfShiftExps of
[] -> shiftExp
_ -> joinStrs [header, shiftExp, " ", restOfShiftExps, footer]
where header = "(bitwise-and "
footer = ")"
emitShiftExpr :: (String,String) -> String
emitShiftExpr (arithExp, restOfArithExps) = case restOfArithExps of
[] -> arithExp
_ -> joinStrs [header, body, footer]
where header = "(shift "
body = join " " [arithExp, restOfArithExps]
footer = ")"
emitZeroPlusArithExprs :: (String,(String,String)) -> String
emitZeroPlusArithExprs (shiftOperator, (arithExp, restOfArithExps)) =
join " " [header, body, footer, restOfArithExps]
where header = "("
body = "\"" ++ shiftOperator ++ "\" " ++ arithExp
footer = ")"
emitZeroPlusAdds :: (String,(String,String)) -> String
emitZeroPlusAdds (oprator, (exp, restOfAdds)) = join " " [header, body, footer,
restOfAdds]
where header = "("
body = "\"" ++ oprator ++ "\"" ++ exp
footer = ")"
emitTerm :: (String,String) -> String
emitTerm (fctr, restOfMults) = case restOfMults of
[] -> fctr
_ -> joinStrs [header, body, footer]
where header = "(term "
body = join " " [fctr, restOfMults]
footer = ")"