forked from tree-sitter/tree-sitter-agda
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
2291 lines (1968 loc) · 90.9 KB
/
parser.y
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
{
{-# LANGUAGE CPP #-}
{-# LANGUAGE PatternGuards #-}
{-| The parser is generated by Happy (<http://www.haskell.org/happy>).
-
- Ideally, ranges should be as precise as possible, to get messages that
- emphasize precisely the faulting term(s) upon error.
-
- However, interactive highlighting is only applied at the end of each
- mutual block, keywords are only highlighted once (see
- `TypeChecking.Rules.Decl'). So if the ranges of two declarations
- interleave, one must ensure that keyword ranges are not included in
- the intersection. (Otherwise they are uncolored by the interactive
- highlighting.)
-
-}
module Agda.Syntax.Parser.Parser (
moduleParser
, moduleNameParser
, exprParser
, exprWhereParser
, tokensParser
, holeContentParser
, splitOnDots -- only used by the internal test-suite
) where
import Control.Monad
import Data.Char
import Data.Functor
import Data.List
import Data.Maybe
import Data.Monoid
import qualified Data.Traversable as T
import Debug.Trace
import Agda.Syntax.Position hiding (tests)
import Agda.Syntax.Parser.Monad
import Agda.Syntax.Parser.Lexer
import Agda.Syntax.Parser.Tokens
import Agda.Syntax.Concrete as C
import Agda.Syntax.Concrete.Attribute
import Agda.Syntax.Concrete.Pattern
import Agda.Syntax.Concrete.Pretty ()
import Agda.Syntax.Common
import Agda.Syntax.Fixity
import Agda.Syntax.Notation
import Agda.Syntax.Literal
import Agda.TypeChecking.Positivity.Occurrence hiding (tests)
import Agda.Utils.Either hiding (tests)
import Agda.Utils.Functor
import Agda.Utils.Hash
import Agda.Utils.List ( spanJust, chopWhen )
import Agda.Utils.Monad
import Agda.Utils.Pretty
import Agda.Utils.Singleton
import Agda.Utils.Tuple
import qualified Agda.Utils.Maybe.Strict as Strict
import Agda.Utils.Impossible
#include "undefined.h"
}
%name tokensParser Tokens
%name exprParser Expr
%name exprWhereParser ExprWhere
%name moduleParser File
%name moduleNameParser ModuleName
%name funclauseParser FunClause
%name holeContentParser HoleContent
%tokentype { Token }
%monad { Parser }
%lexer { lexer } { TokEOF{} }
%expect 9
-- * shift/reduce for \ x y z -> foo = bar
-- shifting means it'll parse as \ x y z -> (foo = bar) rather than
-- (\ x y z -> foo) = bar
--
-- * Telescope let and do-notation let.
-- Expr2 -> 'let' Declarations . LetBody
-- TypedBinding -> '(' 'let' Declarations . ')'
-- ')' shift, and enter state 486
-- (reduce using rule 189)
-- A do-block cannot end in a 'let' so committing to TypedBinding with a
-- shift is the right thing to do here.
--
-- * Named implicits in TypedBinding {x = y}. When encountering the '=' shift
-- treats this as a named implicit and reducing would fail later.
-- This is a trick to get rid of shift/reduce conflicts arising because we want
-- to parse things like "m >>= \x -> k x". See the Expr rule for more
-- information.
%nonassoc LOWEST
%nonassoc '->'
%token
'abstract' { TokKeyword KwAbstract $$ }
'codata' { TokKeyword KwCoData $$ }
'coinductive' { TokKeyword KwCoInductive $$ }
'constructor' { TokKeyword KwConstructor $$ }
'data' { TokKeyword KwData $$ }
'eta-equality' { TokKeyword KwEta $$ }
'field' { TokKeyword KwField $$ }
'forall' { TokKeyword KwForall $$ }
'variable' { TokKeyword KwVariable $$ }
'hiding' { TokKeyword KwHiding $$ }
'import' { TokKeyword KwImport $$ }
'in' { TokKeyword KwIn $$ }
'inductive' { TokKeyword KwInductive $$ }
'infix' { TokKeyword KwInfix $$ }
'infixl' { TokKeyword KwInfixL $$ }
'infixr' { TokKeyword KwInfixR $$ }
'instance' { TokKeyword KwInstance $$ }
'overlap' { TokKeyword KwOverlap $$ }
'let' { TokKeyword KwLet $$ }
'macro' { TokKeyword KwMacro $$ }
'module' { TokKeyword KwModule $$ }
'mutual' { TokKeyword KwMutual $$ }
'no-eta-equality' { TokKeyword KwNoEta $$ }
'open' { TokKeyword KwOpen $$ }
'pattern' { TokKeyword KwPatternSyn $$ }
'postulate' { TokKeyword KwPostulate $$ }
'primitive' { TokKeyword KwPrimitive $$ }
'private' { TokKeyword KwPrivate $$ }
'Prop' { TokKeyword KwProp $$ }
'public' { TokKeyword KwPublic $$ }
'quote' { TokKeyword KwQuote $$ }
'quoteContext' { TokKeyword KwQuoteContext $$ }
'quoteGoal' { TokKeyword KwQuoteGoal $$ }
'quoteTerm' { TokKeyword KwQuoteTerm $$ }
'record' { TokKeyword KwRecord $$ }
'renaming' { TokKeyword KwRenaming $$ }
'rewrite' { TokKeyword KwRewrite $$ }
'Set' { TokKeyword KwSet $$ }
'syntax' { TokKeyword KwSyntax $$ }
'tactic' { TokKeyword KwTactic $$ }
'to' { TokKeyword KwTo $$ }
'unquote' { TokKeyword KwUnquote $$ }
'unquoteDecl' { TokKeyword KwUnquoteDecl $$ }
'unquoteDef' { TokKeyword KwUnquoteDef $$ }
'using' { TokKeyword KwUsing $$ }
'where' { TokKeyword KwWhere $$ }
'do' { TokKeyword KwDo $$ }
'with' { TokKeyword KwWith $$ }
'BUILTIN' { TokKeyword KwBUILTIN $$ }
'CATCHALL' { TokKeyword KwCATCHALL $$ }
'DISPLAY' { TokKeyword KwDISPLAY $$ }
'ETA' { TokKeyword KwETA $$ }
'FOREIGN' { TokKeyword KwFOREIGN $$ }
'COMPILE' { TokKeyword KwCOMPILE $$ }
'IMPOSSIBLE' { TokKeyword KwIMPOSSIBLE $$ }
'INJECTIVE' { TokKeyword KwINJECTIVE $$ }
'INLINE' { TokKeyword KwINLINE $$ }
'NOINLINE' { TokKeyword KwNOINLINE $$ }
'MEASURE' { TokKeyword KwMEASURE $$ }
'NO_TERMINATION_CHECK' { TokKeyword KwNO_TERMINATION_CHECK $$ }
'NO_POSITIVITY_CHECK' { TokKeyword KwNO_POSITIVITY_CHECK $$ }
'NO_UNIVERSE_CHECK' { TokKeyword KwNO_UNIVERSE_CHECK $$ }
'NON_TERMINATING' { TokKeyword KwNON_TERMINATING $$ }
'OPTIONS' { TokKeyword KwOPTIONS $$ }
'POLARITY' { TokKeyword KwPOLARITY $$ }
'WARNING_ON_USAGE' { TokKeyword KwWARNING_ON_USAGE $$ }
'REWRITE' { TokKeyword KwREWRITE $$ }
'STATIC' { TokKeyword KwSTATIC $$ }
'TERMINATING' { TokKeyword KwTERMINATING $$ }
setN { TokSetN $$ }
propN { TokPropN $$ }
tex { TokTeX $$ }
comment { TokComment $$ }
'...' { TokSymbol SymEllipsis $$ }
'..' { TokSymbol SymDotDot $$ }
'.' { TokSymbol SymDot $$ }
';' { TokSymbol SymSemi $$ }
':' { TokSymbol SymColon $$ }
'=' { TokSymbol SymEqual $$ }
'_' { TokSymbol SymUnderscore $$ }
'?' { TokSymbol SymQuestionMark $$ }
'->' { TokSymbol SymArrow $$ }
'\\' { TokSymbol SymLambda $$ }
'@' { TokSymbol SymAs $$ }
'|' { TokSymbol SymBar $$ }
'(' { TokSymbol SymOpenParen $$ }
')' { TokSymbol SymCloseParen $$ }
'(|' { TokSymbol SymOpenIdiomBracket $$ }
'|)' { TokSymbol SymCloseIdiomBracket $$ }
'{{' { TokSymbol SymDoubleOpenBrace $$ }
'}}' { TokSymbol SymDoubleCloseBrace $$ }
'{' { TokSymbol SymOpenBrace $$ }
'}' { TokSymbol SymCloseBrace $$ }
-- ':{' { TokSymbol SymColonBrace $$ }
vopen { TokSymbol SymOpenVirtualBrace $$ }
vclose { TokSymbol SymCloseVirtualBrace $$ }
vsemi { TokSymbol SymVirtualSemi $$ }
'{-#' { TokSymbol SymOpenPragma $$ }
'#-}' { TokSymbol SymClosePragma $$ }
id { TokId $$ }
q_id { TokQId $$ }
string { TokString $$ }
literal { TokLiteral $$ }
%%
{--------------------------------------------------------------------------
Parsing the token stream. Used by the TeX compiler.
--------------------------------------------------------------------------}
-- Parse a list of tokens.
Tokens :: { [Token] }
Tokens : TokensR { reverse $1 }
-- Happy is much better at parsing left recursive grammars (constant
-- stack size vs. linear stack size for right recursive).
TokensR :: { [Token] }
TokensR : TokensR Token { $2 : $1 }
| { [] }
-- Parse single token.
Token :: { Token }
Token
: 'abstract' { TokKeyword KwAbstract $1 }
| 'codata' { TokKeyword KwCoData $1 }
| 'coinductive' { TokKeyword KwCoInductive $1 }
| 'constructor' { TokKeyword KwConstructor $1 }
| 'data' { TokKeyword KwData $1 }
| 'eta-equality' { TokKeyword KwEta $1 }
| 'field' { TokKeyword KwField $1 }
| 'forall' { TokKeyword KwForall $1 }
| 'variable' { TokKeyword KwVariable $1 }
| 'hiding' { TokKeyword KwHiding $1 }
| 'import' { TokKeyword KwImport $1 }
| 'in' { TokKeyword KwIn $1 }
| 'inductive' { TokKeyword KwInductive $1 }
| 'infix' { TokKeyword KwInfix $1 }
| 'infixl' { TokKeyword KwInfixL $1 }
| 'infixr' { TokKeyword KwInfixR $1 }
| 'instance' { TokKeyword KwInstance $1 }
| 'overlap' { TokKeyword KwOverlap $1 }
| 'let' { TokKeyword KwLet $1 }
| 'macro' { TokKeyword KwMacro $1 }
| 'module' { TokKeyword KwModule $1 }
| 'mutual' { TokKeyword KwMutual $1 }
| 'no-eta-equality' { TokKeyword KwNoEta $1 }
| 'open' { TokKeyword KwOpen $1 }
| 'pattern' { TokKeyword KwPatternSyn $1 }
| 'postulate' { TokKeyword KwPostulate $1 }
| 'primitive' { TokKeyword KwPrimitive $1 }
| 'private' { TokKeyword KwPrivate $1 }
| 'Prop' { TokKeyword KwProp $1 }
| 'public' { TokKeyword KwPublic $1 }
| 'quote' { TokKeyword KwQuote $1 }
| 'quoteContext' { TokKeyword KwQuoteContext $1 }
| 'quoteGoal' { TokKeyword KwQuoteGoal $1 }
| 'quoteTerm' { TokKeyword KwQuoteTerm $1 }
| 'record' { TokKeyword KwRecord $1 }
| 'renaming' { TokKeyword KwRenaming $1 }
| 'rewrite' { TokKeyword KwRewrite $1 }
| 'Set' { TokKeyword KwSet $1 }
| 'syntax' { TokKeyword KwSyntax $1 }
| 'tactic' { TokKeyword KwTactic $1 }
| 'to' { TokKeyword KwTo $1 }
| 'unquote' { TokKeyword KwUnquote $1 }
| 'unquoteDecl' { TokKeyword KwUnquoteDecl $1 }
| 'unquoteDef' { TokKeyword KwUnquoteDef $1 }
| 'using' { TokKeyword KwUsing $1 }
| 'where' { TokKeyword KwWhere $1 }
| 'do' { TokKeyword KwDo $1 }
| 'with' { TokKeyword KwWith $1 }
| 'BUILTIN' { TokKeyword KwBUILTIN $1 }
| 'CATCHALL' { TokKeyword KwCATCHALL $1 }
| 'DISPLAY' { TokKeyword KwDISPLAY $1 }
| 'ETA' { TokKeyword KwETA $1 }
| 'FOREIGN' { TokKeyword KwFOREIGN $1 }
| 'COMPILE' { TokKeyword KwCOMPILE $1 }
| 'IMPOSSIBLE' { TokKeyword KwIMPOSSIBLE $1 }
| 'INJECTIVE' { TokKeyword KwINJECTIVE $1 }
| 'INLINE' { TokKeyword KwINLINE $1 }
| 'NOINLINE' { TokKeyword KwNOINLINE $1 }
| 'MEASURE' { TokKeyword KwMEASURE $1 }
| 'NO_TERMINATION_CHECK' { TokKeyword KwNO_TERMINATION_CHECK $1 }
| 'NO_POSITIVITY_CHECK' { TokKeyword KwNO_POSITIVITY_CHECK $1 }
| 'NO_UNIVERSE_CHECK' { TokKeyword KwNO_UNIVERSE_CHECK $1 }
| 'NON_TERMINATING' { TokKeyword KwNON_TERMINATING $1 }
| 'OPTIONS' { TokKeyword KwOPTIONS $1 }
| 'POLARITY' { TokKeyword KwPOLARITY $1 }
| 'REWRITE' { TokKeyword KwREWRITE $1 }
| 'STATIC' { TokKeyword KwSTATIC $1 }
| 'TERMINATING' { TokKeyword KwTERMINATING $1 }
| 'WARNING_ON_USAGE' { TokKeyword KwWARNING_ON_USAGE $1 }
| setN { TokSetN $1 }
| propN { TokPropN $1 }
| tex { TokTeX $1 }
| comment { TokComment $1 }
| '...' { TokSymbol SymEllipsis $1 }
| '..' { TokSymbol SymDotDot $1 }
| '.' { TokSymbol SymDot $1 }
| ';' { TokSymbol SymSemi $1 }
| ':' { TokSymbol SymColon $1 }
| '=' { TokSymbol SymEqual $1 }
| '_' { TokSymbol SymUnderscore $1 }
| '?' { TokSymbol SymQuestionMark $1 }
| '->' { TokSymbol SymArrow $1 }
| '\\' { TokSymbol SymLambda $1 }
| '@' { TokSymbol SymAs $1 }
| '|' { TokSymbol SymBar $1 }
| '(' { TokSymbol SymOpenParen $1 }
| ')' { TokSymbol SymCloseParen $1 }
| '(|' { TokSymbol SymOpenIdiomBracket $1 }
| '|)' { TokSymbol SymCloseIdiomBracket $1 }
| '{{' { TokSymbol SymDoubleOpenBrace $1 }
| '}}' { TokSymbol SymDoubleCloseBrace $1 }
| '{' { TokSymbol SymOpenBrace $1 }
| '}' { TokSymbol SymCloseBrace $1 }
| vopen { TokSymbol SymOpenVirtualBrace $1 }
| vclose { TokSymbol SymCloseVirtualBrace $1 }
| vsemi { TokSymbol SymVirtualSemi $1 }
| '{-#' { TokSymbol SymOpenPragma $1 }
| '#-}' { TokSymbol SymClosePragma $1 }
| id { TokId $1 }
| q_id { TokQId $1 }
| string { TokString $1 }
| literal { TokLiteral $1 }
{--------------------------------------------------------------------------
Top level
--------------------------------------------------------------------------}
File :: { ([Pragma], [Declaration]) }
File : vopen TopLevel maybe_vclose { takeOptionsPragmas $2 }
maybe_vclose :: { () }
maybe_vclose : {- empty -} { () }
| vclose { () }
{--------------------------------------------------------------------------
Meta rules
--------------------------------------------------------------------------}
-- The first token in a file decides the indentation of the top-level layout
-- block. Or not. It will if we allow the top-level module to be omitted.
-- topen : {- empty -} {% pushCurrentContext }
{- A layout block might have to be closed by a parse error. Example:
let x = e in e'
Here the 'let' starts a layout block which should end before the 'in'. The
problem is that the lexer doesn't know this, so there is no virtual close
brace. However when the parser sees the 'in' there will be a parse error.
This is our cue to close the layout block.
-}
close :: { () }
close : vclose { () }
| error {% popContext }
-- You can use concrete semi colons in a layout block started with a virtual
-- brace, so we don't have to distinguish between the two semi colons. You can't
-- use a virtual semi colon in a block started by a concrete brace, but this is
-- simply because the lexer will not generate virtual semis in this case.
semi :: { Interval }
semi : ';' { $1 }
| vsemi { $1 }
-- Enter the 'imp_dir' lex state, where we can parse the keyword 'to'.
beginImpDir :: { () }
beginImpDir : {- empty -} {% pushLexState imp_dir }
{--------------------------------------------------------------------------
Helper rules
--------------------------------------------------------------------------}
-- An integer. Used in fixity declarations.
Int :: { Integer }
Int : literal {% case $1 of {
LitNat _ i -> return i;
_ -> parseError $ "Expected integer"
}
}
{--------------------------------------------------------------------------
Names
--------------------------------------------------------------------------}
-- A name is really a sequence of parts, but the lexer just sees it as a
-- string, so we have to do the translation here.
Id :: { Name }
Id : id {% mkName $1 }
-- Space separated list of one or more identifiers.
SpaceIds :: { [Name] }
SpaceIds
: Id SpaceIds { $1 : $2 }
| Id { [$1] }
-- When looking for a double closed brace, we accept either a single token '}}'
-- (which is what the unicode character "RIGHT WHITE CURLY BRACKET" is
-- postprocessed into in LexActions.hs), but also two consecutive tokens '}'
-- (which a string '}}' is lexed to). This small hack allows us to keep
-- "record { a = record { }}" working. In the second case, we check that the two
-- tokens '}' are immediately consecutive.
DoubleCloseBrace :: { Range }
DoubleCloseBrace
: '}}' { getRange $1 }
| '}' '}' {%
if posPos (fromJust (rEnd' (getRange $2))) -
posPos (fromJust (rStart' (getRange $1))) > 2
then parseErrorRange $2 "Expecting '}}', found separated '}'s."
else return $ getRange ($1, $2)
}
-- A possibly dotted identifier.
MaybeDottedId :: { Arg Name }
MaybeDottedId
: '..' Id { setRelevance NonStrict $ defaultArg $2 }
| '.' Id { setRelevance Irrelevant $ defaultArg $2 }
| Id { defaultArg $1 }
-- Space separated list of one or more possibly dotted identifiers.
MaybeDottedIds :: { [Arg Name] }
MaybeDottedIds
: MaybeDottedId MaybeDottedIds { $1 : $2 }
| MaybeDottedId { [$1] }
-- Space separated list of one or more identifiers, some of which may
-- be surrounded by braces or dotted.
ArgIds :: { [Arg Name] }
ArgIds
: MaybeDottedId ArgIds { $1 : $2 }
| MaybeDottedId { [$1] }
| '{{' MaybeDottedIds DoubleCloseBrace ArgIds { map makeInstance $2 ++ $4 }
| '{{' MaybeDottedIds DoubleCloseBrace { map makeInstance $2 }
| '{' MaybeDottedIds '}' ArgIds { map hide $2 ++ $4 }
| '{' MaybeDottedIds '}' { map hide $2 }
| '.' '{' SpaceIds '}' ArgIds { map (hide . setRelevance Irrelevant . defaultArg) $3 ++ $5 }
| '.' '{' SpaceIds '}' { map (hide . setRelevance Irrelevant . defaultArg) $3 }
| '.' '{{' SpaceIds DoubleCloseBrace ArgIds { map (makeInstance . setRelevance Irrelevant . defaultArg) $3 ++ $5 }
| '.' '{{' SpaceIds DoubleCloseBrace { map (makeInstance . setRelevance Irrelevant . defaultArg) $3 }
| '..' '{' SpaceIds '}' ArgIds { map (hide . setRelevance NonStrict . defaultArg) $3 ++ $5 }
| '..' '{' SpaceIds '}' { map (hide . setRelevance NonStrict . defaultArg) $3 }
| '..' '{{' SpaceIds DoubleCloseBrace ArgIds { map (makeInstance . setRelevance NonStrict . defaultArg) $3 ++ $5 }
| '..' '{{' SpaceIds DoubleCloseBrace { map (makeInstance . setRelevance NonStrict . defaultArg) $3 }
-- Modalities preceeding identifiers
ModalArgIds :: { [Arg Name] }
ModalArgIds : Attributes ArgIds {% mapM (applyAttrs $1) $2 }
-- Attributes are parsed as '@' followed by an atomic expression.
Attribute :: { Attr }
Attribute : '@' ExprOrAttr {% setRange (getRange ($1,$2)) `fmap` toAttribute $2 }
-- Parse a reverse list of modalities
Attributes :: { [Attr] }
Attributes : {- empty -} { [] }
| Attributes Attribute { $2 : $1 }
Attributes1 :: { [Attr] }
Attributes1 : Attribute { [$1] }
| Attributes1 Attribute { $2 : $1 }
QId :: { QName }
QId : q_id {% mkQName $1 }
| Id { QName $1 }
-- A module name is just a qualified name
ModuleName :: { QName }
ModuleName : QId { $1 }
-- A binding variable. Can be '_'
BId :: { Name }
BId : Id { $1 }
| '_' { Name (getRange $1) InScope [Hole] }
{- UNUSED
-- A binding variable. Can be '_'
MaybeDottedBId :: { (Relevance, Name) }
MaybeDottedBId
: BId { (Relevant , $1) }
| '.' BId { (Irrelevant, $2) }
| '..' BId { (NonStrict, $2) }
-}
-- Space separated list of binding identifiers. Used in fixity
-- declarations infixl 100 + -
SpaceBIds :: { [Name] }
SpaceBIds
: BId SpaceBIds { $1 : $2 }
| BId { [$1] }
{- DOES PRODUCE REDUCE/REDUCE CONFLICTS!
-- Space-separated list of binding identifiers. Used in dependent
-- function spaces: (x y z : Nat) -> ...
-- (Used to be comma-separated; hence the name)
-- QUESTION: Should this be replaced by SpaceBIds above?
--CommaBIds :: { [(Relevance,Name)] }
CommaBIds :: { [Name] }
CommaBIds
: CommaBIds BId { $1 ++ [$2] } -- SWITCHING DOES NOT HELP
| BId { [$1] }
-}
-- Space-separated list of binding identifiers. Used in dependent
-- function spaces: (x y z : Nat) -> ...
-- (Used to be comma-separated; hence the name)
-- QUESTION: Should this be replaced by SpaceBIds above?
-- Andreas, 2011-04-07 the trick avoids reduce/reduce conflicts
-- when parsing (x y z : A) -> B
-- at point (x y it is not clear whether x y is an application or
-- a variable list. We could be parsing (x y z) -> B
-- with ((x y) z) being a type.
CommaBIds :: { [NamedArg BoundName] }
CommaBIds : CommaBIdAndAbsurds {%
case $1 of
Left ns -> return ns
Right _ -> parseError $ "expected sequence of bound identifiers, not absurd pattern"
}
CommaBIdAndAbsurds :: { Either [NamedArg BoundName] [Expr] }
CommaBIdAndAbsurds
: Application {% boundNamesOrAbsurd $1 }
| QId '=' QId {% fmap (Left . (:[])) $ mkNamedArg (Just $1) (Left $3) }
| '_' '=' QId {% fmap (Left . (:[])) $ mkNamedArg Nothing (Left $3) }
| QId '=' '_' {% fmap (Left . (:[])) $ mkNamedArg (Just $1) (Right $ getRange $3) }
| '_' '=' '_' {% fmap (Left . (:[])) $ mkNamedArg Nothing (Right $ getRange $3) }
-- Parse a sequence of identifiers, including hiding info.
-- Does not include instance arguments.
-- E.g. x {y z} _ {v}
-- To be used in typed bindings, like (x {y z} _ {v} : Nat).
BIdsWithHiding :: { [NamedArg BoundName] }
BIdsWithHiding : Application {%
let -- interpret an expression as name
getName :: Expr -> Maybe Name
getName (Ident (QName x)) = Just x
getName (Underscore r _) = Just (Name r InScope [Hole])
getName _ = Nothing
getNames :: Expr -> Maybe [Name]
getNames (RawApp _ es) = mapM getName es
getNames e = singleton `fmap` getName e
-- interpret an expression as name or list of hidden names
getName1 :: Expr -> Maybe [Arg Name]
getName1 (Ident (QName x)) = Just [defaultArg x]
getName1 (Underscore r _) = Just [defaultArg $ Name r InScope [Hole]]
getName1 (HiddenArg _ (Named Nothing e))
= map (setHiding Hidden . defaultArg) `fmap` getNames e
getName1 _ = Nothing
in
case mapM getName1 $1 of
Just good -> return $ (map . fmap) (unnamed . mkBoundName_) $ concat good
Nothing -> parseError $ "expected sequence of possibly hidden bound identifiers"
}
-- Space separated list of strings in a pragma.
PragmaStrings :: { [String] }
PragmaStrings
: {- empty -} { [] }
| string PragmaStrings { snd $1 : $2 }
PragmaString :: { String }
PragmaString
: string { snd $1 }
Strings :: { [(Interval, String)] }
Strings : {- empty -} { [] }
| string Strings { $1 : $2 }
ForeignCode :: { [(Interval, String)] }
ForeignCode
: {- empty -} { [] }
| string ForeignCode { $1 : $2 }
| '{-#' ForeignCode '#-}' ForeignCode { [($1, "{-#")] ++ $2 ++ [($3, "#-}")] ++ $4 }
PragmaName :: { Name }
PragmaName : string {% mkName $1 }
PragmaQName :: { QName }
PragmaQName : string {% pragmaQName $1 } -- Issue 2125. WAS: string {% fmap QName (mkName $1) }
PragmaQNames :: { [QName] }
PragmaQNames : Strings {% mapM pragmaQName $1 }
{--------------------------------------------------------------------------
Expressions (terms and types)
--------------------------------------------------------------------------}
{- Expressions. You might expect lambdas and lets to appear in the first
expression category (lowest precedence). The reason they don't is that we
want to parse things like
m >>= \x -> k x
This will leads to a conflict in the following case
m >>= \x -> k x >>= \y -> k' y
At the second '>>=' we can either shift or reduce. We solve this problem
using Happy's precedence directives. The rule 'Expr -> Expr1' (which is the
rule you shouldn't use to reduce when seeing '>>=') is given LOWEST
precedence. The terminals '->' and op (which is what you should shift)
is given higher precedence.
-}
-- Top level: Function types.
Expr :: { Expr }
Expr
: TeleArrow Expr { Pi $1 $2 }
| Application3 '->' Expr { Fun (getRange ($1,$2,$3))
(defaultArg $ RawApp (getRange $1) $1)
$3 }
| Attributes1 Application3 '->' Expr {% applyAttrs $1 (defaultArg $ RawApp (getRange ($1,$2)) $2) <&> \ dom ->
Fun (getRange ($1,$2,$3,$4)) dom $4 }
| Expr1 '=' Expr { Equal (getRange ($1, $2, $3)) $1 $3 }
| Expr1 %prec LOWEST { $1 }
-- Level 1: Application
Expr1 :: { Expr }
Expr1 : WithExprs {% case $1 of
{ [e] -> return e
; e : es -> return $ WithApp (fuseRange e es) e es
; [] -> parseError "impossible: empty with expressions"
}
}
WithExprs :: { [Expr] }
WithExprs
: Application3 '|' WithExprs { RawApp (getRange $1) $1 : $3 }
| Application { [RawApp (getRange $1) $1] }
Application :: { [Expr] }
Application
: Expr2 { [$1] }
| Expr3 Application { $1 : $2 }
-- Level 2: Lambdas and lets
Expr2 :: { Expr }
Expr2
: '\\' LamBindings Expr { Lam (getRange ($1,$2,$3)) $2 $3 }
| ExtendedOrAbsurdLam { $1 }
| 'forall' ForallBindings Expr { forallPi $2 $3 }
| 'let' Declarations LetBody { Let (getRange ($1,$2,$3)) $2 $3 }
| 'do' vopen DoStmts close { DoBlock (getRange ($1, $3)) $3 }
| Expr3 { $1 }
| 'quoteGoal' Id 'in' Expr { QuoteGoal (getRange ($1,$2,$3,$4)) $2 $4 }
| 'tactic' Application3 { Tactic (getRange ($1, $2)) (RawApp (getRange $2) $2) [] }
| 'tactic' Application3 '|' WithExprs { Tactic (getRange ($1, $2, $3, $4)) (RawApp (getRange $2) $2) $4 }
LetBody :: { Maybe Expr }
LetBody : 'in' Expr { Just $2 }
| {- empty -} { Nothing }
ExtendedOrAbsurdLam :: { Expr }
ExtendedOrAbsurdLam
: '\\' '{' LamClauses '}' { ExtendedLam (getRange ($1,$2,$3,$4)) (reverse $3) }
| '\\' 'where' vopen LamWhereClauses close { ExtendedLam (getRange ($1, $2, $4)) (reverse $4) }
| '\\' AbsurdLamBindings {% case $2 of
Left (bs, h) -> if null bs then return $ AbsurdLam r h else
return $ Lam r bs (AbsurdLam r h)
where r = fuseRange $1 bs
Right es -> do -- it is of the form @\ { p1 ... () }@
p <- exprToLHS (RawApp (getRange es) es);
return $ ExtendedLam (fuseRange $1 es)
[LamClause (p [] []) AbsurdRHS NoWhere False]
}
Application3 :: { [Expr] }
Application3
: Expr3 { [$1] }
| Expr3 Application3 { $1 : $2 }
-- Christian Sattler, 2017-08-04, issue #2671
-- We allow empty lists of expressions for the LHS of extended lambda clauses.
-- I am not sure what Application3 is otherwise used for, so I keep the
-- original type and create this copy solely for extended lambda clauses.
Application3PossiblyEmpty :: { [Expr] }
Application3PossiblyEmpty
: { [] }
| Expr3 Application3PossiblyEmpty { $1 : $2 }
-- Level 3: Atoms
Expr3Curly :: { Expr }
Expr3Curly
: '{' Expr '}' { HiddenArg (getRange ($1,$2,$3)) (maybeNamed $2) }
| '{' '}' { let r = fuseRange $1 $2 in HiddenArg r $ unnamed $ Absurd r }
Expr3NoCurly :: { Expr }
Expr3NoCurly
: '?' { QuestionMark (getRange $1) Nothing }
| '_' { Underscore (getRange $1) Nothing }
| 'Prop' { Prop (getRange $1) }
| 'Set' { Set (getRange $1) }
| 'quote' { Quote (getRange $1) }
| 'quoteTerm' { QuoteTerm (getRange $1) }
| 'quoteContext' { QuoteContext (getRange $1) }
| 'unquote' { Unquote (getRange $1) }
| setN { SetN (getRange (fst $1)) (snd $1) }
| propN { PropN (getRange (fst $1)) (snd $1) }
| '{{' Expr DoubleCloseBrace { InstanceArg (getRange ($1,$2,$3)) (maybeNamed $2) }
| '(|' Expr '|)' { IdiomBrackets (getRange ($1,$2,$3)) $2 }
| '(' ')' { Absurd (fuseRange $1 $2) }
| '{{' DoubleCloseBrace { let r = fuseRange $1 $2 in InstanceArg r $ unnamed $ Absurd r }
| Id '@' Expr3 { As (getRange ($1,$2,$3)) $1 $3 }
| '.' Expr3 { Dot (fuseRange $1 $2) $2 }
| 'record' '{' RecordAssignments '}' { Rec (getRange ($1,$2,$3,$4)) $3 }
| 'record' Expr3NoCurly '{' FieldAssignments '}' { RecUpdate (getRange ($1,$2,$3,$4,$5)) $2 $4 }
| '...' { Ellipsis (getRange $1) }
| ExprOrAttr { $1 }
ExprOrAttr :: { Expr }
ExprOrAttr
: QId { Ident $1 }
| literal { Lit $1 }
| '(' Expr ')' { Paren (getRange ($1,$2,$3)) $2 }
Expr3 :: { Expr }
Expr3
: Expr3Curly { $1 }
| Expr3NoCurly { $1 }
RecordAssignments :: { RecordAssignments }
RecordAssignments
: {- empty -} { [] }
| RecordAssignments1 { $1 }
RecordAssignments1 :: { RecordAssignments }
RecordAssignments1
: RecordAssignment { [$1] }
| RecordAssignment ';' RecordAssignments1 { $1 : $3 }
RecordAssignment :: { RecordAssignment }
RecordAssignment
: FieldAssignment { Left $1 }
| ModuleAssignment { Right $1 }
ModuleAssignment :: { ModuleAssignment }
ModuleAssignment
: ModuleName OpenArgs ImportDirective { ModuleAssignment $1 $2 $3 }
FieldAssignments :: { [FieldAssignment] }
FieldAssignments
: {- empty -} { [] }
| FieldAssignments1 { $1 }
FieldAssignments1 :: { [FieldAssignment] }
FieldAssignments1
: FieldAssignment { [$1] }
| FieldAssignment ';' FieldAssignments1 { $1 : $3 }
FieldAssignment :: { FieldAssignment }
FieldAssignment
: Id '=' Expr { FieldAssignment $1 $3 }
{--------------------------------------------------------------------------
Bindings
--------------------------------------------------------------------------}
-- "Delta ->" to avoid conflict between Delta -> Gamma and Delta -> A.
TeleArrow :: { Telescope }
TeleArrow : Telescope1 '->' { $1 }
Telescope1 :: { Telescope }
Telescope1 : TypedBindings { $1 }
TypedBindings :: { [TypedBinding] }
TypedBindings
: TypedBinding TypedBindings { $1 : $2 }
| TypedBinding { [$1] }
-- A typed binding is either (x1 .. xn : A) or {y1 .. ym : B}
-- Andreas, 2011-04-07: or .(x1 .. xn : A) or .{y1 .. ym : B}
-- Andreas, 2011-04-27: or ..(x1 .. xn : A) or ..{y1 .. ym : B}
TypedBinding :: { TypedBinding }
TypedBinding
: '.' '(' TBindWithHiding ')' { setRange (getRange ($2,$3,$4)) $
setRelevance Irrelevant $3 }
| '.' '{' TBind '}' { setRange (getRange ($2,$3,$4)) $
setHiding Hidden $
setRelevance Irrelevant $3 }
| '.' '{{' TBind DoubleCloseBrace
{ setRange (getRange ($2,$3,$4)) $
makeInstance $
setRelevance Irrelevant $3 }
| '..' '(' TBindWithHiding ')' { setRange (getRange ($2,$3,$4)) $
setRelevance NonStrict $3 }
| '..' '{' TBind '}' { setRange (getRange ($2,$3,$4)) $
setHiding Hidden $
setRelevance NonStrict $3 }
| '..' '{{' TBind DoubleCloseBrace
{ setRange (getRange ($2,$3,$4)) $
makeInstance $
setRelevance NonStrict $3 }
| '(' TBindWithHiding ')' { setRange (getRange ($1,$2,$3)) $2 }
| '(' ModalTBindWithHiding ')' { setRange (getRange ($1,$2,$3)) $2 }
| '{{' TBind DoubleCloseBrace
{ setRange (getRange ($1,$2,$3)) $
makeInstance $2 }
| '{{' ModalTBind DoubleCloseBrace
{ setRange (getRange ($1,$2,$3)) $
makeInstance $2 }
| '{' TBind '}' { setRange (getRange ($1,$2,$3)) $
setHiding Hidden $2 }
| '{' ModalTBind '}' { setRange (getRange ($1,$2,$3)) $
setHiding Hidden $2 }
| '(' Open ')' { TLet (getRange ($1,$3)) $2 }
| '(' 'let' Declarations ')' { TLet (getRange ($1,$4)) $3 }
-- x1 .. xn : A
-- x1 .. xn :{i1 i2 ..} A
TBind :: { TypedBinding }
TBind : CommaBIds ':' Expr {
let r = getRange ($1,$2,$3) -- the range is approximate only for TypedBindings
in TBind r $1 $3
}
ModalTBind :: { TypedBinding }
ModalTBind : Attributes1 CommaBIds ':' Expr {% do
let r = getRange ($1,$2,$3,$4) -- the range is approximate only for TypedBindings
xs <- mapM (applyAttrs $1) $2
return $ TBind r xs $4
}
-- x {y z} _ {v} : A
TBindWithHiding :: { TypedBinding }
TBindWithHiding : BIdsWithHiding ':' Expr {
let r = getRange ($1,$2,$3) -- the range is approximate only for TypedBindings
in TBind r $1 $3
}
ModalTBindWithHiding :: { TypedBinding }
ModalTBindWithHiding : Attributes1 BIdsWithHiding ':' Expr {% do
let r = getRange ($1,$2,$3,$4) -- the range is approximate only for TypedBindings
xs <- mapM (applyAttrs $1) $2
return $ TBind r xs $4
}
-- A non-empty sequence of lambda bindings.
LamBindings :: { [LamBinding] }
LamBindings
: LamBinds '->' {%
case reverse $1 of
Left _ : _ -> parseError "Absurd lambda cannot have a body."
_ : _ -> return [ b | Right b <- $1 ]
[] -> parsePanic "Empty LamBinds"
}
AbsurdLamBindings :: { Either ([LamBinding], Hiding) [Expr] }
AbsurdLamBindings
: LamBindsAbsurd {%
case $1 of
Left lb -> case reverse lb of
Right _ : _ -> parseError "Missing body for lambda"
Left h : _ -> return $ Left ([ b | Right b <- init lb], h)
_ -> parseError "Unsupported variant of lambda"
Right es -> return $ Right es
}
-- absurd lambda is represented by @Left hiding@
LamBinds :: { [Either Hiding LamBinding] }
LamBinds
: DomainFreeBinding LamBinds { map Right $1 ++ $2 }
| TypedBinding LamBinds { Right (DomainFull $1) : $2 }
| DomainFreeBinding { map Right $1 }
| TypedBinding { [Right $ DomainFull $1] }
| '(' ')' { [Left NotHidden] }
| '{' '}' { [Left Hidden] }
| '{{' DoubleCloseBrace { [Left (Instance NoOverlap)] }
-- Like LamBinds, but could also parse an absurd LHS of an extended lambda @{ p1 ... () }@
LamBindsAbsurd :: { Either [Either Hiding LamBinding] [Expr] }
LamBindsAbsurd
: DomainFreeBinding LamBinds { Left $ map Right $1 ++ $2 }
| TypedBinding LamBinds { Left $ Right (DomainFull $1) : $2 }
| DomainFreeBindingAbsurd { case $1 of
Left lb -> Left $ map Right lb
Right es -> Right es }
| TypedBinding { Left [Right $ DomainFull $1] }
| '(' ')' { Left [Left NotHidden] }
| '{' '}' { Left [Left Hidden] }
| '{{' DoubleCloseBrace { Left [Left (Instance NoOverlap)] }
-- FNF, 2011-05-05: No where clauses in extended lambdas for now
NonAbsurdLamClause :: { LamClause }
NonAbsurdLamClause
: Application3PossiblyEmpty '->' Expr {% do
p <- exprToLHS (RawApp (getRange $1) $1) ;
return LamClause{ lamLHS = p [] []
, lamRHS = RHS $3
, lamWhere = NoWhere
, lamCatchAll = False }
}
| CatchallPragma Application3PossiblyEmpty '->' Expr {% do
p <- exprToLHS (RawApp (getRange $2) $2) ;
return LamClause{ lamLHS = p [] []
, lamRHS = RHS $4
, lamWhere = NoWhere
, lamCatchAll = True }
}
AbsurdLamClause :: { LamClause }
AbsurdLamClause
-- FNF, 2011-05-09: By being more liberal here, we avoid shift/reduce and reduce/reduce errors.
-- Later stages such as scope checking will complain if we let something through which we should not
: Application {% do
p <- exprToLHS (RawApp (getRange $1) $1);
return LamClause{ lamLHS = p [] []
, lamRHS = AbsurdRHS
, lamWhere = NoWhere
, lamCatchAll = False }
}
| CatchallPragma Application {% do
p <- exprToLHS (RawApp (getRange $2) $2);
return LamClause{ lamLHS = p [] []
, lamRHS = AbsurdRHS
, lamWhere = NoWhere
, lamCatchAll = True }
}
LamClause :: { LamClause }
LamClause
: NonAbsurdLamClause { $1 }
| AbsurdLamClause { $1 }
-- Parses all extended lambda clauses except for a single absurd clause, which is taken care of
-- in AbsurdLambda
LamClauses :: { [LamClause] }
LamClauses
: LamClauses semi LamClause { $3 : $1 }
| AbsurdLamClause semi LamClause { [$3, $1] }
| NonAbsurdLamClause { [$1] }
-- | {- empty -} { [] }
-- Parses all extended lambda clauses including a single absurd clause. For λ
-- where this is not taken care of in AbsurdLambda
LamWhereClauses :: { [LamClause] }
LamWhereClauses
: LamWhereClauses semi LamClause { $3 : $1 }
| LamClause { [$1] }
ForallBindings :: { [LamBinding] }
ForallBindings
: TypedUntypedBindings1 '->' { $1 }
-- A non-empty sequence of possibly untyped bindings.
TypedUntypedBindings1 :: { [LamBinding] }
TypedUntypedBindings1
: DomainFreeBinding TypedUntypedBindings1 { $1 ++ $2 }
| TypedBinding TypedUntypedBindings1 { DomainFull $1 : $2 }
| DomainFreeBinding { $1 }
| TypedBinding { [DomainFull $1] }
-- A possibly empty sequence of possibly untyped bindings.
-- This is used as telescope in data and record decls.
TypedUntypedBindings :: { [LamBinding] }
TypedUntypedBindings
: DomainFreeBinding TypedUntypedBindings { $1 ++ $2 }
| TypedBinding TypedUntypedBindings { DomainFull $1 : $2 }
| { [] }
-- A domain free binding is either x or {x1 .. xn}
DomainFreeBinding :: { [LamBinding] }
DomainFreeBinding
: DomainFreeBindingAbsurd {% case $1 of
Left lbs -> return lbs
Right _ -> parseError "expected sequence of bound identifiers, not absurd pattern"
}
-- A domain free binding is either x or {x1 .. xn}
DomainFreeBindingAbsurd :: { Either [LamBinding] [Expr]}
DomainFreeBindingAbsurd
: BId { Left [DomainFree $ defaultNamedArg $ mkBoundName_ $1] }
| '.' BId { Left [DomainFree $ setRelevance Irrelevant $ defaultNamedArg $ mkBoundName_ $2] }
| '..' BId { Left [DomainFree $ setRelevance NonStrict $ defaultNamedArg $ mkBoundName_ $2] }