diff options
Diffstat (limited to 'xtask/src/codegen')
-rw-r--r-- | xtask/src/codegen/gen_syntax.rs | 249 | ||||
-rw-r--r-- | xtask/src/codegen/rust.ungram | 529 |
2 files changed, 762 insertions, 16 deletions
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 745a25862..24e8be1fb 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs | |||
@@ -3,34 +3,43 @@ | |||
3 | //! Specifically, it generates the `SyntaxKind` enum and a number of newtype | 3 | //! Specifically, it generates the `SyntaxKind` enum and a number of newtype |
4 | //! wrappers around `SyntaxNode` which implement `ra_syntax::AstNode`. | 4 | //! wrappers around `SyntaxNode` which implement `ra_syntax::AstNode`. |
5 | 5 | ||
6 | use std::{collections::HashSet, fmt::Write}; | 6 | use std::{ |
7 | collections::{BTreeSet, HashSet}, | ||
8 | fmt::Write, | ||
9 | }; | ||
7 | 10 | ||
8 | use proc_macro2::{Punct, Spacing}; | 11 | use proc_macro2::{Punct, Spacing}; |
9 | use quote::{format_ident, quote}; | 12 | use quote::{format_ident, quote}; |
13 | use ungrammar::{Grammar, Rule}; | ||
10 | 14 | ||
11 | use crate::{ | 15 | use crate::{ |
12 | ast_src::{AstSrc, Field, FieldSrc, KindsSrc, AST_SRC, KINDS_SRC}, | 16 | ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Field, FieldSrc, KindsSrc, KINDS_SRC}, |
13 | codegen::{self, update, Mode}, | 17 | codegen::{self, update, Mode}, |
14 | project_root, Result, | 18 | project_root, Result, |
15 | }; | 19 | }; |
16 | 20 | ||
17 | pub fn generate_syntax(mode: Mode) -> Result<()> { | 21 | pub fn generate_syntax(mode: Mode) -> Result<()> { |
22 | let grammar = include_str!("rust.ungram") | ||
23 | .parse::<Grammar>() | ||
24 | .unwrap_or_else(|err| panic!("\n \x1b[91merror\x1b[0m: {}\n", err)); | ||
25 | let ast = lower(&grammar); | ||
26 | |||
18 | let syntax_kinds_file = project_root().join(codegen::SYNTAX_KINDS); | 27 | let syntax_kinds_file = project_root().join(codegen::SYNTAX_KINDS); |
19 | let syntax_kinds = generate_syntax_kinds(KINDS_SRC)?; | 28 | let syntax_kinds = generate_syntax_kinds(KINDS_SRC)?; |
20 | update(syntax_kinds_file.as_path(), &syntax_kinds, mode)?; | 29 | update(syntax_kinds_file.as_path(), &syntax_kinds, mode)?; |
21 | 30 | ||
22 | let ast_tokens_file = project_root().join(codegen::AST_TOKENS); | 31 | let ast_tokens_file = project_root().join(codegen::AST_TOKENS); |
23 | let contents = generate_tokens(AST_SRC)?; | 32 | let contents = generate_tokens(&ast)?; |
24 | update(ast_tokens_file.as_path(), &contents, mode)?; | 33 | update(ast_tokens_file.as_path(), &contents, mode)?; |
25 | 34 | ||
26 | let ast_nodes_file = project_root().join(codegen::AST_NODES); | 35 | let ast_nodes_file = project_root().join(codegen::AST_NODES); |
27 | let contents = generate_nodes(KINDS_SRC, AST_SRC)?; | 36 | let contents = generate_nodes(KINDS_SRC, &ast)?; |
28 | update(ast_nodes_file.as_path(), &contents, mode)?; | 37 | update(ast_nodes_file.as_path(), &contents, mode)?; |
29 | 38 | ||
30 | Ok(()) | 39 | Ok(()) |
31 | } | 40 | } |
32 | 41 | ||
33 | fn generate_tokens(grammar: AstSrc<'_>) -> Result<String> { | 42 | fn generate_tokens(grammar: &AstSrc) -> Result<String> { |
34 | let tokens = grammar.tokens.iter().map(|token| { | 43 | let tokens = grammar.tokens.iter().map(|token| { |
35 | let name = format_ident!("{}", token); | 44 | let name = format_ident!("{}", token); |
36 | let kind = format_ident!("{}", to_upper_snake_case(token)); | 45 | let kind = format_ident!("{}", to_upper_snake_case(token)); |
@@ -62,13 +71,13 @@ fn generate_tokens(grammar: AstSrc<'_>) -> Result<String> { | |||
62 | Ok(pretty) | 71 | Ok(pretty) |
63 | } | 72 | } |
64 | 73 | ||
65 | fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> { | 74 | fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result<String> { |
66 | let (node_defs, node_boilerplate_impls): (Vec<_>, Vec<_>) = grammar | 75 | let (node_defs, node_boilerplate_impls): (Vec<_>, Vec<_>) = grammar |
67 | .nodes | 76 | .nodes |
68 | .iter() | 77 | .iter() |
69 | .map(|node| { | 78 | .map(|node| { |
70 | let name = format_ident!("{}", node.name); | 79 | let name = format_ident!("{}", node.name); |
71 | let kind = format_ident!("{}", to_upper_snake_case(node.name)); | 80 | let kind = format_ident!("{}", to_upper_snake_case(&node.name)); |
72 | let traits = node.traits.iter().map(|trait_name| { | 81 | let traits = node.traits.iter().map(|trait_name| { |
73 | let trait_name = format_ident!("{}", trait_name); | 82 | let trait_name = format_ident!("{}", trait_name); |
74 | quote!(impl ast::#trait_name for #name {}) | 83 | quote!(impl ast::#trait_name for #name {}) |
@@ -192,8 +201,8 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> { | |||
192 | }) | 201 | }) |
193 | .unzip(); | 202 | .unzip(); |
194 | 203 | ||
195 | let enum_names = grammar.enums.iter().map(|it| it.name); | 204 | let enum_names = grammar.enums.iter().map(|it| &it.name); |
196 | let node_names = grammar.nodes.iter().map(|it| it.name); | 205 | let node_names = grammar.nodes.iter().map(|it| &it.name); |
197 | 206 | ||
198 | let display_impls = | 207 | let display_impls = |
199 | enum_names.chain(node_names.clone()).map(|it| format_ident!("{}", it)).map(|name| { | 208 | enum_names.chain(node_names.clone()).map(|it| format_ident!("{}", it)).map(|name| { |
@@ -212,9 +221,11 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> { | |||
212 | .nodes | 221 | .nodes |
213 | .iter() | 222 | .iter() |
214 | .map(|kind| to_pascal_case(kind)) | 223 | .map(|kind| to_pascal_case(kind)) |
215 | .filter(|name| !defined_nodes.contains(name.as_str())) | 224 | .filter(|name| !defined_nodes.iter().any(|&it| it == name)) |
216 | { | 225 | { |
217 | eprintln!("Warning: node {} not defined in ast source", node); | 226 | drop(node) |
227 | // TODO: restore this | ||
228 | // eprintln!("Warning: node {} not defined in ast source", node); | ||
218 | } | 229 | } |
219 | 230 | ||
220 | let ast = quote! { | 231 | let ast = quote! { |
@@ -236,12 +247,12 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> { | |||
236 | let mut res = String::with_capacity(ast.len() * 2); | 247 | let mut res = String::with_capacity(ast.len() * 2); |
237 | 248 | ||
238 | let mut docs = | 249 | let mut docs = |
239 | grammar.nodes.iter().map(|it| it.doc).chain(grammar.enums.iter().map(|it| it.doc)); | 250 | grammar.nodes.iter().map(|it| &it.doc).chain(grammar.enums.iter().map(|it| &it.doc)); |
240 | 251 | ||
241 | for chunk in ast.split("# [ pretty_doc_comment_placeholder_workaround ]") { | 252 | for chunk in ast.split("# [ pretty_doc_comment_placeholder_workaround ]") { |
242 | res.push_str(chunk); | 253 | res.push_str(chunk); |
243 | if let Some(doc) = docs.next() { | 254 | if let Some(doc) = docs.next() { |
244 | write_doc_comment(doc, &mut res); | 255 | write_doc_comment(&doc, &mut res); |
245 | } | 256 | } |
246 | } | 257 | } |
247 | 258 | ||
@@ -249,7 +260,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> { | |||
249 | Ok(pretty) | 260 | Ok(pretty) |
250 | } | 261 | } |
251 | 262 | ||
252 | fn write_doc_comment(contents: &[&str], dest: &mut String) { | 263 | fn write_doc_comment(contents: &[String], dest: &mut String) { |
253 | for line in contents { | 264 | for line in contents { |
254 | writeln!(dest, "///{}", line).unwrap(); | 265 | writeln!(dest, "///{}", line).unwrap(); |
255 | } | 266 | } |
@@ -413,7 +424,11 @@ fn to_pascal_case(s: &str) -> String { | |||
413 | buf | 424 | buf |
414 | } | 425 | } |
415 | 426 | ||
416 | impl Field<'_> { | 427 | fn pluralize(s: &str) -> String { |
428 | format!("{}s", s) | ||
429 | } | ||
430 | |||
431 | impl Field { | ||
417 | fn is_many(&self) -> bool { | 432 | fn is_many(&self) -> bool { |
418 | matches!(self, Field::Node { src: FieldSrc::Many(_), .. }) | 433 | matches!(self, Field::Node { src: FieldSrc::Many(_), .. }) |
419 | } | 434 | } |
@@ -429,7 +444,7 @@ impl Field<'_> { | |||
429 | fn method_name(&self) -> proc_macro2::Ident { | 444 | fn method_name(&self) -> proc_macro2::Ident { |
430 | match self { | 445 | match self { |
431 | Field::Token(name) => { | 446 | Field::Token(name) => { |
432 | let name = match *name { | 447 | let name = match name.as_str() { |
433 | ";" => "semicolon", | 448 | ";" => "semicolon", |
434 | "->" => "thin_arrow", | 449 | "->" => "thin_arrow", |
435 | "'{'" => "l_curly", | 450 | "'{'" => "l_curly", |
@@ -448,6 +463,7 @@ impl Field<'_> { | |||
448 | "." => "dot", | 463 | "." => "dot", |
449 | ".." => "dotdot", | 464 | ".." => "dotdot", |
450 | "..." => "dotdotdot", | 465 | "..." => "dotdotdot", |
466 | "..=" => "dotdoteq", | ||
451 | "=>" => "fat_arrow", | 467 | "=>" => "fat_arrow", |
452 | "@" => "at", | 468 | "@" => "at", |
453 | ":" => "colon", | 469 | ":" => "colon", |
@@ -474,3 +490,204 @@ impl Field<'_> { | |||
474 | } | 490 | } |
475 | } | 491 | } |
476 | } | 492 | } |
493 | |||
494 | fn lower(grammar: &Grammar) -> AstSrc { | ||
495 | let mut res = AstSrc::default(); | ||
496 | res.tokens = vec!["Whitespace".into(), "Comment".into(), "String".into(), "RawString".into()]; | ||
497 | |||
498 | let nodes = grammar | ||
499 | .iter() | ||
500 | .filter(|&node| match grammar[node].rule { | ||
501 | Rule::Node(it) if it == node => false, | ||
502 | _ => true, | ||
503 | }) | ||
504 | .collect::<Vec<_>>(); | ||
505 | |||
506 | for &node in &nodes { | ||
507 | let name = grammar[node].name.clone(); | ||
508 | let rule = &grammar[node].rule; | ||
509 | match lower_enum(grammar, rule) { | ||
510 | Some(variants) => { | ||
511 | let enum_src = AstEnumSrc { doc: Vec::new(), name, traits: Vec::new(), variants }; | ||
512 | res.enums.push(enum_src); | ||
513 | } | ||
514 | None => { | ||
515 | let mut fields = Vec::new(); | ||
516 | lower_rule(&mut fields, grammar, rule); | ||
517 | res.nodes.push(AstNodeSrc { doc: Vec::new(), name, traits: Vec::new(), fields }); | ||
518 | } | ||
519 | } | ||
520 | } | ||
521 | |||
522 | deduplicate_fields(&mut res); | ||
523 | extract_enums(&mut res); | ||
524 | extract_struct_traits(&mut res); | ||
525 | extract_enum_traits(&mut res); | ||
526 | res | ||
527 | } | ||
528 | |||
529 | fn lower_enum(grammar: &Grammar, rule: &Rule) -> Option<Vec<String>> { | ||
530 | let alternatives = match rule { | ||
531 | Rule::Alt(it) => it, | ||
532 | _ => return None, | ||
533 | }; | ||
534 | let mut variants = Vec::new(); | ||
535 | for alternative in alternatives { | ||
536 | match alternative { | ||
537 | Rule::Node(it) => variants.push(grammar[*it].name.clone()), | ||
538 | _ => return None, | ||
539 | } | ||
540 | } | ||
541 | Some(variants) | ||
542 | } | ||
543 | |||
544 | fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) { | ||
545 | match rule { | ||
546 | Rule::Node(node) => { | ||
547 | let field = Field::Node { name: grammar[*node].name.clone(), src: FieldSrc::Shorthand }; | ||
548 | acc.push(field); | ||
549 | } | ||
550 | Rule::Token(token) => { | ||
551 | let mut name = grammar[*token].name.clone(); | ||
552 | if name != "int_number" && name != "string" { | ||
553 | if "[]{}()".contains(&name) { | ||
554 | name = format!("'{}'", name); | ||
555 | } | ||
556 | let field = Field::Token(name); | ||
557 | acc.push(field); | ||
558 | } | ||
559 | } | ||
560 | Rule::Rep(inner) => { | ||
561 | if let Rule::Node(node) = &**inner { | ||
562 | let name = grammar[*node].name.clone(); | ||
563 | let label = pluralize(&to_lower_snake_case(&name)); | ||
564 | let field = Field::Node { name: label.clone(), src: FieldSrc::Many(name) }; | ||
565 | acc.push(field); | ||
566 | return; | ||
567 | } | ||
568 | todo!("{:?}", rule) | ||
569 | } | ||
570 | Rule::Labeled { label, rule } => { | ||
571 | let node = match &**rule { | ||
572 | Rule::Rep(inner) | Rule::Opt(inner) => match &**inner { | ||
573 | Rule::Node(node) => node, | ||
574 | _ => todo!("{:?}", rule), | ||
575 | }, | ||
576 | Rule::Node(node) => node, | ||
577 | _ => todo!("{:?}", rule), | ||
578 | }; | ||
579 | let field = Field::Node { | ||
580 | name: label.clone(), | ||
581 | src: match &**rule { | ||
582 | Rule::Rep(_) => FieldSrc::Many(grammar[*node].name.clone()), | ||
583 | _ => FieldSrc::Optional(grammar[*node].name.clone()), | ||
584 | }, | ||
585 | }; | ||
586 | acc.push(field); | ||
587 | } | ||
588 | Rule::Seq(rules) | Rule::Alt(rules) => { | ||
589 | for rule in rules { | ||
590 | lower_rule(acc, grammar, rule) | ||
591 | } | ||
592 | } | ||
593 | Rule::Opt(rule) => lower_rule(acc, grammar, rule), | ||
594 | } | ||
595 | } | ||
596 | |||
597 | fn deduplicate_fields(ast: &mut AstSrc) { | ||
598 | eprintln!(); | ||
599 | for node in &mut ast.nodes { | ||
600 | let mut i = 0; | ||
601 | 'outer: while i < node.fields.len() { | ||
602 | for j in 0..i { | ||
603 | let f1 = &node.fields[i]; | ||
604 | let f2 = &node.fields[j]; | ||
605 | if f1 == f2 { | ||
606 | node.fields.remove(i); | ||
607 | continue 'outer; | ||
608 | } | ||
609 | } | ||
610 | i += 1; | ||
611 | } | ||
612 | } | ||
613 | } | ||
614 | |||
615 | fn extract_enums(ast: &mut AstSrc) { | ||
616 | for node in &mut ast.nodes { | ||
617 | for enm in &ast.enums { | ||
618 | let mut to_remove = Vec::new(); | ||
619 | for (i, field) in node.fields.iter().enumerate() { | ||
620 | let ty = field.ty().to_string(); | ||
621 | if enm.variants.iter().any(|it| it == &ty) { | ||
622 | to_remove.push(i); | ||
623 | } | ||
624 | } | ||
625 | if to_remove.len() == enm.variants.len() { | ||
626 | node.remove_field(to_remove); | ||
627 | node.fields.push(Field::Node { name: enm.name.clone(), src: FieldSrc::Shorthand }); | ||
628 | } | ||
629 | } | ||
630 | } | ||
631 | } | ||
632 | |||
633 | fn extract_struct_traits(ast: &mut AstSrc) { | ||
634 | let traits: &[(&str, &[&str])] = &[ | ||
635 | ("AttrsOwner", &["attrs"]), | ||
636 | ("NameOwner", &["name"]), | ||
637 | ("VisibilityOwner", &["visibility"]), | ||
638 | ("TypeParamsOwner", &["type_param_list", "where_clause"]), | ||
639 | ("TypeBoundsOwner", &["type_bound_list", "colon_token"]), | ||
640 | ("ModuleItemOwner", &["items"]), | ||
641 | ("TypeAscriptionOwner", &["ascribed_type"]), | ||
642 | ("LoopBodyOwner", &["label", "loop_body"]), | ||
643 | ("ArgListOwner", &["arg_list"]), | ||
644 | ]; | ||
645 | |||
646 | for node in &mut ast.nodes { | ||
647 | for (name, methods) in traits { | ||
648 | extract_struct_trait(node, name, methods); | ||
649 | } | ||
650 | } | ||
651 | } | ||
652 | |||
653 | fn extract_struct_trait(node: &mut AstNodeSrc, trait_name: &str, methods: &[&str]) { | ||
654 | let mut to_remove = Vec::new(); | ||
655 | for (i, field) in node.fields.iter().enumerate() { | ||
656 | let method_name = field.method_name().to_string(); | ||
657 | if methods.iter().any(|&it| it == &method_name) { | ||
658 | to_remove.push(i); | ||
659 | } | ||
660 | } | ||
661 | if to_remove.len() == methods.len() { | ||
662 | node.traits.push(trait_name.to_string()); | ||
663 | node.remove_field(to_remove); | ||
664 | } | ||
665 | } | ||
666 | |||
667 | fn extract_enum_traits(ast: &mut AstSrc) { | ||
668 | for enm in &mut ast.enums { | ||
669 | let nodes = &ast.nodes; | ||
670 | let mut variant_traits = enm | ||
671 | .variants | ||
672 | .iter() | ||
673 | .map(|var| nodes.iter().find(|it| &it.name == var).unwrap()) | ||
674 | .map(|node| node.traits.iter().cloned().collect::<BTreeSet<_>>()); | ||
675 | |||
676 | let mut enum_traits = match variant_traits.next() { | ||
677 | Some(it) => it, | ||
678 | None => continue, | ||
679 | }; | ||
680 | for traits in variant_traits { | ||
681 | enum_traits = enum_traits.intersection(&traits).cloned().collect(); | ||
682 | } | ||
683 | enm.traits = enum_traits.into_iter().collect(); | ||
684 | } | ||
685 | } | ||
686 | |||
687 | impl AstNodeSrc { | ||
688 | fn remove_field(&mut self, to_remove: Vec<usize>) { | ||
689 | to_remove.into_iter().rev().for_each(|idx| { | ||
690 | self.fields.remove(idx); | ||
691 | }); | ||
692 | } | ||
693 | } | ||
diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram new file mode 100644 index 000000000..b6ec5d5e7 --- /dev/null +++ b/xtask/src/codegen/rust.ungram | |||
@@ -0,0 +1,529 @@ | |||
1 | SourceFile = | ||
2 | Attr* | ||
3 | items:ModuleItem* | ||
4 | |||
5 | FnDef = | ||
6 | Attr* Visibility? Abi? 'const' 'default' 'async' 'unsafe' 'fn' Name TypeParamList? | ||
7 | ParamList RetType? | ||
8 | WhereClause? | ||
9 | (body:BlockExpr | ';') | ||
10 | |||
11 | RetType = | ||
12 | '->' TypeRef | ||
13 | |||
14 | StructDef = | ||
15 | Attr* Visibility? 'struct' Name TypeParamList? ( | ||
16 | WhereClause? (RecordFieldDefList | ';') | ||
17 | | TupleFieldDefList WhereClause? ';' | ||
18 | ) | ||
19 | |||
20 | UnionDef = | ||
21 | Attr* Visibility? 'union' Name TypeParamList? WhereClause? | ||
22 | RecordFieldDefList | ||
23 | |||
24 | RecordFieldDefList = | ||
25 | '{' fields:RecordFieldDef* '}' | ||
26 | |||
27 | RecordFieldDef = | ||
28 | Attr* Visibility? Name ':' ascribed_type:TypeRef | ||
29 | |||
30 | TupleFieldDefList = | ||
31 | '(' fields:TupleFieldDef* ')' | ||
32 | |||
33 | TupleFieldDef = | ||
34 | Attr* Visibility? Name TypeRef | ||
35 | |||
36 | FieldDefList = | ||
37 | RecordFieldDefList | ||
38 | | TupleFieldDefList | ||
39 | |||
40 | EnumDef = | ||
41 | Attr* Visibility? 'enum' Name TypeParamList? WhereClause? | ||
42 | variant_list:EnumVariantList | ||
43 | |||
44 | EnumVariantList = | ||
45 | '{' variants:EnumVariant* '}' | ||
46 | |||
47 | EnumVariant = | ||
48 | Attr* Visibility? Name FieldDefList ('=' Expr)? | ||
49 | |||
50 | TraitDef = | ||
51 | Attr* Visibility? 'unsafe'? 'auto'? 'trait' Name TypeParamList | ||
52 | (':' TypeBoundList?)? WhereClause | ||
53 | ItemList | ||
54 | |||
55 | Module = | ||
56 | Attr* Visibility? 'mod' Name | ||
57 | (ItemList | ';') | ||
58 | |||
59 | ItemList = | ||
60 | '{' | ||
61 | AssocItem* | ||
62 | items:ModuleItem* | ||
63 | '}' | ||
64 | |||
65 | ConstDef = | ||
66 | Attr* Visibility? 'default'? 'const' Name ':' ascribed_type:TypeRef | ||
67 | '=' body:Expr ';' | ||
68 | |||
69 | StaticDef = | ||
70 | Attr* Visibility? 'static'? 'mut'? 'static' Name ':' ascribed_type:TypeRef | ||
71 | '=' body:Expr ';' | ||
72 | |||
73 | TypeAliasDef = | ||
74 | Attr* Visibility? 'default'? 'type' Name TypeParamList? WhereClause? (':' TypeBoundList?)? | ||
75 | '=' TypeRef ';' | ||
76 | |||
77 | ImplDef = | ||
78 | Attr* Visibility? 'const'? 'default'? 'unsafe'? 'impl' TypeParamList? '!'? 'for' | ||
79 | WhereClause? | ||
80 | ItemList | ||
81 | |||
82 | ParenType = | ||
83 | '(' TypeRef ')' | ||
84 | |||
85 | TupleType = | ||
86 | '(' fields:TypeRef* ')' | ||
87 | |||
88 | NeverType = | ||
89 | '!' | ||
90 | |||
91 | PathType = | ||
92 | Path | ||
93 | |||
94 | PointerType = | ||
95 | '*' ('const' | 'mut') TypeRef | ||
96 | |||
97 | ArrayType = | ||
98 | '[' TypeRef ';' Expr ']' | ||
99 | |||
100 | SliceType = | ||
101 | '[' TypeRef ']' | ||
102 | |||
103 | ReferenceType = | ||
104 | '&' 'lifetime'? 'mut'? TypeRef | ||
105 | |||
106 | PlaceholderType = | ||
107 | '_' | ||
108 | |||
109 | FnPointerType = | ||
110 | Abi 'unsafe'? 'fn' ParamList RetType? | ||
111 | |||
112 | ForType = | ||
113 | 'for' TypeParamList TypeRef | ||
114 | |||
115 | ImplTraitType = | ||
116 | 'impl' TypeBoundList | ||
117 | |||
118 | DynTraitType = | ||
119 | 'dyn' TypeBoundList | ||
120 | |||
121 | TupleExpr = | ||
122 | Attr* '(' Expr* ')' | ||
123 | |||
124 | ArrayExpr = | ||
125 | Attr* '[' (Expr* | Expr ';' Expr) ']' | ||
126 | |||
127 | ParenExpr = | ||
128 | Attr* '(' Expr ')' | ||
129 | |||
130 | PathExpr = | ||
131 | Path | ||
132 | |||
133 | LambdaExpr = | ||
134 | Attr* 'static'? 'async'? 'move'? ParamList RetType? | ||
135 | body:Expr | ||
136 | |||
137 | IfExpr = | ||
138 | Attr* 'if' Condition | ||
139 | |||
140 | Condition = | ||
141 | 'let' Pat '=' Expr | ||
142 | | Expr | ||
143 | |||
144 | EffectExpr = | ||
145 | Attr* Label? ('try' | 'unsafe' | 'async') BlockExpr | ||
146 | |||
147 | LoopExpr = | ||
148 | Attr* Label? 'loop' | ||
149 | loop_body:BlockExpr? | ||
150 | |||
151 | ForExpr = | ||
152 | Attr* Label? 'for' Pat 'in' iterable:Expr | ||
153 | loop_body:BlockExpr? | ||
154 | |||
155 | WhileExpr = | ||
156 | Attr* Label? 'while' Condition | ||
157 | loop_body:BlockExpr? | ||
158 | |||
159 | ContinueExpr = | ||
160 | Attr* 'continue' 'lifetime'? | ||
161 | |||
162 | BreakExpr = | ||
163 | Attr* 'break' 'lifetime'? Expr? | ||
164 | |||
165 | Label = | ||
166 | 'lifetime' | ||
167 | |||
168 | BlockExpr = | ||
169 | Attr* Label | ||
170 | '{' | ||
171 | items:ModuleItem* | ||
172 | statements:Stmt* | ||
173 | Expr? | ||
174 | '}' | ||
175 | |||
176 | ReturnExpr = | ||
177 | Attr* 'return' Expr | ||
178 | |||
179 | CallExpr = | ||
180 | Attr* Expr ArgList | ||
181 | |||
182 | MethodCallExpr = | ||
183 | Attr* Expr '.' NameRef TypeArgList? ArgList | ||
184 | |||
185 | ArgList = | ||
186 | '(' args:Expr* ')' | ||
187 | |||
188 | FieldExpr = | ||
189 | Attr* Expr '.' NameRef | ||
190 | |||
191 | IndexExpr = | ||
192 | Attr* '[' ']' | ||
193 | |||
194 | AwaitExpr = | ||
195 | Attr* Expr '.' 'await' | ||
196 | |||
197 | TryExpr = | ||
198 | Attr* Expr '?' | ||
199 | |||
200 | CastExpr = | ||
201 | Attr* Expr 'as' TypeRef | ||
202 | |||
203 | RefExpr = | ||
204 | Attr* '&' ('raw' | 'mut' | 'const') Expr | ||
205 | |||
206 | PrefixExpr = | ||
207 | Attr* Expr | ||
208 | |||
209 | BoxExpr = | ||
210 | Attr* 'box' Expr | ||
211 | |||
212 | RangeExpr = | ||
213 | Attr* | ||
214 | |||
215 | BinExpr = | ||
216 | Attr* | ||
217 | |||
218 | Literal = | ||
219 | 'int_number' | ||
220 | |||
221 | MatchExpr = | ||
222 | Attr* 'match' Expr MatchArmList | ||
223 | |||
224 | MatchArmList = | ||
225 | '{' arms:MatchArm* '}' | ||
226 | |||
227 | MatchArm = | ||
228 | Attr* Pat guard:MatchGuard? '=>' Expr | ||
229 | |||
230 | MatchGuard = | ||
231 | 'if' Expr | ||
232 | |||
233 | RecordLit = | ||
234 | Path RecordFieldList | ||
235 | |||
236 | RecordFieldList = | ||
237 | '{' | ||
238 | fields:RecordField* | ||
239 | ('..' spread:Expr)? | ||
240 | '}' | ||
241 | |||
242 | RecordField = | ||
243 | Attr* NameRef (':' Expr)? | ||
244 | |||
245 | OrPat = | ||
246 | Pat* | ||
247 | |||
248 | ParenPat = | ||
249 | '(' Pat ')' | ||
250 | |||
251 | RefPat = | ||
252 | '&' 'mut'? Pat | ||
253 | |||
254 | BoxPat = | ||
255 | 'box' Path | ||
256 | |||
257 | BindPat = | ||
258 | Attr* 'ref'? 'mut'? Name ('@' Pat)? | ||
259 | |||
260 | PlaceholderPat = | ||
261 | '_' | ||
262 | |||
263 | DotDotPat = | ||
264 | '..' | ||
265 | |||
266 | PathPat = | ||
267 | Path | ||
268 | |||
269 | SlicePat = | ||
270 | '[' args:Pat* ']' | ||
271 | |||
272 | RangePat = | ||
273 | '..' | '..=' | ||
274 | |||
275 | LiteralPat = | ||
276 | Literal | ||
277 | |||
278 | MacroPat = | ||
279 | MacroCall | ||
280 | |||
281 | RecordPat = | ||
282 | Path RecordFieldPatList | ||
283 | |||
284 | RecordFieldPatList = | ||
285 | '{' | ||
286 | record_field_pats:RecordFieldPat* | ||
287 | BindPat* | ||
288 | '..'? | ||
289 | '}' | ||
290 | |||
291 | RecordFieldPat = | ||
292 | Attr* NameRef ':' Pat | ||
293 | |||
294 | TupleStructPat = | ||
295 | Path '(' args:Pat* ')' | ||
296 | |||
297 | TuplePat = | ||
298 | '(' args:Pat* ')' | ||
299 | |||
300 | Visibility = | ||
301 | 'pub' ('(' 'super' | 'self' | 'crate' | 'in' Path ')')? | ||
302 | |||
303 | Name = | ||
304 | 'ident' | ||
305 | |||
306 | NameRef = | ||
307 | 'ident' | 'int_number' | ||
308 | |||
309 | MacroCall = | ||
310 | Attr* Path '!' Name? TokenTree ';'? | ||
311 | |||
312 | MacroDef = | ||
313 | Name TokenTree | ||
314 | |||
315 | TokenTree = | ||
316 | '(' ')' | '{' '}' | '[' ']' | ||
317 | |||
318 | MacroItems = | ||
319 | items:ModuleItem* | ||
320 | |||
321 | MacroStmts = | ||
322 | statements:Stmt* | ||
323 | Expr? | ||
324 | |||
325 | Attr = | ||
326 | '#' '!'? '[' Path ('=' input:AttrInput)? ']' | ||
327 | |||
328 | TypeParamList = | ||
329 | '<' | ||
330 | TypeParam* | ||
331 | LifetimeParam* | ||
332 | ConstParam* | ||
333 | '>' | ||
334 | |||
335 | TypeParam = | ||
336 | Attr* Name (':' TypeBoundList?)? | ||
337 | ('=' default_type:TypeRef)? | ||
338 | |||
339 | ConstParam = | ||
340 | Attr* 'const' Name ':' ascribed_type:TypeRef | ||
341 | ('=' default_val:Expr)? | ||
342 | |||
343 | LifetimeParam = | ||
344 | Attr* 'lifetime' | ||
345 | |||
346 | TypeBound = | ||
347 | 'lifetime' | 'const'? TypeRef | ||
348 | |||
349 | TypeBoundList = | ||
350 | bounds:TypeBound* | ||
351 | |||
352 | WherePred = | ||
353 | ('for' TypeParamList)? ('lifetime' | TypeRef) ':' TypeBoundList | ||
354 | |||
355 | WhereClause = | ||
356 | 'where' predicates:WherePred* | ||
357 | |||
358 | Abi = | ||
359 | 'string' | ||
360 | |||
361 | ExprStmt = | ||
362 | Attr* Expr ';' | ||
363 | |||
364 | LetStmt = | ||
365 | Attr* 'let' Pat (':' ascribed_type:TypeRef) | ||
366 | '=' initializer:Expr ';' | ||
367 | |||
368 | ParamList = | ||
369 | '(' SelfParam Param* ')' | ||
370 | |||
371 | SelfParam = | ||
372 | Attr* ('&' 'lifetime'?)? 'mut'? 'self' (':' ascribed_type:TypeRef) | ||
373 | |||
374 | Param = | ||
375 | Attr* Pat (':' ascribed_type:TypeRef) | ||
376 | | '...' | ||
377 | |||
378 | UseItem = | ||
379 | Attr* Visibility? 'use' UseTree ';' | ||
380 | |||
381 | UseTree = | ||
382 | Path ('::' ('*' | UseTreeList)) Alias? | ||
383 | |||
384 | UseTreeList = | ||
385 | '{' UseTree* '}' | ||
386 | |||
387 | Alias = | ||
388 | 'as' Name | ||
389 | |||
390 | ExternCrateItem = | ||
391 | Attr* Visibility? 'extern' 'crate' (NameRef | 'self') Alias? ';' | ||
392 | |||
393 | Path = | ||
394 | (qualifier:Path '::')? segment:PathSegment | ||
395 | |||
396 | PathSegment = | ||
397 | '::' | 'crate' | 'self' | 'super' | ||
398 | | '<' NameRef TypeArgList ParamList RetType PathType '>' | ||
399 | |||
400 | TypeArgList = | ||
401 | '::'? '<' | ||
402 | TypeArg* | ||
403 | LifetimeArg* | ||
404 | AssocTypeArg* | ||
405 | ConstArg* | ||
406 | '>' | ||
407 | |||
408 | TypeArg = | ||
409 | TypeRef | ||
410 | |||
411 | AssocTypeArg = | ||
412 | NameRef (':' TypeBoundList | '=' TypeRef) | ||
413 | |||
414 | LifetimeArg = | ||
415 | 'lifetime' | ||
416 | |||
417 | ConstArg = | ||
418 | Literal | BlockExpr BlockExpr | ||
419 | |||
420 | ExternBlock = | ||
421 | Attr* Abi ExternItemList | ||
422 | |||
423 | ExternItemList = | ||
424 | '{' extern_items:ExternItem* '}' | ||
425 | |||
426 | MetaItem = | ||
427 | Path '=' AttrInput nested_meta_items:MetaItem* | ||
428 | |||
429 | AdtDef = | ||
430 | StructDef | ||
431 | | EnumDef | ||
432 | | UnionDef | ||
433 | |||
434 | TypeRef = | ||
435 | ParenType | ||
436 | | TupleType | ||
437 | | NeverType | ||
438 | | PathType | ||
439 | | PointerType | ||
440 | | ArrayType | ||
441 | | SliceType | ||
442 | | ReferenceType | ||
443 | | PlaceholderType | ||
444 | | FnPointerType | ||
445 | | ForType | ||
446 | | ImplTraitType | ||
447 | | DynTraitType | ||
448 | |||
449 | AssocItem = | ||
450 | FnDef | ||
451 | | TypeAliasDef | ||
452 | | ConstDef | ||
453 | |||
454 | ExternItem = | ||
455 | FnDef | StaticDef | ||
456 | |||
457 | ModuleItem = | ||
458 | StructDef | ||
459 | | UnionDef | ||
460 | | EnumDef | ||
461 | | FnDef | ||
462 | | TraitDef | ||
463 | | TypeAliasDef | ||
464 | | ImplDef | ||
465 | | UseItem | ||
466 | | ExternCrateItem | ||
467 | | ConstDef | ||
468 | | StaticDef | ||
469 | | Module | ||
470 | | MacroCall | ||
471 | | ExternBlock | ||
472 | |||
473 | AttrInput = | ||
474 | Literal | ||
475 | | TokenTree | ||
476 | |||
477 | Stmt = | ||
478 | LetStmt | ||
479 | | ExprStmt | ||
480 | |||
481 | Pat = | ||
482 | OrPat | ||
483 | | ParenPat | ||
484 | | RefPat | ||
485 | | BoxPat | ||
486 | | BindPat | ||
487 | | PlaceholderPat | ||
488 | | DotDotPat | ||
489 | | PathPat | ||
490 | | RecordPat | ||
491 | | TupleStructPat | ||
492 | | TuplePat | ||
493 | | SlicePat | ||
494 | | RangePat | ||
495 | | LiteralPat | ||
496 | | MacroPat | ||
497 | |||
498 | Expr = | ||
499 | TupleExpr | ||
500 | | ArrayExpr | ||
501 | | ParenExpr | ||
502 | | PathExpr | ||
503 | | LambdaExpr | ||
504 | | IfExpr | ||
505 | | LoopExpr | ||
506 | | ForExpr | ||
507 | | WhileExpr | ||
508 | | ContinueExpr | ||
509 | | BreakExpr | ||
510 | | Label | ||
511 | | BlockExpr | ||
512 | | ReturnExpr | ||
513 | | MatchExpr | ||
514 | | RecordLit | ||
515 | | CallExpr | ||
516 | | IndexExpr | ||
517 | | MethodCallExpr | ||
518 | | FieldExpr | ||
519 | | AwaitExpr | ||
520 | | TryExpr | ||
521 | | EffectExpr | ||
522 | | CastExpr | ||
523 | | RefExpr | ||
524 | | PrefixExpr | ||
525 | | RangeExpr | ||
526 | | BinExpr | ||
527 | | Literal | ||
528 | | MacroCall | ||
529 | | BoxExpr | ||