diff options
Diffstat (limited to 'xtask')
-rw-r--r-- | xtask/src/ast_src.rs | 375 | ||||
-rw-r--r-- | xtask/src/codegen/gen_syntax.rs | 87 |
2 files changed, 342 insertions, 120 deletions
diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs index d9f51ec39..5fed777ac 100644 --- a/xtask/src/ast_src.rs +++ b/xtask/src/ast_src.rs | |||
@@ -70,7 +70,7 @@ pub(crate) const KINDS_SRC: KindsSrc = KindsSrc { | |||
70 | "match", "mod", "move", "mut", "pub", "ref", "return", "self", "static", "struct", "super", | 70 | "match", "mod", "move", "mut", "pub", "ref", "return", "self", "static", "struct", "super", |
71 | "trait", "true", "try", "type", "unsafe", "use", "where", "while", | 71 | "trait", "true", "try", "type", "unsafe", "use", "where", "while", |
72 | ], | 72 | ], |
73 | contextual_keywords: &["auto", "default", "existential", "union"], | 73 | contextual_keywords: &["auto", "default", "existential", "union", "raw"], |
74 | literals: &[ | 74 | literals: &[ |
75 | "INT_NUMBER", | 75 | "INT_NUMBER", |
76 | "FLOAT_NUMBER", | 76 | "FLOAT_NUMBER", |
@@ -297,235 +297,310 @@ macro_rules! ast_enums { | |||
297 | 297 | ||
298 | pub(crate) const AST_SRC: AstSrc = AstSrc { | 298 | pub(crate) const AST_SRC: AstSrc = AstSrc { |
299 | nodes: &ast_nodes! { | 299 | nodes: &ast_nodes! { |
300 | struct SourceFile: ModuleItemOwner, FnDefOwner { | 300 | struct SourceFile: ModuleItemOwner, FnDefOwner, AttrsOwner { |
301 | modules: [Module], | 301 | modules: [Module], |
302 | } | 302 | } |
303 | 303 | ||
304 | struct FnDef: VisibilityOwner, NameOwner, TypeParamsOwner, DocCommentsOwner, AttrsOwner { | 304 | struct FnDef: VisibilityOwner, NameOwner, TypeParamsOwner, DocCommentsOwner, AttrsOwner { |
305 | Abi, | ||
306 | ConstKw, | ||
307 | DefaultKw, | ||
308 | AsyncKw, | ||
309 | UnsafeKw, | ||
310 | FnKw, | ||
305 | ParamList, | 311 | ParamList, |
306 | RetType, | 312 | RetType, |
307 | body: BlockExpr, | 313 | body: BlockExpr, |
314 | Semi | ||
308 | } | 315 | } |
309 | 316 | ||
310 | struct RetType { TypeRef } | 317 | struct RetType { ThinArrow, TypeRef } |
311 | 318 | ||
312 | struct StructDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner { | 319 | struct StructDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner { |
320 | StructKw, | ||
321 | FieldDefList, | ||
322 | Semi | ||
313 | } | 323 | } |
314 | 324 | ||
315 | struct UnionDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner { | 325 | struct UnionDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner { |
326 | UnionKw, | ||
316 | RecordFieldDefList, | 327 | RecordFieldDefList, |
317 | } | 328 | } |
318 | 329 | ||
319 | struct RecordFieldDefList { fields: [RecordFieldDef] } | 330 | struct RecordFieldDefList { LCurly, fields: [RecordFieldDef], RCurly } |
320 | struct RecordFieldDef: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { } | 331 | struct RecordFieldDef: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { } |
321 | 332 | ||
322 | struct TupleFieldDefList { fields: [TupleFieldDef] } | 333 | struct TupleFieldDefList { LParen, fields: [TupleFieldDef], RParen } |
323 | struct TupleFieldDef: VisibilityOwner, AttrsOwner { | 334 | struct TupleFieldDef: VisibilityOwner, AttrsOwner { |
324 | TypeRef, | 335 | TypeRef, |
325 | } | 336 | } |
326 | 337 | ||
327 | struct EnumDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner { | 338 | struct EnumDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner { |
339 | EnumKw, | ||
328 | variant_list: EnumVariantList, | 340 | variant_list: EnumVariantList, |
329 | } | 341 | } |
330 | struct EnumVariantList { | 342 | struct EnumVariantList { |
343 | LCurly, | ||
331 | variants: [EnumVariant], | 344 | variants: [EnumVariant], |
345 | RCurly | ||
332 | } | 346 | } |
333 | struct EnumVariant: NameOwner, DocCommentsOwner, AttrsOwner { | 347 | struct EnumVariant: VisibilityOwner, NameOwner, DocCommentsOwner, AttrsOwner { |
348 | FieldDefList, | ||
349 | Eq, | ||
334 | Expr | 350 | Expr |
335 | } | 351 | } |
336 | 352 | ||
337 | struct TraitDef: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner, TypeParamsOwner, TypeBoundsOwner { | 353 | struct TraitDef: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner, TypeParamsOwner, TypeBoundsOwner { |
354 | UnsafeKw, | ||
355 | AutoKw, | ||
356 | TraitKw, | ||
338 | ItemList, | 357 | ItemList, |
339 | } | 358 | } |
340 | 359 | ||
341 | struct Module: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner { | 360 | struct Module: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner { |
361 | ModKw, | ||
342 | ItemList, | 362 | ItemList, |
363 | Semi | ||
343 | } | 364 | } |
344 | 365 | ||
345 | struct ItemList: FnDefOwner, ModuleItemOwner { | 366 | struct ItemList: FnDefOwner, ModuleItemOwner { |
367 | LCurly, | ||
346 | impl_items: [ImplItem], | 368 | impl_items: [ImplItem], |
369 | RCurly | ||
347 | } | 370 | } |
348 | 371 | ||
349 | struct ConstDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { | 372 | struct ConstDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { |
373 | DefaultKw, | ||
374 | ConstKw, | ||
375 | Eq, | ||
350 | body: Expr, | 376 | body: Expr, |
377 | Semi | ||
351 | } | 378 | } |
352 | 379 | ||
353 | struct StaticDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { | 380 | struct StaticDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { |
381 | StaticKw, | ||
382 | MutKw, | ||
383 | Eq, | ||
354 | body: Expr, | 384 | body: Expr, |
385 | Semi | ||
355 | } | 386 | } |
356 | 387 | ||
357 | struct TypeAliasDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeBoundsOwner { | 388 | struct TypeAliasDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeBoundsOwner { |
389 | DefaultKw, | ||
390 | TypeKw, | ||
391 | Eq, | ||
358 | TypeRef, | 392 | TypeRef, |
393 | Semi | ||
359 | } | 394 | } |
360 | 395 | ||
361 | struct ImplDef: TypeParamsOwner, AttrsOwner { | 396 | struct ImplDef: TypeParamsOwner, AttrsOwner { |
397 | DefaultKw, | ||
398 | ConstKw, | ||
399 | UnsafeKw, | ||
400 | ImplKw, | ||
401 | Excl, | ||
402 | ForKw, | ||
362 | ItemList, | 403 | ItemList, |
363 | } | 404 | } |
364 | 405 | ||
365 | struct ParenType { TypeRef } | 406 | struct ParenType { LParen, TypeRef, RParen } |
366 | struct TupleType { fields: [TypeRef] } | 407 | struct TupleType { LParen, fields: [TypeRef], RParen } |
367 | struct NeverType { } | 408 | struct NeverType { Excl } |
368 | struct PathType { Path } | 409 | struct PathType { Path } |
369 | struct PointerType { TypeRef } | 410 | struct PointerType { Star, ConstKw, TypeRef } |
370 | struct ArrayType { TypeRef, Expr } | 411 | struct ArrayType { LBrack, TypeRef, Semi, Expr, RBrack } |
371 | struct SliceType { TypeRef } | 412 | struct SliceType { LBrack, TypeRef, RBrack } |
372 | struct ReferenceType { TypeRef } | 413 | struct ReferenceType { Amp, Lifetime, MutKw, TypeRef } |
373 | struct PlaceholderType { } | 414 | struct PlaceholderType { Underscore } |
374 | struct FnPointerType { ParamList, RetType } | 415 | struct FnPointerType { Abi, UnsafeKw, FnKw, ParamList, RetType } |
375 | struct ForType { TypeRef } | 416 | struct ForType { ForKw, TypeParamList, TypeRef } |
376 | struct ImplTraitType: TypeBoundsOwner {} | 417 | struct ImplTraitType: TypeBoundsOwner { ImplKw } |
377 | struct DynTraitType: TypeBoundsOwner {} | 418 | struct DynTraitType: TypeBoundsOwner { DynKw } |
378 | 419 | ||
379 | struct TupleExpr { exprs: [Expr] } | 420 | struct TupleExpr: AttrsOwner { LParen, exprs: [Expr], RParen } |
380 | struct ArrayExpr { exprs: [Expr] } | 421 | struct ArrayExpr: AttrsOwner { LBrack, exprs: [Expr], Semi, RBrack } |
381 | struct ParenExpr { Expr } | 422 | struct ParenExpr: AttrsOwner { LParen, Expr, RParen } |
382 | struct PathExpr { Path } | 423 | struct PathExpr { Path } |
383 | struct LambdaExpr { | 424 | struct LambdaExpr: AttrsOwner { |
425 | StaticKw, | ||
426 | AsyncKw, | ||
427 | MoveKw, | ||
384 | ParamList, | 428 | ParamList, |
385 | RetType, | 429 | RetType, |
386 | body: Expr, | 430 | body: Expr, |
387 | } | 431 | } |
388 | struct IfExpr { Condition } | 432 | struct IfExpr: AttrsOwner { IfKw, Condition } |
389 | struct LoopExpr: LoopBodyOwner { } | 433 | struct LoopExpr: AttrsOwner, LoopBodyOwner { LoopKw } |
390 | struct TryBlockExpr { body: BlockExpr } | 434 | struct TryBlockExpr: AttrsOwner { TryKw, body: BlockExpr } |
391 | struct ForExpr: LoopBodyOwner { | 435 | struct ForExpr: AttrsOwner, LoopBodyOwner { |
436 | ForKw, | ||
392 | Pat, | 437 | Pat, |
438 | InKw, | ||
393 | iterable: Expr, | 439 | iterable: Expr, |
394 | } | 440 | } |
395 | struct WhileExpr: LoopBodyOwner { Condition } | 441 | struct WhileExpr: AttrsOwner, LoopBodyOwner { WhileKw, Condition } |
396 | struct ContinueExpr {} | 442 | struct ContinueExpr: AttrsOwner { ContinueKw, Lifetime } |
397 | struct BreakExpr { Expr } | 443 | struct BreakExpr: AttrsOwner { BreakKw, Lifetime, Expr } |
398 | struct Label {} | 444 | struct Label { Lifetime } |
399 | struct BlockExpr { Block } | 445 | struct BlockExpr: AttrsOwner { Label, UnsafeKw, Block } |
400 | struct ReturnExpr { Expr } | 446 | struct ReturnExpr: AttrsOwner { Expr } |
401 | struct CallExpr: ArgListOwner { Expr } | 447 | struct CallExpr: ArgListOwner { Expr } |
402 | struct MethodCallExpr: ArgListOwner { | 448 | struct MethodCallExpr: AttrsOwner, ArgListOwner { |
403 | Expr, NameRef, TypeArgList, | 449 | Expr, Dot, NameRef, TypeArgList, |
404 | } | 450 | } |
405 | struct IndexExpr {} | 451 | struct IndexExpr: AttrsOwner { LBrack, RBrack } |
406 | struct FieldExpr { Expr, NameRef } | 452 | struct FieldExpr: AttrsOwner { Expr, Dot, NameRef } |
407 | struct AwaitExpr { Expr } | 453 | struct AwaitExpr: AttrsOwner { Expr, Dot, AwaitKw } |
408 | struct TryExpr { Expr } | 454 | struct TryExpr: AttrsOwner { TryKw, Expr } |
409 | struct CastExpr { Expr, TypeRef } | 455 | struct CastExpr: AttrsOwner { Expr, AsKw, TypeRef } |
410 | struct RefExpr { Expr } | 456 | struct RefExpr: AttrsOwner { Amp, RawKw, MutKw, Expr } |
411 | struct PrefixExpr { Expr } | 457 | struct PrefixExpr: AttrsOwner { PrefixOp, Expr } |
412 | struct BoxExpr { Expr } | 458 | struct BoxExpr: AttrsOwner { BoxKw, Expr } |
413 | struct RangeExpr {} | 459 | struct RangeExpr: AttrsOwner { RangeOp } |
414 | struct BinExpr {} | 460 | struct BinExpr: AttrsOwner { BinOp } |
415 | struct Literal {} | 461 | struct Literal { LiteralToken } |
416 | 462 | ||
417 | struct MatchExpr { Expr, MatchArmList } | 463 | struct MatchExpr: AttrsOwner { MatchKw, Expr, MatchArmList } |
418 | struct MatchArmList: AttrsOwner { arms: [MatchArm] } | 464 | struct MatchArmList: AttrsOwner { LCurly, arms: [MatchArm], RCurly } |
419 | struct MatchArm: AttrsOwner { | 465 | struct MatchArm: AttrsOwner { |
420 | pat: Pat, | 466 | pat: Pat, |
421 | guard: MatchGuard, | 467 | guard: MatchGuard, |
468 | FatArrow, | ||
422 | Expr, | 469 | Expr, |
423 | } | 470 | } |
424 | struct MatchGuard { Expr } | 471 | struct MatchGuard { IfKw, Expr } |
425 | 472 | ||
426 | struct RecordLit { Path, RecordFieldList } | 473 | struct RecordLit { Path, RecordFieldList} |
427 | struct RecordFieldList { | 474 | struct RecordFieldList { |
475 | LCurly, | ||
428 | fields: [RecordField], | 476 | fields: [RecordField], |
477 | Dotdot, | ||
429 | spread: Expr, | 478 | spread: Expr, |
479 | RCurly | ||
430 | } | 480 | } |
431 | struct RecordField { NameRef, Expr } | 481 | struct RecordField: AttrsOwner { NameRef, Colon, Expr } |
432 | 482 | ||
433 | struct OrPat { pats: [Pat] } | 483 | struct OrPat { pats: [Pat] } |
434 | struct ParenPat { Pat } | 484 | struct ParenPat { LParen, Pat, RParen } |
435 | struct RefPat { Pat } | 485 | struct RefPat { Amp, MutKw, Pat } |
436 | struct BoxPat { Pat } | 486 | struct BoxPat { BoxKw, Pat } |
437 | struct BindPat: NameOwner { Pat } | 487 | struct BindPat: AttrsOwner, NameOwner { RefKw, MutKw, Pat } |
438 | struct PlaceholderPat { } | 488 | struct PlaceholderPat { Underscore } |
439 | struct DotDotPat { } | 489 | struct DotDotPat { Dotdot } |
440 | struct PathPat { Path } | 490 | struct PathPat { Path } |
441 | struct SlicePat { args: [Pat] } | 491 | struct SlicePat { LBrack, args: [Pat], RBrack } |
442 | struct RangePat {} | 492 | struct RangePat { RangeSeparator } |
443 | struct LiteralPat { Literal } | 493 | struct LiteralPat { Literal } |
444 | struct MacroPat { MacroCall } | 494 | struct MacroPat { MacroCall } |
445 | 495 | ||
446 | struct RecordPat { RecordFieldPatList, Path } | 496 | struct RecordPat { RecordFieldPatList, Path } |
447 | struct RecordFieldPatList { | 497 | struct RecordFieldPatList { |
498 | LCurly, | ||
499 | pats: [RecordInnerPat], | ||
448 | record_field_pats: [RecordFieldPat], | 500 | record_field_pats: [RecordFieldPat], |
449 | bind_pats: [BindPat], | 501 | bind_pats: [BindPat], |
502 | Dotdot, | ||
503 | RCurly | ||
450 | } | 504 | } |
451 | struct RecordFieldPat: NameOwner { Pat } | 505 | struct RecordFieldPat: AttrsOwner, NameOwner { Colon, Pat } |
452 | 506 | ||
453 | struct TupleStructPat { Path, args: [Pat] } | 507 | struct TupleStructPat { Path, LParen, args: [Pat], RParen } |
454 | struct TuplePat { args: [Pat] } | 508 | struct TuplePat { LParen, args: [Pat], RParen } |
455 | 509 | ||
456 | struct Visibility {} | 510 | struct Visibility { PubKw, SuperKw, SelfKw, CrateKw } |
457 | struct Name {} | 511 | struct Name { Ident } |
458 | struct NameRef {} | 512 | struct NameRef { NameRefToken } |
459 | 513 | ||
460 | struct MacroCall: NameOwner, AttrsOwner,DocCommentsOwner { | 514 | struct MacroCall: NameOwner, AttrsOwner,DocCommentsOwner { |
461 | TokenTree, Path | 515 | Path, Excl, TokenTree, Semi |
462 | } | 516 | } |
463 | struct Attr { Path, input: AttrInput } | 517 | struct Attr { Pound, Excl, LBrack, Path, Eq, input: AttrInput, RBrack } |
464 | struct TokenTree {} | 518 | struct TokenTree {} |
465 | struct TypeParamList { | 519 | struct TypeParamList { |
520 | LAngle, | ||
521 | generic_params: [GenericParam], | ||
466 | type_params: [TypeParam], | 522 | type_params: [TypeParam], |
467 | lifetime_params: [LifetimeParam], | 523 | lifetime_params: [LifetimeParam], |
524 | const_params: [ConstParam], | ||
525 | RAngle | ||
468 | } | 526 | } |
469 | struct TypeParam: NameOwner, AttrsOwner, TypeBoundsOwner { | 527 | struct TypeParam: NameOwner, AttrsOwner, TypeBoundsOwner { |
528 | Eq, | ||
470 | default_type: TypeRef, | 529 | default_type: TypeRef, |
471 | } | 530 | } |
472 | struct ConstParam: NameOwner, AttrsOwner, TypeAscriptionOwner { | 531 | struct ConstParam: NameOwner, AttrsOwner, TypeAscriptionOwner { |
532 | Eq, | ||
473 | default_val: Expr, | 533 | default_val: Expr, |
474 | } | 534 | } |
475 | struct LifetimeParam: AttrsOwner { } | 535 | struct LifetimeParam: AttrsOwner { Lifetime} |
476 | struct TypeBound { TypeRef} | 536 | struct TypeBound { Lifetime, /* Question, */ ConstKw, /* Question, */ TypeRef} |
477 | struct TypeBoundList { bounds: [TypeBound] } | 537 | struct TypeBoundList { bounds: [TypeBound] } |
478 | struct WherePred: TypeBoundsOwner { TypeRef } | 538 | struct WherePred: TypeBoundsOwner { Lifetime, TypeRef } |
479 | struct WhereClause { predicates: [WherePred] } | 539 | struct WhereClause { WhereKw, predicates: [WherePred] } |
480 | struct ExprStmt { Expr } | 540 | struct Abi { String } |
481 | struct LetStmt: TypeAscriptionOwner { | 541 | struct ExprStmt: AttrsOwner { Expr, Semi } |
542 | struct LetStmt: AttrsOwner, TypeAscriptionOwner { | ||
543 | LetKw, | ||
482 | Pat, | 544 | Pat, |
545 | Eq, | ||
483 | initializer: Expr, | 546 | initializer: Expr, |
484 | } | 547 | } |
485 | struct Condition { Pat, Expr } | 548 | struct Condition { LetKw, Pat, Eq, Expr } |
486 | struct Block: AttrsOwner, ModuleItemOwner { | 549 | struct Block: AttrsOwner, ModuleItemOwner { |
550 | LCurly, | ||
487 | statements: [Stmt], | 551 | statements: [Stmt], |
488 | Expr, | 552 | Expr, |
553 | RCurly, | ||
489 | } | 554 | } |
490 | struct ParamList { | 555 | struct ParamList { |
556 | LParen, | ||
491 | SelfParam, | 557 | SelfParam, |
492 | params: [Param], | 558 | params: [Param], |
559 | RParen | ||
493 | } | 560 | } |
494 | struct SelfParam: TypeAscriptionOwner, AttrsOwner { } | 561 | struct SelfParam: TypeAscriptionOwner, AttrsOwner { Amp, Lifetime, SelfKw } |
495 | struct Param: TypeAscriptionOwner, AttrsOwner { | 562 | struct Param: TypeAscriptionOwner, AttrsOwner { |
496 | Pat, | 563 | Pat, |
564 | Dotdotdot | ||
497 | } | 565 | } |
498 | struct UseItem: AttrsOwner, VisibilityOwner { | 566 | struct UseItem: AttrsOwner, VisibilityOwner { |
567 | UseKw, | ||
499 | UseTree, | 568 | UseTree, |
500 | } | 569 | } |
501 | struct UseTree { | 570 | struct UseTree { |
502 | Path, UseTreeList, Alias | 571 | Path, Star, UseTreeList, Alias |
503 | } | 572 | } |
504 | struct Alias: NameOwner { } | 573 | struct Alias: NameOwner { AsKw } |
505 | struct UseTreeList { use_trees: [UseTree] } | 574 | struct UseTreeList { LCurly, use_trees: [UseTree], RCurly } |
506 | struct ExternCrateItem: AttrsOwner, VisibilityOwner { | 575 | struct ExternCrateItem: AttrsOwner, VisibilityOwner { |
507 | NameRef, Alias, | 576 | ExternKw, CrateKw, NameRef, Alias, |
508 | } | 577 | } |
509 | struct ArgList { | 578 | struct ArgList { |
579 | LParen, | ||
510 | args: [Expr], | 580 | args: [Expr], |
581 | RParen | ||
511 | } | 582 | } |
512 | struct Path { | 583 | struct Path { |
513 | segment: PathSegment, | 584 | segment: PathSegment, |
514 | qualifier: Path, | 585 | qualifier: Path, |
515 | } | 586 | } |
516 | struct PathSegment { | 587 | struct PathSegment { |
517 | NameRef, TypeArgList, ParamList, RetType, PathType, | 588 | Coloncolon, LAngle, NameRef, TypeArgList, ParamList, RetType, PathType, RAngle |
518 | } | 589 | } |
519 | struct TypeArgList { | 590 | struct TypeArgList { |
591 | Coloncolon, | ||
592 | LAngle, | ||
593 | generic_args: [GenericArg], | ||
520 | type_args: [TypeArg], | 594 | type_args: [TypeArg], |
521 | lifetime_args: [LifetimeArg], | 595 | lifetime_args: [LifetimeArg], |
522 | assoc_type_args: [AssocTypeArg], | 596 | assoc_type_args: [AssocTypeArg], |
523 | const_arg: [ConstArg], | 597 | const_args: [ConstArg], |
598 | RAngle | ||
524 | } | 599 | } |
525 | struct TypeArg { TypeRef } | 600 | struct TypeArg { TypeRef } |
526 | struct AssocTypeArg { NameRef, TypeRef } | 601 | struct AssocTypeArg : TypeBoundsOwner { NameRef, Eq, TypeRef } |
527 | struct LifetimeArg {} | 602 | struct LifetimeArg { Lifetime } |
528 | struct ConstArg { Literal, BlockExpr } | 603 | struct ConstArg { Literal, Eq, BlockExpr } |
529 | 604 | ||
530 | struct MacroItems: ModuleItemOwner, FnDefOwner { } | 605 | struct MacroItems: ModuleItemOwner, FnDefOwner { } |
531 | 606 | ||
@@ -533,12 +608,44 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { | |||
533 | statements: [Stmt], | 608 | statements: [Stmt], |
534 | Expr, | 609 | Expr, |
535 | } | 610 | } |
611 | |||
612 | struct ExternItemList: FnDefOwner, ModuleItemOwner { | ||
613 | LCurly, | ||
614 | extern_items: [ExternItem], | ||
615 | RCurly | ||
616 | } | ||
617 | |||
618 | struct ExternBlock { | ||
619 | Abi, | ||
620 | ExternItemList | ||
621 | } | ||
622 | |||
623 | struct MetaItem { | ||
624 | Path, Eq, AttrInput, nested_meta_items: [MetaItem] | ||
625 | } | ||
626 | |||
627 | struct MacroDef { | ||
628 | Name, TokenTree | ||
629 | } | ||
536 | }, | 630 | }, |
537 | enums: &ast_enums! { | 631 | enums: &ast_enums! { |
538 | enum NominalDef: NameOwner, TypeParamsOwner, AttrsOwner { | 632 | enum NominalDef: NameOwner, TypeParamsOwner, AttrsOwner { |
539 | StructDef, EnumDef, UnionDef, | 633 | StructDef, EnumDef, UnionDef, |
540 | } | 634 | } |
541 | 635 | ||
636 | enum GenericParam { | ||
637 | LifetimeParam, | ||
638 | TypeParam, | ||
639 | ConstParam | ||
640 | } | ||
641 | |||
642 | enum GenericArg { | ||
643 | LifetimeArg, | ||
644 | TypeArg, | ||
645 | ConstArg, | ||
646 | AssocTypeArg | ||
647 | } | ||
648 | |||
542 | enum TypeRef { | 649 | enum TypeRef { |
543 | ParenType, | 650 | ParenType, |
544 | TupleType, | 651 | TupleType, |
@@ -555,7 +662,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { | |||
555 | DynTraitType, | 662 | DynTraitType, |
556 | } | 663 | } |
557 | 664 | ||
558 | enum ModuleItem: AttrsOwner, VisibilityOwner { | 665 | enum ModuleItem: NameOwner, AttrsOwner, VisibilityOwner { |
559 | StructDef, | 666 | StructDef, |
560 | UnionDef, | 667 | UnionDef, |
561 | EnumDef, | 668 | EnumDef, |
@@ -569,13 +676,20 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { | |||
569 | StaticDef, | 676 | StaticDef, |
570 | Module, | 677 | Module, |
571 | MacroCall, | 678 | MacroCall, |
679 | ExternBlock | ||
680 | } | ||
681 | |||
682 | /* impl blocks can also contain MacroCall */ | ||
683 | enum ImplItem: NameOwner, AttrsOwner { | ||
684 | FnDef, TypeAliasDef, ConstDef | ||
572 | } | 685 | } |
573 | 686 | ||
574 | enum ImplItem: AttrsOwner { | 687 | /* extern blocks can also contain MacroCall */ |
575 | FnDef, TypeAliasDef, ConstDef, | 688 | enum ExternItem: NameOwner, AttrsOwner, VisibilityOwner { |
689 | FnDef, StaticDef | ||
576 | } | 690 | } |
577 | 691 | ||
578 | enum Expr { | 692 | enum Expr: AttrsOwner { |
579 | TupleExpr, | 693 | TupleExpr, |
580 | ArrayExpr, | 694 | ArrayExpr, |
581 | ParenExpr, | 695 | ParenExpr, |
@@ -627,7 +741,86 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { | |||
627 | MacroPat, | 741 | MacroPat, |
628 | } | 742 | } |
629 | 743 | ||
744 | enum RecordInnerPat { | ||
745 | RecordFieldPat, | ||
746 | BindPat | ||
747 | } | ||
748 | |||
630 | enum AttrInput { Literal, TokenTree } | 749 | enum AttrInput { Literal, TokenTree } |
631 | enum Stmt { ExprStmt, LetStmt } | 750 | enum Stmt { |
751 | LetStmt, | ||
752 | ExprStmt, | ||
753 | // macro calls are parsed as expression statements */ | ||
754 | } | ||
755 | |||
756 | enum LeftDelimiter { LParen, LBrack, LCurly } | ||
757 | enum RightDelimiter { RParen, RBrack, RCurly } | ||
758 | enum RangeSeparator { Dotdot, Dotdotdot, Dotdoteq} | ||
759 | |||
760 | enum BinOp { | ||
761 | Pipepipe, | ||
762 | Ampamp, | ||
763 | Eqeq, | ||
764 | Neq, | ||
765 | Lteq, | ||
766 | Gteq, | ||
767 | LAngle, | ||
768 | RAngle, | ||
769 | Plus, | ||
770 | Star, | ||
771 | Minus, | ||
772 | Slash, | ||
773 | Percent, | ||
774 | Shl, | ||
775 | Shr, | ||
776 | Caret, | ||
777 | Pipe, | ||
778 | Amp, | ||
779 | Eq, | ||
780 | Pluseq, | ||
781 | Slasheq, | ||
782 | Stareq, | ||
783 | Percenteq, | ||
784 | Shreq, | ||
785 | Shleq, | ||
786 | Minuseq, | ||
787 | Pipeeq, | ||
788 | Ampeq, | ||
789 | Careteq, | ||
790 | } | ||
791 | |||
792 | enum PrefixOp { | ||
793 | Minus, | ||
794 | Excl, | ||
795 | Star | ||
796 | } | ||
797 | |||
798 | enum RangeOp { | ||
799 | Dotdot, | ||
800 | Dotdoteq | ||
801 | } | ||
802 | |||
803 | enum LiteralToken { | ||
804 | IntNumber, | ||
805 | FloatNumber, | ||
806 | String, | ||
807 | RawString, | ||
808 | TrueKw, | ||
809 | FalseKw, | ||
810 | ByteString, | ||
811 | RawByteString, | ||
812 | Char, | ||
813 | Byte | ||
814 | } | ||
815 | |||
816 | enum NameRefToken { | ||
817 | Ident, | ||
818 | IntNumber | ||
819 | } | ||
820 | |||
821 | enum FieldDefList { | ||
822 | RecordFieldDefList, | ||
823 | TupleFieldDefList, | ||
824 | } | ||
632 | }, | 825 | }, |
633 | }; | 826 | }; |
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 2dfb68371..6dae93aa2 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs | |||
@@ -146,14 +146,23 @@ fn generate_ast(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> { | |||
146 | FieldSrc::Many(_) => { | 146 | FieldSrc::Many(_) => { |
147 | quote! { | 147 | quote! { |
148 | pub fn #method_name(&self) -> AstChildren<#ty> { | 148 | pub fn #method_name(&self) -> AstChildren<#ty> { |
149 | AstChildren::new(&self.syntax) | 149 | support::children(&self.syntax) |
150 | } | 150 | } |
151 | } | 151 | } |
152 | } | 152 | } |
153 | FieldSrc::Optional(_) | FieldSrc::Shorthand => { | 153 | FieldSrc::Optional(_) | FieldSrc::Shorthand => { |
154 | quote! { | 154 | let is_token = element_kinds_map[&ty.to_string()].has_tokens; |
155 | pub fn #method_name(&self) -> Option<#ty> { | 155 | if is_token { |
156 | AstChildren::new(&self.syntax).next() | 156 | quote! { |
157 | pub fn #method_name(&self) -> Option<#ty> { | ||
158 | support::token(&self.syntax) | ||
159 | } | ||
160 | } | ||
161 | } else { | ||
162 | quote! { | ||
163 | pub fn #method_name(&self) -> Option<#ty> { | ||
164 | support::child(&self.syntax) | ||
165 | } | ||
157 | } | 166 | } |
158 | } | 167 | } |
159 | } | 168 | } |
@@ -205,6 +214,48 @@ fn generate_ast(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> { | |||
205 | quote!(impl ast::#trait_name for #name {}) | 214 | quote!(impl ast::#trait_name for #name {}) |
206 | }); | 215 | }); |
207 | 216 | ||
217 | let element_kinds = &element_kinds_map[&en.name.to_string()]; | ||
218 | assert!( | ||
219 | element_kinds.has_nodes ^ element_kinds.has_tokens, | ||
220 | "{}: {:#?}", | ||
221 | name, | ||
222 | element_kinds | ||
223 | ); | ||
224 | let specific_ast_trait = { | ||
225 | let (ast_trait, syntax_type) = if element_kinds.has_tokens { | ||
226 | (quote!(AstToken), quote!(SyntaxToken)) | ||
227 | } else { | ||
228 | (quote!(AstNode), quote!(SyntaxNode)) | ||
229 | }; | ||
230 | |||
231 | quote! { | ||
232 | impl #ast_trait for #name { | ||
233 | fn can_cast(kind: SyntaxKind) -> bool { | ||
234 | match kind { | ||
235 | #(#kinds)|* => true, | ||
236 | _ => false, | ||
237 | } | ||
238 | } | ||
239 | fn cast(syntax: #syntax_type) -> Option<Self> { | ||
240 | let res = match syntax.kind() { | ||
241 | #( | ||
242 | #kinds => #name::#variants(#variants { syntax }), | ||
243 | )* | ||
244 | _ => return None, | ||
245 | }; | ||
246 | Some(res) | ||
247 | } | ||
248 | fn syntax(&self) -> &#syntax_type { | ||
249 | match self { | ||
250 | #( | ||
251 | #name::#variants(it) => &it.syntax, | ||
252 | )* | ||
253 | } | ||
254 | } | ||
255 | } | ||
256 | } | ||
257 | }; | ||
258 | |||
208 | quote! { | 259 | quote! { |
209 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 260 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
210 | pub enum #name { | 261 | pub enum #name { |
@@ -225,30 +276,8 @@ fn generate_ast(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> { | |||
225 | } | 276 | } |
226 | } | 277 | } |
227 | 278 | ||
228 | impl AstNode for #name { | 279 | #specific_ast_trait |
229 | fn can_cast(kind: SyntaxKind) -> bool { | 280 | |
230 | match kind { | ||
231 | #(#kinds)|* => true, | ||
232 | _ => false, | ||
233 | } | ||
234 | } | ||
235 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
236 | let res = match syntax.kind() { | ||
237 | #( | ||
238 | #kinds => #name::#variants(#variants { syntax }), | ||
239 | )* | ||
240 | _ => return None, | ||
241 | }; | ||
242 | Some(res) | ||
243 | } | ||
244 | fn syntax(&self) -> &SyntaxNode { | ||
245 | match self { | ||
246 | #( | ||
247 | #name::#variants(it) => &it.syntax, | ||
248 | )* | ||
249 | } | ||
250 | } | ||
251 | } | ||
252 | #(#traits)* | 281 | #(#traits)* |
253 | } | 282 | } |
254 | }); | 283 | }); |
@@ -268,7 +297,7 @@ fn generate_ast(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> { | |||
268 | #[allow(unused_imports)] | 297 | #[allow(unused_imports)] |
269 | use crate::{ | 298 | use crate::{ |
270 | SyntaxNode, SyntaxToken, SyntaxElement, NodeOrToken, SyntaxKind::{self, *}, | 299 | SyntaxNode, SyntaxToken, SyntaxElement, NodeOrToken, SyntaxKind::{self, *}, |
271 | ast::{self, AstNode, AstToken, AstChildren}, | 300 | ast::{self, AstNode, AstToken, AstChildren, support}, |
272 | }; | 301 | }; |
273 | 302 | ||
274 | #(#tokens)* | 303 | #(#tokens)* |