bran.opp.exp 25 KB
Newer Older
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
%{
(* parserが利用する変数、関数、型などの定義 *)
open Base
open Ast_t
open With.Loc

let add_type loc x =
  (x, Type.Meta.create loc)

let add_type_loc x =
  add_type x.tag x.desc

let ast loc x =
  create loc & add_type loc x

let ast_on start end_ x =
  ast (Location.union start end_) x

let constr_args = function
  | { With.Loc.desc = (Tuple(xs), _) } -> xs
  | e -> [e]

let constr_pattern_args = function
  | { With.Loc.desc = PtTuple(xs) } -> xs
  | x -> [x]

let combine e1 e2 =
  let typ = Type.app_unit e1.tag in
  ast_on e1.tag e2.tag
    (LetVar ((Id.gentmp (Type.prefix typ), typ), e1, e2))

let rev_combine_list = function
  | [] -> create Location.zero (Unit, Type.app_unit Location.zero)
  | init :: stmts ->
    List.fold_left (fun s1 s2 -> combine s2 s1) init stmts

%}
%start prog
%token <Location.t> WITH
%token <Location.t> VAR
%token <Location.t> TYPE
%token <Location.t> TRY
%token <Location.t> TOPVAR
%token <Location.t> TOPDEF
%token <Location.t> TO
%token <Location.t> THEN
%token <Location.t> RETURN
%token <Location.t> RECEIVE
%token <Location.t> REC
%token <Location.t> RBRACK
%token <Location.t> RBRACE
%token <Location.t> RAISE
%token <Id.t With.Loc.t> QIDENT
%token <Location.t> PIPE
%token <Location.t> PERFORM
%token <Location.t> OF
%token <Location.t> NOT
%token <Location.t> MATCH
%token <Location.t> IN
%token <Location.t> IF
%token <Location.t> GREATER_GREATER
%token <Location.t> FUN
%token <Location.t> FOR
%token <Location.t> EXTERNAL
%token <Location.t> EXCEPTION
%token <Location.t> EOF
%token <Location.t> END
%token <Location.t> ELSE
%token <Location.t> DEF
%token <Location.t> COMMA
%token <Location.t> COLON
%token <Location.t> ASSERT
%token <Location.t> AND
%token <Location.t> SEMI
%token <Location.t> NL
%token <Location.t> DOL
%token <Location.t> LARROW
%token <Location.t> RARROW
%token <Location.t> AS
%token <Location.t> LESS_GREATER
%token <Location.t> LESS_EQUAL
%token <Location.t> LESS
%token <Location.t> GREATER_EQUAL
%token <Location.t> GREATER
%token <Location.t> EQUAL
%token <Location.t> LAND
%token <Location.t> LOR
%token <Location.t> UARROW
%token <Location.t> CONS
%token <Location.t> PLUS_DOT
%token <Location.t> PLUS
%token <Location.t> MINUS_DOT
%token <Location.t> MINUS
%token <Location.t> SLASH_DOT
%token <Location.t> SLASH
%token <Location.t> MOD
%token <Location.t> AST_DOT
%token <Location.t> AST
%token <Location.t> DOT
%token <Id.t With.Loc.t> UIDENT
%token <string With.Loc.t> STRING
%token <Location.t> LESS_LESS
%token <IntRepr.t With.Loc.t> INT
%token <Id.t With.Loc.t> IDENT
%token <float With.Loc.t> FLOAT
%token <Location.t> EXCL
%token <Location.t> DO
%token <string With.Loc.t> CHAR
%token <bool With.Loc.t> BOOL
%token <string With.Loc.t> ATOM
%token <Location.t> ASSIGN
%token <Location.t> LPAREN
%token <Location.t> LBRACK
%token <Location.t> LBRACE
%token <Location.t> RPAREN
%right prec_stmt 
%nonassoc prec_simple_expr prec_mutual_def prec_constr_decl 
%nonassoc AND 
%right SEMI NL 
%right DOL 
%right LARROW 
%left RARROW 
%nonassoc prec_pattern 
%nonassoc AS 
%left LESS_GREATER LESS_EQUAL LESS GREATER_EQUAL GREATER EQUAL 
%right LAND 
%right LOR 
%right UARROW 
%nonassoc prec_pattern_constr_name 
%right CONS 
%left PLUS_DOT PLUS MINUS_DOT MINUS 
%left SLASH_DOT SLASH MOD AST_DOT AST 
%right prec_unary_minus 
%left prec_app 
%left DOT 
%right UIDENT 
%nonassoc STRING LESS_LESS INT IDENT FLOAT EXCL DO CHAR BOOL ATOM ASSIGN 
%left LPAREN LBRACK LBRACE 
%nonassoc prec_type_expr_tuple 
%nonassoc RPAREN 
%type <Ast_t.def list> prog
%%

prog:
| _1 = definitions _2 = EOF
    {                  ( _1 )}

definitions:
| 
    {      ( [] )}
| _1 = rev_definitions
    {      ( List.rev & List.filter (fun def -> def.desc <> Nop) _1 )}

rev_definitions:
| _1 = definition
    {      ( [_1] )}
| _1 = rev_definitions _2 = definition
    {      ( _2 :: _1 )}

definition:
| _1 = TOPVAR _2 = IDENT _3 = EQUAL _4 = nl_opt _5 = expr
    {      ( from_range _1 _5.tag (VarDef (add_type_loc _2, _5)) )}
| _1 = TOPDEF _2 = fundef _3 = mutual_fundefs_opt
    {      ( create _1 (RecDef _2) )}
| _1 = TOPDEF _2 = REC _3 = fundef _4 = mutual_fundefs_opt
    {      ( create _1 (RecDef _3) )}
| _1 = TYPE _2 = typedef
    {      ( create _1 _2 )}
| _1 = AND _2 = typedef
    {      ( create _1 _2 )}
| _1 = EXCEPTION _2 = UIDENT
    {      ( create _1 Nop )}
| _1 = EXCEPTION _2 = UIDENT _3 = OF _4 = type_expr
    {      ( create _1 Nop )}
| _1 = EXCEPTION _2 = UIDENT _3 = EQUAL _4 = constr
    {      ( create _1 Nop )}
| _1 = TOPDEF _2 = sigdef
    {      ( create _1 (SigDef _2) )}
| _1 = TOPVAR _2 = sigdef
    {      ( create _1 (SigDef _2) )}
| _1 = EXTERNAL _2 = ext_sigdef
    {      ( create _1 (SigDef _2) )}
| _1 = NL
    {      ( create _1 Nop )}
| _1 = error
    {      ( raise (Syntax_error (Location.create
                             (Position.of_lexing_pos _startpos)
                             (Position.of_lexing_pos _endpos), None)) )}

mutual_fundefs_opt:
|  %prec prec_mutual_def
    {      ( [] )}
| _1 = rev_mutual_fundefs %prec prec_mutual_def
    {      ( List.rev _1 )}

rev_mutual_fundefs:
| _1 = mutual_fundef
    {      ( [_1] )}
| _1 = rev_mutual_fundefs _2 = NL _3 = mutual_fundef
    {      ( _3 :: _1 )}

mutual_fundef:
| _1 = AND _2 = fundef
    {                 ( _2 )}

simple_expr:
| _1 = primary
    {              ( _1 )}
| _1 = field_expr
    {                 ( _1 )}
| _1 = array_expr
    {                 ( _1 )}
| _1 = EXCL _2 = simple_expr
    {                       ( _2 )}

primary:
| _1 = binding
    {      ( _1 )}
| _1 = LPAREN _2 = expr _3 = RPAREN
    {      ( _2 )}
| _1 = LPAREN _2 = RPAREN
    {      ( ast_on _1 _2 Unit )}
| _1 = BOOL
    {      ( ast _1.tag (Bool _1.desc) )}
| _1 = INT
    {      ( ast _1.tag (Int _1.desc) )}
| _1 = FLOAT
    {      ( ast _1.tag (Float _1.desc) )}
| _1 = CHAR
    {      ( ast _1.tag (Char _1.desc) )}
| _1 = STRING
    {      ( ast _1.tag (String _1.desc) )}
| _1 = ATOM
    {      ( ast _1.tag (Atom _1.desc) )}
| _1 = UIDENT
    {      ( ast _1.tag (Constr(Binding.of_string _1.desc, [])) )}
| _1 = LBRACK _2 = list_ _3 = RBRACK
    {      ( ast_on _1 _3 (List _2) )}
| _1 = LBRACK _2 = PIPE _3 = list_ _4 = PIPE _5 = RBRACK
    {      ( ast_on _1 _5 (Array _3) )}
| _1 = LESS_LESS _2 = bitstring _3 = GREATER_GREATER
    {      ( ast_on _1 _3 (Bitstring _2) )}

binding:
| _1 = value_name
    {      ( ast _1.tag (Var (`Unbound (Binding.of_string _1.desc))) )}
| _1 = module_path
    {      ( ast (tag_of_list _1) (Var (`Unbound (Binding.of_list & descs _1))) )}

value_name:
| _1 = IDENT
    {            ( _1 )}

module_path:
| _1 = rev_module_path _2 = value_name
    {      ( List.rev & _2 :: _1 )}

rev_module_path:
| _1 = UIDENT _2 = DOT
    {      ( [_1] )}
| _1 = rev_module_path _2 = UIDENT _3 = DOT
    {      ( _2 :: _1 )}

field_expr:
| _1 = primary _2 = DOT _3 = binding
    {      ( _1 )}

array_expr:
| _1 = primary _2 = DOT _3 = LPAREN _4 = expr _5 = RPAREN
    {      ( ast_on _1.tag _5 (Get (_1, _4)) )}

expr:
| _1 = simple_expr %prec prec_simple_expr
    {      ( _1 )}
| _1 = NOT _2 = expr %prec prec_app
    {      ( ast_on _1 _2.tag (Not _2) )}
| _1 = MINUS _2 = expr %prec prec_unary_minus
    {      ( ast_on _1 _2.tag (Neg _2) )}
| _1 = expr _2 = PLUS _3 = expr
    {      ( ast_on _1.tag _3.tag (Add (_1, _3)) )}
| _1 = expr _2 = MINUS _3 = expr
    {      ( ast_on _1.tag _3.tag (Sub(_1, _3)) )}
| _1 = expr _2 = AST _3 = expr
    {      ( ast_on _1.tag _3.tag (Mul(_1, _3)) )}
| _1 = expr _2 = SLASH _3 = expr
    {      ( ast_on _1.tag _3.tag (Div(_1, _3)) )}
| _1 = expr _2 = MOD _3 = expr
    {      ( ast_on _1.tag _3.tag (Div(_1, _3)) )}
| _1 = expr _2 = PLUS_DOT _3 = expr
    {      ( ast_on _1.tag _3.tag (Add(_1, _3)) )}
| _1 = expr _2 = MINUS_DOT _3 = expr
    {      ( ast_on _1.tag _3.tag (Sub(_1, _3)) )}
| _1 = expr _2 = AST_DOT _3 = expr
    {      ( ast_on _1.tag _3.tag (Mul(_1, _3)) )}
| _1 = expr _2 = SLASH_DOT _3 = expr
    {      ( ast_on _1.tag _3.tag (Div(_1, _3)) )}
| _1 = expr _2 = UARROW _3 = expr
    {      ( ast_on _1.tag _3.tag (Concat(_1, _3)) )}
| _1 = expr _2 = CONS _3 = expr
    {      ( ast_on _1.tag _3.tag (Constr(Binding.of_string "Cons", [_1; _3])) )}
| _1 = expr _2 = LAND _3 = expr
    {      ( ast_on _1.tag _3.tag (And(_1, _3)) )}
| _1 = expr _2 = LOR _3 = expr
    {      ( ast_on _1.tag _3.tag (Or(_1, _3)) )}
| _1 = expr _2 = EQUAL _3 = expr
    {      ( ast_on _1.tag _3.tag (Eq(_1, _3)) )}
| _1 = expr _2 = LESS_GREATER _3 = expr
    {      ( let body = ast_on _1.tag _3.tag (Eq (_1, _3)) in
        ast_on _1.tag _3.tag (Not body) )}
| _1 = expr _2 = LESS _3 = expr
    {      ( let body = ast_on _1.tag _3.tag (LE (_3, _1)) in
        ast_on _1.tag _3.tag (Not body) )}
| _1 = expr _2 = GREATER _3 = expr
    {      ( let body = ast_on _1.tag _3.tag (LE (_1, _3)) in
        ast_on _1.tag _3.tag (Not body) )}
| _1 = expr _2 = LESS_EQUAL _3 = expr
    {      ( ast_on _1.tag _3.tag (LE(_1, _3)) )}
| _1 = expr _2 = GREATER_EQUAL _3 = expr
    {      ( ast_on _1.tag _3.tag (LE(_3, _1)) )}
| _1 = expr _2 = DOL _3 = expr
    {      ( ast_on _1.tag _3.tag (App(_1, [_3])) )}
| _1 = tuple
    {              ( _1 )}
| _1 = if_exp
    {               ( _1 )}
| _1 = expr _2 = actual_args %prec prec_app
    {      ( ast_on _1.tag (List.last _2).tag (App(_1, _2)) )}
| _1 = expr _2 = actual_args _3 = do_block
    {      ( ast_on _1.tag _3.tag (App(_1, _2)) )}
| _1 = UIDENT _2 = simple_expr
    {      ( ast_on _1.tag _2.tag (Constr(Binding.of_string _1.desc, constr_args _2)) )}
| _1 = LBRACE _2 = fields _3 = RBRACE
    {      ( ast_on _1 _3 (Record(_2)) )}
| _1 = VAR _2 = IDENT _3 = EQUAL _4 = nl_opt _5 = expr _6 = term _7 = block
    {      ( ast_on _1 _7.tag (LetVar(add_type_loc _2, _5, _7)) )}
| _1 = DEF _2 = fundef _3 = IN _4 = nl_opt _5 = block
    {      ( ast_on _1 _5.tag (LetRec(_2, _5)) )}
| _1 = DEF _2 = REC _3 = fundef _4 = IN _5 = nl_opt _6 = block
    {      ( ast_on _1 _6.tag (LetRec(_3, _6)) )}
| _1 = MATCH _2 = nl_opt _3 = expr _4 = WITH _5 = nl_opt _6 = pattern_matching _7 = END
    {      ( ast_on _1 _7 (Match (_3, _6)) )}
| _1 = field_expr _2 = LARROW _3 = expr
    {      ( _1 )}
| _1 = array_expr _2 = LARROW _3 = expr
    {      ( match _1.desc with
          | Get (e1, e2), _ ->
        ast_on _1.tag _3.tag (Put (e1, e2, _3))
          | _ -> assert false
    )}
| _1 = PERFORM _2 = nl_opt _3 = block _4 = END
    {      ( ast_on _1 _4 (Perform _3)  )}
| _1 = IDENT _2 = LARROW _3 = expr
    {      ( ast_on _1.tag _3.tag (Bind (add_type_loc _1, _3)) )}
| _1 = RETURN _2 = expr %prec prec_app
    {      ( ast_on _1 _2.tag (Return _2) )}
| _1 = FOR _2 = IDENT _3 = EQUAL _4 = expr _5 = TO _6 = nl_opt _7 = expr _8 = nl_opt _9 = DO _10 = nl_opt _11 = block _12 = END
    {      ( ast_on _1 _12 Unit )}
| _1 = TRY _2 = nl_opt _3 = expr _4 = nl_opt _5 = WITH _6 = nl_opt _7 = pattern_matching _8 = END
    {      ( ast_on _1 _8 Unit )}
| _1 = RAISE _2 = expr %prec prec_app
    {      ( ast_on _1 _2.tag Unit )}
| _1 = FUN _2 = nl_opt _3 = rev_formal_args _4 = RARROW _5 = nl_opt _6 = block _7 = END
    {      ( ast_on _1 _7 Unit )}
| _1 = FUN _2 = nl_opt _3 = pattern_matching _4 = END
    {      ( ast_on _1 _1 Unit )}
| _1 = ASSERT _2 = expr %prec prec_app
    {      ( ast_on _1 _2.tag Unit )}
| _1 = IDENT _2 = ASSIGN _3 = nl_opt _4 = expr
    {      ( ast_on _1.tag _4.tag Unit )}
| _1 = field_expr _2 = ASSIGN _3 = nl_opt _4 = expr
    {      ( ast_on _1.tag _4.tag Unit )}
| _1 = RECEIVE _2 = nl_opt _3 = pattern_matching _4 = END
    {      ( ast_on _1 _4 Unit )}

if_exp:
| _1 = IF _2 = expr _3 = THEN _4 = nl_opt _5 = multi_exps_block _6 = END
    {      ( let other = create _1 (Unit, Type.app_unit _1) in
        ast_on _1 _6 (If (_2, _5, other)) )}
| _1 = IF _2 = expr _3 = THEN _4 = nl_opt _5 = multi_exps_block _6 = ELSE _7 = nl_opt _8 = multi_exps_block _9 = END
    {      ( ast_on _1 _9 (If (_2, _5, _8)) )}
| _1 = IF _2 = expr _3 = THEN _4 = nl_opt _5 = simple_expr _6 = nl_opt _7 = ELSE _8 = nl_opt _9 = simple_expr
    {      ( ast_on _1 _9.tag (If (_2, _5, _9)) )}

do_block:
| _1 = DO _2 = nl_opt _3 = rev_formal_args _4 = RARROW _5 = nl_opt _6 = block _7 = END
    {      ( create _1 (Unit, Type.app_unit) (* TODO *) )}
| _1 = DO _2 = nl_opt _3 = pattern_matching _4 = END
    {      ( create _1 (Unit, Type.app_unit) (* TODO *) )}

nl_opt:
|  %prec prec_constr_decl
    {                                         ()}
| _1 = NL
    {         ()}

multi_exps_block:
| _1 = rev_stmts _2 = SEMI _3 = expr
    {                          ( rev_combine_list (_3 :: _1) )}
| _1 = rev_stmts _2 = SEMI _3 = expr _4 = NL
    {                             ( rev_combine_list (_3 :: _1) )}
| _1 = rev_stmts _2 = NL _3 = expr
    {                        ( rev_combine_list (_3 :: _1) )}
| _1 = rev_stmts _2 = NL _3 = expr _4 = NL
    {                           ( rev_combine_list (_3 :: _1) )}

block:
| _1 = rev_stmts %prec prec_stmt
    {                                ( rev_combine_list _1 )}
| _1 = rev_stmts _2 = NL %prec prec_stmt
    {                                   ( rev_combine_list _1 )}

rev_stmts:
| _1 = stmt
    {           ( [_1] )}
| _1 = rev_stmts _2 = SEMI _3 = stmt
    {                          ( _3 :: _1 )}
| _1 = rev_stmts _2 = NL _3 = stmt
    {                        ( _3 :: _1 )}

stmt:
| _1 = expr %prec prec_stmt
    {                           ( _1 )}

term:
| _1 = SEMI
    {           ()}
| _1 = NL
    {         ()}

tuple:
| _1 = LPAREN _2 = rev_tuple _3 = RPAREN
    {      ( ast_on _1 _3 (Tuple (List.rev _2)) )}

rev_tuple:
| _1 = rev_tuple _2 = COMMA _3 = expr
    {      ( _3 :: _1 )}
| _1 = expr _2 = COMMA _3 = expr
    {      ( [_3; _1] )}

fundef:
| _1 = IDENT _2 = rev_formal_args _3 = EQUAL _4 = nl_opt _5 = block
    {      ( let (_, args, body) = List.fold_left
          (fun (i, args, e1) (ptn, t) ->
             let x = "_t" ^ string_of_int i in
             let e2 = Match (create ptn.tag
                               (Var (`Local x), t),
                             [(ptn, e1)])
             in
             (i + 1, (x, t) :: args, ast_on ptn.tag e1.tag e2))
          (0, [], _5) _2
        in
        { name = add_type_loc _1; args = args; body = body } )}

rev_formal_args:
| _1 = formal_arg
    {      ( [add_type_loc _1] )}
| _1 = rev_formal_args _2 = formal_arg
    {      ( add_type_loc _2 :: _1 )}

formal_arg:
| _1 = pattern %prec prec_pattern
    {      ( create Location.zero _1 (* TODO: location *) )}

actual_args:
| _1 = actual_args _2 = simple_expr
    {    ( _1 @ [_2] )}
| _1 = simple_expr
    {    ( [_1] )}

fields:
| _1 = field _2 = fields_tail
    {    ( _1 :: _2 )}

fields_tail:
| 
    {    ( [] )}
| _1 = SEMI _2 = field _3 = fields_tail
    {    ( _2 :: _3 )}

field:
| _1 = IDENT _2 = EQUAL _3 = expr
    {    ( (_1.desc, _3) )}

pattern_matching:
| _1 = rev_pattern_matching
    {      ( List.rev _1 )}

rev_pattern_matching:
| _1 = rev_pattern_matching_elts
    {                                ( _1 )}
| _1 = PIPE _2 = rev_pattern_matching_elts
    {                                     ( _2 )}

rev_pattern_matching_elts:
| _1 = pattern_matching_elt
    {      ( [_1] )}
| _1 = rev_pattern_matching_elts _2 = PIPE _3 = pattern_matching_elt
    {      ( _3 :: _1 )}

pattern_matching_elt:
| _1 = pattern _2 = RARROW _3 = nl_opt _4 = block
    {      ( (_1, _4) )}

pattern:
| _1 = IDENT
    {      ( create _1.tag (PtVar (_1.desc, Type.Meta.create _1.tag)) )}
| _1 = LPAREN _2 = RPAREN
    {      ( from_range _1 _2 PtUnit )}
| _1 = BOOL
    {      ( create _1.tag (PtBool _1.desc) )}
| _1 = INT
    {      ( create _1.tag (PtInt _1.desc) )}
| _1 = FLOAT
    {      ( create _1.tag (PtFloat _1.desc) )}
| _1 = ATOM
    {      ( create _1.tag (PtAtom _1.desc) )}
| _1 = STRING
    {      ( create _1.tag (PtString _1.desc) )}
| _1 = pattern _2 = AS _3 = value_name
    {      ( create _1.tag (PtAlias (_1, _3.desc, Type.Meta.create _3.tag)) )}
| _1 = LPAREN _2 = pattern _3 = RPAREN
    {      ( _2 )}
| _1 = LPAREN _2 = pattern _3 = COLON _4 = type_expr _5 = RPAREN
    {      ( _2 )}
| _1 = LPAREN _2 = tuple_pattern _3 = RPAREN
    {      ( from_range _1 _3 (PtTuple _2) )}
| _1 = LBRACE _2 = field_patterns _3 = RBRACE
    {      ( from_range _1 _3 (PtRecord _2) )}
| _1 = constr_name %prec prec_pattern_constr_name
    {      ( create (tag_of_list _1) (PtConstr(Binding.of_list & descs _1, [], Type.Meta.create (tag_of_list _1))) )}
| _1 = constr_name _2 = pattern %prec prec_pattern_constr_name
    {      ( from_range (tag_of_list _1) _2.tag (PtConstr(Binding.of_list & descs _1, constr_pattern_args _2, Type.Meta.create (tag_of_list _1))) )}
| _1 = LBRACK _2 = list_pattern _3 = RBRACK
    {      ( from_range _1 _3 (PtList _2) )}
| _1 = pattern _2 = CONS _3 = pattern
    {      ( from_range _1.tag _3.tag (PtCons (_1, _3)) )}
| _1 = LBRACK _2 = PIPE _3 = list_pattern _4 = PIPE _5 = RBRACK
    {      ( create _1 PtUnit )}

constr_name:
| _1 = UIDENT
    {      ( [_1] )}
| _1 = rev_module_path _2 = UIDENT
    {      ( List.rev & _2 :: _1 )}

tuple_pattern:
| _1 = rev_tuple_pattern
    {      ( List.rev _1 )}

rev_tuple_pattern:
| _1 = rev_tuple_pattern _2 = COMMA _3 = pattern
    {      ( _3 :: _1 )}
| _1 = pattern _2 = COMMA _3 = pattern
    {      ( [_3; _1] )}

field_patterns:
| _1 = field_pattern _2 = COMMA _3 = rev_field_patterns
    {      ( List.rev (_1 :: _3) )}
| _1 = field_pattern _2 = COMMA _3 = rev_field_patterns _4 = COMMA
    {      ( List.rev (_1 :: _3) )}

rev_field_patterns:
| _1 = field_pattern
    {      ( [_1] )}
| _1 = rev_field_patterns _2 = COMMA _3 = field_pattern
    {      ( _3 :: _1 )}

field_pattern:
| _1 = IDENT _2 = EQUAL _3 = pattern
    {      ( (_1.desc, _3) )}

typedef:
| _1 = type_params_opt _2 = IDENT _3 = EQUAL _4 = nl_opt _5 = type_expr
    {      ( TypeDef(_2.desc, Type_t.TyFun(_1, _5)) )}
| _1 = type_params_opt _2 = IDENT _3 = EQUAL _4 = nl_opt _5 = constr_decls
    {      ( TypeDef(_2.desc, Type_t.TyFun(_1, create _2.tag (Type_t.App (Type_t.Variant (_2.desc, _5), [])))) )}
| _1 = type_params_opt _2 = IDENT _3 = EQUAL _4 = nl_opt _5 = PIPE _6 = constr_decls
    {      ( TypeDef(_2.desc, Type_t.TyFun(_1, create _2.tag (Type_t.App (Type_t.Variant (_2.desc, _6), [])))) )}
| _1 = type_params_opt _2 = IDENT _3 = EQUAL _4 = nl_opt _5 = LBRACE _6 = field_decls _7 = RBRACE
    {      ( TypeDef(_2.desc, Type_t.TyFun(_1, create _2.tag (Type_t.App(Type_t.Record(_2.desc, List.map fst _6), List.map snd _6)))) )}

type_params_opt:
| 
    {      ( [] )}
| _1 = type_params
    {      ( _1 )}

type_params:
| _1 = type_param
    {      ( [_1] )}
| _1 = LPAREN _2 = rev_type_params _3 = RPAREN
    {      ( List.rev _2 )}

rev_type_params:
| _1 = type_param
    {      ( [_1] )}
| _1 = type_params _2 = COMMA _3 = type_param
    {      ( _3 :: _1 )}

type_param:
| _1 = QIDENT
    {      ( _1.desc )}

type_expr:
| _1 = simple_type_expr
    {      ( _1 )}
| _1 = type_expr_tuple
    {      ( _1 )}
| _1 = type_expr _2 = type_constr
    {      ( from_range _1.tag _2.tag & Type_t.App (_2.desc, [_1]) )}
| _1 = type_expr _2 = RARROW _3 = type_expr
    {      ( from_range _1.tag _3.tag & Type_t.App (Type_t.Arrow, [_1; _3]) )}

simple_type_expr:
| _1 = QIDENT
    {      ( create _1.tag (Type_t.Var _1.desc) )}
| _1 = LPAREN _2 = type_expr _3 = RPAREN
    {      ( from_range _1 _3 & Type_t.App (Type_t.Tuple, [_2]) )}
| _1 = type_constr
    {      ( Type.void_app _1.tag _1.desc )}
| _1 = LPAREN _2 = type_constr_params _3 = RPAREN _4 = type_constr
    {      ( from_range _1 _4.tag & Type_t.App (_4.desc, _2) )}

type_expr_tuple:
| _1 = simple_type_expr _2 = rev_type_expr_tuple_tail
    {      ( let es = _1 :: List.rev _2 in
        create (tag_of_list es) (Type_t.App (Type_t.Tuple, es)) )}

rev_type_expr_tuple_tail:
| _1 = AST _2 = simple_type_expr
    {      ( [_2] )}
| _1 = rev_type_expr_tuple_tail _2 = AST _3 = simple_type_expr
    {      ( _3 :: _1 )}

type_constr:
| _1 = constr
    {      ( create _1.tag & Type_t.NameTycon (Binding.to_string _1.desc, ref None) )}

constr:
| _1 = IDENT
    {      ( create _1.tag & Binding.of_list [_1.desc] )}
| _1 = rev_constr_path _2 = IDENT
    {      ( let es = union & List.rev (_2 :: _1) in
        create es.tag & Binding.of_list es.desc )}

rev_constr_path:
| _1 = UIDENT
    {      ( [_1] )}
| _1 = rev_constr_path _2 = DOT _3 = UIDENT
    {      ( _3 :: _1 )}

type_constr_params:
| _1 = rev_type_constr_params
    {                             ( List.rev _1 )}

rev_type_constr_params:
| _1 = type_expr %prec prec_type_expr_tuple
    {      ( [_1] )}
| _1 = rev_type_constr_params _2 = COMMA _3 = type_expr
    {      ( _3 :: _1 )}

constr_decls:
| _1 = rev_constr_decls
    {      ( List.rev _1 )}

rev_constr_decls:
| _1 = constr_decl
    {      ( [_1] )}
| _1 = rev_constr_decls _2 = PIPE _3 = constr_decl
    {      ( _3 :: _1 )}

constr_decl:
| _1 = UIDENT _2 = constr_decl_type _3 = nl_opt
    {      ( (_1.desc, _2) )}

constr_decl_type:
| 
    {      ( [] )}
| _1 = OF _2 = type_expr
    {      ( match _2.desc with
        | Type_t.App (Type_t.Tuple, es) -> es
        | _ -> [_2]
      )}

field_decls:
| _1 = rev_field_decls _2 = nl_opt
    {                             ( List.rev _1 )}
| _1 = NL _2 = rev_field_decls _3 = nl_opt
    {                                ( List.rev _2 )}
| _1 = rev_field_decls _2 = COMMA _3 = nl_opt
    {                                   ( List.rev _1 )}
| _1 = NL _2 = rev_field_decls _3 = COMMA _4 = nl_opt
    {                                      ( List.rev _2 )}

rev_field_decls:
| _1 = field_decl
    {      ( [_1] )}
| _1 = rev_field_decls _2 = COMMA _3 = field_decl
    {      ( _3 :: _1 )}
| _1 = rev_field_decls _2 = COMMA _3 = NL _4 = field_decl
    {      ( _4 :: _1 )}

field_decl:
| _1 = IDENT _2 = COLON _3 = type_expr
    {      ( _1.desc, _3 )}

list_:
| 
    {      ( [] )}
| _1 = rev_list_elts
    {      ( List.rev _1 )}
| _1 = rev_list_elts _2 = COMMA
    {      ( List.rev _1 )}

rev_list_elts:
| _1 = expr
    {      ( [_1] )}
| _1 = rev_list_elts _2 = COMMA _3 = expr
    {      ( _3 :: _1 )}

list_pattern:
| 
    {                  ( [] )}
| _1 = rev_list_pattern_elts
    {                            ( List.rev _1 )}
| _1 = rev_list_pattern_elts _2 = COMMA
    {                                  ( List.rev _1 )}

rev_list_pattern_elts:
| _1 = pattern
    {      ( [_1] )}
| _1 = rev_list_pattern_elts _2 = COMMA _3 = pattern
    {      ( _3 :: _1 )}

bitstring:
| 
    {      ( [] )}
| _1 = rev_bitstring
    {      ( List.rev _1 )}

rev_bitstring:
| _1 = segment
    {      ( [_1] )}
| _1 = rev_bitstring _2 = COMMA _3 = segment
    {      ( _3 :: _1 )}

segment:
| _1 = bits_value
    {      ( Bitstring.Bits.create _1 )}
| _1 = bits_value _2 = COLON _3 = INT
    {      ( Bitstring.Bits.create _1 ~size:(IntRepr.to_int _3.desc) )}
| _1 = bits_value _2 = COLON _3 = INT _4 = SLASH _5 = bits_spec_list
    {      ( { _5 with Bitstring.Bits.value = _1;
                  size = Some (IntRepr.to_int _3.desc) } )}
| _1 = bits_value _2 = SLASH _3 = bits_spec_list
    {      ( { _3 with Bitstring.Bits.value = _1; } )}

bits_value:
| _1 = INT
    {          ( Bitstring.Bits.Int (IntRepr.to_int _1.desc) )}
| _1 = FLOAT
    {            ( Bitstring.Bits.Float _1.desc )}
| _1 = STRING
    {             ( Bitstring.Bits.String _1.desc )}
| _1 = IDENT
    {            ( Bitstring.Bits.Var _1.desc )}

bits_spec_list:
| _1 = rev_bits_spec_list
    {      ( let open Bitstring.Bits in
        List.fold_left
          (fun v spec ->
             match spec with
             | `Int -> { v with typ = `Int }
             | `Signed_int -> { v with typ = `Int; sign = Some `Signed }
             | `Float -> { v with typ = `Float }
             | `Binary -> { v with typ = `Binary }
             | `Bitstring -> { v with typ = `Bitstring }
             | `UTF8 -> { v with typ = `UTF8 }
             | `UTF16 -> { v with typ = `UTF16 }
             | `UTF32 -> { v with typ = `UTF32 }
             | `Signed -> { v with sign = Some `Signed }
             | `Unsigned -> { v with sign = Some `Unsigned }
             | `Big -> { v with endian = Some `Big }
             | `Little -> { v with endian = Some `Little }
             | `Native -> { v with endian = Some `Native }
             | `Unit size -> { v with unit = Some size })
          (create (Int 0)) _1
      )}

rev_bits_spec_list:
| _1 = bits_spec
    {      ( [_1] )}
| _1 = rev_bits_spec_list _2 = MINUS _3 = bits_spec
    {      ( _3 :: _1 )}

bits_spec:
| _1 = IDENT
    {      ( match _1.desc with
        | "int" -> `Int
        | "integer" -> `Int
        | "sint" -> `Signed_int
        | "float" -> `Float
        | "binary" -> `Binary
        | "bytes" -> `Binary
        | "bitstring" -> `Bitstring
        | "bits" -> `Bitstring
        | "utf8" -> `UTF8
        | "utf16" -> `UTF16
        | "utf32" -> `UTF32
        | "big" -> `Big
        | "little" -> `Little
        | "native" -> `Native
        | "signed" -> `Signed
        | "unsigned" -> `Unsigned
        | _ -> raise (Syntax_error (_1.tag, Some ("Unknown type " ^ _1.desc)))
      )}
| _1 = IDENT _2 = COLON _3 = INT
    {      ( match _1.desc with
        | "unit" -> `Unit (IntRepr.to_int _3.desc)
        | _ -> raise (Syntax_error (_1.tag, Some ("Unknown type " ^ _1.desc)))
      )}

sigdef:
| _1 = IDENT _2 = COLON _3 = type_expr
    {      ( { sig_name = (_1.desc, _3); sig_ext = None } )}

ext_sigdef:
| _1 = sigdef _2 = EQUAL _3 = nl_opt _4 = STRING
    {      ( { _1 with sig_ext = Some _4.desc } )}

%%