1 -- See <https://github.com/simonmar/happy/issues/93> for more information 2 -- This is an example of a grammar that has more than 2^15 entries in `happyTable` (39817). 3 { 4 import System.Exit 5 import Data.Char 6 } 7 8 %name parseLit lit 9 %name parseAttr export_attribute 10 %name parseTy export_ty 11 %name parsePat pat 12 %name parseStmt stmt 13 %name parseExpr expr 14 %name parseItem mod_item 15 %name parseSourceFileContents source_file 16 %name parseBlock export_block 17 %name parseImplItem impl_item 18 %name parseTraitItem trait_item 19 %name parseTt token_tree 20 %name parseTokenStream token_stream 21 %name parseTyParam ty_param 22 %name parseLifetimeDef lifetime_def 23 %name parseWhereClause where_clause 24 %name parseGenerics generics 25 26 %tokentype { Token } 27 %lexer { lexNonSpace `bindP` } { Eof } 28 %monad { P } { bindP } { returnP } 29 30 %error { parseError } 31 32 %expect 0 33 34 %token 35 36 37 '=' { Equal } 38 '<' { Less } 39 '>' { Greater } 40 '!' { Exclamation } 41 '~' { Tilde } 42 43 '+' { Plus } 44 '-' { Minus } 45 '*' { Star } 46 '/' { Slash } 47 '%' { Percent } 48 '^' { Caret } 49 '&' { Ampersand } 50 '|' { Pipe } 51 52 53 '@' { At } 54 '...' { DotDotDot } 55 '..' { DotDot } 56 '.' { Dot } 57 ',' { Comma } 58 ';' { Semicolon } 59 '::' { ModSep } 60 ':' { Colon } 61 '->' { RArrow } 62 '<-' { LArrow } 63 '=>' { FatArrow } 64 '#' { Pound } 65 '$' { Dollar } 66 '?' { Question } 67 '#!' { Shebang } 68 69 '||' { PipePipe } 70 '&&' { AmpersandAmpersand } 71 '>=' { GreaterEqual } 72 '>>=' { GreaterGreaterEqual } 73 '<<' { LessLess } 74 '>>' { GreaterGreater } 75 76 '==' { EqualEqual } 77 '!=' { NotEqual } 78 '<=' { LessEqual } 79 '<<=' { LessLessEqual } 80 '-=' { MinusEqual } 81 '&=' { AmpersandEqual } 82 '|=' { PipeEqual } 83 '+=' { PlusEqual } 84 '*=' { StarEqual } 85 '/=' { SlashEqual } 86 '^=' { CaretEqual } 87 '%=' { PercentEqual } 88 89 '(' { OpenParen } 90 '[' { OpenBracket } 91 '{' { OpenBrace } 92 ')' { CloseParen } 93 ']' { CloseBracket } 94 '}' { CloseBrace } 95 96 97 byte { ByteTok{} } 98 char { CharTok{} } 99 int { IntegerTok{} } 100 float { FloatTok{} } 101 str { StrTok{} } 102 byteStr { ByteStrTok{} } 103 rawStr { StrRawTok{} } 104 rawByteStr { ByteStrRawTok{} } 105 106 107 as { IdentTok "as" } 108 box { IdentTok "box" } 109 break { IdentTok "break" } 110 const { IdentTok "const" } 111 continue { IdentTok "continue" } 112 crate { IdentTok "crate" } 113 else { IdentTok "else" } 114 enum { IdentTok "enum" } 115 extern { IdentTok "extern" } 116 false { IdentTok "false" } 117 fn { IdentTok "fn" } 118 for { IdentTok "for" } 119 if { IdentTok "if" } 120 impl { IdentTok "impl" } 121 in { IdentTok "in" } 122 let { IdentTok "let" } 123 loop { IdentTok "loop" } 124 match { IdentTok "match" } 125 mod { IdentTok "mod" } 126 move { IdentTok "move" } 127 mut { IdentTok "mut" } 128 pub { IdentTok "pub" } 129 ref { IdentTok "ref" } 130 return { IdentTok "return" } 131 Self { IdentTok "Self" } 132 self { IdentTok "self" } 133 static { IdentTok "static" } 134 struct { IdentTok "struct" } 135 super { IdentTok "super" } 136 trait { IdentTok "trait" } 137 true { IdentTok "true" } 138 type { IdentTok "type" } 139 unsafe { IdentTok "unsafe" } 140 use { IdentTok "use" } 141 where { IdentTok "where" } 142 while { IdentTok "while" } 143 do { IdentTok "do" } 144 145 abstract { IdentTok "abstract" } 146 alignof { IdentTok "alignof" } 147 become { IdentTok "become" } 148 final { IdentTok "final" } 149 macro { IdentTok "macro" } 150 offsetof { IdentTok "offsetof" } 151 override { IdentTok "override" } 152 priv { IdentTok "priv" } 153 proc { IdentTok "proc" } 154 pure { IdentTok "pure" } 155 sizeof { IdentTok "sizeof" } 156 typeof { IdentTok "typeof" } 157 unsized { IdentTok "unsized" } 158 virtual { IdentTok "virtual" } 159 yield { IdentTok "yield" } 160 161 162 default { IdentTok "default" } 163 union { IdentTok "union" } 164 catch { IdentTok "catch" } 165 166 167 outerDoc { OuterDoc } 168 innerDoc { InnerDoc } 169 170 171 IDENT { IdentTok{} } 172 '_' { Underscore } 173 174 175 LIFETIME { LifetimeTok _ } 176 177 178 ntItem { Interpolated 0 } 179 ntBlock { Interpolated 1 } 180 ntStmt { Interpolated 2 } 181 ntPat { Interpolated 3 } 182 ntExpr { Interpolated 4 } 183 ntTy { Interpolated 5 } 184 ntIdent { Interpolated 6 } 185 ntPath { Interpolated 7 } 186 ntTT { Interpolated 8 } 187 ntArm { Interpolated 9 } 188 ntImplItem { Interpolated 10 } 189 ntTraitItem { Interpolated 11 } 190 ntGenerics { Interpolated 12 } 191 ntWhereClause { Interpolated 13 } 192 ntArg { Interpolated 14 } 193 ntLit { Interpolated 15 } 194 195 %nonassoc SEG 196 %nonassoc mut DEF EQ '::' 197 %nonassoc IDENT ntIdent default union catch self 198 %nonassoc box return break continue IMPLTRAIT LAMBDA 199 %right '=' '>>=' '<<=' '-=' '+=' '*=' '/=' '^=' '|=' '&=' '%=' 200 %right '<-' 201 %nonassoc SINGLERNG 202 %nonassoc INFIXRNG 203 %nonassoc POSTFIXRNG 204 %nonassoc PREFIXRNG 205 %nonassoc '..' '...' 206 %left '||' 207 %left '&&' 208 %left '==' '!=' '<' '>' '<=' '>=' 209 %left '|' 210 %left '^' 211 %left '&' 212 %left '<<' '>>' 213 %left '+' '-' 214 %left '*' '/' '%' 215 %nonassoc ':' as 216 %nonassoc UNARY 217 %nonassoc FIELD VIS PATH WHERE NOSEMI 218 %nonassoc '?' '.' 219 %nonassoc '{' ntBlock '[' '(' '!' ';' 220 221 %% 222 223 ident :: { Int } 224 : ntIdent { 0 } 225 | union { 1 } 226 | default { 2 } 227 | catch { 3 } 228 | IDENT { 4 } 229 230 gt :: { Int } 231 : {- empty -} { 5 } 232 233 some(p) :: { Int } 234 : some(p) p { 6 } 235 | p { 7 } 236 237 many(p) :: { Int } 238 : some(p) { 8 } 239 | {- empty -} { 9 } 240 241 sep_by1(p,sep) :: { Int } 242 : sep_by1(p,sep) sep p { 10 } 243 | p { 11 } 244 245 sep_by(p,sep) :: { Int } 246 : sep_by1(p,sep) { 12 } 247 | {- empty -} { 13 } 248 249 sep_by1T(p,sep) :: { Int } 250 : sep_by1(p,sep) sep { 14 } 251 | sep_by1(p,sep) { 15 } 252 253 sep_byT(p,sep) :: { Int } 254 : sep_by1T(p,sep) { 16 } 255 | {- empty -} { 17 } 256 257 source_file :: { Int } 258 : inner_attrs many(mod_item) { 18 } 259 | many(mod_item) { 19 } 260 261 outer_attribute :: { Int } 262 : '#' '[' mod_path token_stream ']' { 20 } 263 | outerDoc { 21 } 264 265 inner_attribute :: { Int } 266 : '#' '!' '[' mod_path token_stream ']' { 22 } 267 | '#!' '[' mod_path token_stream ']' { 23 } 268 | innerDoc { 24 } 269 270 inner_attrs :: { Int } 271 : inner_attrs inner_attribute { 25 } 272 | inner_attribute { 26 } 273 274 lit :: { Int } 275 : ntLit { 27 } 276 | byte { 28 } 277 | char { 29 } 278 | int { 30 } 279 | float { 31 } 280 | true { 32 } 281 | false { 33 } 282 | string { 34 } 283 284 string :: { Int } 285 : str { 35 } 286 | rawStr { 36 } 287 | byteStr { 37 } 288 | rawByteStr { 38 } 289 290 qual_path(segs) :: { Int } 291 : '<' qual_path_suf(segs) { 39 } 292 | lt_ty_qual_path as ty_path '>' '::' segs { 40 } 293 294 qual_path_suf(segs) :: { Int } 295 : ty '>' '::' segs { 41 } 296 | ty as ty_path '>' '::' segs { 42 } 297 298 lt_ty_qual_path :: { Int } 299 : '<<' qual_path_suf(path_segments_without_colons) { 43 } 300 301 generic_values :: { Int } 302 : '<' sep_by1(lifetime,',') ',' sep_by1T(ty,',') gt '>' { 45 } 303 | '<' sep_by1(lifetime,',') ',' sep_by1T(binding,',') gt '>' { 46 } 304 | '<' sep_by1T(lifetime,',') gt '>' { 47 } 305 | '<' sep_by1(ty,',') ',' sep_by1T(binding,',') gt '>' { 48 } 306 | '<' sep_by1T(ty,',') gt '>' { 49 } 307 | '<' sep_by1T(binding,',') gt '>' { 50 } 308 | '<' gt '>' { 51 } 309 | lt_ty_qual_path ',' sep_by1T(ty,',') gt '>' { 53 } 310 | lt_ty_qual_path ',' sep_by1T(binding,',') gt '>' { 54 } 311 | lt_ty_qual_path gt '>' { 55 } 312 313 binding :: { Int } 314 : ident '=' ty { 56 } 315 316 ty_path :: { Int } 317 : ntPath { 57 } 318 | path_segments_without_colons { 58 } 319 | '::' path_segments_without_colons { 59 } 320 321 ty_qual_path :: { Int } 322 : qual_path(path_segments_without_colons) { 60 } 323 324 path_segments_without_colons :: { Int } 325 : sep_by1(path_segment_without_colons, '::') %prec SEG { 61 } 326 327 path_segment_without_colons :: { Int } 328 : self_or_ident path_parameter1 { 62 } 329 330 path_parameter1 :: { Int } 331 : generic_values { 63 } 332 | '(' sep_byT(ty,',') ')' { 64 } 333 | '(' sep_byT(ty,',') ')' '->' ty_no_plus { 65 } 334 | {- empty -} %prec IDENT { 66 } 335 336 expr_path :: { Int } 337 : ntPath { 67 } 338 | path_segments_with_colons { 68 } 339 | '::' path_segments_with_colons { 69 } 340 341 expr_qual_path :: { Int } 342 : qual_path(path_segments_with_colons) { 70 } 343 344 path_segments_with_colons :: { Int } 345 : self_or_ident { 71 } 346 | path_segments_with_colons '::' self_or_ident { 72 } 347 | path_segments_with_colons '::' generic_values { 73 } 348 349 mod_path :: { Int } 350 : ntPath { 74 } 351 | self_or_ident { 75 } 352 | '::' self_or_ident { 76 } 353 | mod_path '::' ident { 77 } 354 355 lifetime :: { Int } 356 : LIFETIME { 78 } 357 358 trait_ref :: { Int } 359 : ty_path { 79 } 360 361 ty :: { Int } 362 : ty_no_plus { 80 } 363 | poly_trait_ref_mod_bound '+' sep_by1T(ty_param_bound_mod,'+') { 81 } 364 365 ty_no_plus :: { Int } 366 : ntTy { 82 } 367 | no_for_ty { 83 } 368 | for_ty_no_plus { 84 } 369 370 ty_prim :: { Int } 371 : no_for_ty_prim { 85 } 372 | for_ty_no_plus { 86 } 373 | poly_trait_ref_mod_bound '+' sep_by1T(ty_param_bound_mod,'+') { 87 } 374 375 no_for_ty :: { Int } 376 : no_for_ty_prim { 88 } 377 | '(' ')' { 89 } 378 | '(' ty ')' { 90 } 379 | '(' ty ',' ')' { 91 } 380 | '(' ty ',' sep_by1T(ty,',') ')' { 92 } 381 | ty_qual_path { 93 } 382 383 no_for_ty_prim :: { Int } 384 : '_' { 94 } 385 | '!' { 95 } 386 | '[' ty ']' { 96 } 387 | '*' ty_no_plus { 97 } 388 | '*' const ty_no_plus { 98 } 389 | '*' mut ty_no_plus { 99 } 390 | '&' ty_no_plus { 100 } 391 | '&' lifetime ty_no_plus { 101 } 392 | '&' mut ty_no_plus { 102 } 393 | '&' lifetime mut ty_no_plus { 103 } 394 | '&&' ty_no_plus { 104 } 395 | '&&' lifetime ty_no_plus { 105 } 396 | '&&' mut ty_no_plus { 106 } 397 | '&&' lifetime mut ty_no_plus { 107 } 398 | ty_path %prec PATH { 108 } 399 | ty_mac { 109 } 400 | unsafe extern abi fn fn_decl(arg_general) { 110 } 401 | unsafe fn fn_decl(arg_general) { 111 } 402 | extern abi fn fn_decl(arg_general) { 112 } 403 | fn fn_decl(arg_general) { 113 } 404 | typeof '(' expr ')' { 114 } 405 | '[' ty ';' expr ']' { 115 } 406 | '?' trait_ref { 116 } 407 | '?' for_lts trait_ref { 117 } 408 409 for_ty_no_plus :: { Int } 410 : for_lts unsafe extern abi fn fn_decl(arg_general) { 118 } 411 | for_lts unsafe fn fn_decl(arg_general) { 119 } 412 | for_lts extern abi fn fn_decl(arg_general) { 120 } 413 | for_lts fn fn_decl(arg_general) { 121 } 414 | for_lts trait_ref { 122 } 415 416 impl_ty :: { Int } 417 : impl sep_by1(ty_param_bound_mod,'+') %prec IMPLTRAIT { 123 } 418 419 lifetime_mut :: { Int } 420 : lifetime mut { 124 } 421 | lifetime { 125 } 422 | mut { 126 } 423 | {- empty -} { 127 } 424 425 fn_decl(arg) :: { Int } 426 : '(' sep_by1(arg,',') ',' '...' ')' ret_ty { 128 } 427 | '(' sep_byT(arg,',') ')' ret_ty { 129 } 428 429 fn_decl_with_self_general :: { Int } 430 : '(' arg_self_general ',' sep_byT(arg_general,',') ')' ret_ty { 130 } 431 | '(' arg_self_general ')' ret_ty { 131 } 432 | '(' ')' ret_ty { 132 } 433 434 fn_decl_with_self_named :: { Int } 435 : '(' arg_self_named ',' sep_by1(arg_named,',') ',' ')' ret_ty { 133 } 436 | '(' arg_self_named ',' sep_by1(arg_named,',') ')' ret_ty { 134 } 437 | '(' arg_self_named ',' ')' ret_ty { 135 } 438 | '(' arg_self_named ')' ret_ty { 136 } 439 | fn_decl(arg_named) { 137 } 440 441 ty_param_bound :: { Int } 442 : lifetime { 138 } 443 | poly_trait_ref { 139 } 444 445 poly_trait_ref_mod_bound :: { Int } 446 : poly_trait_ref { 140 } 447 | '?' poly_trait_ref { 141 } 448 449 ty_param_bound_mod :: { Int } 450 : ty_param_bound { 142 } 451 | '?' poly_trait_ref { 143 } 452 453 abi :: { Int } 454 : str { 144 } 455 | {- empty -} { 145 } 456 457 ret_ty :: { Int } 458 : '->' ty_no_plus { 146 } 459 | '->' impl_ty { 147 } 460 | {- empty -} { 148 } 461 462 poly_trait_ref :: { Int } 463 : trait_ref { 149 } 464 | for_lts trait_ref { 150 } 465 466 for_lts :: { Int } 467 : for '<' sep_byT(lifetime_def,',') '>' { 151 } 468 469 lifetime_def :: { Int } 470 : many(outer_attribute) lifetime ':' sep_by1T(lifetime,'+') { 152 } 471 | many(outer_attribute) lifetime { 153 } 472 473 arg_named :: { Int } 474 : ntArg { 154 } 475 | pat ':' ty { 155 } 476 477 arg_general :: { Int } 478 : ntArg { 156 } 479 | ty { 157 } 480 | '_' ':' ty { 158 } 481 | ident ':' ty { 159 } 482 | mut ident ':' ty { 160 } 483 | '&' '_' ':' ty { 161 } 484 | '&' ident ':' ty { 162 } 485 | '&&' '_' ':' ty { 163 } 486 | '&&' ident ':' ty { 164 } 487 488 arg_self_general :: { Int } 489 : mut self { 165 } 490 | self ':' ty { 166 } 491 | mut self ':' ty { 167 } 492 | arg_general { 168 } 493 494 arg_self_named :: { Int } 495 : self { 169 } 496 | mut self { 170 } 497 | '&' self { 171 } 498 | '&' lifetime self { 172 } 499 | '&' mut self { 173 } 500 | '&' lifetime mut self { 174 } 501 | self ':' ty { 175 } 502 | mut self ':' ty { 176 } 503 504 lambda_arg :: { Int } 505 : ntArg { 177 } 506 | pat ':' ty { 178 } 507 | pat { 179 } 508 509 pat :: { Int } 510 : ntPat { 180 } 511 | '_' { 181 } 512 | '&' mut pat { 182 } 513 | '&' pat { 183 } 514 | '&&' mut pat { 184 } 515 | '&&' pat { 185 } 516 | lit_expr { 186 } 517 | '-' lit_expr { 187 } 518 | box pat { 188 } 519 | binding_mode1 ident '@' pat { 189 } 520 | binding_mode1 ident { 190 } 521 | ident '@' pat { 191 } 522 | expr_path { 192 } 523 | expr_qual_path { 193 } 524 | lit_or_path '...' lit_or_path { 194 } 525 | expr_path '{' '..' '}' { 195 } 526 | expr_path '{' pat_fields '}' { 196 } 527 | expr_path '(' pat_tup ')' { 197 } 528 | expr_mac { 198 } 529 | '[' pat_slice ']' { 199 } 530 | '(' pat_tup ')' { 200 } 531 532 pat_tup :: { Int } 533 : sep_by1(pat,',') ',' '..' ',' sep_by1(pat,',') { 201 } 534 | sep_by1(pat,',') ',' '..' ',' sep_by1(pat,',') ',' { 202 } 535 | sep_by1(pat,',') ',' '..' { 203 } 536 | sep_by1(pat,',') { 204 } 537 | sep_by1(pat,',') ',' { 205 } 538 | '..' ',' sep_by1(pat,',') { 206 } 539 | '..' ',' sep_by1(pat,',') ',' { 207 } 540 | '..' { 208 } 541 | {- empty -} { 209 } 542 543 pat_slice :: { Int } 544 : sep_by1(pat,',') ',' '..' ',' sep_by1T(pat,',') { 210 } 545 | sep_by1(pat,',') ',' '..' { 211 } 546 | sep_by1(pat,',') '..' ',' sep_by1T(pat,',') { 212 } 547 | sep_by1(pat,',') '..' { 213 } 548 | sep_by1T(pat,',') { 214 } 549 | '..' ',' sep_by1T(pat,',') { 215 } 550 | '..' { 216 } 551 | {- empty -} { 217 } 552 553 lit_or_path :: { Int } 554 : expr_path { 218 } 555 | expr_qual_path { 219 } 556 | '-' lit_expr { 220 } 557 | lit_expr { 221 } 558 559 pat_fields :: { Int } 560 : sep_byT(pat_field,',') { 222 } 561 | sep_by1(pat_field,',') ',' '..' { 223 } 562 563 pat_field :: { Int } 564 : binding_mode ident { 224 } 565 | box binding_mode ident { 225 } 566 | binding_mode ident ':' pat { 226 } 567 568 binding_mode1 :: { Int } 569 : ref mut { 227 } 570 | ref { 228 } 571 | mut { 229 } 572 573 binding_mode :: { Int } 574 : binding_mode1 { 230 } 575 | {- empty -} { 231 } 576 577 gen_expression(lhs,rhs,rhs2) :: { Int } 578 : ntExpr { 232 } 579 | lit_expr { 233 } 580 | '[' sep_byT(expr,',') ']' { 234 } 581 | '[' inner_attrs sep_byT(expr,',') ']' { 235 } 582 | '[' expr ';' expr ']' { 236 } 583 | expr_mac { 237 } 584 | expr_path %prec PATH { 238 } 585 | expr_qual_path { 239 } 586 | '*' rhs %prec UNARY { 240 } 587 | '!' rhs %prec UNARY { 241 } 588 | '-' rhs %prec UNARY { 242 } 589 | '&' rhs %prec UNARY { 243 } 590 | '&' mut rhs %prec UNARY { 244 } 591 | '&&' rhs %prec UNARY { 245 } 592 | '&&' mut rhs %prec UNARY { 246 } 593 | box rhs %prec UNARY { 247 } 594 | left_gen_expression(lhs,rhs,rhs2) { 248 } 595 | '..' rhs2 %prec PREFIXRNG { 249 } 596 | '...' rhs2 %prec PREFIXRNG { 250 } 597 | '..' %prec SINGLERNG { 251 } 598 | '...' %prec SINGLERNG { 252 } 599 | return { 253 } 600 | return rhs { 254 } 601 | continue { 255 } 602 | continue lifetime { 256 } 603 | break { 257 } 604 | break rhs { 258 } 605 | break lifetime { 259 } 606 | break lifetime rhs %prec break { 260 } 607 | move lambda_args rhs %prec LAMBDA { 261 } 608 | lambda_args rhs %prec LAMBDA { 262 } 609 610 left_gen_expression(lhs,rhs,rhs2) :: { Int } 611 : postfix_blockexpr(lhs) { 263 } 612 | lhs '[' expr ']' { 264 } 613 | lhs '(' sep_byT(expr,',') ')' { 265 } 614 | lhs ':' ty_no_plus { 266 } 615 | lhs as ty_no_plus { 267 } 616 | lhs '*' rhs { 268 } 617 | lhs '/' rhs { 269 } 618 | lhs '%' rhs { 270 } 619 | lhs '+' rhs { 271 } 620 | lhs '-' rhs { 272 } 621 | lhs '<<' rhs { 273 } 622 | lhs '>>' rhs { 274 } 623 | lhs '&' rhs { 275 } 624 | lhs '^' rhs { 276 } 625 | lhs '|' rhs { 277 } 626 | lhs '==' rhs { 278 } 627 | lhs '!=' rhs { 279 } 628 | lhs '<' rhs { 280 } 629 | lhs '>' rhs { 281 } 630 | lhs '<=' rhs { 282 } 631 | lhs '>=' rhs { 283 } 632 | lhs '&&' rhs { 284 } 633 | lhs '||' rhs { 285 } 634 | lhs '..' %prec POSTFIXRNG { 286 } 635 | lhs '...' %prec POSTFIXRNG { 287 } 636 | lhs '..' rhs2 %prec INFIXRNG { 288 } 637 | lhs '...' rhs2 %prec INFIXRNG { 289 } 638 | lhs '<-' rhs { 290 } 639 | lhs '=' rhs { 291 } 640 | lhs '>>=' rhs { 292 } 641 | lhs '<<=' rhs { 293 } 642 | lhs '-=' rhs { 294 } 643 | lhs '+=' rhs { 295 } 644 | lhs '*=' rhs { 296 } 645 | lhs '/=' rhs { 297 } 646 | lhs '^=' rhs { 298 } 647 | lhs '|=' rhs { 299 } 648 | lhs '&=' rhs { 300 } 649 | lhs '%=' rhs { 301 } 650 651 postfix_blockexpr(lhs) :: { Int } 652 : lhs '?' { 302 } 653 | lhs '.' ident %prec FIELD { 303 } 654 | lhs '.' ident '(' sep_byT(expr,',') ')' { 304 } 655 | lhs '.' ident '::' '<' sep_byT(ty,',') '>' '(' sep_byT(expr,',') ')' { 305 } 656 | lhs '.' int { 306 } 657 658 expr :: { Int } 659 : gen_expression(expr,expr,expr) { 307 } 660 | paren_expr { 308 } 661 | struct_expr { 309 } 662 | block_expr { 310 } 663 | lambda_expr_block { 311 } 664 665 nostruct_expr :: { Int } 666 : gen_expression(nostruct_expr,nostruct_expr,nonstructblock_expr) { 312 } 667 | paren_expr { 313 } 668 | block_expr { 314 } 669 670 nonstructblock_expr :: { Int } 671 : gen_expression(nonstructblock_expr,nostruct_expr,nonstructblock_expr) { 315 } 672 | paren_expr { 316 } 673 | block_like_expr { 317 } 674 | unsafe inner_attrs_block { 318 } 675 676 nonblock_expr :: { Int } 677 : gen_expression(nonblock_expr,expr,expr) { 319 } 678 | paren_expr { 320 } 679 | struct_expr { 321 } 680 | lambda_expr_block { 322 } 681 682 blockpostfix_expr :: { Int } 683 : postfix_blockexpr(block_like_expr) { 323 } 684 | postfix_blockexpr(vis_safety_block) { 324 } 685 | left_gen_expression(blockpostfix_expr,expr,expr) { 325 } 686 687 lit_expr :: { Int } 688 : lit { 326 } 689 690 block_expr :: { Int } 691 : block_like_expr { 327 } 692 | inner_attrs_block { 328 } 693 | unsafe inner_attrs_block { 329 } 694 695 696 block_like_expr :: { Int } 697 : if_expr { 330 } 698 | loop inner_attrs_block { 331 } 699 | lifetime ':' loop inner_attrs_block { 332 } 700 | for pat in nostruct_expr inner_attrs_block { 333 } 701 | lifetime ':' for pat in nostruct_expr inner_attrs_block { 334 } 702 | while nostruct_expr inner_attrs_block { 335 } 703 | lifetime ':' while nostruct_expr inner_attrs_block { 336 } 704 | while let pat '=' nostruct_expr inner_attrs_block { 337 } 705 | lifetime ':' while let pat '=' nostruct_expr inner_attrs_block { 338 } 706 | match nostruct_expr '{' '}' { 339 } 707 | match nostruct_expr '{' inner_attrs '}' { 340 } 708 | match nostruct_expr '{' arms '}' { 341 } 709 | match nostruct_expr '{' inner_attrs arms '}' { 342 } 710 | expr_path '!' '{' token_stream '}' { 343 } 711 | do catch inner_attrs_block { 344 } 712 713 if_expr :: { Int } 714 : if nostruct_expr block else_expr { 345 } 715 | if let pat '=' nostruct_expr block else_expr { 346 } 716 717 else_expr :: { Int } 718 : else block { 347 } 719 | else if_expr { 348 } 720 | {- empty -} { 349 } 721 722 arms :: { Int } 723 : ntArm { 350 } 724 | ntArm arms { 351 } 725 | many(outer_attribute) sep_by1(pat,'|') arm_guard '=>' expr_arms { 352 } 726 727 arm_guard :: { Int } 728 : {- empty -} { 353 } 729 | if expr { 354 } 730 731 comma_arms :: { Int } 732 : {- empty -} { 355 } 733 | ',' { 356 } 734 | ',' arms { 357 } 735 736 expr_arms :: { Int } 737 : nonblock_expr comma_arms { 358 } 738 | blockpostfix_expr comma_arms { 359 } 739 | vis_safety_block comma_arms { 360 } 740 | vis_safety_block arms { 361 } 741 | block_like_expr comma_arms { 362 } 742 | block_like_expr arms { 363 } 743 744 paren_expr :: { Int } 745 : '(' ')' { 364 } 746 | '(' inner_attrs ')' { 365 } 747 | '(' expr ')' { 366 } 748 | '(' inner_attrs expr ')' { 367 } 749 | '(' expr ',' ')' { 368 } 750 | '(' inner_attrs expr ',' ')' { 369 } 751 | '(' expr ',' sep_by1T(expr,',') ')' { 370 } 752 | '(' inner_attrs expr ',' sep_by1T(expr,',') ')' { 371 } 753 754 lambda_expr_block :: { Int } 755 : move lambda_args '->' ty_no_plus block { 372 } 756 | lambda_args '->' ty_no_plus block { 373 } 757 758 lambda_args :: { Int } 759 : '||' { 374 } 760 | '|' sep_byT(lambda_arg,',') '|' { 375 } 761 762 struct_expr :: { Int } 763 : expr_path '{' '..' expr '}' { 376 } 764 | expr_path '{' inner_attrs '..' expr '}' { 377 } 765 | expr_path '{' sep_by1(field,',') ',' '..' expr '}' { 378 } 766 | expr_path '{' inner_attrs sep_by1(field,',') ',' '..' expr '}' { 379 } 767 | expr_path '{' sep_byT(field,',') '}' { 380 } 768 | expr_path '{' inner_attrs sep_byT(field,',') '}' { 381 } 769 770 field :: { Int } 771 : ident ':' expr { 382 } 772 | ident { 383 } 773 774 vis_safety_block :: { Int } 775 : pub_or_inherited safety inner_attrs_block { 384 } 776 777 vis_union_nonblock_expr :: { Int } 778 : union_expr { 385 } 779 | left_gen_expression(vis_union_nonblock_expr, expr, expr) { 386 } 780 781 union_expr :: { Int } 782 : pub_or_inherited union { 387 } 783 784 stmt :: { Int } 785 : ntStmt { 388 } 786 | many(outer_attribute) let pat ':' ty initializer ';' { 389 } 787 | many(outer_attribute) let pat initializer ';' { 390 } 788 | many(outer_attribute) nonblock_expr ';' { 391 } 789 | many(outer_attribute) block_like_expr ';' { 392 } 790 | many(outer_attribute) blockpostfix_expr ';' { 393 } 791 | many(outer_attribute) vis_union_nonblock_expr ';' { 394 } 792 | many(outer_attribute) block_like_expr %prec NOSEMI { 395 } 793 | many(outer_attribute) vis_safety_block ';' { 396 } 794 | many(outer_attribute) vis_safety_block %prec NOSEMI { 397 } 795 | gen_item(pub_or_inherited) { 398 } 796 | many(outer_attribute) expr_path '!' ident '[' token_stream ']' ';' { 399 } 797 | many(outer_attribute) expr_path '!' ident '(' token_stream ')' ';' { 400 } 798 | many(outer_attribute) expr_path '!' ident '{' token_stream '}' { 401 } 799 800 pub_or_inherited :: { Int } 801 : pub %prec VIS { 402 } 802 | {- empty -} %prec VIS { 403 } 803 804 stmtOrSemi :: { Int } 805 : ';' { 404 } 806 | stmt { 405 } 807 808 stmts_possibly_no_semi :: { Int } 809 : stmtOrSemi stmts_possibly_no_semi { 406 } 810 | stmtOrSemi { 407 } 811 | many(outer_attribute) nonblock_expr { 408 } 812 | many(outer_attribute) blockpostfix_expr { 409 } 813 814 initializer :: { Int } 815 : '=' expr { 410 } 816 | {- empty -} { 411 } 817 818 block :: { Int } 819 : ntBlock { 412 } 820 | '{' '}' { 413 } 821 | '{' stmts_possibly_no_semi '}' { 414 } 822 823 inner_attrs_block :: { Int } 824 : block { 415 } 825 | '{' inner_attrs '}' { 416 } 826 | '{' inner_attrs stmts_possibly_no_semi '}' { 417 } 827 828 gen_item(vis) :: { Int } 829 : many(outer_attribute) vis static ident ':' ty '=' expr ';' { 418 } 830 | many(outer_attribute) vis static mut ident ':' ty '=' expr ';' { 419 } 831 | many(outer_attribute) vis const ident ':' ty '=' expr ';' { 420 } 832 | many(outer_attribute) vis type ident generics where_clause '=' ty ';' { 421 } 833 | many(outer_attribute) vis use view_path ';' { 422 } 834 | many(outer_attribute) vis safety extern crate ident ';' { 423 } 835 | many(outer_attribute) vis safety extern crate ident as ident ';' { 424 } 836 | many(outer_attribute) vis const safety fn ident generics fn_decl(arg_named) where_clause inner_attrs_block { 425 } 837 | many(outer_attribute) vis safety extern abi fn ident generics fn_decl(arg_named) where_clause inner_attrs_block { 426 } 838 | many(outer_attribute) vis safety fn ident generics fn_decl(arg_named) where_clause inner_attrs_block { 427 } 839 | many(outer_attribute) vis mod ident ';' { 428 } 840 | many(outer_attribute) vis mod ident '{' many(mod_item) '}' { 429 } 841 | many(outer_attribute) vis mod ident '{' inner_attrs many(mod_item) '}' { 430 } 842 | many(outer_attribute) vis safety extern abi '{' many(foreign_item) '}' { 431 } 843 | many(outer_attribute) vis safety extern abi '{' inner_attrs many(foreign_item) '}' { 432 } 844 | many(outer_attribute) vis struct ident generics struct_decl_args { 433 } 845 | many(outer_attribute) vis union ident generics struct_decl_args { 434 } 846 | many(outer_attribute) vis enum ident generics where_clause '{' sep_byT(enum_def,',') '}' { 435 } 847 | many(outer_attribute) vis safety trait ident generics where_clause '{' many(trait_item) '}' { 437 } 848 | many(outer_attribute) vis safety impl generics ty_prim where_clause '{' impl_items '}' { 438 } 849 | many(outer_attribute) vis default safety impl generics ty_prim where_clause '{' impl_items '}' { 439 } 850 | many(outer_attribute) vis safety impl generics '(' ty_no_plus ')' where_clause '{' impl_items '}' { 440 } 851 | many(outer_attribute) vis default safety impl generics '(' ty_no_plus ')' where_clause '{' impl_items '}' { 441 } 852 | many(outer_attribute) vis safety impl generics '!' trait_ref for ty where_clause '{' impl_items '}' { 442 } 853 | many(outer_attribute) vis default safety impl generics '!' trait_ref for ty where_clause '{' impl_items '}' { 443 } 854 | many(outer_attribute) vis safety impl generics trait_ref for ty where_clause '{' impl_items '}' { 444 } 855 | many(outer_attribute) vis default safety impl generics trait_ref for ty where_clause '{' impl_items '}' { 445 } 856 | many(outer_attribute) vis safety impl generics trait_ref for '..' '{' '}' { 446 } 857 858 mod_item :: { Int } 859 : ntItem { 447 } 860 | gen_item(vis) { 448 } 861 | many(outer_attribute) expr_path '!' ident '[' token_stream ']' ';' { 449 } 862 | many(outer_attribute) expr_path '!' '[' token_stream ']' ';' { 450 } 863 | many(outer_attribute) expr_path '!' ident '(' token_stream ')' ';' { 451 } 864 | many(outer_attribute) expr_path '!' '(' token_stream ')' ';' { 452 } 865 | many(outer_attribute) expr_path '!' ident '{' token_stream '}' { 453 } 866 | many(outer_attribute) expr_path '!' '{' token_stream '}' { 454 } 867 868 foreign_item :: { Int } 869 : many(outer_attribute) vis static ident ':' ty ';' { 455 } 870 | many(outer_attribute) vis static mut ident ':' ty ';' { 456 } 871 | many(outer_attribute) vis fn ident generics fn_decl(arg_named) where_clause ';' { 457 } 872 873 874 875 generics :: { Int } 876 : ntGenerics { 458 } 877 | '<' sep_by1(lifetime_def,',') ',' sep_by1T(ty_param,',') gt '>' { 459 } 878 | '<' sep_by1T(lifetime_def,',') gt '>' { 460 } 879 | '<' sep_by1T(ty_param,',') gt '>' { 461 } 880 | '<' gt '>' { 462 } 881 | {- empty -} { 463 } 882 883 ty_param :: { Int } 884 : many(outer_attribute) ident { 464 } 885 | many(outer_attribute) ident ':' sep_by1T(ty_param_bound_mod,'+') { 465 } 886 | many(outer_attribute) ident '=' ty { 466 } 887 | many(outer_attribute) ident ':' sep_by1T(ty_param_bound_mod,'+') '=' ty { 467 } 888 889 struct_decl_args :: { Int } 890 : where_clause ';' { 468 } 891 | where_clause '{' sep_byT(struct_decl_field,',') '}' { 469 } 892 | '(' sep_byT(tuple_decl_field,',') ')' where_clause ';' { 470 } 893 894 struct_decl_field :: { Int } 895 : many(outer_attribute) vis ident ':' ty { 471 } 896 897 tuple_decl_field :: { Int } 898 : many(outer_attribute) vis ty { 472 } 899 900 enum_def :: { Int } 901 : many(outer_attribute) ident '{' sep_byT(struct_decl_field,',') '}' { 473 } 902 | many(outer_attribute) ident '(' sep_byT(tuple_decl_field,',') ')' { 474 } 903 | many(outer_attribute) ident initializer { 475 } 904 905 where_clause :: { Int } 906 : {- empty -} { 476 } 907 | ntWhereClause { 477 } 908 | where sep_by(where_predicate,',') %prec WHERE { 478 } 909 | where sep_by1(where_predicate,',') ',' %prec WHERE { 479 } 910 911 where_predicate :: { Int } 912 : lifetime { 480 } 913 | lifetime ':' sep_by1T(lifetime,'+') { 481 } 914 | no_for_ty %prec EQ { 482 } 915 | no_for_ty '=' ty { 483 } 916 | no_for_ty ':' sep_by1T(ty_param_bound_mod,'+') { 484 } 917 | for_lts no_for_ty { 485 } 918 | for_lts no_for_ty ':' sep_by1T(ty_param_bound_mod,'+') { 486 } 919 920 impl_items :: { Int } 921 : many(impl_item) { 487 } 922 | inner_attrs many(impl_item) { 488 } 923 924 impl_item :: { Int } 925 : many(outer_attribute) vis def type ident '=' ty ';' { 489 } 926 | many(outer_attribute) vis def const ident ':' ty '=' expr ';' { 490 } 927 | many(outer_attribute) def mod_mac { 491 } 928 929 trait_item :: { Int } 930 : ntTraitItem { 494 } 931 | many(outer_attribute) const ident ':' ty initializer ';' { 495 } 932 | many(outer_attribute) mod_mac { 496 } 933 | many(outer_attribute) type ident ';' { 497 } 934 | many(outer_attribute) type ident '=' ty ';' { 498 } 935 | many(outer_attribute) type ident ':' sep_by1T(ty_param_bound_mod,'+') ';' { 499 } 936 937 safety :: { Int } 938 : {- empty -} { 503 } 939 | unsafe { 504 } 940 941 ext_abi :: { Int } 942 : {- empty -} { 505 } 943 | extern abi { 506 } 944 945 vis :: { Int } 946 : {- empty -} %prec VIS { 507 } 947 | pub %prec VIS { 508 } 948 | pub '(' crate ')' { 509 } 949 | pub '(' in mod_path ')' { 510 } 950 | pub '(' super ')' { 511 } 951 | pub '(' self ')' { 512 } 952 953 def :: { Int } 954 : {- empty -} %prec DEF { 513 } 955 | default { 514 } 956 957 view_path :: { Int } 958 : '::' sep_by1(self_or_ident,'::') { 515 } 959 | '::' sep_by1(self_or_ident,'::') as ident { 516 } 960 | '::' '*' { 517 } 961 | '::' sep_by1(self_or_ident,'::') '::' '*' { 518 } 962 | '::' sep_by1(self_or_ident,'::') '::' '{' sep_byT(plist,',') '}' { 519 } 963 | '::' '{' sep_byT(plist,',') '}' { 520 } 964 | sep_by1(self_or_ident,'::') { 521 } 965 | sep_by1(self_or_ident,'::') as ident { 522 } 966 | '*' { 523 } 967 | sep_by1(self_or_ident,'::') '::' '*' { 524 } 968 | sep_by1(self_or_ident,'::') '::' '{' sep_byT(plist,',') '}' { 525 } 969 | '{' sep_byT(plist,',') '}' { 526 } 970 971 self_or_ident :: { Int } 972 : ident { 527 } 973 | self { 528 } 974 | Self { 529 } 975 | super { 530 } 976 977 plist :: { Int } 978 : self_or_ident { 531 } 979 | self_or_ident as ident { 532 } 980 981 expr_mac :: { Int } 982 : expr_path '!' '[' token_stream ']' { 533 } 983 | expr_path '!' '(' token_stream ')' { 534 } 984 985 ty_mac :: { Int } 986 : ty_path '!' '[' token_stream ']' { 535 } 987 | ty_path '!' '{' token_stream '}' { 536 } 988 | ty_path '!' '(' token_stream ')' { 537 } 989 990 mod_mac :: { Int } 991 : mod_path '!' '[' token_stream ']' ';' { 538 } 992 | mod_path '!' '{' token_stream '}' { 539 } 993 | mod_path '!' '(' token_stream ')' ';' { 540 } 994 995 token_stream :: { Int } 996 : {- empty -} { 541 } 997 | some(token_tree) { 542 } 998 999 token_tree :: { Int } 1000 : ntTT { 543 } 1001 | '(' token_stream ')' { 544 } 1002 | '{' token_stream '}' { 545 } 1003 | '[' token_stream ']' { 546 } 1004 | token { 547 } 1005 1006 token :: { Int } 1007 : '=' { 548 } 1008 | '<' { 549 } 1009 | '>' { 550 } 1010 | '!' { 551 } 1011 | '~' { 552 } 1012 | '-' { 553 } 1013 | '/' { 554 } 1014 | '+' { 555 } 1015 | '*' { 556 } 1016 | '%' { 557 } 1017 | '^' { 558 } 1018 | '&' { 559 } 1019 | '|' { 560 } 1020 | '<<=' { 561 } 1021 | '>>=' { 562 } 1022 | '-=' { 563 } 1023 | '&=' { 564 } 1024 | '|=' { 565 } 1025 | '+=' { 566 } 1026 | '*=' { 567 } 1027 | '/=' { 568 } 1028 | '^=' { 569 } 1029 | '%=' { 571 } 1030 | '||' { 572 } 1031 | '&&' { 573 } 1032 | '==' { 574 } 1033 | '!=' { 575 } 1034 | '<=' { 576 } 1035 | '>=' { 577 } 1036 | '<<' { 578 } 1037 | '>>' { 579 } 1038 | '@' { 580 } 1039 | '...' { 581 } 1040 | '..' { 582 } 1041 | '.' { 583 } 1042 | ',' { 584 } 1043 | ';' { 585 } 1044 | '::' { 586 } 1045 | ':' { 587 } 1046 | '->' { 588 } 1047 | '<-' { 589 } 1048 | '=>' { 590 } 1049 | '#' { 591 } 1050 | '$' { 592 } 1051 | '?' { 593 } 1052 | '#!' { 594 } 1053 | byte { 595 } 1054 | char { 596 } 1055 | int { 597 } 1056 | float { 598 } 1057 | str { 599 } 1058 | byteStr { 600 } 1059 | rawStr { 601 } 1060 | rawByteStr { 602 } 1061 | as { 603 } 1062 | box { 604 } 1063 | break { 605 } 1064 | const { 606 } 1065 | continue { 607 } 1066 | crate { 608 } 1067 | else { 609 } 1068 | enum { 610 } 1069 | extern { 611 } 1070 | false { 612 } 1071 | fn { 613 } 1072 | for { 614 } 1073 | if { 615 } 1074 | impl { 616 } 1075 | in { 617 } 1076 | let { 618 } 1077 | loop { 619 } 1078 | match { 620 } 1079 | mod { 621 } 1080 | move { 622 } 1081 | mut { 623 } 1082 | pub { 624 } 1083 | ref { 625 } 1084 | return { 626 } 1085 | Self { 627 } 1086 | self { 628 } 1087 | static { 629 } 1088 | struct { 630 } 1089 | super { 631 } 1090 | trait { 632 } 1091 | true { 633 } 1092 | type { 634 } 1093 | unsafe { 635 } 1094 | use { 636 } 1095 | where { 637 } 1096 | while { 638 } 1097 | abstract { 639 } 1098 | alignof { 640 } 1099 | become { 641 } 1100 | do { 642 } 1101 | final { 643 } 1102 | macro { 644 } 1103 | offsetof { 645 } 1104 | override { 646 } 1105 | priv { 647 } 1106 | proc { 648 } 1107 | pure { 649 } 1108 | sizeof { 650 } 1109 | typeof { 651 } 1110 | unsized { 652 } 1111 | virtual { 653 } 1112 | yield { 654 } 1113 | default { 655 } 1114 | union { 656 } 1115 | catch { 657 } 1116 | outerDoc { 658 } 1117 | innerDoc { 659 } 1118 | IDENT { 660 } 1119 | '_' { 661 } 1120 | LIFETIME { 662 } 1121 1122 export_attribute :: { Int } 1123 : inner_attribute { 663 } 1124 | outer_attribute { 664 } 1125 1126 export_block :: { Int } 1127 : ntBlock { 665 } 1128 | safety '{' '}' { 666 } 1129 | safety '{' stmts_possibly_no_semi '}' { 667 } 1130 1131 export_ty :: { Int } 1132 : ty { 668 } 1133 | impl_ty { 669 } 1134 1135 1136 { 1137 1138 type P a = String -> Either String (a, String) 1139 1140 bindP :: P a -> (a -> P b) -> P b 1141 bindP p f s = case p s of 1142 Left m -> Left m 1143 Right (x,s') -> f x s' 1144 1145 returnP :: a -> P a 1146 returnP x s = Right (x,s) 1147 1148 parseError :: Show b => b -> P a 1149 parseError b _ = Left ("Syntax error: the symbol `" ++ show b ++ "' does not fit here") 1150 1151 1152 data Token 1153 = Equal 1154 | Less 1155 | Greater 1156 | Ampersand 1157 | Pipe 1158 | Exclamation 1159 | Tilde 1160 | Plus 1161 | Minus 1162 | Star 1163 | Slash 1164 | Percent 1165 | Caret 1166 | GreaterEqual 1167 | GreaterGreaterEqual 1168 | AmpersandAmpersand 1169 | PipePipe 1170 | LessLess 1171 | GreaterGreater 1172 | EqualEqual 1173 | NotEqual 1174 | LessEqual 1175 | LessLessEqual 1176 | MinusEqual 1177 | AmpersandEqual 1178 | PipeEqual 1179 | PlusEqual 1180 | StarEqual 1181 | SlashEqual 1182 | CaretEqual 1183 | PercentEqual 1184 | At 1185 | Dot 1186 | DotDot 1187 | DotDotDot 1188 | Comma 1189 | Semicolon 1190 | Colon 1191 | ModSep 1192 | RArrow 1193 | LArrow 1194 | FatArrow 1195 | Pound 1196 | Dollar 1197 | Question 1198 | OpenParen 1199 | OpenBracket 1200 | OpenBrace 1201 | CloseParen 1202 | CloseBracket 1203 | CloseBrace 1204 | IdentTok String 1205 | Underscore 1206 | LifetimeTok String 1207 | Space 1208 | InnerDoc 1209 | OuterDoc 1210 | Shebang 1211 | Eof 1212 | ByteTok String 1213 | CharTok String 1214 | IntegerTok String 1215 | FloatTok String 1216 | StrTok String 1217 | StrRawTok String 1218 | ByteStrTok String 1219 | ByteStrRawTok String 1220 | Interpolated Int 1221 deriving Show 1222 1223 1224 -- This is an intentionally simplfied tokenizer 1225 lexNonSpace :: P Token 1226 lexNonSpace "" = Right (Eof, "") 1227 lexNonSpace ('.':cs) = Right (Dot, cs) 1228 lexNonSpace ('+':cs) = Right (Plus, cs) 1229 lexNonSpace (';':cs) = Right (Semicolon, cs) 1230 lexNonSpace (',':cs) = Right (Comma, cs) 1231 lexNonSpace ('=':cs) = Right (Equal, cs) 1232 lexNonSpace ('{':cs) = Right (OpenBrace, cs) 1233 lexNonSpace ('}':cs) = Right (CloseBrace, cs) 1234 lexNonSpace ('(':cs) = Right (OpenParen, cs) 1235 lexNonSpace (')':cs) = Right (CloseParen, cs) 1236 lexNonSpace (c:cs) 1237 | isSpace c = lexNonSpace cs 1238 | isNumber c = let (tok,cs') = span isNumber (c:cs) in Right (IntegerTok tok, cs') 1239 | isAlpha c = let (tok,cs') = span isAlphaNum (c:cs) in Right (IdentTok tok, cs') 1240 | otherwise = Left ("Unexpected character: `" ++ [c] ++ "'") 1241 1242 1243 main = case parseStmt "union.1 + 2;" of 1244 Right (394, "") -> pure () 1245 _ -> exitWith (ExitFailure 1) 1246 } 1247