fork/rust-analyzer
git clone https://git.julia.blue/fork/rust-analyzer
4f6b2a20fd
2022-06-13
1// Rust Un-Grammar. 2// 3// This grammar specifies the structure of Rust's concrete syntax tree. 4// It does not specify parsing rules (ambiguities, precedence, etc are out of scope). 5// Tokens are processed -- contextual keywords are recognised, compound operators glued. 6// 7// Legend: 8// 9// // -- comment 10// Name = -- non-terminal definition 11// 'ident' -- token (terminal) 12// A B -- sequence 13// A | B -- alternation 14// A* -- zero or more repetition 15// A? -- zero or one repetition 16// (A) -- same as A 17// label:A -- suggested name for field of AST node 18 19//*************************// 20// Names, Paths and Macros // 21//*************************// 22 23Name = 24 'ident' | 'self' 25 26NameRef = 27 'ident' | 'int_number' | 'self' | 'super' | 'crate' | 'Self' 28 29Lifetime = 30 'lifetime_ident' 31 32Path = 33 (qualifier:Path '::')? segment:PathSegment 34 35PathSegment = 36 '::'? NameRef 37| NameRef GenericArgList? 38| NameRef ParamList RetType? 39| '<' PathType ('as' PathType)? '>' 40 41GenericArgList = 42 '::'? '<' (GenericArg (',' GenericArg)* ','?)? '>' 43 44GenericArg = 45 TypeArg 46| AssocTypeArg 47| LifetimeArg 48| ConstArg 49 50TypeArg = 51 Type 52 53AssocTypeArg = 54 NameRef GenericParamList? (':' TypeBoundList | ('=' Type | ConstArg)) 55 56LifetimeArg = 57 Lifetime 58 59ConstArg = 60 Expr 61 62MacroCall = 63 Attr* Path '!' TokenTree ';'? 64 65TokenTree = 66 '(' ')' 67| '{' '}' 68| '[' ']' 69 70MacroItems = 71 Item* 72 73MacroStmts = 74 statements:Stmt* 75 Expr? 76 77//*************************// 78// Items // 79//*************************// 80 81SourceFile = 82 'shebang'? 83 Attr* 84 Item* 85 86Item = 87 Const 88| Enum 89| ExternBlock 90| ExternCrate 91| Fn 92| Impl 93| MacroCall 94| MacroRules 95| MacroDef 96| Module 97| Static 98| Struct 99| Trait 100| TypeAlias 101| Union 102| Use 103 104MacroRules = 105 Attr* Visibility? 106 'macro_rules' '!' Name 107 TokenTree 108 109MacroDef = 110 Attr* Visibility? 111 'macro' Name args:TokenTree? 112 body:TokenTree 113 114Module = 115 Attr* Visibility? 116 'mod' Name 117 (ItemList | ';') 118 119ItemList = 120 '{' Attr* Item* '}' 121 122ExternCrate = 123 Attr* Visibility? 124 'extern' 'crate' NameRef Rename? ';' 125 126Rename = 127 'as' (Name | '_') 128 129Use = 130 Attr* Visibility? 131 'use' UseTree ';' 132 133UseTree = 134 (Path? '::')? ('*' | UseTreeList) 135| Path Rename? 136 137UseTreeList = 138 '{' (UseTree (',' UseTree)* ','?)? '}' 139 140Fn = 141 Attr* Visibility? 142 'default'? 'const'? 'async'? 'unsafe'? Abi? 143 'fn' Name GenericParamList? ParamList RetType? WhereClause? 144 (body:BlockExpr | ';') 145 146Abi = 147 'extern' 'string'? 148 149ParamList = 150 '('( 151 SelfParam 152 | (SelfParam ',')? (Param (',' Param)* ','?)? 153 )')' 154| '|' (Param (',' Param)* ','?)? '|' 155 156SelfParam = 157 Attr* ( 158 ('&' Lifetime?)? 'mut'? Name 159 | 'mut'? Name ':' Type 160 ) 161 162Param = 163 Attr* ( 164 Pat (':' Type)? 165 | Type 166 | '...' 167 ) 168 169RetType = 170 '->' Type 171 172TypeAlias = 173 Attr* Visibility? 174 'default'? 175 'type' Name GenericParamList? (':' TypeBoundList?)? WhereClause? 176 ('=' Type)? ';' 177 178Struct = 179 Attr* Visibility? 180 'struct' Name GenericParamList? ( 181 WhereClause? (RecordFieldList | ';') 182 | TupleFieldList WhereClause? ';' 183 ) 184 185RecordFieldList = 186 '{' fields:(RecordField (',' RecordField)* ','?)? '}' 187 188RecordField = 189 Attr* Visibility? 190 Name ':' Type 191 192TupleFieldList = 193 '(' fields:(TupleField (',' TupleField)* ','?)? ')' 194 195TupleField = 196 Attr* Visibility? 197 Type 198 199FieldList = 200 RecordFieldList 201| TupleFieldList 202 203Enum = 204 Attr* Visibility? 205 'enum' Name GenericParamList? WhereClause? 206 VariantList 207 208VariantList = 209 '{' (Variant (',' Variant)* ','?)? '}' 210 211Variant = 212 Attr* Visibility? 213 Name FieldList? ('=' Expr)? 214 215Union = 216 Attr* Visibility? 217 'union' Name GenericParamList? WhereClause? 218 RecordFieldList 219 220// A Data Type. 221// 222// Not used directly in the grammar, but handy to have anyway. 223Adt = 224 Enum 225| Struct 226| Union 227 228Const = 229 Attr* Visibility? 230 'default'? 231 'const' (Name | '_') ':' Type 232 ('=' body:Expr)? ';' 233 234Static = 235 Attr* Visibility? 236 'static' 'mut'? Name ':' Type 237 ('=' body:Expr)? ';' 238 239Trait = 240 Attr* Visibility? 241 'unsafe'? 'auto'? 242 'trait' Name GenericParamList? (':' TypeBoundList?)? WhereClause? 243 AssocItemList 244 245AssocItemList = 246 '{' Attr* AssocItem* '}' 247 248AssocItem = 249 Const 250| Fn 251| MacroCall 252| TypeAlias 253 254Impl = 255 Attr* Visibility? 256 'default'? 'unsafe'? 257 'impl' GenericParamList? ('const'? '!'? trait:Type 'for')? self_ty:Type WhereClause? 258 AssocItemList 259 260ExternBlock = 261 Attr* 'unsafe'? Abi ExternItemList 262 263ExternItemList = 264 '{' Attr* ExternItem* '}' 265 266ExternItem = 267 Fn 268| MacroCall 269| Static 270| TypeAlias 271 272GenericParamList = 273 '<' (GenericParam (',' GenericParam)* ','?)? '>' 274 275GenericParam = 276 ConstParam 277| LifetimeParam 278| TypeParam 279 280TypeParam = 281 Attr* Name (':' TypeBoundList?)? 282 ('=' default_type:Type)? 283 284ConstParam = 285 Attr* 'const' Name ':' Type 286 ('=' default_val:Expr)? 287 288LifetimeParam = 289 Attr* Lifetime (':' TypeBoundList?)? 290 291WhereClause = 292 'where' predicates:(WherePred (',' WherePred)* ','?) 293 294WherePred = 295 ('for' GenericParamList)? (Lifetime | Type) ':' TypeBoundList? 296 297Visibility = 298 'pub' ('(' 'in'? Path ')')? 299 300Attr = 301 '#' '!'? '[' Meta ']' 302 303Meta = 304 Path ('=' Expr | TokenTree)? 305 306//****************************// 307// Statements and Expressions // 308//****************************// 309 310Stmt = 311 ';' 312| ExprStmt 313| Item 314| LetStmt 315 316LetStmt = 317 Attr* 'let' Pat (':' Type)? 318 '=' initializer:Expr 319 LetElse? 320 ';' 321 322LetElse = 323 'else' BlockExpr 324 325ExprStmt = 326 Expr ';'? 327 328Expr = 329 ArrayExpr 330| AwaitExpr 331| BinExpr 332| BlockExpr 333| BoxExpr 334| BreakExpr 335| CallExpr 336| CastExpr 337| ClosureExpr 338| ContinueExpr 339| FieldExpr 340| ForExpr 341| IfExpr 342| IndexExpr 343| Literal 344| LoopExpr 345| MacroExpr 346| MacroStmts 347| MatchExpr 348| MethodCallExpr 349| ParenExpr 350| PathExpr 351| PrefixExpr 352| RangeExpr 353| RecordExpr 354| RefExpr 355| ReturnExpr 356| TryExpr 357| TupleExpr 358| WhileExpr 359| YieldExpr 360| LetExpr 361| UnderscoreExpr 362 363MacroExpr = 364 MacroCall 365 366Literal = 367 Attr* value:( 368 'int_number' | 'float_number' 369 | 'string' | 'raw_string' 370 | 'byte_string' | 'raw_byte_string' 371 | 'true' | 'false' 372 | 'char' | 'byte' 373 ) 374 375PathExpr = 376 Attr* Path 377 378StmtList = 379 '{' 380 Attr* 381 statements:Stmt* 382 tail_expr:Expr? 383 '}' 384 385RefExpr = 386 Attr* '&' ('raw' | 'mut' | 'const') Expr 387 388TryExpr = 389 Attr* Expr '?' 390 391BlockExpr = 392 Attr* Label? ('try' | 'unsafe' | 'async' | 'const') StmtList 393 394PrefixExpr = 395 Attr* op:('-' | '!' | '*') Expr 396 397BinExpr = 398 Attr* 399 lhs:Expr 400 op:( 401 '||' | '&&' 402 | '==' | '!=' | '<=' | '>=' | '<' | '>' 403 | '+' | '*' | '-' | '/' | '%' | '<<' | '>>' | '^' | '|' | '&' 404 | '=' | '+=' | '/=' | '*=' | '%=' | '>>=' | '<<=' | '-=' | '|=' | '&=' | '^=' 405 ) 406 rhs:Expr 407 408CastExpr = 409 Attr* Expr 'as' Type 410 411ParenExpr = 412 Attr* '(' Attr* Expr ')' 413 414ArrayExpr = 415 Attr* '[' Attr* ( 416 (Expr (',' Expr)* ','?)? 417 | Expr ';' Expr 418 ) ']' 419 420IndexExpr = 421 Attr* base:Expr '[' index:Expr ']' 422 423TupleExpr = 424 Attr* '(' Attr* fields:(Expr (',' Expr)* ','?)? ')' 425 426RecordExpr = 427 Path RecordExprFieldList 428 429RecordExprFieldList = 430 '{' 431 Attr* 432 fields:(RecordExprField (',' RecordExprField)* ','?)? 433 ('..' spread:Expr?)? 434 '}' 435 436RecordExprField = 437 Attr* (NameRef ':')? Expr 438 439CallExpr = 440 Attr* Expr ArgList 441 442ArgList = 443 '(' args:(Expr (',' Expr)* ','?)? ')' 444 445MethodCallExpr = 446 Attr* receiver:Expr '.' NameRef GenericArgList? ArgList 447 448FieldExpr = 449 Attr* Expr '.' NameRef 450 451ClosureExpr = 452 Attr* ('for' GenericParamList)? 'static'? 'async'? 'move'? ParamList RetType? 453 body:Expr 454 455IfExpr = 456 Attr* 'if' condition:Expr then_branch:BlockExpr 457 ('else' else_branch:(IfExpr | BlockExpr))? 458 459LoopExpr = 460 Attr* Label? 'loop' 461 loop_body:BlockExpr 462 463ForExpr = 464 Attr* Label? 'for' Pat 'in' iterable:Expr 465 loop_body:BlockExpr 466 467WhileExpr = 468 Attr* Label? 'while' condition:Expr 469 loop_body:BlockExpr 470 471Label = 472 Lifetime ':' 473 474BreakExpr = 475 Attr* 'break' Lifetime? Expr? 476 477ContinueExpr = 478 Attr* 'continue' Lifetime? 479 480RangeExpr = 481 Attr* start:Expr? op:('..' | '..=') end:Expr? 482 483MatchExpr = 484 Attr* 'match' Expr MatchArmList 485 486MatchArmList = 487 '{' 488 Attr* 489 arms:MatchArm* 490 '}' 491 492MatchArm = 493 Attr* Pat guard:MatchGuard? '=>' Expr ','? 494 495MatchGuard = 496 'if' condition:Expr 497 498ReturnExpr = 499 Attr* 'return' Expr? 500 501YieldExpr = 502 Attr* 'yield' Expr? 503 504LetExpr = 505 Attr* 'let' Pat '=' Expr 506 507UnderscoreExpr = 508 Attr* '_' 509 510AwaitExpr = 511 Attr* Expr '.' 'await' 512 513BoxExpr = 514 Attr* 'box' Expr 515 516//*************************// 517// Types // 518//*************************// 519 520Type = 521 ArrayType 522| DynTraitType 523| FnPtrType 524| ForType 525| ImplTraitType 526| InferType 527| MacroType 528| NeverType 529| ParenType 530| PathType 531| PtrType 532| RefType 533| SliceType 534| TupleType 535 536ParenType = 537 '(' Type ')' 538 539NeverType = 540 '!' 541 542MacroType = 543 MacroCall 544 545PathType = 546 Path 547 548TupleType = 549 '(' fields:(Type (',' Type)* ','?)? ')' 550 551PtrType = 552 '*' ('const' | 'mut') Type 553 554RefType = 555 '&' Lifetime? 'mut'? Type 556 557ArrayType = 558 '[' Type ';' Expr ']' 559 560SliceType = 561 '[' Type ']' 562 563InferType = 564 '_' 565 566FnPtrType = 567 'const'? 'async'? 'unsafe'? Abi? 'fn' ParamList RetType? 568 569ForType = 570 'for' GenericParamList Type 571 572ImplTraitType = 573 'impl' TypeBoundList 574 575DynTraitType = 576 'dyn' TypeBoundList 577 578TypeBoundList = 579 bounds:(TypeBound ('+' TypeBound)* '+'?) 580 581TypeBound = 582 Lifetime 583| ('?' | '~' 'const')? Type 584 585//************************// 586// Patterns // 587//************************// 588 589Pat = 590 IdentPat 591| BoxPat 592| RestPat 593| LiteralPat 594| MacroPat 595| OrPat 596| ParenPat 597| PathPat 598| WildcardPat 599| RangePat 600| RecordPat 601| RefPat 602| SlicePat 603| TuplePat 604| TupleStructPat 605| ConstBlockPat 606 607LiteralPat = 608 Literal 609 610IdentPat = 611 Attr* 'ref'? 'mut'? Name ('@' Pat)? 612 613WildcardPat = 614 '_' 615 616RangePat = 617 // 1.. 618 start:Pat op:('..' | '..=') 619 // 1..2 620 | start:Pat op:('..' | '..=') end:Pat 621 // ..2 622 | op:('..' | '..=') end:Pat 623 624RefPat = 625 '&' 'mut'? Pat 626 627RecordPat = 628 Path RecordPatFieldList 629 630RecordPatFieldList = 631 '{' 632 fields:(RecordPatField (',' RecordPatField)* ','?)? 633 RestPat? 634 '}' 635 636RecordPatField = 637 Attr* (NameRef ':')? Pat 638 639TupleStructPat = 640 Path '(' fields:(Pat (',' Pat)* ','?)? ')' 641 642TuplePat = 643 '(' fields:(Pat (',' Pat)* ','?)? ')' 644 645ParenPat = 646 '(' Pat ')' 647 648SlicePat = 649 '[' (Pat (',' Pat)* ','?)? ']' 650 651PathPat = 652 Path 653 654OrPat = 655 (Pat ('|' Pat)* '|'?) 656 657BoxPat = 658 'box' Pat 659 660RestPat = 661 Attr* '..' 662 663MacroPat = 664 MacroCall 665 666ConstBlockPat = 667 'const' BlockExpr