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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
|
-----------------------------------------------------------------------------
(c) Simon Marlow, Sven Panne 1997-2000
Haskell grammar.
-----------------------------------------------------------------------------
-- Adapted to Frown, Ralf Hinze, 2001
Compile me with
frown --lexer --expected --optimize --signature HsParser.lg
ghc -c -v -Rghc-timing HsParser.hs +RTS -c -M150M
frown --lexer --expected --signature --optimize --code=compact HsParser.lg
ghc -c -v -Rghc-timing HsParser.hs +RTS -c -M150M
ghc -c -v -O -Rghc-timing HsParser.hs +RTS -c -M150M
frown --lexer --expected --signature --optimize --code=stackless HsParser.lg
ghc -c -v -Rghc-timing HsParser.hs +RTS -c -M150M
ghc -c -v -O -Rghc-timing HsParser.hs +RTS -c -M150M
frown --lexer --expected --signature --optimize --code=gvstack HsParser.lg
ToDo: Is (,) valid as exports? We don't allow it.
ToDo: Check exactly which names must be qualified with Prelude (commas and friends)
ToDo: Inst (MPCs?)
ToDo: Polish constr a bit
ToDo: Ugly: infixexp is used for lhs, pat, exp0, ...
ToDo: Differentiate between record updates and labeled construction.
Conflicts: 10 shift/reduce
7 for abiguity in 'if x then y else z + 1'
(shift parses as 'if x then y else (z + 1)', as per longest-parse rule)
1 for ambiguity in 'if x then y else z :: T'
(shift parses as 'if x then y else (z :: T)', as per longest-parse rule)
2 for ambiguity in 'case x of y :: a -> b'
(don't know whether to reduce 'a' as a btype or shift the '->'.
conclusion: bogus expression anyway, doesn't matter)
> module HsParser (xmodule) where
>
> import HsSyn
> import HsParseMonad hiding ( Result )
> import HsLexer
> import HsParseUtils
> import List
> import Monad
>
> type Terminal = Token
> type Result a = Lex a
>
> -- frown t = parseError ("syntax error: " ++ show t)
> frown la t = parseError ("syntax error: " ++ show t
> ++ "\nexpected: " ++ concat (intersperse ", " (map wrap la)))
>
> wrap :: String -> String
> wrap "" = ""
> wrap s
> | head s == '<' && last s == '>'
> = s
> | otherwise = "`" ++ s ++ "'"
>
> %{
>
> Terminal = VarId {String} as "<varid>"
> | QVarId {(String,String)} as "<qualified varid> "
> | ConId {String} as "<conid>"
> | QConId {(String,String)} as "<qualified conid>"
> | VarSym {String} as "<varsym>"
> | QVarSym {(String,String)} as "<qualified varsym>"
> | ConSym {String} as "<consym>"
> | QConSym {(String,String)} as "<qualified consym>"
> | IntTok {String} as "<integer numeral>"
> | FloatTok {String} as "<float numeral>"
> | Character {Char} as "<char literal>"
> | StringTok {String} as "<string literal>"
Symbols.
> | LeftParen as "("
> | RightParen as ")"
> | SemiColon as ";"
> | LeftCurly as "{"
> | RightCurly as "}"
> | VRightCurly as "virtual }"
> | LeftSquare as "["
> | RightSquare as "]"
> | Comma as ","
> | Underscore as "_"
> | BackQuote as "`"
Reserved operators.
> | DotDot as ".."
> | DoubleColon as "::"
> | Equals as "="
> | Backslash as "\\"
> | Bar as "|"
> | LeftArrow as "<-"
> | RightArrow as "->"
> | At as "@"
> | Tilde as "~"
> | DoubleArrow as "=>"
> | Minus as "-"
> | Exclamation as "!"
Reserved Ids.
> | KW_As as "as"
> | KW_Case as "case"
> | KW_Class as "class"
> | KW_Data as "data"
> | KW_Default as "default"
> | KW_Deriving as "deriving"
> | KW_Do as "do"
> | KW_Else as "else"
> | KW_Hiding as "hiding"
> | KW_If as "if"
> | KW_Import as "import"
> | KW_In as "in"
> | KW_Infix as "infix"
> | KW_InfixL as "infixl"
> | KW_InfixR as "infixr"
> | KW_Instance as "instance"
> | KW_Let as "let"
> | KW_Module as "module"
> | KW_NewType as "newtype"
> | KW_Of as "of"
> | KW_Then as "then"
> | KW_Type as "type"
> | KW_Where as "where"
> | KW_Qualified as "qualified"
>
> | *EOF as "<end of input>";
>
> {-
> Nonterminal = xmodule {HsModule}
> | body {([HsImportDecl],[HsDecl])}
> | bodyaux {([HsImportDecl],[HsDecl])}
> | optsemi
> | maybeexports {Maybe [HsExportSpec]}
> | exports {[HsExportSpec]}
> | maybecomma
> | exportlist {[HsExportSpec]}
> | export {HsExportSpec}
> | qcnames {[HsQName]}
> | qcname {HsQName}
> | impdecls {[HsImportDecl]}
> | impdecl {HsImportDecl}
> | optqualified {Bool}
> | maybeas {Maybe Module}
> | maybeimpspec {Maybe (Bool, [HsImportSpec])}
> | impspec {(Bool, [HsImportSpec])}
> | importlist {[HsImportSpec]}
> | import {HsImportSpec}
> | cnames {[HsName]}
> | cname {HsName}
> | fixdecl {HsDecl}
> | precedence {Int}
> | infix {HsAssoc}
> | ops {[HsName]}
> | topdecls {[HsDecl]}
> | topdecl {HsDecl}
> | decls {[HsDecl]}
> | decls1 {[HsDecl]}
> | decl {HsDecl}
> | decllist {[HsDecl]}
> | signdecl {HsDecl}
> | vars {[HsName]}
> | type {HsType}
> | btype {HsType}
> | atype {HsType}
> | gtycon {HsQName}
> | ctype {HsQualType}
> | types {[HsType]}
> | simpletype {(HsName, [HsName])}
> | tyvars {[HsName]}
> | constrs {[HsConDecl]}
> | constr {HsConDecl}
> | scontype {(HsName, [HsBangType])}
> | scontype1 {(HsName, [HsBangType])}
> | satype {HsBangType}
> | sbtype {HsBangType}
> | fielddecls {[([HsName],HsBangType)]}
> | fielddecl {([HsName],HsBangType)}
> | stype {HsBangType}
> | deriving {[HsQName]}
> | dclasses {[HsQName]}
> | optcbody {[HsDecl]}
> | cbody {[HsDecl]}
> | cmethods {[HsDecl]}
> | cdefaults {[HsDecl]}
> | optvaldefs {[HsDecl]}
> | valdefs {[HsDecl]}
> | valdef {HsDecl}
> | rhs {HsRhs}
> | gdrhs {[HsGuardedRhs]}
> | gdrh {HsGuardedRhs}
> | exp {HsExp}
> | infixexp {HsExp}
> | exp10 {HsExp}
> | fexp {HsExp}
> | aexps {[HsExp]}
> | aexp {HsExp}
> | aexp1 {HsExp}
> | commas {Int}
> | texps {[HsExp]}
> | listexp {HsExp}
> | lexps {[HsExp]}
> | quals {[HsStmt]}
> | qual {HsStmt}
> | altslist {[HsAlt]}
> | alts {[HsAlt]}
> | alt {HsAlt}
> | ralt {HsGuardedAlts}
> | gdpats {[HsGuardedAlt]}
> | gdpat {HsGuardedAlt}
> | stmtlist {[HsStmt]}
> | stmts {[HsStmt]}
> | stmts1 {[HsStmt]}
> | fbinds {[HsFieldUpdate]}
> | fbind {HsFieldUpdate}
> | gcon {HsExp}
> | var {HsName}
> | qvar {HsQName}
> | con {HsName}
> | qcon {HsQName}
> | varop {HsName}
> | qvarop {HsQName}
> | qvaropm {HsQName}
> | conop {HsName}
> | qconop {HsQName}
> | op {HsName}
> | qop {HsExp}
> | qopm {HsExp}
> | qvarid {HsQName}
> | varid {HsName}
> | qconid {HsQName}
> | conid {HsName}
> | qconsym {HsQName}
> | consym {HsName}
> | qvarsym {HsQName}
> | qvarsymm {HsQName}
> | varsym {HsName}
> | varsymm {HsName}
> | qvarsym1 {HsQName}
> | literal {HsExp}
> | srcloc {SrcLoc}
> | close {()}
> | layout_on {()}
> | modid {Module}
> | tyconorcls {HsName}
> | tycon {HsName}
> | qtyconorcls {HsQName}
> | qtycls {HsQName}
> | tyvar {HsName};
> -}
Module Header.
> xmodule {HsModule};
> xmodule {mkModule x2 x3 x5}
> : KW_Module, modid {x2}, maybeexports {x3}, KW_Where, body {x5};
> {mkModule main_mod Nothing x1}
> | body {x1};
>
> body {([HsImportDecl],[HsDecl])};
> body {x3}
> : "{", bodyaux {x3}, "}";
> {x2}
> | layout_on {_}, bodyaux {x2}, close {_};
>
> bodyaux {([HsImportDecl],[HsDecl])};
> bodyaux {(x1, x3)}
> : impdecls {x1}, ";", topdecls {x3}, optsemi;
> {([], x1)}
> | topdecls {x1}, optsemi;
> {(x1, [])}
> | impdecls {x1}, optsemi;
> {([], [])}
> | ;
>
> optsemi;
> optsemi
> : ";";
> | ;
The Export List.
> maybeexports {Maybe [HsExportSpec]};
> maybeexports {Just x1}
> : exports {x1};
> {Nothing}
> | ;
>
> exports {[HsExportSpec]};
> exports {reverse x2}
> : "(", exportlist {x2}, maybecomma, ")";
> {[]}
> | "(", ")";
>
> maybecomma;
> maybecomma
> : ",";
> | ;
>
> exportlist {[HsExportSpec]};
> exportlist {x3 : x1}
> : exportlist {x1}, ",", export {x3};
> {[x1]}
> | export {x1};
>
> export {HsExportSpec};
> export {HsEVar x1}
> : qvar {x1};
> {HsEAbs x1}
> | qtyconorcls {x1};
> {HsEThingAll x1}
> | qtyconorcls {x1}, "(", DotDot, ")";
> {HsEThingWith x1 []}
> | qtyconorcls {x1}, "(", ")";
> {HsEThingWith x1 (reverse x3)}
> | qtyconorcls {x1}, "(", qcnames {x3}, ")";
> {HsEModuleContents x2}
> | KW_Module, modid {x2};
>
> qcnames {[HsQName]};
> qcnames {x3 : x1}
> : qcnames {x1}, ",", qcname {x3};
> {[x1]}
> | qcname {x1};
>
> qcname {HsQName};
> qcname {x1}
> : qvar {x1};
> {x1}
> | qcon {x1};
Import Declarations.
> impdecls {[HsImportDecl]};
> impdecls {x3 : x1}
> : impdecls {x1}, ";", impdecl {x3};
> {[x1]}
> | impdecl {x1};
>
> impdecl {HsImportDecl};
> impdecl {HsImportDecl x2 x4 x3 x5 x6}
> : KW_Import, srcloc {x2}, optqualified {x3}, modid {x4}, maybeas {x5}, maybeimpspec {x6};
>
> optqualified {Bool};
> optqualified {True}
> : KW_Qualified;
> {False}
> | ;
>
> maybeas {Maybe Module};
> maybeas {Just x2}
> : KW_As, modid {x2};
> {Nothing}
> | ;
>
> maybeimpspec {Maybe (Bool, [HsImportSpec])};
> maybeimpspec {Just x1}
> : impspec {x1};
> {Nothing}
> | ;
>
> impspec {(Bool, [HsImportSpec])};
> impspec {(False, reverse x2)}
> : "(", importlist {x2}, maybecomma, ")";
> {(True, reverse x3)}
> | KW_Hiding, "(", importlist {x3}, maybecomma, ")";
>
> importlist {[HsImportSpec]};
> importlist {x3 : x1}
> : importlist {x1}, ",", import {x3};
> {[x1]}
> | import {x1};
>
> import {HsImportSpec};
> import {HsIVar x1}
> : var {x1};
> {HsIAbs x1}
> | tyconorcls {x1};
> {HsIThingAll x1}
> | tyconorcls {x1}, "(", DotDot, ")";
> {HsIThingWith x1 []}
> | tyconorcls {x1}, "(", ")";
> {HsIThingWith x1 (reverse x3)}
> | tyconorcls {x1}, "(", cnames {x3}, ")";
>
> cnames {[HsName]};
> cnames {x3 : x1}
> : cnames {x1}, ",", cname {x3};
> {[x1]}
> | cname {x1};
>
> cname {HsName};
> cname {x1}
> : var {x1};
> {x1}
> | con {x1};
Fixity Declarations.
> fixdecl {HsDecl};
> fixdecl {HsInfixDecl x1 x2 x3 (reverse x4)}
> : srcloc {x1}, infix {x2}, precedence {x3}, ops {x4};
>
> precedence {Int};
> precedence {9}
> : ;
> {% checkPrec x1 >>= \p -> return (fromInteger (readInteger p))}
> | IntTok {x1};
>
> infix {HsAssoc};
> infix {HsAssocNone}
> : KW_Infix;
> {HsAssocLeft}
> | KW_InfixL;
> {HsAssocRight}
> | KW_InfixR;
>
> ops {[HsName]};
> ops {x3 : x1}
> : ops {x1}, ",", op {x3};
> {[x1]}
> | op {x1};
Top-Level Declarations.
Note: The report allows topdecls to be empty. This would result in
another shift/reduce-conflict, so we don't handle this case here, but
in bodyaux.
> topdecls {[HsDecl]};
> topdecls {x3 : x1}
> : topdecls {x1}, ";", topdecl {x3};
> {[x1]}
> | topdecl {x1};
>
> topdecl {HsDecl};
> topdecl {HsTypeDecl x3 (fst x2) (snd x2) x5}
> : KW_Type, simpletype {x2}, srcloc {x3}, Equals, type {x5};
> {% checkDataHeader x2 >>= \(cs,c,t) -> return (HsDataDecl x3 cs c t (reverse x5) x6)}
> | KW_Data, ctype {x2}, srcloc {x3}, Equals, constrs {x5}, deriving {x6};
> {% checkDataHeader x2 >>= \(cs,c,t) -> return (HsNewTypeDecl x3 cs c t x5 x6)}
> | KW_NewType, ctype {x2}, srcloc {x3}, Equals, constr {x5}, deriving {x6};
> {HsClassDecl x2 x3 x4}
> | KW_Class, srcloc {x2}, ctype {x3}, optcbody {x4};
> {HsInstDecl x2 x3 x4}
> | KW_Instance, srcloc {x2}, ctype {x3}, optvaldefs {x4};
> {HsDefaultDecl x2 x3}
> | KW_Default, srcloc {x2}, type {x3};
> {x1}
> | decl {x1};
>
> decls {[HsDecl]};
> decls {reverse x1}
> : decls1 {x1}, optsemi;
> {[]}
> | optsemi;
>
> decls1 {[HsDecl]};
> decls1 {x3 : x1}
> : decls1 {x1}, ";", decl {x3};
> {[x1]}
> | decl {x1};
>
> decl {HsDecl};
> decl {x1}
> : signdecl {x1};
> {x1}
> | fixdecl {x1};
> {x1}
> | valdef {x1};
>
> decllist {[HsDecl]};
> decllist {x3}
> : "{", decls {x3}, "}";
> {x2}
> | layout_on {_}, decls {x2}, close {_};
>
> signdecl {HsDecl};
> signdecl {HsTypeSig x2 (reverse x1) x4}
> : vars {x1}, srcloc {x2}, DoubleColon, ctype {x4};
ATTENTION: Dirty Hackery Ahead! If the second alternative of vars is
var instead of qvar, we get another shift/reduce-conflict. Consider
the following programs:
{ (+) :: ... } only var
{ (+) x y = ... } could (incorrectly) be qvar
We re-use expressions for patterns, so a qvar would be allowed in
patterns instead of a var only (which would be correct). But deciding
what the + is, would require more lookahead. So let's check for
ourselves ...
> vars {[HsName]};
> vars {x3 : x1}
> : vars {x1}, ",", var {x3};
> {% checkUnQual x1 >>= \n -> return [n]}
> | qvar {x1};
Types.
> type {HsType};
> type {HsTyFun x1 x3}
> : btype {x1}, RightArrow, type {x3};
> {x1}
> | btype {x1};
>
> btype {HsType};
> btype {HsTyApp x1 x2}
> : btype {x1}, atype {x2};
> {x1}
> | atype {x1};
>
> atype {HsType};
> atype {HsTyCon x1}
> : gtycon {x1};
> {HsTyVar x1}
> | tyvar {x1};
> {HsTyTuple (reverse x2)}
> | "(", types {x2}, ")";
> {HsTyApp list_tycon x2}
> | "[", type {x2}, "]";
> {x2}
> | "(", type {x2}, ")";
>
> gtycon {HsQName};
> gtycon {x1}
> : qconid {x1};
> {unit_tycon_name}
> | "(", ")";
> {fun_tycon_name}
> | "(", RightArrow, ")";
> {list_tycon_name}
> | "[", "]";
> {tuple_tycon_name x2}
> | "(", commas {x2}, ")";
(Slightly edited) Comment from GHC's hsparser.y:
"context => type" vs "type" is a problem, because you can't
distinguish between
foo :: (Baz a, Baz a)
bar :: (Baz a, Baz a) => [a] -> [a] -> [a]
with one token of lookahead. The HACK is to parse the context as a
btype (more specifically as a tuple type), then check that it has the
right form C a, or (C1 a, C2 b, ... Cn z) and convert it into a
context. Blaach!
> ctype {HsQualType};
> ctype {% checkContext x1 >>= \c -> return (HsQualType c x3)}
> : btype {x1}, DoubleArrow, type {x3};
> {HsUnQualType x1}
> | type {x1};
>
> types {[HsType]};
> types {x3 : x1}
> : types {x1}, ",", type {x3};
> {[x3, x1]}
> | type {x1}, ",", type {x3};
>
> simpletype {(HsName, [HsName])};
> simpletype {(x1,reverse x2)}
> : tycon {x1}, tyvars {x2};
>
> tyvars {[HsName]};
> tyvars {x2 : x1}
> : tyvars {x1}, tyvar {x2};
> {[]}
> | ;
Datatype declarations.
> constrs {[HsConDecl]};
> constrs {x3 : x1}
> : constrs {x1}, Bar, constr {x3};
> {[x1]}
> | constr {x1};
>
> constr {HsConDecl};
> constr {HsConDecl x1 (fst x2) (snd x2)}
> : srcloc {x1}, scontype {x2};
> {HsConDecl x1 x3 [x2,x4]}
> | srcloc {x1}, sbtype {x2}, conop {x3}, sbtype {x4};
> {HsRecDecl x1 x2 (reverse x5)}
> | srcloc {x1}, con {x2}, "{", fielddecls {x5}, "}";
>
> scontype {(HsName, [HsBangType])};
> scontype {% splitTyConApp x1 >>= \(c,ts) -> return (c,map HsUnBangedTy ts)}
> : btype {x1};
> {x1}
> | scontype1 {x1};
>
> scontype1 {(HsName, [HsBangType])};
> scontype1 {% splitTyConApp x1 >>= \(c,ts) -> return (c,map HsUnBangedTy ts ++ [HsBangedTy x3])}
> : btype {x1}, Exclamation, atype {x3};
> {(fst x1, snd x1 ++ [x2] )}
> | scontype1 {x1}, satype {x2};
>
> satype {HsBangType};
> satype {HsUnBangedTy x1}
> : atype {x1};
> {HsBangedTy x2}
> | Exclamation, atype {x2};
>
> sbtype {HsBangType};
> sbtype {HsUnBangedTy x1}
> : btype {x1};
> {HsBangedTy x2}
> | Exclamation, atype {x2};
>
> fielddecls {[([HsName],HsBangType)]};
> fielddecls {x3 : x1}
> : fielddecls {x1}, ",", fielddecl {x3};
> {[x1]}
> | fielddecl {x1};
>
> fielddecl {([HsName],HsBangType)};
> fielddecl {(reverse x1, x3)}
> : vars {x1}, DoubleColon, stype {x3};
>
> stype {HsBangType};
> stype {HsUnBangedTy x1}
> : type {x1};
> {HsBangedTy x2}
> | Exclamation, atype {x2};
>
> deriving {[HsQName]};
> deriving {[]}
> : ;
> {[x2]}
> | KW_Deriving, qtycls {x2};
> {[]}
> | KW_Deriving, "(", ")";
> {reverse x3}
> | KW_Deriving, "(", dclasses {x3}, ")";
>
> dclasses {[HsQName]};
> dclasses {x3 : x1}
> : dclasses {x1}, ",", qtycls {x3};
> {[x1]}
> | qtycls {x1};
Class declarations.
> optcbody {[HsDecl]};
> optcbody {x4}
> : KW_Where, "{", cbody {x4}, "}";
> {x3}
> | KW_Where, layout_on {_}, cbody {x3}, close {_};
> {[]}
> | ;
>
> cbody {[HsDecl]};
> cbody {reverse x1 ++ reverse x3}
> : cmethods {x1}, ";", cdefaults {x3}, optsemi;
> {reverse x1}
> | cmethods {x1}, optsemi;
> {[]}
> | optsemi;
>
> cmethods {[HsDecl]};
> cmethods {x3 : x1}
> : cmethods {x1}, ";", signdecl {x3};
> {[x1]}
> | signdecl {x1};
>
> cdefaults {[HsDecl]};
> cdefaults {x3 : x1}
> : cdefaults {x1}, ";", valdef {x3};
> {[x1]}
> | valdef {x1};
Instance declarations.
> optvaldefs {[HsDecl]};
> optvaldefs {x4}
> : KW_Where, "{", valdefs {x4}, "}";
> {x3}
> | KW_Where, layout_on {_}, valdefs {x3}, close {_};
> {[]}
> | ;
Recycling ...
> valdefs {[HsDecl]};
> valdefs {reverse x1}
> : cdefaults {x1}, optsemi;
> {[]}
> | optsemi;
Value definitions.
> valdef {HsDecl};
> valdef {% checkValDef (x2, x1, x3, [])}
> : infixexp {x1}, srcloc {x2}, rhs {x3};
> {% checkValDef (x2, x1, x3, x5)}
> | infixexp {x1}, srcloc {x2}, rhs {x3}, KW_Where, decllist {x5};
>
> rhs {HsRhs};
> rhs {% checkExpr x2 >>= \e -> return (HsUnGuardedRhs e)}
> : Equals, exp {x2};
> {HsGuardedRhss (reverse x1)}
> | gdrhs {x1};
>
> gdrhs {[HsGuardedRhs]};
> gdrhs {x2 : x1}
> : gdrhs {x1}, gdrh {x2};
> {[x1]}
> | gdrh {x1};
>
> gdrh {HsGuardedRhs};
> gdrh {% checkExpr x2 >>= \g -> checkExpr x5 >>= \e -> return (HsGuardedRhs x3 g e)}
> : Bar, exp {x2}, srcloc {x3}, Equals, exp {x5};
Expressions.
> exp {HsExp};
> exp {HsExpTypeSig x3 x1 x4}
> : infixexp {x1}, DoubleColon, srcloc {x3}, ctype {x4};
> {x1}
> | infixexp {x1};
>
> infixexp {HsExp};
> infixexp {x1}
> : exp10 {x1};
> {HsInfixApp x1 x2 x3}
> | infixexp {x1}, qop {x2}, exp10 {x3};
>
> exp10 {HsExp};
> exp10 {% checkPatterns (reverse x2) >>= \ps -> return (HsLambda ps x4)}
> : Backslash, aexps {x2}, RightArrow, exp {x4};
> {HsLet x2 x4}
> | KW_Let, decllist {x2}, KW_In, exp {x4};
> {HsIf x2 x4 x6}
> | KW_If, exp {x2}, KW_Then, exp {x4}, KW_Else, exp {x6};
> {HsCase x2 x4}
> | KW_Case, exp {x2}, KW_Of, altslist {x4};
> {HsNegApp x2}
> | Minus, fexp {x2};
> {HsDo x2}
> | KW_Do, stmtlist {x2};
> {x1}
> | fexp {x1};
>
> fexp {HsExp};
> fexp {HsApp x1 x2}
> : fexp {x1}, aexp {x2};
> {x1}
> | aexp {x1};
>
> aexps {[HsExp]};
> aexps {x2 : x1}
> : aexps {x1}, aexp {x2};
> {[x1]}
> | aexp {x1};
UGLY: Because patterns and expressions are mixed, aexp has to be split
into two rules: One left-recursive and one right-recursive. Otherwise
we get two reduce/reduce-errors (for as-patterns and irrefutable
patters).
Note: The first alternative of aexp is not neccessarily a record
update, it could be a labeled construction, too.
> aexp {HsExp};
> aexp {% mkRecConstrOrUpdate x1 (reverse x4)}
> : aexp {x1}, "{", fbinds {x4}, "}";
> {x1}
> | aexp1 {x1};
Even though the variable in an as-pattern cannot be qualified, we use
qvar here to avoid a shift/reduce conflict, and then check it
ourselves (as for vars above).
> aexp1 {HsExp};
> aexp1 {HsVar x1}
> : qvar {x1};
> {x1}
> | gcon {x1};
> {x1}
> | literal {x1};
> {HsParen x2}
> | "(", exp {x2}, ")";
> {HsTuple (reverse x2)}
> | "(", texps {x2}, ")";
> {x2}
> | "[", listexp {x2}, "]";
> {HsLeftSection x3 x2}
> | "(", infixexp {x2}, qop {x3}, ")";
> {HsRightSection x3 x2}
> | "(", qopm {x2}, infixexp {x3}, ")";
> {% checkUnQual x1 >>= \n -> return (HsAsPat n x3)}
> | qvar {x1}, At, aexp1 {x3};
> {HsWildCard}
> | "_";
> {HsIrrPat x2}
> | Tilde, aexp1 {x2};
>
> commas {Int};
> commas {x1 + 1}
> : commas {x1}, ",";
> {1}
> | ",";
>
> texps {[HsExp]};
> texps {x3 : x1}
> : texps {x1}, ",", exp {x3};
> {[x3,x1]}
> | exp {x1}, ",", exp {x3};
List expressions
The rules below are little bit contorted to keep lexps left-recursive
while avoiding another shift/reduce-conflict.
> listexp {HsExp};
> listexp {HsList [x1]}
> : exp {x1};
> {HsList (reverse x1)}
> | lexps {x1};
> {HsEnumFrom x1}
> | exp {x1}, DotDot;
> {HsEnumFromThen x1 x3}
> | exp {x1}, ",", exp {x3}, DotDot;
> {HsEnumFromTo x1 x3}
> | exp {x1}, DotDot, exp {x3};
> {HsEnumFromThenTo x1 x3 x5}
> | exp {x1}, ",", exp {x3}, DotDot, exp {x5};
> {HsListComp x1 (reverse x3)}
> | exp {x1}, Bar, quals {x3};
>
> lexps {[HsExp]};
> lexps {x3 : x1}
> : lexps {x1}, ",", exp {x3};
> {[x3,x1]}
> | exp {x1}, ",", exp {x3};
List comprehensions.
> quals {[HsStmt]};
> quals {x3 : x1}
> : quals {x1}, ",", qual {x3};
> {[x1]}
> | qual {x1};
>
> qual {HsStmt};
> qual {% checkPattern x1 >>= \p -> return (HsGenerator p x3)}
> : infixexp {x1}, LeftArrow, exp {x3};
> {HsQualifier x1}
> | exp {x1};
> {HsLetStmt x2}
> | KW_Let, decllist {x2};
Case alternatives.
> altslist {[HsAlt]};
> altslist {reverse x3}
> : "{", alts {x3}, optsemi, "}";
> {reverse x2}
> | layout_on {_}, alts {x2}, optsemi, close {_};
>
> alts {[HsAlt]};
> alts {x3 : x1}
> : alts {x1}, ";", alt {x3};
> {[x1]}
> | alt {x1};
>
> alt {HsAlt};
> alt {% checkPattern x1 >>= \p -> return (HsAlt x2 p x3 [])}
> : infixexp {x1}, srcloc {x2}, ralt {x3};
> {% checkPattern x1 >>= \p -> return (HsAlt x2 p x3 x5)}
> | infixexp {x1}, srcloc {x2}, ralt {x3}, KW_Where, decllist {x5};
>
> ralt {HsGuardedAlts};
> ralt {HsUnGuardedAlt x2}
> : RightArrow, exp {x2};
> {HsGuardedAlts (reverse x1)}
> | gdpats {x1};
>
> gdpats {[HsGuardedAlt]};
> gdpats {x2 : x1}
> : gdpats {x1}, gdpat {x2};
> {[x1]}
> | gdpat {x1};
>
> gdpat {HsGuardedAlt};
> gdpat {HsGuardedAlt x3 x2 x5}
> : Bar, exp {x2}, srcloc {x3}, RightArrow, exp {x5};
Statement sequences.
> stmtlist {[HsStmt]};
> stmtlist {x3}
> : "{", stmts {x3}, "}";
> {x2}
> | layout_on {_}, stmts {x2}, close {_};
>
> stmts {[HsStmt]};
> stmts {reverse (HsQualifier x3 : x1)}
> : stmts1 {x1}, ";", exp {x3};
> {[HsQualifier x1]}
> | exp {x1};
>
> stmts1 {[HsStmt]};
> stmts1 {x3 : x1}
> : stmts1 {x1}, ";", qual {x3};
> {[x1]}
> | qual {x1};
Record Field Update/Construction.
> fbinds {[HsFieldUpdate]};
> fbinds {x3 : x1}
> : fbinds {x1}, ",", fbind {x3};
> {[x1]}
> | fbind {x1};
>
> fbind {HsFieldUpdate};
> fbind {HsFieldUpdate x1 x3}
> : qvar {x1}, Equals, exp {x3};
Variables, Constructors and Operators.
> gcon {HsExp};
> gcon {unit_con}
> : "(", ")";
> {HsList []}
> | "[", "]";
> {tuple_con x2}
> | "(", commas {x2}, ")";
> {HsCon x1}
> | qcon {x1};
>
> var {HsName};
> var {x1}
> : varid {x1};
> {x2}
> | "(", varsym {x2}, ")";
>
> qvar {HsQName};
> qvar {x1}
> : qvarid {x1};
> {x2}
> | "(", qvarsym {x2}, ")";
>
> con {HsName};
> con {x1}
> : conid {x1};
> {x2}
> | "(", consym {x2}, ")";
>
> qcon {HsQName};
> qcon {x1}
> : qconid {x1};
> {x2}
> | "(", qconsym {x2}, ")";
>
> varop {HsName};
> varop {x1}
> : varsym {x1};
> {x2}
> | "`", varid {x2}, "`";
>
> qvarop {HsQName};
> qvarop {x1}
> : qvarsym {x1};
> {x2}
> | "`", qvarid {x2}, "`";
>
> qvaropm {HsQName};
> qvaropm {x1}
> : qvarsymm {x1};
> {x2}
> | "`", qvarid {x2}, "`";
>
> conop {HsName};
> conop {x1}
> : consym {x1};
> {x2}
> | "`", conid {x2}, "`";
>
> qconop {HsQName};
> qconop {x1}
> : qconsym {x1};
> {x2}
> | "`", qconid {x2}, "`";
>
> op {HsName};
> op {x1}
> : varop {x1};
> {x1}
> | conop {x1};
>
> qop {HsExp};
> qop {HsVar x1}
> : qvarop {x1};
> {HsCon x1}
> | qconop {x1};
>
> qopm {HsExp};
> qopm {HsVar x1}
> : qvaropm {x1};
> {HsCon x1}
> | qconop {x1};
>
> qvarid {HsQName};
> qvarid {UnQual x1}
> : varid {x1};
> {Qual (Module (fst x1)) (HsIdent (snd x1))}
> | QVarId {x1};
>
> varid {HsName};
> varid {HsIdent x1}
> : VarId {x1};
> {as_name}
> | KW_As;
> {qualified_name}
> | KW_Qualified;
> {hiding_name}
> | KW_Hiding;
>
> qconid {HsQName};
> qconid {UnQual x1}
> : conid {x1};
> {Qual (Module (fst x1)) (HsIdent (snd x1))}
> | QConId {x1};
>
> conid {HsName};
> conid {HsIdent x1}
> : ConId {x1};
>
> qconsym {HsQName};
> qconsym {UnQual x1}
> : consym {x1};
> {Qual (Module (fst x1)) (HsSymbol (snd x1))}
> | QConSym {x1};
>
> consym {HsName};
> consym {HsSymbol x1}
> : ConSym {x1};
>
> qvarsym {HsQName};
> qvarsym {UnQual x1}
> : varsym {x1};
> {x1}
> | qvarsym1 {x1};
>
> qvarsymm {HsQName};
> qvarsymm {UnQual x1}
> : varsymm {x1};
> {x1}
> | qvarsym1 {x1};
>
> varsym {HsName};
> varsym {HsSymbol x1}
> : VarSym {x1};
> {minus_name}
> | Minus;
> {pling_name}
> | Exclamation;
>
> varsymm {HsName};
> varsymm {HsSymbol x1} -- varsym not including '-'
> : VarSym {x1};
> {pling_name}
> | Exclamation;
>
> qvarsym1 {HsQName};
> qvarsym1 {Qual (Module (fst x1)) (HsSymbol (snd x1))}
> : QVarSym {x1};
>
> literal {HsExp};
> literal {HsLit (HsInt (readInteger x1))}
> : IntTok {x1};
> {HsLit (HsChar x1)}
> | Character {x1};
> {HsLit (HsFrac (readRational x1))}
> | FloatTok {x1};
> {HsLit (HsString x1)}
> | StringTok {x1};
>
> srcloc {SrcLoc};
> srcloc {% getSrcLoc}
> : ;
Layout.
> close {()};
> close {()}
> : "virtual }";
> {% popContext}
> | insert "virtual }";
>
> layout_on {()};
> layout_on {% getSrcLoc >>= \(SrcLoc r c) -> pushContext (Layout c)}
> : ;
Miscellaneous (mostly renamings).
> modid {Module};
> modid {Module x1}
> : ConId {x1};
>
> tyconorcls {HsName};
> tyconorcls {x1}
> : conid {x1};
>
> tycon {HsName};
> tycon {x1}
> : conid {x1};
>
> qtyconorcls {HsQName};
> qtyconorcls {x1}
> : qconid {x1};
>
> qtycls {HsQName};
> qtycls {x1}
> : qconid {x1};
>
> tyvar {HsName};
> tyvar {x1}
> : varid {x1};
>
> }%
|