From 627eddbc7e5eb13fc17c1c655ee1c3864c6dd4fe Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 29 Jul 2020 11:48:32 +0200 Subject: Owned AST IR --- xtask/src/codegen/gen_syntax.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 745a25862..5a18b3e2b 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -9,28 +9,29 @@ use proc_macro2::{Punct, Spacing}; use quote::{format_ident, quote}; use crate::{ - ast_src::{AstSrc, Field, FieldSrc, KindsSrc, AST_SRC, KINDS_SRC}, + ast_src::{rust_ast, AstSrc, Field, FieldSrc, KindsSrc, KINDS_SRC}, codegen::{self, update, Mode}, project_root, Result, }; pub fn generate_syntax(mode: Mode) -> Result<()> { + let ast = rust_ast(); let syntax_kinds_file = project_root().join(codegen::SYNTAX_KINDS); let syntax_kinds = generate_syntax_kinds(KINDS_SRC)?; update(syntax_kinds_file.as_path(), &syntax_kinds, mode)?; let ast_tokens_file = project_root().join(codegen::AST_TOKENS); - let contents = generate_tokens(AST_SRC)?; + let contents = generate_tokens(&ast)?; update(ast_tokens_file.as_path(), &contents, mode)?; let ast_nodes_file = project_root().join(codegen::AST_NODES); - let contents = generate_nodes(KINDS_SRC, AST_SRC)?; + let contents = generate_nodes(KINDS_SRC, &ast)?; update(ast_nodes_file.as_path(), &contents, mode)?; Ok(()) } -fn generate_tokens(grammar: AstSrc<'_>) -> Result { +fn generate_tokens(grammar: &AstSrc) -> Result { let tokens = grammar.tokens.iter().map(|token| { let name = format_ident!("{}", token); let kind = format_ident!("{}", to_upper_snake_case(token)); @@ -62,13 +63,13 @@ fn generate_tokens(grammar: AstSrc<'_>) -> Result { Ok(pretty) } -fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { +fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result { let (node_defs, node_boilerplate_impls): (Vec<_>, Vec<_>) = grammar .nodes .iter() .map(|node| { let name = format_ident!("{}", node.name); - let kind = format_ident!("{}", to_upper_snake_case(node.name)); + let kind = format_ident!("{}", to_upper_snake_case(&node.name)); let traits = node.traits.iter().map(|trait_name| { let trait_name = format_ident!("{}", trait_name); quote!(impl ast::#trait_name for #name {}) @@ -192,8 +193,8 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { }) .unzip(); - let enum_names = grammar.enums.iter().map(|it| it.name); - let node_names = grammar.nodes.iter().map(|it| it.name); + let enum_names = grammar.enums.iter().map(|it| &it.name); + let node_names = grammar.nodes.iter().map(|it| &it.name); let display_impls = enum_names.chain(node_names.clone()).map(|it| format_ident!("{}", it)).map(|name| { @@ -212,7 +213,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { .nodes .iter() .map(|kind| to_pascal_case(kind)) - .filter(|name| !defined_nodes.contains(name.as_str())) + .filter(|name| !defined_nodes.iter().any(|&it| it == name)) { eprintln!("Warning: node {} not defined in ast source", node); } @@ -236,12 +237,12 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { let mut res = String::with_capacity(ast.len() * 2); let mut docs = - grammar.nodes.iter().map(|it| it.doc).chain(grammar.enums.iter().map(|it| it.doc)); + grammar.nodes.iter().map(|it| &it.doc).chain(grammar.enums.iter().map(|it| &it.doc)); for chunk in ast.split("# [ pretty_doc_comment_placeholder_workaround ]") { res.push_str(chunk); if let Some(doc) = docs.next() { - write_doc_comment(doc, &mut res); + write_doc_comment(&doc, &mut res); } } @@ -249,7 +250,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { Ok(pretty) } -fn write_doc_comment(contents: &[&str], dest: &mut String) { +fn write_doc_comment(contents: &[String], dest: &mut String) { for line in contents { writeln!(dest, "///{}", line).unwrap(); } @@ -413,7 +414,7 @@ fn to_pascal_case(s: &str) -> String { buf } -impl Field<'_> { +impl Field { fn is_many(&self) -> bool { matches!(self, Field::Node { src: FieldSrc::Many(_), .. }) } @@ -429,7 +430,7 @@ impl Field<'_> { fn method_name(&self) -> proc_macro2::Ident { match self { Field::Token(name) => { - let name = match *name { + let name = match name.as_str() { ";" => "semicolon", "->" => "thin_arrow", "'{'" => "l_curly", -- cgit v1.2.3 From 3d28292157e1b6c9675ef64eddf53786c3e7dc5f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 29 Jul 2020 15:45:23 +0200 Subject: Switch to ungrammar from ast_src The primary advantage of ungrammar is that it (eventually) allows one to describe concrete syntax tree structure -- with alternatives and specific sequence of tokens & nodes. That should be re-usable for: * generate `make` calls * Rust reference * Hypothetical parser's evented API We loose doc comments for the time being unfortunately. I don't think we should add support for doc comments to ungrammar -- they'll make grammar file hard to read. We might supply docs as out-of band info, or maybe just via a reference, but we'll think about that once things are no longer in flux --- xtask/src/codegen/gen_syntax.rs | 224 ++++++++++++++++- xtask/src/codegen/rust.ungram | 529 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 749 insertions(+), 4 deletions(-) create mode 100644 xtask/src/codegen/rust.ungram (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 5a18b3e2b..24e8be1fb 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -3,19 +3,27 @@ //! Specifically, it generates the `SyntaxKind` enum and a number of newtype //! wrappers around `SyntaxNode` which implement `ra_syntax::AstNode`. -use std::{collections::HashSet, fmt::Write}; +use std::{ + collections::{BTreeSet, HashSet}, + fmt::Write, +}; use proc_macro2::{Punct, Spacing}; use quote::{format_ident, quote}; +use ungrammar::{Grammar, Rule}; use crate::{ - ast_src::{rust_ast, AstSrc, Field, FieldSrc, KindsSrc, KINDS_SRC}, + ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Field, FieldSrc, KindsSrc, KINDS_SRC}, codegen::{self, update, Mode}, project_root, Result, }; pub fn generate_syntax(mode: Mode) -> Result<()> { - let ast = rust_ast(); + let grammar = include_str!("rust.ungram") + .parse::() + .unwrap_or_else(|err| panic!("\n \x1b[91merror\x1b[0m: {}\n", err)); + let ast = lower(&grammar); + let syntax_kinds_file = project_root().join(codegen::SYNTAX_KINDS); let syntax_kinds = generate_syntax_kinds(KINDS_SRC)?; update(syntax_kinds_file.as_path(), &syntax_kinds, mode)?; @@ -215,7 +223,9 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result { .map(|kind| to_pascal_case(kind)) .filter(|name| !defined_nodes.iter().any(|&it| it == name)) { - eprintln!("Warning: node {} not defined in ast source", node); + drop(node) + // TODO: restore this + // eprintln!("Warning: node {} not defined in ast source", node); } let ast = quote! { @@ -414,6 +424,10 @@ fn to_pascal_case(s: &str) -> String { buf } +fn pluralize(s: &str) -> String { + format!("{}s", s) +} + impl Field { fn is_many(&self) -> bool { matches!(self, Field::Node { src: FieldSrc::Many(_), .. }) @@ -449,6 +463,7 @@ impl Field { "." => "dot", ".." => "dotdot", "..." => "dotdotdot", + "..=" => "dotdoteq", "=>" => "fat_arrow", "@" => "at", ":" => "colon", @@ -475,3 +490,204 @@ impl Field { } } } + +fn lower(grammar: &Grammar) -> AstSrc { + let mut res = AstSrc::default(); + res.tokens = vec!["Whitespace".into(), "Comment".into(), "String".into(), "RawString".into()]; + + let nodes = grammar + .iter() + .filter(|&node| match grammar[node].rule { + Rule::Node(it) if it == node => false, + _ => true, + }) + .collect::>(); + + for &node in &nodes { + let name = grammar[node].name.clone(); + let rule = &grammar[node].rule; + match lower_enum(grammar, rule) { + Some(variants) => { + let enum_src = AstEnumSrc { doc: Vec::new(), name, traits: Vec::new(), variants }; + res.enums.push(enum_src); + } + None => { + let mut fields = Vec::new(); + lower_rule(&mut fields, grammar, rule); + res.nodes.push(AstNodeSrc { doc: Vec::new(), name, traits: Vec::new(), fields }); + } + } + } + + deduplicate_fields(&mut res); + extract_enums(&mut res); + extract_struct_traits(&mut res); + extract_enum_traits(&mut res); + res +} + +fn lower_enum(grammar: &Grammar, rule: &Rule) -> Option> { + let alternatives = match rule { + Rule::Alt(it) => it, + _ => return None, + }; + let mut variants = Vec::new(); + for alternative in alternatives { + match alternative { + Rule::Node(it) => variants.push(grammar[*it].name.clone()), + _ => return None, + } + } + Some(variants) +} + +fn lower_rule(acc: &mut Vec, grammar: &Grammar, rule: &Rule) { + match rule { + Rule::Node(node) => { + let field = Field::Node { name: grammar[*node].name.clone(), src: FieldSrc::Shorthand }; + acc.push(field); + } + Rule::Token(token) => { + let mut name = grammar[*token].name.clone(); + if name != "int_number" && name != "string" { + if "[]{}()".contains(&name) { + name = format!("'{}'", name); + } + let field = Field::Token(name); + acc.push(field); + } + } + Rule::Rep(inner) => { + if let Rule::Node(node) = &**inner { + let name = grammar[*node].name.clone(); + let label = pluralize(&to_lower_snake_case(&name)); + let field = Field::Node { name: label.clone(), src: FieldSrc::Many(name) }; + acc.push(field); + return; + } + todo!("{:?}", rule) + } + Rule::Labeled { label, rule } => { + let node = match &**rule { + Rule::Rep(inner) | Rule::Opt(inner) => match &**inner { + Rule::Node(node) => node, + _ => todo!("{:?}", rule), + }, + Rule::Node(node) => node, + _ => todo!("{:?}", rule), + }; + let field = Field::Node { + name: label.clone(), + src: match &**rule { + Rule::Rep(_) => FieldSrc::Many(grammar[*node].name.clone()), + _ => FieldSrc::Optional(grammar[*node].name.clone()), + }, + }; + acc.push(field); + } + Rule::Seq(rules) | Rule::Alt(rules) => { + for rule in rules { + lower_rule(acc, grammar, rule) + } + } + Rule::Opt(rule) => lower_rule(acc, grammar, rule), + } +} + +fn deduplicate_fields(ast: &mut AstSrc) { + eprintln!(); + for node in &mut ast.nodes { + let mut i = 0; + 'outer: while i < node.fields.len() { + for j in 0..i { + let f1 = &node.fields[i]; + let f2 = &node.fields[j]; + if f1 == f2 { + node.fields.remove(i); + continue 'outer; + } + } + i += 1; + } + } +} + +fn extract_enums(ast: &mut AstSrc) { + for node in &mut ast.nodes { + for enm in &ast.enums { + let mut to_remove = Vec::new(); + for (i, field) in node.fields.iter().enumerate() { + let ty = field.ty().to_string(); + if enm.variants.iter().any(|it| it == &ty) { + to_remove.push(i); + } + } + if to_remove.len() == enm.variants.len() { + node.remove_field(to_remove); + node.fields.push(Field::Node { name: enm.name.clone(), src: FieldSrc::Shorthand }); + } + } + } +} + +fn extract_struct_traits(ast: &mut AstSrc) { + let traits: &[(&str, &[&str])] = &[ + ("AttrsOwner", &["attrs"]), + ("NameOwner", &["name"]), + ("VisibilityOwner", &["visibility"]), + ("TypeParamsOwner", &["type_param_list", "where_clause"]), + ("TypeBoundsOwner", &["type_bound_list", "colon_token"]), + ("ModuleItemOwner", &["items"]), + ("TypeAscriptionOwner", &["ascribed_type"]), + ("LoopBodyOwner", &["label", "loop_body"]), + ("ArgListOwner", &["arg_list"]), + ]; + + for node in &mut ast.nodes { + for (name, methods) in traits { + extract_struct_trait(node, name, methods); + } + } +} + +fn extract_struct_trait(node: &mut AstNodeSrc, trait_name: &str, methods: &[&str]) { + let mut to_remove = Vec::new(); + for (i, field) in node.fields.iter().enumerate() { + let method_name = field.method_name().to_string(); + if methods.iter().any(|&it| it == &method_name) { + to_remove.push(i); + } + } + if to_remove.len() == methods.len() { + node.traits.push(trait_name.to_string()); + node.remove_field(to_remove); + } +} + +fn extract_enum_traits(ast: &mut AstSrc) { + for enm in &mut ast.enums { + let nodes = &ast.nodes; + let mut variant_traits = enm + .variants + .iter() + .map(|var| nodes.iter().find(|it| &it.name == var).unwrap()) + .map(|node| node.traits.iter().cloned().collect::>()); + + let mut enum_traits = match variant_traits.next() { + Some(it) => it, + None => continue, + }; + for traits in variant_traits { + enum_traits = enum_traits.intersection(&traits).cloned().collect(); + } + enm.traits = enum_traits.into_iter().collect(); + } +} + +impl AstNodeSrc { + fn remove_field(&mut self, to_remove: Vec) { + to_remove.into_iter().rev().for_each(|idx| { + self.fields.remove(idx); + }); + } +} diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram new file mode 100644 index 000000000..8a3eb7b29 --- /dev/null +++ b/xtask/src/codegen/rust.ungram @@ -0,0 +1,529 @@ +SourceFile = + Attr* + items:ModuleItem* + +FnDef = + Attr* Visibility? Abi? 'const' 'default' 'async' 'unsafe' 'fn' Name TypeParamList? + ParamList RetType? + WhereClause? + (body:BlockExpr | ';') + +RetType = + '->' TypeRef + +StructDef = + Attr* Visibility? 'struct' Name TypeParamList? ( + WhereClause? (RecordFieldDefList | ';') + | TupleFieldDefList WhereClause? ';' + ) + +UnionDef = + Attr* Visibility? 'union' Name TypeParamList? WhereClause? + RecordFieldDefList + +RecordFieldDefList = + '{' fields:RecordFieldDef* '}' + +RecordFieldDef = + Attr* Visibility? Name ':' ascribed_type:TypeRef + +TupleFieldDefList = + '(' fields:TupleFieldDef* ')' + +TupleFieldDef = + Attr* Visibility? Name TypeRef + +FieldDefList = + RecordFieldDefList +| TupleFieldDefList + +EnumDef = + Attr* Visibility? 'enum' Name TypeParamList? WhereClause? + variant_list:EnumVariantList + +EnumVariantList = + '{' variants:EnumVariant* '}' + +EnumVariant = + Attr* Visibility? Name FieldDefList ('=' Expr)? + +TraitDef = + Attr* Visibility? 'unsafe'? 'auto'? 'trait' Name TypeParamList + (':' TypeBoundList?)? WhereClause + ItemList + +Module = + Attr* Visibility? 'mod' Name + (ItemList | ';') + +ItemList = + '{' + AssocItem* + items:ModuleItem* + '}' + +ConstDef = + Attr* Visibility? 'default'? 'const' Name ':' ascribed_type:TypeRef + '=' body:Expr ';' + +StaticDef = + Attr* Visibility? 'static'? 'mut'? 'static' Name ':' ascribed_type:TypeRef + '=' body:Expr ';' + +TypeAliasDef = + Attr* Visibility? 'default'? 'type' Name TypeParamList? WhereClause? (':' TypeBoundList?)? + '=' TypeRef ';' + +ImplDef = + Attr* Visibility? 'const'? 'default'? 'unsafe'? 'impl' TypeParamList? '!'? 'for' + WhereClause? + ItemList + +ParenType = + '(' TypeRef ')' + +TupleType = + '(' fields:TypeRef* ')' + +NeverType = + '!' + +PathType = + Path + +PointerType = + '*' ('const' | 'mut') TypeRef + +ArrayType = + '[' TypeRef ';' Expr ']' + +SliceType = + '[' TypeRef ']' + +ReferenceType = + '&' 'lifetime'? 'mut'? TypeRef + +PlaceholderType = + '_' + +FnPointerType = + Abi 'unsafe'? 'fn' ParamList RetType? + +ForType = + 'for' TypeParamList TypeRef + +ImplTraitType = + 'impl' TypeBoundList + +DynTraitType = + 'dyn' TypeBoundList + +TupleExpr = + Attr* '(' Expr* ')' + +ArrayExpr = + Attr* '[' (Expr* | Expr ';' Expr) ']' + +ParenExpr = + Attr* '(' Expr ')' + +PathExpr = + Path + +LambdaExpr = + Attr* 'static'? 'async'? 'move'? ParamList RetType? + body:Expr + +IfExpr = + Attr* 'if' Condition + +Condition = + 'let' Pat '=' Expr +| Expr + +EffectExpr = + Attr* Label? ('try' | 'unsafe' | 'async') BlockExpr + +LoopExpr = + Attr* Label? 'loop' + loop_body:BlockExpr? + +ForExpr = + Attr* Label? 'for' Pat 'in' iterable:Expr + loop_body:BlockExpr? + +WhileExpr = + Attr* Label? 'while' Condition + loop_body:BlockExpr? + +ContinueExpr = + Attr* 'continue' 'lifetime'? + +BreakExpr = + Attr* 'break' 'lifetime'? Expr? + +Label = + 'lifetime' + +BlockExpr = + Attr* Label + '{' + items:ModuleItem* + statements:Stmt* + Expr? + '}' + +ReturnExpr = + Attr* 'return' Expr + +CallExpr = + Attr* Expr ArgList + +MethodCallExpr = + Attr* Expr '.' NameRef TypeArgList? ArgList + +ArgList = + '(' args:Expr* ')' + +FieldExpr = + Attr* Expr '.' NameRef + +IndexExpr = + Attr* '[' ']' + +AwaitExpr = + Attr* Expr '.' 'await' + +TryExpr = + Attr* Expr '?' + +CastExpr = + Attr* Expr 'as' TypeRef + +RefExpr = + Attr* '&' ('raw' | 'mut' | 'const') Expr + +PrefixExpr = + Attr* Expr + +BoxExpr = + Attr* 'box' Expr + +RangeExpr = + Attr* + +BinExpr = + Attr* + +Literal = + 'int_number' + +MatchExpr = + Attr* 'match' Expr MatchArmList + +MatchArmList = + '{' arms:MatchArm* '}' + +MatchArm = + Attr* Pat guard:MatchGuard? '=>' Expr + +MatchGuard = + 'if' Expr + +RecordLit = + Path RecordFieldList + +RecordFieldList = + '{' + fields:RecordField* + ('..' spread:Expr)? + '}' + +RecordField = + Attr* NameRef (':' Expr)? + +OrPat = + Pat* + +ParenPat = + '(' Pat ')' + +RefPat = + '&' 'mut'? Pat + +BoxPat = + 'box' Path + +BindPat = + Attr* 'ref'? 'mut'? Name ('@' Pat)? + +PlaceholderPat = + '_' + +DotDotPat = + '..' + +PathPat = + Path + +SlicePat = + '[' args:Pat* ']' + +RangePat = + '..' | '..=' + +LiteralPat = + Literal + +MacroPat = + MacroCall + +RecordPat = + Path RecordFieldPatList + +RecordFieldPatList = + '{' + record_field_pats:RecordFieldPat* + BindPat* + '..'? + '}' + +RecordFieldPat = + Attr* NameRef ':' Pat + +TupleStructPat = + Path '(' args:Pat* ')' + +TuplePat = + '(' args:Pat* ')' + +Visibility = + 'pub' ('(' 'super' | 'self' | 'crate' | 'in' Path ')')? + +Name = + 'ident' + +NameRef = + 'ident' | 'int_number' + +MacroCall = + Attr* Path '!' Name? TokenTree ';'? + +MacroDef = + Name TokenTree + +TokenTree = + '(' ')' | '{' '}' | '[' ']' + +MacroItems = + items:ModuleItem* + +MacroStmts = + statements:Stmt* + Expr? + +Attr = + '#' '!'? '[' Path ('=' input:AttrInput)? ']' + +TypeParamList = + '<' + TypeParam* + LifetimeParam* + ConstParam* + '>' + +TypeParam = + Attr* Name (':' TypeBoundList?)? + ('=' default_type:TypeRef)? + +ConstParam = + Attr* 'const' Name ':' ascribed_type:TypeRef + ('=' default_val:Expr)? + +LifetimeParam = + Attr* 'lifetime' + +TypeBound = + 'lifetime' | 'const'? TypeRef + +TypeBoundList = + bounds:TypeBound* + +WherePred = + ('for' TypeParamList)? ('lifetime' | TypeRef) ':' TypeBoundList + +WhereClause = + 'where' predicates:WherePred* + +Abi = + 'string' + +ExprStmt = + Attr* Expr ';' + +LetStmt = + Attr* 'let' Pat (':' ascribed_type:TypeRef) + '=' initializer:Expr ';' + +ParamList = + '(' SelfParam Param* ')' + +SelfParam = + Attr* ('&' 'lifetime'?)? 'mut'? 'self' (':' ascribed_type:TypeRef) + +Param = + Attr* Pat (':' ascribed_type:TypeRef) +| '...' + +UseItem = + Attr* Visibility? 'use' UseTree ';' + +UseTree = + Path ('::' ('*' | UseTreeList)) Alias? + +UseTreeList = + '{' UseTree* '}' + +Alias = + 'as' Name + +ExternCrateItem = + Attr* Visibility? 'extern' 'crate' (NameRef | 'self') Alias? ';' + +Path = + (qualifier:Path '::')? segment:PathSegment + +PathSegment = + '::' | 'crate' | 'self' | 'super' +| '<' NameRef TypeArgList ParamList RetType PathType '>' + +TypeArgList = + '::'? '<' + TypeArg* + LifetimeArg* + AssocTypeArg* + ConstArg* + '>' + +TypeArg = + TypeRef + +AssocTypeArg = + NameRef (':' TypeBoundList | '=' TypeRef) + +LifetimeArg = + 'lifetime' + +ConstArg = + Literal | BlockExpr BlockExpr + +ExternBlock = + Attr* Abi ExternItemList + +ExternItemList = + '{' extern_items:ExternItem* '}' + +MetaItem = + Path '=' AttrInput nested_meta_items:MetaItem* + +NominalDef = + StructDef +| EnumDef +| UnionDef + +TypeRef = + ParenType +| TupleType +| NeverType +| PathType +| PointerType +| ArrayType +| SliceType +| ReferenceType +| PlaceholderType +| FnPointerType +| ForType +| ImplTraitType +| DynTraitType + +AssocItem = + FnDef +| TypeAliasDef +| ConstDef + +ExternItem = + FnDef | StaticDef + +ModuleItem = + StructDef +| UnionDef +| EnumDef +| FnDef +| TraitDef +| TypeAliasDef +| ImplDef +| UseItem +| ExternCrateItem +| ConstDef +| StaticDef +| Module +| MacroCall +| ExternBlock + +AttrInput = + Literal +| TokenTree + +Stmt = + LetStmt +| ExprStmt + +Pat = + OrPat +| ParenPat +| RefPat +| BoxPat +| BindPat +| PlaceholderPat +| DotDotPat +| PathPat +| RecordPat +| TupleStructPat +| TuplePat +| SlicePat +| RangePat +| LiteralPat +| MacroPat + +Expr = + TupleExpr +| ArrayExpr +| ParenExpr +| PathExpr +| LambdaExpr +| IfExpr +| LoopExpr +| ForExpr +| WhileExpr +| ContinueExpr +| BreakExpr +| Label +| BlockExpr +| ReturnExpr +| MatchExpr +| RecordLit +| CallExpr +| IndexExpr +| MethodCallExpr +| FieldExpr +| AwaitExpr +| TryExpr +| EffectExpr +| CastExpr +| RefExpr +| PrefixExpr +| RangeExpr +| BinExpr +| Literal +| MacroCall +| BoxExpr -- cgit v1.2.3 From 76202a2c7371a6930db7b83af75c0f5a8ae1d061 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 29 Jul 2020 19:22:15 +0200 Subject: Rename NomialDef -> AdtDef --- xtask/src/codegen/rust.ungram | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 8a3eb7b29..b6ec5d5e7 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -426,7 +426,7 @@ ExternItemList = MetaItem = Path '=' AttrInput nested_meta_items:MetaItem* -NominalDef = +AdtDef = StructDef | EnumDef | UnionDef -- cgit v1.2.3 From 6636f56e79b55f22b88094b7edaed6ec88880500 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 00:23:03 +0200 Subject: Rename ModuleItem -> Item --- xtask/src/codegen/rust.ungram | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index b6ec5d5e7..08db70093 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -1,6 +1,22 @@ SourceFile = Attr* - items:ModuleItem* + Item* + +Item = + ConstDef +| EnumDef +| ExternBlock +| ExternCrateItem +| FnDef +| ImplDef +| MacroCall +| Module +| StaticDef +| StructDef +| TraitDef +| TypeAliasDef +| UnionDef +| UseItem FnDef = Attr* Visibility? Abi? 'const' 'default' 'async' 'unsafe' 'fn' Name TypeParamList? @@ -59,7 +75,7 @@ Module = ItemList = '{' AssocItem* - items:ModuleItem* + Item* '}' ConstDef = @@ -168,7 +184,7 @@ Label = BlockExpr = Attr* Label '{' - items:ModuleItem* + Item* statements:Stmt* Expr? '}' @@ -316,7 +332,7 @@ TokenTree = '(' ')' | '{' '}' | '[' ']' MacroItems = - items:ModuleItem* + Item* MacroStmts = statements:Stmt* @@ -454,22 +470,6 @@ AssocItem = ExternItem = FnDef | StaticDef -ModuleItem = - StructDef -| UnionDef -| EnumDef -| FnDef -| TraitDef -| TypeAliasDef -| ImplDef -| UseItem -| ExternCrateItem -| ConstDef -| StaticDef -| Module -| MacroCall -| ExternBlock - AttrInput = Literal | TokenTree -- cgit v1.2.3 From ede5d17b0409f9d5a209aaf16508262dbd2a4489 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 00:27:00 +0200 Subject: Finish SourceFile grammar --- xtask/src/codegen/gen_syntax.rs | 2 +- xtask/src/codegen/rust.ungram | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 24e8be1fb..9b49712c1 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -374,6 +374,7 @@ fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result { #([#all_keywords_idents] => { $crate::SyntaxKind::#all_keywords };)* [lifetime] => { $crate::SyntaxKind::LIFETIME }; [ident] => { $crate::SyntaxKind::IDENT }; + [shebang] => { $crate::SyntaxKind::SHEBANG }; } }; @@ -595,7 +596,6 @@ fn lower_rule(acc: &mut Vec, grammar: &Grammar, rule: &Rule) { } fn deduplicate_fields(ast: &mut AstSrc) { - eprintln!(); for node in &mut ast.nodes { let mut i = 0; 'outer: while i < node.fields.len() { diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 08db70093..a93cb3815 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -1,4 +1,5 @@ SourceFile = + 'shebang'? Attr* Item* -- cgit v1.2.3 From 2984da672e0c73d56501c6b6e4d19fd28152b5eb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 11:42:51 +0200 Subject: Split ItemList & AssocItemList --- xtask/src/codegen/rust.ungram | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index a93cb3815..d0518cead 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -19,6 +19,13 @@ Item = | UnionDef | UseItem +Module = + Attr* Visibility? 'mod' Name + (ItemList | ';') + +ItemList = + '{' Item* '}' + FnDef = Attr* Visibility? Abi? 'const' 'default' 'async' 'unsafe' 'fn' Name TypeParamList? ParamList RetType? @@ -67,17 +74,10 @@ EnumVariant = TraitDef = Attr* Visibility? 'unsafe'? 'auto'? 'trait' Name TypeParamList (':' TypeBoundList?)? WhereClause - ItemList + AssocItemList -Module = - Attr* Visibility? 'mod' Name - (ItemList | ';') - -ItemList = - '{' - AssocItem* - Item* - '}' +AssocItemList = + '{' AssocItem* '}' ConstDef = Attr* Visibility? 'default'? 'const' Name ':' ascribed_type:TypeRef @@ -94,7 +94,7 @@ TypeAliasDef = ImplDef = Attr* Visibility? 'const'? 'default'? 'unsafe'? 'impl' TypeParamList? '!'? 'for' WhereClause? - ItemList + AssocItemList ParenType = '(' TypeRef ')' @@ -467,6 +467,7 @@ AssocItem = FnDef | TypeAliasDef | ConstDef +| MacroCall ExternItem = FnDef | StaticDef -- cgit v1.2.3 From 7d09e5ed618b9b9d6e00b57b24db0b9c8a8c12d7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 11:44:22 +0200 Subject: Finish Module grammar --- xtask/src/codegen/rust.ungram | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index d0518cead..470ac8c8f 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -24,7 +24,7 @@ Module = (ItemList | ';') ItemList = - '{' Item* '}' + '{' Attr* Item* '}' FnDef = Attr* Visibility? Abi? 'const' 'default' 'async' 'unsafe' 'fn' Name TypeParamList? -- cgit v1.2.3 From 6cd2131cafd29ae17442efbcce652bd447576f27 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 11:58:41 +0200 Subject: Rename Rename --- xtask/src/codegen/rust.ungram | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 470ac8c8f..76c070402 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -396,16 +396,16 @@ UseItem = Attr* Visibility? 'use' UseTree ';' UseTree = - Path ('::' ('*' | UseTreeList)) Alias? + Path ('::' ('*' | UseTreeList)) Rename? UseTreeList = '{' UseTree* '}' -Alias = +Rename = 'as' Name ExternCrateItem = - Attr* Visibility? 'extern' 'crate' (NameRef | 'self') Alias? ';' + Attr* Visibility? 'extern' 'crate' (NameRef | 'self') Rename? ';' Path = (qualifier:Path '::')? segment:PathSegment -- cgit v1.2.3 From d032f872b619c651dc66bfd669ef783108fc122c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 12:26:57 +0200 Subject: Finish extern crates grammar --- xtask/src/codegen/rust.ungram | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 76c070402..e6e7c7518 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -7,7 +7,7 @@ Item = ConstDef | EnumDef | ExternBlock -| ExternCrateItem +| ExternCrate | FnDef | ImplDef | MacroCall @@ -26,6 +26,9 @@ Module = ItemList = '{' Attr* Item* '}' +ExternCrate = + Attr* Visibility? 'extern' 'crate' (NameRef | 'self') Rename? ';' + FnDef = Attr* Visibility? Abi? 'const' 'default' 'async' 'unsafe' 'fn' Name TypeParamList? ParamList RetType? @@ -404,9 +407,6 @@ UseTreeList = Rename = 'as' Name -ExternCrateItem = - Attr* Visibility? 'extern' 'crate' (NameRef | 'self') Rename? ';' - Path = (qualifier:Path '::')? segment:PathSegment -- cgit v1.2.3 From e381c02ef304fdeafde1c94afd1a10c2085ab716 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 14:06:04 +0200 Subject: Add comma list to use tree --- xtask/src/codegen/gen_syntax.rs | 35 +++++++++++++++++++++++++++++++++++ xtask/src/codegen/rust.ungram | 25 +++++++++++++------------ 2 files changed, 48 insertions(+), 12 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 9b49712c1..c77fc8a8d 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -543,6 +543,10 @@ fn lower_enum(grammar: &Grammar, rule: &Rule) -> Option> { } fn lower_rule(acc: &mut Vec, grammar: &Grammar, rule: &Rule) { + if lower_comma_list(acc, grammar, rule) { + return; + } + match rule { Rule::Node(node) => { let field = Field::Node { name: grammar[*node].name.clone(), src: FieldSrc::Shorthand }; @@ -595,6 +599,37 @@ fn lower_rule(acc: &mut Vec, grammar: &Grammar, rule: &Rule) { } } +// (T (',' T)* ','?)? +fn lower_comma_list(acc: &mut Vec, grammar: &Grammar, rule: &Rule) -> bool { + let rule = match rule { + Rule::Opt(it) => it, + _ => return false, + }; + let rule = match &**rule { + Rule::Seq(it) => it, + _ => return false, + }; + let (node, repeat, trailing_comma) = match rule.as_slice() { + [Rule::Node(node), Rule::Rep(repeat), Rule::Opt(trailing_comma)] => { + (node, repeat, trailing_comma) + } + _ => return false, + }; + let repeat = match &**repeat { + Rule::Seq(it) => it, + _ => return false, + }; + match repeat.as_slice() { + [comma, Rule::Node(n)] if comma == &**trailing_comma && n == node => (), + _ => return false, + } + let name = grammar[*node].name.clone(); + let label = pluralize(&to_lower_snake_case(&name)); + let field = Field::Node { name: label.clone(), src: FieldSrc::Many(name) }; + acc.push(field); + true +} + fn deduplicate_fields(ast: &mut AstSrc) { for node in &mut ast.nodes { let mut i = 0; diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index e6e7c7518..2ba68457f 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -29,6 +29,19 @@ ItemList = ExternCrate = Attr* Visibility? 'extern' 'crate' (NameRef | 'self') Rename? ';' +Rename = + 'as' (Name | '_') + +UseItem = + Attr* Visibility? 'use' UseTree ';' + +UseTree = + (Path? '::')? ('*' | UseTreeList ) +| Path Rename? + +UseTreeList = + '{' (UseTree (',' UseTree)* ','?)? '}' + FnDef = Attr* Visibility? Abi? 'const' 'default' 'async' 'unsafe' 'fn' Name TypeParamList? ParamList RetType? @@ -395,18 +408,6 @@ Param = Attr* Pat (':' ascribed_type:TypeRef) | '...' -UseItem = - Attr* Visibility? 'use' UseTree ';' - -UseTree = - Path ('::' ('*' | UseTreeList)) Rename? - -UseTreeList = - '{' UseTree* '}' - -Rename = - 'as' Name - Path = (qualifier:Path '::')? segment:PathSegment -- cgit v1.2.3 From b1332670c7c471a59f3da113b366e74ac194c38b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 14:12:04 +0200 Subject: Rename UseItem -> Use --- xtask/src/codegen/rust.ungram | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 2ba68457f..449b0242f 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -17,7 +17,7 @@ Item = | TraitDef | TypeAliasDef | UnionDef -| UseItem +| Use Module = Attr* Visibility? 'mod' Name @@ -32,7 +32,7 @@ ExternCrate = Rename = 'as' (Name | '_') -UseItem = +Use = Attr* Visibility? 'use' UseTree ';' UseTree = -- cgit v1.2.3 From 1142112c70b705f59b7d559d9d72cdc831865158 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 14:51:08 +0200 Subject: Rename FnDef -> Fn --- xtask/src/codegen/gen_syntax.rs | 7 ++----- xtask/src/codegen/rust.ungram | 46 ++++++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 24 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index c77fc8a8d..072527208 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -471,6 +471,7 @@ impl Field { "::" => "coloncolon", "#" => "pound", "?" => "question_mark", + "," => "comma", _ => name, }; format_ident!("{}_token", name) @@ -599,13 +600,9 @@ fn lower_rule(acc: &mut Vec, grammar: &Grammar, rule: &Rule) { } } -// (T (',' T)* ','?)? +// (T (',' T)* ','?) fn lower_comma_list(acc: &mut Vec, grammar: &Grammar, rule: &Rule) -> bool { let rule = match rule { - Rule::Opt(it) => it, - _ => return false, - }; - let rule = match &**rule { Rule::Seq(it) => it, _ => return false, }; diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 449b0242f..98968dece 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -8,7 +8,7 @@ Item = | EnumDef | ExternBlock | ExternCrate -| FnDef +| Fn | ImplDef | MacroCall | Module @@ -42,12 +42,33 @@ UseTree = UseTreeList = '{' (UseTree (',' UseTree)* ','?)? '}' -FnDef = - Attr* Visibility? Abi? 'const' 'default' 'async' 'unsafe' 'fn' Name TypeParamList? - ParamList RetType? +Fn = + Attr* Visibility? + 'default'? ('async' | 'const')? 'unsafe'? Abi? + 'fn' Name TypeParamList? ParamList RetType? WhereClause? (body:BlockExpr | ';') +Abi = + 'extern' 'string'? + +ParamList = + '('( + (Param (',' Param)* ','?)? + | SelfParam ','? + | SelfParam ',' (Param (',' Param)* ','?) + )')' + +SelfParam = + Attr* ( + ('&' 'lifetime'?)? 'mut'? 'self' + | 'mut'? 'self' ':' ascribed_type:TypeRef + ) + +Param = + Attr* Pat (':' ascribed_type:TypeRef) +| '...' + RetType = '->' TypeRef @@ -388,9 +409,6 @@ WherePred = WhereClause = 'where' predicates:WherePred* -Abi = - 'string' - ExprStmt = Attr* Expr ';' @@ -398,16 +416,6 @@ LetStmt = Attr* 'let' Pat (':' ascribed_type:TypeRef) '=' initializer:Expr ';' -ParamList = - '(' SelfParam Param* ')' - -SelfParam = - Attr* ('&' 'lifetime'?)? 'mut'? 'self' (':' ascribed_type:TypeRef) - -Param = - Attr* Pat (':' ascribed_type:TypeRef) -| '...' - Path = (qualifier:Path '::')? segment:PathSegment @@ -465,13 +473,13 @@ TypeRef = | DynTraitType AssocItem = - FnDef + Fn | TypeAliasDef | ConstDef | MacroCall ExternItem = - FnDef | StaticDef + Fn | StaticDef AttrInput = Literal -- cgit v1.2.3 From eb2f8063444b11257111f4f8ade990ec810e0361 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 15:25:46 +0200 Subject: Rename TypeAliasDef -> TypeAlias --- xtask/src/codegen/rust.ungram | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 98968dece..760a8dd95 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -15,7 +15,7 @@ Item = | StaticDef | StructDef | TraitDef -| TypeAliasDef +| TypeAlias | UnionDef | Use @@ -72,6 +72,10 @@ Param = RetType = '->' TypeRef +TypeAlias = + Attr* Visibility? 'default'? 'type' Name TypeParamList? (':' TypeBoundList?)? WhereClause? + '=' TypeRef ';' + StructDef = Attr* Visibility? 'struct' Name TypeParamList? ( WhereClause? (RecordFieldDefList | ';') @@ -124,10 +128,6 @@ StaticDef = Attr* Visibility? 'static'? 'mut'? 'static' Name ':' ascribed_type:TypeRef '=' body:Expr ';' -TypeAliasDef = - Attr* Visibility? 'default'? 'type' Name TypeParamList? WhereClause? (':' TypeBoundList?)? - '=' TypeRef ';' - ImplDef = Attr* Visibility? 'const'? 'default'? 'unsafe'? 'impl' TypeParamList? '!'? 'for' WhereClause? @@ -474,7 +474,7 @@ TypeRef = AssocItem = Fn -| TypeAliasDef +| TypeAlias | ConstDef | MacroCall -- cgit v1.2.3 From 28ef4c375a9f56d69daf885504aea3df7012bb81 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 15:36:21 +0200 Subject: Rename TypeParamList -> GenericParamList --- xtask/src/codegen/gen_syntax.rs | 2 +- xtask/src/codegen/rust.ungram | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 072527208..f79cd972e 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -667,7 +667,7 @@ fn extract_struct_traits(ast: &mut AstSrc) { ("AttrsOwner", &["attrs"]), ("NameOwner", &["name"]), ("VisibilityOwner", &["visibility"]), - ("TypeParamsOwner", &["type_param_list", "where_clause"]), + ("GenericParamsOwner", &["generic_param_list", "where_clause"]), ("TypeBoundsOwner", &["type_bound_list", "colon_token"]), ("ModuleItemOwner", &["items"]), ("TypeAscriptionOwner", &["ascribed_type"]), diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 760a8dd95..833ffd9e0 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -45,7 +45,7 @@ UseTreeList = Fn = Attr* Visibility? 'default'? ('async' | 'const')? 'unsafe'? Abi? - 'fn' Name TypeParamList? ParamList RetType? + 'fn' Name GenericParamList? ParamList RetType? WhereClause? (body:BlockExpr | ';') @@ -73,17 +73,17 @@ RetType = '->' TypeRef TypeAlias = - Attr* Visibility? 'default'? 'type' Name TypeParamList? (':' TypeBoundList?)? WhereClause? + Attr* Visibility? 'default'? 'type' Name GenericParamList? (':' TypeBoundList?)? WhereClause? '=' TypeRef ';' StructDef = - Attr* Visibility? 'struct' Name TypeParamList? ( + Attr* Visibility? 'struct' Name GenericParamList? ( WhereClause? (RecordFieldDefList | ';') | TupleFieldDefList WhereClause? ';' ) UnionDef = - Attr* Visibility? 'union' Name TypeParamList? WhereClause? + Attr* Visibility? 'union' Name GenericParamList? WhereClause? RecordFieldDefList RecordFieldDefList = @@ -103,7 +103,7 @@ FieldDefList = | TupleFieldDefList EnumDef = - Attr* Visibility? 'enum' Name TypeParamList? WhereClause? + Attr* Visibility? 'enum' Name GenericParamList? WhereClause? variant_list:EnumVariantList EnumVariantList = @@ -113,7 +113,7 @@ EnumVariant = Attr* Visibility? Name FieldDefList ('=' Expr)? TraitDef = - Attr* Visibility? 'unsafe'? 'auto'? 'trait' Name TypeParamList + Attr* Visibility? 'unsafe'? 'auto'? 'trait' Name GenericParamList (':' TypeBoundList?)? WhereClause AssocItemList @@ -129,7 +129,7 @@ StaticDef = '=' body:Expr ';' ImplDef = - Attr* Visibility? 'const'? 'default'? 'unsafe'? 'impl' TypeParamList? '!'? 'for' + Attr* Visibility? 'const'? 'default'? 'unsafe'? 'impl' GenericParamList? '!'? 'for' WhereClause? AssocItemList @@ -164,7 +164,7 @@ FnPointerType = Abi 'unsafe'? 'fn' ParamList RetType? ForType = - 'for' TypeParamList TypeRef + 'for' GenericParamList TypeRef ImplTraitType = 'impl' TypeBoundList @@ -379,7 +379,7 @@ MacroStmts = Attr = '#' '!'? '[' Path ('=' input:AttrInput)? ']' -TypeParamList = +GenericParamList = '<' TypeParam* LifetimeParam* @@ -404,7 +404,7 @@ TypeBoundList = bounds:TypeBound* WherePred = - ('for' TypeParamList)? ('lifetime' | TypeRef) ':' TypeBoundList + ('for' GenericParamList)? ('lifetime' | TypeRef) ':' TypeBoundList WhereClause = 'where' predicates:WherePred* -- cgit v1.2.3 From 98ec5f2c21d0072e4811309ac111db75b87146d1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 16:12:35 +0200 Subject: Minor, reorder --- xtask/src/codegen/rust.ungram | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 833ffd9e0..7f3063efb 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -82,10 +82,6 @@ StructDef = | TupleFieldDefList WhereClause? ';' ) -UnionDef = - Attr* Visibility? 'union' Name GenericParamList? WhereClause? - RecordFieldDefList - RecordFieldDefList = '{' fields:RecordFieldDef* '}' @@ -102,6 +98,11 @@ FieldDefList = RecordFieldDefList | TupleFieldDefList + +UnionDef = + Attr* Visibility? 'union' Name GenericParamList? WhereClause? + RecordFieldDefList + EnumDef = Attr* Visibility? 'enum' Name GenericParamList? WhereClause? variant_list:EnumVariantList -- cgit v1.2.3 From 6f8aa75329d0a4e588e58b8f22f7932bf3d3a706 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 16:21:30 +0200 Subject: Rename RecordLit -> RecordExpr --- xtask/src/codegen/rust.ungram | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 7f3063efb..49b2c9ff8 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -285,16 +285,16 @@ MatchArm = MatchGuard = 'if' Expr -RecordLit = - Path RecordFieldList +RecordExpr = + Path RecordExprFieldList -RecordFieldList = +RecordExprFieldList = '{' - fields:RecordField* + fields:RecordExprField* ('..' spread:Expr)? '}' -RecordField = +RecordExprField = Attr* NameRef (':' Expr)? OrPat = @@ -523,7 +523,7 @@ Expr = | BlockExpr | ReturnExpr | MatchExpr -| RecordLit +| RecordExpr | CallExpr | IndexExpr | MethodCallExpr -- cgit v1.2.3 From 0a9e3ccc262fbcbd4cdaab30384f8cb71584544b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 16:49:13 +0200 Subject: Rename FieldDef -> Field --- xtask/src/codegen/rust.ungram | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 49b2c9ff8..ba922af0d 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -78,30 +78,30 @@ TypeAlias = StructDef = Attr* Visibility? 'struct' Name GenericParamList? ( - WhereClause? (RecordFieldDefList | ';') - | TupleFieldDefList WhereClause? ';' + WhereClause? (RecordFieldList | ';') + | TupleFieldList WhereClause? ';' ) -RecordFieldDefList = - '{' fields:RecordFieldDef* '}' +RecordFieldList = + '{' fields:RecordField* '}' -RecordFieldDef = +RecordField = Attr* Visibility? Name ':' ascribed_type:TypeRef -TupleFieldDefList = - '(' fields:TupleFieldDef* ')' +TupleFieldList = + '(' fields:TupleField* ')' -TupleFieldDef = +TupleField = Attr* Visibility? Name TypeRef -FieldDefList = - RecordFieldDefList -| TupleFieldDefList +FieldList = + RecordFieldList +| TupleFieldList UnionDef = Attr* Visibility? 'union' Name GenericParamList? WhereClause? - RecordFieldDefList + RecordFieldList EnumDef = Attr* Visibility? 'enum' Name GenericParamList? WhereClause? @@ -111,7 +111,7 @@ EnumVariantList = '{' variants:EnumVariant* '}' EnumVariant = - Attr* Visibility? Name FieldDefList ('=' Expr)? + Attr* Visibility? Name FieldList ('=' Expr)? TraitDef = Attr* Visibility? 'unsafe'? 'auto'? 'trait' Name GenericParamList -- cgit v1.2.3 From d549f6164c89bed66432ab5e5ea6e38cc8b4da6b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 17:10:44 +0200 Subject: Simplify codegen --- xtask/src/codegen/gen_syntax.rs | 42 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index f79cd972e..b435d8a9c 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -13,7 +13,7 @@ use quote::{format_ident, quote}; use ungrammar::{Grammar, Rule}; use crate::{ - ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Field, FieldSrc, KindsSrc, KINDS_SRC}, + ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Field, KindsSrc, Valence, KINDS_SRC}, codegen::{self, update, Mode}, project_root, Result, }; @@ -431,7 +431,7 @@ fn pluralize(s: &str) -> String { impl Field { fn is_many(&self) -> bool { - matches!(self, Field::Node { src: FieldSrc::Many(_), .. }) + matches!(self, Field::Node { valence: Valence::Many, .. }) } fn token_kind(&self) -> Option { match self { @@ -476,19 +476,13 @@ impl Field { }; format_ident!("{}_token", name) } - Field::Node { name, src } => match src { - FieldSrc::Shorthand => format_ident!("{}", to_lower_snake_case(name)), - _ => format_ident!("{}", name), - }, + Field::Node { name, .. } => format_ident!("{}", name), } } fn ty(&self) -> proc_macro2::Ident { match self { Field::Token(_) => format_ident!("SyntaxToken"), - Field::Node { name, src } => match src { - FieldSrc::Optional(ty) | FieldSrc::Many(ty) => format_ident!("{}", ty), - FieldSrc::Shorthand => format_ident!("{}", name), - }, + Field::Node { ty, .. } => format_ident!("{}", ty), } } } @@ -550,7 +544,9 @@ fn lower_rule(acc: &mut Vec, grammar: &Grammar, rule: &Rule) { match rule { Rule::Node(node) => { - let field = Field::Node { name: grammar[*node].name.clone(), src: FieldSrc::Shorthand }; + let ty = grammar[*node].name.clone(); + let name = to_lower_snake_case(&ty); + let field = Field::Node { name, ty, valence: Valence::Optional }; acc.push(field); } Rule::Token(token) => { @@ -565,9 +561,9 @@ fn lower_rule(acc: &mut Vec, grammar: &Grammar, rule: &Rule) { } Rule::Rep(inner) => { if let Rule::Node(node) = &**inner { - let name = grammar[*node].name.clone(); - let label = pluralize(&to_lower_snake_case(&name)); - let field = Field::Node { name: label.clone(), src: FieldSrc::Many(name) }; + let ty = grammar[*node].name.clone(); + let name = pluralize(&to_lower_snake_case(&ty)); + let field = Field::Node { name, ty, valence: Valence::Many }; acc.push(field); return; } @@ -582,11 +578,13 @@ fn lower_rule(acc: &mut Vec, grammar: &Grammar, rule: &Rule) { Rule::Node(node) => node, _ => todo!("{:?}", rule), }; + let ty = grammar[*node].name.clone(); let field = Field::Node { name: label.clone(), - src: match &**rule { - Rule::Rep(_) => FieldSrc::Many(grammar[*node].name.clone()), - _ => FieldSrc::Optional(grammar[*node].name.clone()), + ty, + valence: match &**rule { + Rule::Rep(_) => Valence::Many, + _ => Valence::Optional, }, }; acc.push(field); @@ -620,9 +618,9 @@ fn lower_comma_list(acc: &mut Vec, grammar: &Grammar, rule: &Rule) -> boo [comma, Rule::Node(n)] if comma == &**trailing_comma && n == node => (), _ => return false, } - let name = grammar[*node].name.clone(); - let label = pluralize(&to_lower_snake_case(&name)); - let field = Field::Node { name: label.clone(), src: FieldSrc::Many(name) }; + let ty = grammar[*node].name.clone(); + let name = pluralize(&to_lower_snake_case(&ty)); + let field = Field::Node { name, ty, valence: Valence::Many }; acc.push(field); true } @@ -656,7 +654,9 @@ fn extract_enums(ast: &mut AstSrc) { } if to_remove.len() == enm.variants.len() { node.remove_field(to_remove); - node.fields.push(Field::Node { name: enm.name.clone(), src: FieldSrc::Shorthand }); + let ty = enm.name.clone(); + let name = to_lower_snake_case(&ty); + node.fields.push(Field::Node { name, ty, valence: Valence::Optional }); } } } -- cgit v1.2.3 From b043947301e9c386c9131d7008ee90a315f48545 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 17:19:51 +0200 Subject: Simplify --- xtask/src/codegen/gen_syntax.rs | 58 +++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 34 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index b435d8a9c..84ddda5cb 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -13,7 +13,7 @@ use quote::{format_ident, quote}; use ungrammar::{Grammar, Rule}; use crate::{ - ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Field, KindsSrc, Valence, KINDS_SRC}, + ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc, KINDS_SRC}, codegen::{self, update, Mode}, project_root, Result, }; @@ -431,7 +431,7 @@ fn pluralize(s: &str) -> String { impl Field { fn is_many(&self) -> bool { - matches!(self, Field::Node { valence: Valence::Many, .. }) + matches!(self, Field::Node { cardinality: Cardinality::Many, .. }) } fn token_kind(&self) -> Option { match self { @@ -509,7 +509,7 @@ fn lower(grammar: &Grammar) -> AstSrc { } None => { let mut fields = Vec::new(); - lower_rule(&mut fields, grammar, rule); + lower_rule(&mut fields, grammar, None, rule); res.nodes.push(AstNodeSrc { doc: Vec::new(), name, traits: Vec::new(), fields }); } } @@ -537,19 +537,20 @@ fn lower_enum(grammar: &Grammar, rule: &Rule) -> Option> { Some(variants) } -fn lower_rule(acc: &mut Vec, grammar: &Grammar, rule: &Rule) { - if lower_comma_list(acc, grammar, rule) { +fn lower_rule(acc: &mut Vec, grammar: &Grammar, label: Option<&String>, rule: &Rule) { + if lower_comma_list(acc, grammar, label, rule) { return; } match rule { Rule::Node(node) => { let ty = grammar[*node].name.clone(); - let name = to_lower_snake_case(&ty); - let field = Field::Node { name, ty, valence: Valence::Optional }; + let name = label.cloned().unwrap_or_else(|| to_lower_snake_case(&ty)); + let field = Field::Node { name, ty, cardinality: Cardinality::Optional }; acc.push(field); } Rule::Token(token) => { + assert!(label.is_none()); let mut name = grammar[*token].name.clone(); if name != "int_number" && name != "string" { if "[]{}()".contains(&name) { @@ -562,44 +563,33 @@ fn lower_rule(acc: &mut Vec, grammar: &Grammar, rule: &Rule) { Rule::Rep(inner) => { if let Rule::Node(node) = &**inner { let ty = grammar[*node].name.clone(); - let name = pluralize(&to_lower_snake_case(&ty)); - let field = Field::Node { name, ty, valence: Valence::Many }; + let name = label.cloned().unwrap_or_else(|| pluralize(&to_lower_snake_case(&ty))); + let field = Field::Node { name, ty, cardinality: Cardinality::Many }; acc.push(field); return; } todo!("{:?}", rule) } - Rule::Labeled { label, rule } => { - let node = match &**rule { - Rule::Rep(inner) | Rule::Opt(inner) => match &**inner { - Rule::Node(node) => node, - _ => todo!("{:?}", rule), - }, - Rule::Node(node) => node, - _ => todo!("{:?}", rule), - }; - let ty = grammar[*node].name.clone(); - let field = Field::Node { - name: label.clone(), - ty, - valence: match &**rule { - Rule::Rep(_) => Valence::Many, - _ => Valence::Optional, - }, - }; - acc.push(field); + Rule::Labeled { label: l, rule } => { + assert!(label.is_none()); + lower_rule(acc, grammar, Some(l), rule); } Rule::Seq(rules) | Rule::Alt(rules) => { for rule in rules { - lower_rule(acc, grammar, rule) + lower_rule(acc, grammar, label, rule) } } - Rule::Opt(rule) => lower_rule(acc, grammar, rule), + Rule::Opt(rule) => lower_rule(acc, grammar, label, rule), } } // (T (',' T)* ','?) -fn lower_comma_list(acc: &mut Vec, grammar: &Grammar, rule: &Rule) -> bool { +fn lower_comma_list( + acc: &mut Vec, + grammar: &Grammar, + label: Option<&String>, + rule: &Rule, +) -> bool { let rule = match rule { Rule::Seq(it) => it, _ => return false, @@ -619,8 +609,8 @@ fn lower_comma_list(acc: &mut Vec, grammar: &Grammar, rule: &Rule) -> boo _ => return false, } let ty = grammar[*node].name.clone(); - let name = pluralize(&to_lower_snake_case(&ty)); - let field = Field::Node { name, ty, valence: Valence::Many }; + let name = label.cloned().unwrap_or_else(|| pluralize(&to_lower_snake_case(&ty))); + let field = Field::Node { name, ty, cardinality: Cardinality::Many }; acc.push(field); true } @@ -656,7 +646,7 @@ fn extract_enums(ast: &mut AstSrc) { node.remove_field(to_remove); let ty = enm.name.clone(); let name = to_lower_snake_case(&ty); - node.fields.push(Field::Node { name, ty, valence: Valence::Optional }); + node.fields.push(Field::Node { name, ty, cardinality: Cardinality::Optional }); } } } -- cgit v1.2.3 From 8ddbf06e39a13ed3f45e57d77727b7a35cec1749 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 17:24:07 +0200 Subject: Finalize structs grammar --- xtask/src/codegen/rust.ungram | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index ba922af0d..28b50f021 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -83,22 +83,21 @@ StructDef = ) RecordFieldList = - '{' fields:RecordField* '}' + '{' fields:(RecordField (',' RecordField)* ','?)? '}' RecordField = Attr* Visibility? Name ':' ascribed_type:TypeRef TupleFieldList = - '(' fields:TupleField* ')' + '(' fields:(TupleField (',' TupleField)* ','?)? ')' TupleField = - Attr* Visibility? Name TypeRef + Attr* Visibility? TypeRef FieldList = RecordFieldList | TupleFieldList - UnionDef = Attr* Visibility? 'union' Name GenericParamList? WhereClause? RecordFieldList -- cgit v1.2.3 From 1ae4721c9cfea746fce59a816b1c266bf373d6cf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 17:36:46 +0200 Subject: Finalize union grammar --- xtask/src/codegen/rust.ungram | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 28b50f021..65082f3d9 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -16,7 +16,7 @@ Item = | StructDef | TraitDef | TypeAlias -| UnionDef +| Union | Use Module = @@ -98,7 +98,7 @@ FieldList = RecordFieldList | TupleFieldList -UnionDef = +Union = Attr* Visibility? 'union' Name GenericParamList? WhereClause? RecordFieldList @@ -455,7 +455,7 @@ MetaItem = AdtDef = StructDef | EnumDef -| UnionDef +| Union TypeRef = ParenType -- cgit v1.2.3 From 216a5344c8ef3c3e430d2761dc8b1a7b60250a15 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 17:50:40 +0200 Subject: Rename StructDef -> Struct --- xtask/src/codegen/gen_syntax.rs | 2 +- xtask/src/codegen/rust.ungram | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 84ddda5cb..e993a750c 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -307,7 +307,7 @@ fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result { let ast = quote! { #![allow(bad_style, missing_docs, unreachable_pub)] - /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`. + /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT`. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[repr(u16)] pub enum SyntaxKind { diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 65082f3d9..d038c5c5a 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -13,7 +13,7 @@ Item = | MacroCall | Module | StaticDef -| StructDef +| Struct | TraitDef | TypeAlias | Union @@ -76,7 +76,7 @@ TypeAlias = Attr* Visibility? 'default'? 'type' Name GenericParamList? (':' TypeBoundList?)? WhereClause? '=' TypeRef ';' -StructDef = +Struct = Attr* Visibility? 'struct' Name GenericParamList? ( WhereClause? (RecordFieldList | ';') | TupleFieldList WhereClause? ';' @@ -453,7 +453,7 @@ MetaItem = Path '=' AttrInput nested_meta_items:MetaItem* AdtDef = - StructDef + Struct | EnumDef | Union -- cgit v1.2.3 From 609680ef97dbf82c07b6b06e21aa366423892304 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 17:52:53 +0200 Subject: Rename EnumDef -> Enum --- xtask/src/codegen/rust.ungram | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index d038c5c5a..9c6797cb7 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -5,7 +5,7 @@ SourceFile = Item = ConstDef -| EnumDef +| Enum | ExternBlock | ExternCrate | Fn @@ -98,11 +98,7 @@ FieldList = RecordFieldList | TupleFieldList -Union = - Attr* Visibility? 'union' Name GenericParamList? WhereClause? - RecordFieldList - -EnumDef = +Enum = Attr* Visibility? 'enum' Name GenericParamList? WhereClause? variant_list:EnumVariantList @@ -112,6 +108,10 @@ EnumVariantList = EnumVariant = Attr* Visibility? Name FieldList ('=' Expr)? +Union = + Attr* Visibility? 'union' Name GenericParamList? WhereClause? + RecordFieldList + TraitDef = Attr* Visibility? 'unsafe'? 'auto'? 'trait' Name GenericParamList (':' TypeBoundList?)? WhereClause @@ -454,7 +454,7 @@ MetaItem = AdtDef = Struct -| EnumDef +| Enum | Union TypeRef = -- cgit v1.2.3 From 1766aae145c6925a33e427f2fe6ef2a56c301665 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 17:56:53 +0200 Subject: Rename EnumVariant -> Variant --- xtask/src/codegen/rust.ungram | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 9c6797cb7..48d4f4b4a 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -100,12 +100,12 @@ FieldList = Enum = Attr* Visibility? 'enum' Name GenericParamList? WhereClause? - variant_list:EnumVariantList + VariantList -EnumVariantList = - '{' variants:EnumVariant* '}' +VariantList = + '{' Variant* '}' -EnumVariant = +Variant = Attr* Visibility? Name FieldList ('=' Expr)? Union = -- cgit v1.2.3 From 6b25f640a6ad8e2322b5cc0664223b742459336d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 17:57:58 +0200 Subject: Finalize Enum grammar --- xtask/src/codegen/rust.ungram | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 48d4f4b4a..b653c14a7 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -103,7 +103,7 @@ Enum = VariantList VariantList = - '{' Variant* '}' + '{' (Variant (',' Variant)* ','?)? '}' Variant = Attr* Visibility? Name FieldList ('=' Expr)? -- cgit v1.2.3 From 3cd4112bdc924c132cb0eab9d064511a215421ec Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 18:02:20 +0200 Subject: Finalize const&static grammar --- xtask/src/codegen/rust.ungram | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index b653c14a7..ef7c3e50e 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -4,7 +4,7 @@ SourceFile = Item* Item = - ConstDef + Const | Enum | ExternBlock | ExternCrate @@ -12,7 +12,7 @@ Item = | ImplDef | MacroCall | Module -| StaticDef +| Static | Struct | TraitDef | TypeAlias @@ -112,6 +112,14 @@ Union = Attr* Visibility? 'union' Name GenericParamList? WhereClause? RecordFieldList +Const = + Attr* Visibility? 'default'? 'const' (Name | '_') ':' ascribed_type:TypeRef + '=' body:Expr ';' + +Static = + Attr* Visibility? 'static'? 'mut'? Name ':' ascribed_type:TypeRef + '=' body:Expr ';' + TraitDef = Attr* Visibility? 'unsafe'? 'auto'? 'trait' Name GenericParamList (':' TypeBoundList?)? WhereClause @@ -120,14 +128,6 @@ TraitDef = AssocItemList = '{' AssocItem* '}' -ConstDef = - Attr* Visibility? 'default'? 'const' Name ':' ascribed_type:TypeRef - '=' body:Expr ';' - -StaticDef = - Attr* Visibility? 'static'? 'mut'? 'static' Name ':' ascribed_type:TypeRef - '=' body:Expr ';' - ImplDef = Attr* Visibility? 'const'? 'default'? 'unsafe'? 'impl' GenericParamList? '!'? 'for' WhereClause? @@ -475,11 +475,11 @@ TypeRef = AssocItem = Fn | TypeAlias -| ConstDef +| Const | MacroCall ExternItem = - Fn | StaticDef + Fn | Static AttrInput = Literal -- cgit v1.2.3 From b2cdb0b22631a66a00be25ba4b2e9c0b34ff426a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 18:15:08 +0200 Subject: Fix param gramamr --- xtask/src/codegen/rust.ungram | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index ef7c3e50e..4f7f6403e 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -66,8 +66,11 @@ SelfParam = ) Param = - Attr* Pat (':' ascribed_type:TypeRef) -| '...' + Attr* ( + Pat (':' ascribed_type:TypeRef) + | ascribed_type:TypeRef + | '...' + ) RetType = '->' TypeRef -- cgit v1.2.3 From c83467796b6c7365ea4f41900d74444384a9e618 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 18:17:28 +0200 Subject: Finalize Trait grammar --- xtask/src/codegen/rust.ungram | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 4f7f6403e..b30658397 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -14,7 +14,7 @@ Item = | Module | Static | Struct -| TraitDef +| Trait | TypeAlias | Union | Use @@ -123,7 +123,7 @@ Static = Attr* Visibility? 'static'? 'mut'? Name ':' ascribed_type:TypeRef '=' body:Expr ';' -TraitDef = +Trait = Attr* Visibility? 'unsafe'? 'auto'? 'trait' Name GenericParamList (':' TypeBoundList?)? WhereClause AssocItemList -- cgit v1.2.3 From c5798c4d75aa807aec47208a49101bdec3affcca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 18:28:28 +0200 Subject: Finalize impl Grammar --- xtask/src/codegen/rust.ungram | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index b30658397..cda0e8fbb 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -9,7 +9,7 @@ Item = | ExternBlock | ExternCrate | Fn -| ImplDef +| Impl | MacroCall | Module | Static @@ -131,9 +131,12 @@ Trait = AssocItemList = '{' AssocItem* '}' -ImplDef = - Attr* Visibility? 'const'? 'default'? 'unsafe'? 'impl' GenericParamList? '!'? 'for' - WhereClause? +Impl = + Attr* Visibility? + 'default'? 'unsafe'? 'impl' 'const'? GenericParamList? ( + TypeRef + | '!'? TypeRef 'for' TypeRef + ) WhereClause? AssocItemList ParenType = -- cgit v1.2.3 From 917c89c103597d09e95bdee273633f79123dd19e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 18:37:46 +0200 Subject: Finaize item grammar --- xtask/src/codegen/rust.ungram | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index cda0e8fbb..cdd3adf45 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -129,7 +129,13 @@ Trait = AssocItemList AssocItemList = - '{' AssocItem* '}' + '{' Attr* AssocItem* '}' + +AssocItem = + Fn +| TypeAlias +| Const +| MacroCall Impl = Attr* Visibility? @@ -139,6 +145,15 @@ Impl = ) WhereClause? AssocItemList +ExternBlock = + Attr* Abi ExternItemList + +ExternItemList = + '{' Attr* ExternItem* '}' + +ExternItem = + Fn | Static | MacroCall + ParenType = '(' TypeRef ')' @@ -449,12 +464,6 @@ LifetimeArg = ConstArg = Literal | BlockExpr BlockExpr -ExternBlock = - Attr* Abi ExternItemList - -ExternItemList = - '{' extern_items:ExternItem* '}' - MetaItem = Path '=' AttrInput nested_meta_items:MetaItem* @@ -478,15 +487,6 @@ TypeRef = | ImplTraitType | DynTraitType -AssocItem = - Fn -| TypeAlias -| Const -| MacroCall - -ExternItem = - Fn | Static - AttrInput = Literal | TokenTree -- cgit v1.2.3 From 3dce34aaf83c67836c94a526cb90eba8f36e3985 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 18:52:02 +0200 Subject: Introduce GenericParam --- xtask/src/codegen/rust.ungram | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index cdd3adf45..4025f7f96 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -154,6 +154,25 @@ ExternItemList = ExternItem = Fn | Static | MacroCall +GenericParamList = + '<' (GenericParam (',' GenericParam)* ','?)? '>' + +GenericParam = + LifetimeParam +| TypeParam +| ConstParam + +TypeParam = + Attr* Name (':' TypeBoundList?)? + ('=' default_type:TypeRef)? + +ConstParam = + Attr* 'const' Name ':' ascribed_type:TypeRef + ('=' default_val:Expr)? + +LifetimeParam = + Attr* 'lifetime' + ParenType = '(' TypeRef ')' @@ -400,24 +419,6 @@ MacroStmts = Attr = '#' '!'? '[' Path ('=' input:AttrInput)? ']' -GenericParamList = - '<' - TypeParam* - LifetimeParam* - ConstParam* - '>' - -TypeParam = - Attr* Name (':' TypeBoundList?)? - ('=' default_type:TypeRef)? - -ConstParam = - Attr* 'const' Name ':' ascribed_type:TypeRef - ('=' default_val:Expr)? - -LifetimeParam = - Attr* 'lifetime' - TypeBound = 'lifetime' | 'const'? TypeRef -- cgit v1.2.3 From ceca94536c7346287e533cd93467c7dc5bf1ed52 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 18:55:49 +0200 Subject: Finalize visibility grammar --- xtask/src/codegen/rust.ungram | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 4025f7f96..b773eaf8b 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -173,6 +173,14 @@ ConstParam = LifetimeParam = Attr* 'lifetime' +Visibility = + 'pub' ('(' + 'super' + | 'self' + | 'crate' + | 'in' Path + ')')? + ParenType = '(' TypeRef ')' @@ -391,9 +399,6 @@ TupleStructPat = TuplePat = '(' args:Pat* ')' -Visibility = - 'pub' ('(' 'super' | 'self' | 'crate' | 'in' Path ')')? - Name = 'ident' -- cgit v1.2.3 From 71a4d325620ede42e8e6fad55ab95430eb47e1fd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 19:10:22 +0200 Subject: Minor --- xtask/src/codegen/rust.ungram | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index b773eaf8b..74fe65633 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -181,6 +181,12 @@ Visibility = | 'in' Path ')')? +Attr = + '#' '!'? '[' Path ('=' input:AttrInput)? ']' + +AttrInput = + Literal | TokenTree + ParenType = '(' TypeRef ')' @@ -421,9 +427,6 @@ MacroStmts = statements:Stmt* Expr? -Attr = - '#' '!'? '[' Path ('=' input:AttrInput)? ']' - TypeBound = 'lifetime' | 'const'? TypeRef @@ -493,10 +496,6 @@ TypeRef = | ImplTraitType | DynTraitType -AttrInput = - Literal -| TokenTree - Stmt = LetStmt | ExprStmt -- cgit v1.2.3 From 01d6c3836b1c540b14d9c9e17974df64afa978c0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 19:10:46 +0200 Subject: Dead code --- xtask/src/codegen/rust.ungram | 3 --- 1 file changed, 3 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 74fe65633..a97cc80e9 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -473,9 +473,6 @@ LifetimeArg = ConstArg = Literal | BlockExpr BlockExpr -MetaItem = - Path '=' AttrInput nested_meta_items:MetaItem* - AdtDef = Struct | Enum -- cgit v1.2.3 From fcce07d2d1b07cf4578af65b00a243e743a67f05 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 20:16:04 +0200 Subject: Finalize attribute grammar --- xtask/src/codegen/rust.ungram | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index a97cc80e9..42ef2fb82 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -182,10 +182,7 @@ Visibility = ')')? Attr = - '#' '!'? '[' Path ('=' input:AttrInput)? ']' - -AttrInput = - Literal | TokenTree + '#' '!'? '[' Path ('=' Literal | TokenTree)? ']' ParenType = '(' TypeRef ')' -- cgit v1.2.3 From fbe60a2e284035d16c2a1ee743ee88db418689aa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 20:27:39 +0200 Subject: simplify --- xtask/src/codegen/rust.ungram | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 42ef2fb82..8c4f953b0 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -54,9 +54,8 @@ Abi = ParamList = '('( - (Param (',' Param)* ','?)? - | SelfParam ','? - | SelfParam ',' (Param (',' Param)* ','?) + SelfParam + | (SelfParam ',')? (Param (',' Param)* ','?)? )')' SelfParam = -- cgit v1.2.3 From 2e2642efccd5855e4158b01a006e7884a96982bb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 20:51:43 +0200 Subject: Remove TypeAscriptionOwner --- xtask/src/codegen/gen_syntax.rs | 1 - xtask/src/codegen/rust.ungram | 16 ++++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index e993a750c..45b788bdb 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -660,7 +660,6 @@ fn extract_struct_traits(ast: &mut AstSrc) { ("GenericParamsOwner", &["generic_param_list", "where_clause"]), ("TypeBoundsOwner", &["type_bound_list", "colon_token"]), ("ModuleItemOwner", &["items"]), - ("TypeAscriptionOwner", &["ascribed_type"]), ("LoopBodyOwner", &["label", "loop_body"]), ("ArgListOwner", &["arg_list"]), ]; diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 8c4f953b0..e09bc875a 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -61,13 +61,13 @@ ParamList = SelfParam = Attr* ( ('&' 'lifetime'?)? 'mut'? 'self' - | 'mut'? 'self' ':' ascribed_type:TypeRef + | 'mut'? 'self' ':' ty:TypeRef ) Param = Attr* ( - Pat (':' ascribed_type:TypeRef) - | ascribed_type:TypeRef + Pat (':' ty:TypeRef) + | ty:TypeRef | '...' ) @@ -88,7 +88,7 @@ RecordFieldList = '{' fields:(RecordField (',' RecordField)* ','?)? '}' RecordField = - Attr* Visibility? Name ':' ascribed_type:TypeRef + Attr* Visibility? Name ':' ty:TypeRef TupleFieldList = '(' fields:(TupleField (',' TupleField)* ','?)? ')' @@ -115,11 +115,11 @@ Union = RecordFieldList Const = - Attr* Visibility? 'default'? 'const' (Name | '_') ':' ascribed_type:TypeRef + Attr* Visibility? 'default'? 'const' (Name | '_') ':' ty:TypeRef '=' body:Expr ';' Static = - Attr* Visibility? 'static'? 'mut'? Name ':' ascribed_type:TypeRef + Attr* Visibility? 'static'? 'mut'? Name ':' ty:TypeRef '=' body:Expr ';' Trait = @@ -166,7 +166,7 @@ TypeParam = ('=' default_type:TypeRef)? ConstParam = - Attr* 'const' Name ':' ascribed_type:TypeRef + Attr* 'const' Name ':' ty:TypeRef ('=' default_val:Expr)? LifetimeParam = @@ -439,7 +439,7 @@ ExprStmt = Attr* Expr ';' LetStmt = - Attr* 'let' Pat (':' ascribed_type:TypeRef) + Attr* 'let' Pat (':' ty:TypeRef) '=' initializer:Expr ';' Path = -- cgit v1.2.3 From f95f425ae4199e814e6956be1d9bb59a14758c07 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 21:02:55 +0200 Subject: Use ty to access most TypeRefs --- xtask/src/codegen/rust.ungram | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index e09bc875a..375df301f 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -72,11 +72,11 @@ Param = ) RetType = - '->' TypeRef + '->' ty:TypeRef TypeAlias = Attr* Visibility? 'default'? 'type' Name GenericParamList? (':' TypeBoundList?)? WhereClause? - '=' TypeRef ';' + '=' ty:TypeRef ';' Struct = Attr* Visibility? 'struct' Name GenericParamList? ( @@ -94,7 +94,7 @@ TupleFieldList = '(' fields:(TupleField (',' TupleField)* ','?)? ')' TupleField = - Attr* Visibility? TypeRef + Attr* Visibility? ty:TypeRef FieldList = RecordFieldList @@ -184,7 +184,7 @@ Attr = '#' '!'? '[' Path ('=' Literal | TokenTree)? ']' ParenType = - '(' TypeRef ')' + '(' ty:TypeRef ')' TupleType = '(' fields:TypeRef* ')' @@ -196,16 +196,16 @@ PathType = Path PointerType = - '*' ('const' | 'mut') TypeRef + '*' ('const' | 'mut') ty:TypeRef ArrayType = - '[' TypeRef ';' Expr ']' + '[' ty:TypeRef ';' Expr ']' SliceType = - '[' TypeRef ']' + '[' ty:TypeRef ']' ReferenceType = - '&' 'lifetime'? 'mut'? TypeRef + '&' 'lifetime'? 'mut'? ty:TypeRef PlaceholderType = '_' @@ -214,7 +214,7 @@ FnPointerType = Abi 'unsafe'? 'fn' ParamList RetType? ForType = - 'for' GenericParamList TypeRef + 'for' GenericParamList ty:TypeRef ImplTraitType = 'impl' TypeBoundList @@ -302,7 +302,7 @@ TryExpr = Attr* Expr '?' CastExpr = - Attr* Expr 'as' TypeRef + Attr* Expr 'as' ty:TypeRef RefExpr = Attr* '&' ('raw' | 'mut' | 'const') Expr -- cgit v1.2.3 From e0f21133cd03c6160fbc97b70bbd50ccde4fe6d9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 12:02:42 +0200 Subject: Reorder --- xtask/src/codegen/rust.ungram | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 375df301f..1c1dec80a 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -114,6 +114,11 @@ Union = Attr* Visibility? 'union' Name GenericParamList? WhereClause? RecordFieldList +AdtDef = + Struct +| Enum +| Union + Const = Attr* Visibility? 'default'? 'const' (Name | '_') ':' ty:TypeRef '=' body:Expr ';' @@ -183,6 +188,21 @@ Visibility = Attr = '#' '!'? '[' Path ('=' Literal | TokenTree)? ']' +TypeRef = + ParenType +| TupleType +| NeverType +| PathType +| PointerType +| ArrayType +| SliceType +| ReferenceType +| PlaceholderType +| FnPointerType +| ForType +| ImplTraitType +| DynTraitType + ParenType = '(' ty:TypeRef ')' @@ -469,26 +489,6 @@ LifetimeArg = ConstArg = Literal | BlockExpr BlockExpr -AdtDef = - Struct -| Enum -| Union - -TypeRef = - ParenType -| TupleType -| NeverType -| PathType -| PointerType -| ArrayType -| SliceType -| ReferenceType -| PlaceholderType -| FnPointerType -| ForType -| ImplTraitType -| DynTraitType - Stmt = LetStmt | ExprStmt -- cgit v1.2.3 From 08ea2271e8050165d0aaf4c994ed3dd746aff3ba Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 12:06:38 +0200 Subject: Rename TypeRef -> Type The TypeRef name comes from IntelliJ days, where you often have both type *syntax* as well as *semantical* representation of types in scope. And naming both Type is confusing. In rust-analyzer however, we use ast types as `ast::Type`, and have many more semantic counterparts to ast types, so avoiding name clash here is just confusing. --- xtask/src/codegen/gen_syntax.rs | 8 +++++- xtask/src/codegen/rust.ungram | 54 ++++++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 28 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 45b788bdb..d6a72ccc0 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -476,7 +476,13 @@ impl Field { }; format_ident!("{}_token", name) } - Field::Node { name, .. } => format_ident!("{}", name), + Field::Node { name, .. } => { + if name == "type" { + format_ident!("ty") + } else { + format_ident!("{}", name) + } + } } } fn ty(&self) -> proc_macro2::Ident { diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 1c1dec80a..8f0e66278 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -61,22 +61,22 @@ ParamList = SelfParam = Attr* ( ('&' 'lifetime'?)? 'mut'? 'self' - | 'mut'? 'self' ':' ty:TypeRef + | 'mut'? 'self' ':' Type ) Param = Attr* ( - Pat (':' ty:TypeRef) - | ty:TypeRef + Pat (':' Type) + | Type | '...' ) RetType = - '->' ty:TypeRef + '->' Type TypeAlias = Attr* Visibility? 'default'? 'type' Name GenericParamList? (':' TypeBoundList?)? WhereClause? - '=' ty:TypeRef ';' + '=' Type ';' Struct = Attr* Visibility? 'struct' Name GenericParamList? ( @@ -88,13 +88,13 @@ RecordFieldList = '{' fields:(RecordField (',' RecordField)* ','?)? '}' RecordField = - Attr* Visibility? Name ':' ty:TypeRef + Attr* Visibility? Name ':' Type TupleFieldList = '(' fields:(TupleField (',' TupleField)* ','?)? ')' TupleField = - Attr* Visibility? ty:TypeRef + Attr* Visibility? Type FieldList = RecordFieldList @@ -120,11 +120,11 @@ AdtDef = | Union Const = - Attr* Visibility? 'default'? 'const' (Name | '_') ':' ty:TypeRef + Attr* Visibility? 'default'? 'const' (Name | '_') ':' Type '=' body:Expr ';' Static = - Attr* Visibility? 'static'? 'mut'? Name ':' ty:TypeRef + Attr* Visibility? 'static'? 'mut'? Name ':' Type '=' body:Expr ';' Trait = @@ -144,8 +144,8 @@ AssocItem = Impl = Attr* Visibility? 'default'? 'unsafe'? 'impl' 'const'? GenericParamList? ( - TypeRef - | '!'? TypeRef 'for' TypeRef + Type + | '!'? Type 'for' Type ) WhereClause? AssocItemList @@ -168,10 +168,10 @@ GenericParam = TypeParam = Attr* Name (':' TypeBoundList?)? - ('=' default_type:TypeRef)? + ('=' default_type:Type)? ConstParam = - Attr* 'const' Name ':' ty:TypeRef + Attr* 'const' Name ':' Type ('=' default_val:Expr)? LifetimeParam = @@ -188,7 +188,7 @@ Visibility = Attr = '#' '!'? '[' Path ('=' Literal | TokenTree)? ']' -TypeRef = +Type = ParenType | TupleType | NeverType @@ -204,10 +204,10 @@ TypeRef = | DynTraitType ParenType = - '(' ty:TypeRef ')' + '(' Type ')' TupleType = - '(' fields:TypeRef* ')' + '(' fields:Type* ')' NeverType = '!' @@ -216,16 +216,16 @@ PathType = Path PointerType = - '*' ('const' | 'mut') ty:TypeRef + '*' ('const' | 'mut') Type ArrayType = - '[' ty:TypeRef ';' Expr ']' + '[' Type ';' Expr ']' SliceType = - '[' ty:TypeRef ']' + '[' Type ']' ReferenceType = - '&' 'lifetime'? 'mut'? ty:TypeRef + '&' 'lifetime'? 'mut'? Type PlaceholderType = '_' @@ -234,7 +234,7 @@ FnPointerType = Abi 'unsafe'? 'fn' ParamList RetType? ForType = - 'for' GenericParamList ty:TypeRef + 'for' GenericParamList Type ImplTraitType = 'impl' TypeBoundList @@ -322,7 +322,7 @@ TryExpr = Attr* Expr '?' CastExpr = - Attr* Expr 'as' ty:TypeRef + Attr* Expr 'as' Type RefExpr = Attr* '&' ('raw' | 'mut' | 'const') Expr @@ -444,13 +444,13 @@ MacroStmts = Expr? TypeBound = - 'lifetime' | 'const'? TypeRef + 'lifetime' | 'const'? Type TypeBoundList = bounds:TypeBound* WherePred = - ('for' GenericParamList)? ('lifetime' | TypeRef) ':' TypeBoundList + ('for' GenericParamList)? ('lifetime' | Type) ':' TypeBoundList WhereClause = 'where' predicates:WherePred* @@ -459,7 +459,7 @@ ExprStmt = Attr* Expr ';' LetStmt = - Attr* 'let' Pat (':' ty:TypeRef) + Attr* 'let' Pat (':' Type) '=' initializer:Expr ';' Path = @@ -478,10 +478,10 @@ TypeArgList = '>' TypeArg = - TypeRef + Type AssocTypeArg = - NameRef (':' TypeBoundList | '=' TypeRef) + NameRef (':' TypeBoundList | '=' Type) LifetimeArg = 'lifetime' -- cgit v1.2.3 From a6e45c6c69bf258118940941c12d057deb79e60c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 13:22:47 +0200 Subject: Reame PlaceholderType -> InferType --- xtask/src/codegen/rust.ungram | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 8f0e66278..5dee85c2d 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -197,7 +197,7 @@ Type = | ArrayType | SliceType | ReferenceType -| PlaceholderType +| InferType | FnPointerType | ForType | ImplTraitType @@ -206,28 +206,28 @@ Type = ParenType = '(' Type ')' -TupleType = - '(' fields:Type* ')' - NeverType = '!' PathType = Path +TupleType = + '(' fields:(Type (',' Type)* ','?)? ')' + PointerType = '*' ('const' | 'mut') Type +ReferenceType = + '&' 'lifetime'? 'mut'? Type + ArrayType = '[' Type ';' Expr ']' SliceType = '[' Type ']' -ReferenceType = - '&' 'lifetime'? 'mut'? Type - -PlaceholderType = +InferType = '_' FnPointerType = -- cgit v1.2.3 From a6527ed92cb7a1156b8a118f5790d8c5fd69ab41 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 14:40:28 +0200 Subject: "Finalize" Types grammar Note that `for` type is rust-analyzer's own invention. Both the reference and syn allow `for` only for fnptr types, and we allow them everywhere. This needs to be checked with respect to type bounds grammar... --- xtask/src/codegen/rust.ungram | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 5dee85c2d..4015e3890 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -231,7 +231,7 @@ InferType = '_' FnPointerType = - Abi 'unsafe'? 'fn' ParamList RetType? + 'const'? 'async'? 'unsafe'? Abi? 'fn' ParamList RetType? ForType = 'for' GenericParamList Type -- cgit v1.2.3 From b250ae6c55c446f65f85914d98da2cd2c75871f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 15:01:18 +0200 Subject: Finalize TypeBound grammar --- xtask/src/codegen/rust.ungram | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 4015e3890..32f2808ea 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -242,6 +242,13 @@ ImplTraitType = DynTraitType = 'dyn' TypeBoundList +TypeBoundList = + bounds:(TypeBound ('+' TypeBound)* '+'?) + +TypeBound = + 'lifetime' +| '?'? Type + TupleExpr = Attr* '(' Expr* ')' @@ -443,12 +450,6 @@ MacroStmts = statements:Stmt* Expr? -TypeBound = - 'lifetime' | 'const'? Type - -TypeBoundList = - bounds:TypeBound* - WherePred = ('for' GenericParamList)? ('lifetime' | Type) ':' TypeBoundList -- cgit v1.2.3 From c04c06c4bc06852610af9d37673b03a8c041b15c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 15:09:20 +0200 Subject: Finalize WhereClause gramamr --- xtask/src/codegen/rust.ungram | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 32f2808ea..fc665b2b2 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -175,7 +175,13 @@ ConstParam = ('=' default_val:Expr)? LifetimeParam = - Attr* 'lifetime' + Attr* 'lifetime' (':' TypeBoundList?)? + +WhereClause = + 'where' predicates:(WherePred (',' WherePred)* ','?) + +WherePred = + ('for' GenericParamList)? ('lifetime' | Type) ':' TypeBoundList Visibility = 'pub' ('(' @@ -450,12 +456,6 @@ MacroStmts = statements:Stmt* Expr? -WherePred = - ('for' GenericParamList)? ('lifetime' | Type) ':' TypeBoundList - -WhereClause = - 'where' predicates:WherePred* - ExprStmt = Attr* Expr ';' -- cgit v1.2.3 From 4d38b0dce1884dab0da7394ccc979eef0a21076c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 15:18:14 +0200 Subject: Move Stmt Grammar --- xtask/src/codegen/rust.ungram | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index fc665b2b2..8271509cf 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -194,6 +194,17 @@ Visibility = Attr = '#' '!'? '[' Path ('=' Literal | TokenTree)? ']' +Stmt = + LetStmt +| ExprStmt + +LetStmt = + Attr* 'let' Pat (':' Type)? + '=' initializer:Expr ';' + +ExprStmt = + Attr* Expr ';'? + Type = ParenType | TupleType @@ -456,13 +467,6 @@ MacroStmts = statements:Stmt* Expr? -ExprStmt = - Attr* Expr ';' - -LetStmt = - Attr* 'let' Pat (':' Type) - '=' initializer:Expr ';' - Path = (qualifier:Path '::')? segment:PathSegment @@ -490,10 +494,6 @@ LifetimeArg = ConstArg = Literal | BlockExpr BlockExpr -Stmt = - LetStmt -| ExprStmt - Pat = OrPat | ParenPat -- cgit v1.2.3 From a7ca6583fbce6f1bddce7b31ad5bb1fc0665b616 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 15:40:48 +0200 Subject: Handwrite Stmt --- xtask/src/codegen/gen_syntax.rs | 51 +++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 25 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index d6a72ccc0..e3d4269f6 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -153,25 +153,10 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result { quote!(impl ast::#trait_name for #name {}) }); - ( - quote! { - #[pretty_doc_comment_placeholder_workaround] - #[derive(Debug, Clone, PartialEq, Eq, Hash)] - pub enum #name { - #(#variants(#variants),)* - } - - #(#traits)* - }, + let ast_node = if en.name == "Stmt" { + quote! {} + } else { quote! { - #( - impl From<#variants> for #name { - fn from(node: #variants) -> #name { - #name::#variants(node) - } - } - )* - impl AstNode for #name { fn can_cast(kind: SyntaxKind) -> bool { match kind { @@ -196,6 +181,28 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result { } } } + } + }; + + ( + quote! { + #[pretty_doc_comment_placeholder_workaround] + #[derive(Debug, Clone, PartialEq, Eq, Hash)] + pub enum #name { + #(#variants(#variants),)* + } + + #(#traits)* + }, + quote! { + #( + impl From<#variants> for #name { + fn from(node: #variants) -> #name { + #name::#variants(node) + } + } + )* + #ast_node }, ) }) @@ -497,13 +504,7 @@ fn lower(grammar: &Grammar) -> AstSrc { let mut res = AstSrc::default(); res.tokens = vec!["Whitespace".into(), "Comment".into(), "String".into(), "RawString".into()]; - let nodes = grammar - .iter() - .filter(|&node| match grammar[node].rule { - Rule::Node(it) if it == node => false, - _ => true, - }) - .collect::>(); + let nodes = grammar.iter().collect::>(); for &node in &nodes { let name = grammar[node].name.clone(); -- cgit v1.2.3 From d4d986c7f850e1f535bb4c22e3a7f7fba5483628 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 15:46:12 +0200 Subject: Item is a Stmt --- xtask/src/codegen/gen_syntax.rs | 3 +++ xtask/src/codegen/rust.ungram | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index e3d4269f6..d9f358513 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -694,6 +694,9 @@ fn extract_struct_trait(node: &mut AstNodeSrc, trait_name: &str, methods: &[&str fn extract_enum_traits(ast: &mut AstSrc) { for enm in &mut ast.enums { + if enm.name == "Stmt" { + continue; + } let nodes = &ast.nodes; let mut variant_traits = enm .variants diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 8271509cf..17de36d7a 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -197,6 +197,7 @@ Attr = Stmt = LetStmt | ExprStmt +| Item LetStmt = Attr* 'let' Pat (':' Type)? @@ -316,7 +317,6 @@ Label = BlockExpr = Attr* Label '{' - Item* statements:Stmt* Expr? '}' -- cgit v1.2.3 From bfcee63e75d6feb21cafbdf3887e0efd508b6b2e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 16:52:08 +0200 Subject: Work on expressions grammar --- xtask/src/codegen/gen_syntax.rs | 3 + xtask/src/codegen/rust.ungram | 330 +++++++++++++++++++++------------------- 2 files changed, 176 insertions(+), 157 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index d9f358513..90f746e96 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -579,6 +579,9 @@ fn lower_rule(acc: &mut Vec, grammar: &Grammar, label: Option<&String>, r } Rule::Labeled { label: l, rule } => { assert!(label.is_none()); + if l == "op" { + return; + } lower_rule(acc, grammar, Some(l), rule); } Rule::Seq(rules) | Rule::Alt(rules) => { diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 17de36d7a..93195befe 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -115,8 +115,8 @@ Union = RecordFieldList AdtDef = - Struct -| Enum + Enum +| Struct | Union Const = @@ -136,10 +136,10 @@ AssocItemList = '{' Attr* AssocItem* '}' AssocItem = - Fn -| TypeAlias -| Const + Const +| Fn | MacroCall +| TypeAlias Impl = Attr* Visibility? @@ -162,9 +162,9 @@ GenericParamList = '<' (GenericParam (',' GenericParam)* ','?)? '>' GenericParam = - LifetimeParam + ConstParam +| LifetimeParam | TypeParam -| ConstParam TypeParam = Attr* Name (':' TypeBoundList?)? @@ -195,9 +195,9 @@ Attr = '#' '!'? '[' Path ('=' Literal | TokenTree)? ']' Stmt = - LetStmt -| ExprStmt + ExprStmt | Item +| LetStmt LetStmt = Attr* 'let' Pat (':' Type)? @@ -206,189 +206,238 @@ LetStmt = ExprStmt = Attr* Expr ';'? -Type = - ParenType -| TupleType -| NeverType -| PathType -| PointerType -| ArrayType -| SliceType -| ReferenceType -| InferType -| FnPointerType -| ForType -| ImplTraitType -| DynTraitType +Expr = + ArrayExpr +| AwaitExpr +| BinExpr +| BlockExpr +| BoxExpr +| BreakExpr +| CallExpr +| CastExpr +| ContinueExpr +| EffectExpr +| FieldExpr +| ForExpr +| IfExpr +| IndexExpr +| Label +| LambdaExpr +| Literal +| LoopExpr +| MacroCall +| MatchExpr +| MethodCallExpr +| ParenExpr +| PathExpr +| PrefixExpr +| RangeExpr +| RecordExpr +| RefExpr +| ReturnExpr +| TryExpr +| TupleExpr +| WhileExpr -ParenType = - '(' Type ')' +Literal = + Attr* 'int_number' -NeverType = - '!' +PathExpr = + Attr* Path -PathType = - Path +BlockExpr = + '{' + Attr* + statements:Stmt* + Expr? + '}' -TupleType = - '(' fields:(Type (',' Type)* ','?)? ')' +RefExpr = + Attr* '&' ('raw' |'mut' | 'const') Expr -PointerType = - '*' ('const' | 'mut') Type +TryExpr = + Attr* Expr '?' -ReferenceType = - '&' 'lifetime'? 'mut'? Type +EffectExpr = + Attr* Label? ('try' | 'unsafe' | 'async') BlockExpr -ArrayType = - '[' Type ';' Expr ']' +PrefixExpr = + Attr* op:('-' | '!' | '*') Expr -SliceType = - '[' Type ']' +BinExpr = + Attr* + Expr + op:( + '||' | '&&' + | '==' | '!=' | '<=' | '>=' | '<' | '>' + | '+' | '*' | '-' | '/' | '%' | '<<' | '>>' | '^' | '|' | '&' + | '=' | '+=' | '/=' | '*=' | '%=' | '>>=' | '<<=' | '-=' | '|=' | '&=' | '^=' + ) + Expr -InferType = - '_' +CastExpr = + Attr* Expr 'as' Type -FnPointerType = - 'const'? 'async'? 'unsafe'? Abi? 'fn' ParamList RetType? +ParenExpr = + Attr* '(' Attr* Expr ')' -ForType = - 'for' GenericParamList Type +ArrayExpr = + Attr* '[' Attr* ( + (Expr (',' Expr)* ','?)? + | Expr ';' Expr + ) ']' -ImplTraitType = - 'impl' TypeBoundList +IndexExpr = + Attr* Expr '[' Expr ']' -DynTraitType = - 'dyn' TypeBoundList +TupleExpr = + Attr* '(' Attr* (Expr (',' Expr)* ','?)? ')' -TypeBoundList = - bounds:(TypeBound ('+' TypeBound)* '+'?) +RecordExpr = + Path RecordExprFieldList -TypeBound = - 'lifetime' -| '?'? Type +RecordExprFieldList = + '{' + Attr* + fields:(RecordExprField (',' RecordExprField)* ','?) + ('..' spread:Expr)? + '}' -TupleExpr = - Attr* '(' Expr* ')' +RecordExprField = + Attr* NameRef (':' Expr)? -ArrayExpr = - Attr* '[' (Expr* | Expr ';' Expr) ']' +CallExpr = + Attr* Expr ArgList -ParenExpr = - Attr* '(' Expr ')' +ArgList = + '(' args:(Expr (',' Expr)* ','?)? ')' -PathExpr = - Path +MethodCallExpr = + Attr* Expr '.' NameRef TypeArgList? ArgList + +FieldExpr = + Attr* Expr '.' NameRef LambdaExpr = Attr* 'static'? 'async'? 'move'? ParamList RetType? body:Expr IfExpr = - Attr* 'if' Condition + Attr* 'if' Condition BlockExpr + ('else' (IfExpr | BlockExpr))? Condition = 'let' Pat '=' Expr | Expr -EffectExpr = - Attr* Label? ('try' | 'unsafe' | 'async') BlockExpr - LoopExpr = Attr* Label? 'loop' - loop_body:BlockExpr? + loop_body:BlockExpr ForExpr = Attr* Label? 'for' Pat 'in' iterable:Expr - loop_body:BlockExpr? + loop_body:BlockExpr WhileExpr = Attr* Label? 'while' Condition loop_body:BlockExpr? -ContinueExpr = - Attr* 'continue' 'lifetime'? +Label = + 'lifetime' BreakExpr = Attr* 'break' 'lifetime'? Expr? -Label = - 'lifetime' - -BlockExpr = - Attr* Label - '{' - statements:Stmt* - Expr? - '}' +ContinueExpr = + Attr* 'continue' 'lifetime'? -ReturnExpr = - Attr* 'return' Expr +RangeExpr = + Attr* Expr? op:('..' | '..=') Expr? -CallExpr = - Attr* Expr ArgList +MatchExpr = + Attr* 'match' Expr MatchArmList -MethodCallExpr = - Attr* Expr '.' NameRef TypeArgList? ArgList +MatchArmList = + '{' + Attr* + arms:MatchArm* + '}' -ArgList = - '(' args:Expr* ')' +MatchArm = + Attr* Pat guard:MatchGuard? '=>' Expr ','? -FieldExpr = - Attr* Expr '.' NameRef +MatchGuard = + 'if' Expr -IndexExpr = - Attr* '[' ']' +ReturnExpr = + Attr* 'return' Expr? AwaitExpr = Attr* Expr '.' 'await' -TryExpr = - Attr* Expr '?' +BoxExpr = + Attr* 'box' Expr -CastExpr = - Attr* Expr 'as' Type +Type = + ArrayType +| DynTraitType +| FnPointerType +| ForType +| ImplTraitType +| InferType +| NeverType +| ParenType +| PathType +| PointerType +| ReferenceType +| SliceType +| TupleType -RefExpr = - Attr* '&' ('raw' | 'mut' | 'const') Expr +ParenType = + '(' Type ')' -PrefixExpr = - Attr* Expr +NeverType = + '!' -BoxExpr = - Attr* 'box' Expr +PathType = + Path -RangeExpr = - Attr* +TupleType = + '(' fields:(Type (',' Type)* ','?)? ')' -BinExpr = - Attr* +PointerType = + '*' ('const' | 'mut') Type -Literal = - 'int_number' +ReferenceType = + '&' 'lifetime'? 'mut'? Type -MatchExpr = - Attr* 'match' Expr MatchArmList +ArrayType = + '[' Type ';' Expr ']' -MatchArmList = - '{' arms:MatchArm* '}' +SliceType = + '[' Type ']' -MatchArm = - Attr* Pat guard:MatchGuard? '=>' Expr +InferType = + '_' -MatchGuard = - 'if' Expr +FnPointerType = + 'const'? 'async'? 'unsafe'? Abi? 'fn' ParamList RetType? -RecordExpr = - Path RecordExprFieldList +ForType = + 'for' GenericParamList Type -RecordExprFieldList = - '{' - fields:RecordExprField* - ('..' spread:Expr)? - '}' +ImplTraitType = + 'impl' TypeBoundList -RecordExprField = - Attr* NameRef (':' Expr)? +DynTraitType = + 'dyn' TypeBoundList + +TypeBoundList = + bounds:(TypeBound ('+' TypeBound)* '+'?) + +TypeBound = + 'lifetime' +| '?'? Type OrPat = Pat* @@ -510,36 +559,3 @@ Pat = | RangePat | LiteralPat | MacroPat - -Expr = - TupleExpr -| ArrayExpr -| ParenExpr -| PathExpr -| LambdaExpr -| IfExpr -| LoopExpr -| ForExpr -| WhileExpr -| ContinueExpr -| BreakExpr -| Label -| BlockExpr -| ReturnExpr -| MatchExpr -| RecordExpr -| CallExpr -| IndexExpr -| MethodCallExpr -| FieldExpr -| AwaitExpr -| TryExpr -| EffectExpr -| CastExpr -| RefExpr -| PrefixExpr -| RangeExpr -| BinExpr -| Literal -| MacroCall -| BoxExpr -- cgit v1.2.3 From 633aace41108b74fe6c93c5ab04272067db033f9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 17:08:58 +0200 Subject: Rename LambdaExpr -> ClosureExpr --- xtask/src/codegen/gen_syntax.rs | 14 +++++++++++++- xtask/src/codegen/rust.ungram | 16 ++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 90f746e96..059538696 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -579,7 +579,19 @@ fn lower_rule(acc: &mut Vec, grammar: &Grammar, label: Option<&String>, r } Rule::Labeled { label: l, rule } => { assert!(label.is_none()); - if l == "op" { + let manually_implemented = matches!( + l.as_str(), + "lhs" + | "rhs" + | "then_branch" + | "else_branch" + | "start" + | "end" + | "op" + | "index" + | "base" + ); + if manually_implemented { return; } lower_rule(acc, grammar, Some(l), rule); diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 93195befe..aef07cb1e 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -222,7 +222,7 @@ Expr = | IfExpr | IndexExpr | Label -| LambdaExpr +| ClosureExpr | Literal | LoopExpr | MacroCall @@ -266,14 +266,14 @@ PrefixExpr = BinExpr = Attr* - Expr + lhs:Expr op:( '||' | '&&' | '==' | '!=' | '<=' | '>=' | '<' | '>' | '+' | '*' | '-' | '/' | '%' | '<<' | '>>' | '^' | '|' | '&' | '=' | '+=' | '/=' | '*=' | '%=' | '>>=' | '<<=' | '-=' | '|=' | '&=' | '^=' ) - Expr + rhs:Expr CastExpr = Attr* Expr 'as' Type @@ -288,7 +288,7 @@ ArrayExpr = ) ']' IndexExpr = - Attr* Expr '[' Expr ']' + Attr* base:Expr '[' index:Expr ']' TupleExpr = Attr* '(' Attr* (Expr (',' Expr)* ','?)? ')' @@ -318,13 +318,13 @@ MethodCallExpr = FieldExpr = Attr* Expr '.' NameRef -LambdaExpr = +ClosureExpr = Attr* 'static'? 'async'? 'move'? ParamList RetType? body:Expr IfExpr = - Attr* 'if' Condition BlockExpr - ('else' (IfExpr | BlockExpr))? + Attr* 'if' Condition then_branch:BlockExpr + ('else' else_branch:(IfExpr | BlockExpr))? Condition = 'let' Pat '=' Expr @@ -352,7 +352,7 @@ ContinueExpr = Attr* 'continue' 'lifetime'? RangeExpr = - Attr* Expr? op:('..' | '..=') Expr? + Attr* start:Expr? op:('..' | '..=') end:Expr? MatchExpr = Attr* 'match' Expr MatchArmList -- cgit v1.2.3 From 7980a7e19a679e0bc128f2c142609f7f4a197bf6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 18:04:57 +0200 Subject: Minor --- xtask/src/codegen/rust.ungram | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index aef07cb1e..8edabc3b9 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -215,14 +215,13 @@ Expr = | BreakExpr | CallExpr | CastExpr +| ClosureExpr | ContinueExpr | EffectExpr | FieldExpr | ForExpr | IfExpr | IndexExpr -| Label -| ClosureExpr | Literal | LoopExpr | MacroCall @@ -340,7 +339,7 @@ ForExpr = WhileExpr = Attr* Label? 'while' Condition - loop_body:BlockExpr? + loop_body:BlockExpr Label = 'lifetime' @@ -418,13 +417,13 @@ SliceType = '[' Type ']' InferType = - '_' + '_' FnPointerType = - 'const'? 'async'? 'unsafe'? Abi? 'fn' ParamList RetType? + 'const'? 'async'? 'unsafe'? Abi? 'fn' ParamList RetType? ForType = - 'for' GenericParamList Type + 'for' GenericParamList Type ImplTraitType = 'impl' TypeBoundList @@ -433,7 +432,7 @@ DynTraitType = 'dyn' TypeBoundList TypeBoundList = - bounds:(TypeBound ('+' TypeBound)* '+'?) + bounds:(TypeBound ('+' TypeBound)* '+'?) TypeBound = 'lifetime' -- cgit v1.2.3 From 8d28289d0f83225672fc42abcf684364582e73c5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 18:16:08 +0200 Subject: Specify literal tokens --- xtask/src/codegen/gen_syntax.rs | 1 + xtask/src/codegen/rust.ungram | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 059538696..e6231ece2 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -590,6 +590,7 @@ fn lower_rule(acc: &mut Vec, grammar: &Grammar, label: Option<&String>, r | "op" | "index" | "base" + | "value" ); if manually_implemented { return; diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 8edabc3b9..bc1dd6761 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -239,7 +239,13 @@ Expr = | WhileExpr Literal = - Attr* 'int_number' + Attr* value:( + 'int_number' | 'float_number' + | 'string' | 'raw_string' + | 'byte_string' | 'raw_byte_string' + | 'true' | 'false' + | 'char' | 'byte' + ) PathExpr = Attr* Path -- cgit v1.2.3 From 54fd09a9ca567fc79cae53237dfeedc5baeec635 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 18:27:23 +0200 Subject: Finalize Path grammar --- xtask/src/codegen/rust.ungram | 57 +++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 27 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index bc1dd6761..13ad67ca1 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -1,3 +1,33 @@ +Path = + (qualifier:Path '::')? segment:PathSegment + +PathSegment = + '::' | 'crate' | 'self' | 'super' +| (NameRef ('::'? TypeArgList)?) +| NameRef ParamList RetType? +| '<' PathType ('as' PathType)? '>' + +TypeArgList = + '::'? '<' + TypeArg* + LifetimeArg* + AssocTypeArg* + ConstArg* + '>' + +TypeArg = + Type + +AssocTypeArg = + NameRef (':' TypeBoundList | '=' Type) + +LifetimeArg = + 'lifetime' + +ConstArg = + Literal | BlockExpr BlockExpr + + SourceFile = 'shebang'? Attr* @@ -521,33 +551,6 @@ MacroStmts = statements:Stmt* Expr? -Path = - (qualifier:Path '::')? segment:PathSegment - -PathSegment = - '::' | 'crate' | 'self' | 'super' -| '<' NameRef TypeArgList ParamList RetType PathType '>' - -TypeArgList = - '::'? '<' - TypeArg* - LifetimeArg* - AssocTypeArg* - ConstArg* - '>' - -TypeArg = - Type - -AssocTypeArg = - NameRef (':' TypeBoundList | '=' Type) - -LifetimeArg = - 'lifetime' - -ConstArg = - Literal | BlockExpr BlockExpr - Pat = OrPat | ParenPat -- cgit v1.2.3 From 91781c7ce8201b28afd56b4e35eba47e076a8498 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 18:29:29 +0200 Subject: Rename TypeArgList -> GenericArgList --- xtask/src/codegen/rust.ungram | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 13ad67ca1..8acd02f75 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -3,11 +3,11 @@ Path = PathSegment = '::' | 'crate' | 'self' | 'super' -| (NameRef ('::'? TypeArgList)?) +| (NameRef ('::'? GenericArgList)?) | NameRef ParamList RetType? | '<' PathType ('as' PathType)? '>' -TypeArgList = +GenericArgList = '::'? '<' TypeArg* LifetimeArg* @@ -348,7 +348,7 @@ ArgList = '(' args:(Expr (',' Expr)* ','?)? ')' MethodCallExpr = - Attr* Expr '.' NameRef TypeArgList? ArgList + Attr* Expr '.' NameRef GenericArgList? ArgList FieldExpr = Attr* Expr '.' NameRef -- cgit v1.2.3 From d21b5db891d605c7c10118daca1f06c09c14b07e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 18:30:02 +0200 Subject: fixup! Finalize Path grammar --- xtask/src/codegen/rust.ungram | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 8acd02f75..cb4cd49fe 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -3,7 +3,7 @@ Path = PathSegment = '::' | 'crate' | 'self' | 'super' -| (NameRef ('::'? GenericArgList)?) +| NameRef GenericArgList? | NameRef ParamList RetType? | '<' PathType ('as' PathType)? '>' -- cgit v1.2.3 From 040b4c800d5279e77a6825fc90cb2921d26c7f95 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 18:41:37 +0200 Subject: Fix GenericArgs grammar --- xtask/src/codegen/rust.ungram | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index cb4cd49fe..fa18acbb3 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -8,12 +8,13 @@ PathSegment = | '<' PathType ('as' PathType)? '>' GenericArgList = - '::'? '<' - TypeArg* - LifetimeArg* - AssocTypeArg* - ConstArg* - '>' + '::'? '<' (GenericArg (',' GenericArg)* ','?)? '>' + +GenericArg = + TypeArg +| AssocTypeArg +| LifetimeArg +| ConstArg TypeArg = Type @@ -27,7 +28,6 @@ LifetimeArg = ConstArg = Literal | BlockExpr BlockExpr - SourceFile = 'shebang'? Attr* -- cgit v1.2.3 From ddf08daddf7e86d67e8d2e5596f8013b376dd522 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 18:46:07 +0200 Subject: Fix const arguments grammar --- xtask/src/codegen/rust.ungram | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index fa18acbb3..8536b9748 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -26,7 +26,7 @@ LifetimeArg = 'lifetime' ConstArg = - Literal | BlockExpr BlockExpr + Expr SourceFile = 'shebang'? -- cgit v1.2.3 From c1c97b289662501cea93fdc10760e08702ff5950 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 18:49:42 +0200 Subject: Fix leading colon --- xtask/src/codegen/rust.ungram | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 8536b9748..1d8bed0b4 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -2,7 +2,8 @@ Path = (qualifier:Path '::')? segment:PathSegment PathSegment = - '::' | 'crate' | 'self' | 'super' + 'crate' | 'self' | 'super' +| '::' NameRef | NameRef GenericArgList? | NameRef ParamList RetType? | '<' PathType ('as' PathType)? '>' -- cgit v1.2.3 From 45e6052406b73d92e1c05d38a352bd3271b9ccf5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 18:53:10 +0200 Subject: Remove dead code --- xtask/src/codegen/rust.ungram | 3 --- 1 file changed, 3 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index aef07cb1e..b560202a1 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -503,9 +503,6 @@ NameRef = MacroCall = Attr* Path '!' Name? TokenTree ';'? -MacroDef = - Name TokenTree - TokenTree = '(' ')' | '{' '}' | '[' ']' -- cgit v1.2.3 From 572f1c08b6ba43bdd57c5cb99f79a08ecd821c1c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 19:49:26 +0200 Subject: Minor gramamr reorder --- xtask/src/codegen/rust.ungram | 124 +++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 61 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 2e3b45011..27bf563b6 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -1,3 +1,9 @@ +Name = + 'ident' + +NameRef = + 'ident' | 'int_number' + Path = (qualifier:Path '::')? segment:PathSegment @@ -29,6 +35,21 @@ LifetimeArg = ConstArg = Expr +MacroCall = + Attr* Path '!' Name? TokenTree ';'? + +TokenTree = + '(' ')' +| '{' '}' +| '[' ']' + +MacroItems = + Item* + +MacroStmts = + statements:Stmt* + Expr? + SourceFile = 'shebang'? Attr* @@ -475,41 +496,37 @@ TypeBound = 'lifetime' | '?'? Type -OrPat = - Pat* - -ParenPat = - '(' Pat ')' - -RefPat = - '&' 'mut'? Pat - -BoxPat = - 'box' Path - -BindPat = - Attr* 'ref'? 'mut'? Name ('@' Pat)? - -PlaceholderPat = - '_' +Pat = + BindPat +| BoxPat +| DotDotPat +| LiteralPat +| MacroPat +| OrPat +| ParenPat +| PathPat +| PlaceholderPat +| RangePat +| RecordPat +| RefPat +| SlicePat +| TuplePat +| TupleStructPat -DotDotPat = - '..' +LiteralPat = + Literal PathPat = Path -SlicePat = - '[' args:Pat* ']' +PlaceholderPat = + '_' RangePat = - '..' | '..=' - -LiteralPat = - Literal + start:Pat op:('..' | '..=') end:Pat -MacroPat = - MacroCall +RefPat = + '&' 'mut'? Pat RecordPat = Path RecordFieldPatList @@ -522,46 +539,31 @@ RecordFieldPatList = '}' RecordFieldPat = - Attr* NameRef ':' Pat + Attr* (NameRef ':')? Pat -TupleStructPat = - Path '(' args:Pat* ')' +OrPat = + Pat* -TuplePat = - '(' args:Pat* ')' +ParenPat = + '(' Pat ')' -Name = - 'ident' +BoxPat = + 'box' Path -NameRef = - 'ident' | 'int_number' +BindPat = + Attr* 'ref'? 'mut'? Name ('@' Pat)? -MacroCall = - Attr* Path '!' Name? TokenTree ';'? +DotDotPat = + '..' -TokenTree = - '(' ')' | '{' '}' | '[' ']' +SlicePat = + '[' args:Pat* ']' -MacroItems = - Item* +MacroPat = + MacroCall -MacroStmts = - statements:Stmt* - Expr? +TupleStructPat = + Path '(' args:Pat* ')' -Pat = - OrPat -| ParenPat -| RefPat -| BoxPat -| BindPat -| PlaceholderPat -| DotDotPat -| PathPat -| RecordPat -| TupleStructPat -| TuplePat -| SlicePat -| RangePat -| LiteralPat -| MacroPat +TuplePat = + '(' args:Pat* ')' -- cgit v1.2.3 From 14cb96ec0e6be3b99bfe4ea373c058dcbd2a4f79 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 19:54:16 +0200 Subject: Allign RecordPat with RecordExpr --- xtask/src/codegen/rust.ungram | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 27bf563b6..643668863 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -529,16 +529,15 @@ RefPat = '&' 'mut'? Pat RecordPat = - Path RecordFieldPatList + Path RecordPatFieldList -RecordFieldPatList = +RecordPatFieldList = '{' - record_field_pats:RecordFieldPat* - BindPat* + fields:(RecordPatField (',' RecordPatField)* ','?) '..'? '}' -RecordFieldPat = +RecordPatField = Attr* (NameRef ':')? Pat OrPat = -- cgit v1.2.3 From d7f75db90d99216c13000681ff2c4a887451c4b2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 20:04:40 +0200 Subject: Reorder --- xtask/src/codegen/rust.ungram | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 643668863..cb8a307fe 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -516,8 +516,8 @@ Pat = LiteralPat = Literal -PathPat = - Path +BindPat = + Attr* 'ref'? 'mut'? Name ('@' Pat)? PlaceholderPat = '_' @@ -540,29 +540,29 @@ RecordPatFieldList = RecordPatField = Attr* (NameRef ':')? Pat -OrPat = - Pat* +TupleStructPat = + Path '(' args:(Pat (',' Pat)* ','?)? ')' + +TuplePat = + '(' args:(Pat (',' Pat)* ','?)? ')' ParenPat = '(' Pat ')' -BoxPat = - 'box' Path +SlicePat = + '[' args:(Pat (',' Pat)* ','?)? ']' -BindPat = - Attr* 'ref'? 'mut'? Name ('@' Pat)? +PathPat = + Path + +OrPat = + (Pat ('|' Pat)* '|'?) + +BoxPat = + 'box' Pat DotDotPat = '..' -SlicePat = - '[' args:Pat* ']' - MacroPat = MacroCall - -TupleStructPat = - Path '(' args:Pat* ')' - -TuplePat = - '(' args:Pat* ')' -- cgit v1.2.3 From 6791eb9685375da94556bb910ea71f78b08be5ec Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 20:07:21 +0200 Subject: Rename PalceholderPat -> WildcardPat --- xtask/src/codegen/rust.ungram | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index cb8a307fe..7c814240a 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -505,7 +505,7 @@ Pat = | OrPat | ParenPat | PathPat -| PlaceholderPat +| WildcardPat | RangePat | RecordPat | RefPat @@ -519,7 +519,7 @@ LiteralPat = BindPat = Attr* 'ref'? 'mut'? Name ('@' Pat)? -PlaceholderPat = +WildcardPat = '_' RangePat = -- cgit v1.2.3 From 98181087984157e27faba0b969e384f3c62c39d5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 20:09:09 +0200 Subject: Rename BindPat -> IdentPat --- xtask/src/codegen/rust.ungram | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 7c814240a..7685f4f06 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -497,7 +497,7 @@ TypeBound = | '?'? Type Pat = - BindPat + IdentPat | BoxPat | DotDotPat | LiteralPat @@ -516,7 +516,7 @@ Pat = LiteralPat = Literal -BindPat = +IdentPat = Attr* 'ref'? 'mut'? Name ('@' Pat)? WildcardPat = -- cgit v1.2.3 From 81359af733f7b13e0bd2196191f2ab294e1b57aa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 20:22:20 +0200 Subject: Simplify trait gramamr --- xtask/src/codegen/gen_syntax.rs | 2 ++ xtask/src/codegen/rust.ungram | 11 +++++------ 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index e6231ece2..4602ff1d7 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -591,6 +591,8 @@ fn lower_rule(acc: &mut Vec, grammar: &Grammar, label: Option<&String>, r | "index" | "base" | "value" + | "target_type" + | "target_trait" ); if manually_implemented { return; diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 7685f4f06..25d6f7a20 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -194,12 +194,11 @@ AssocItem = | TypeAlias Impl = - Attr* Visibility? - 'default'? 'unsafe'? 'impl' 'const'? GenericParamList? ( - Type - | '!'? Type 'for' Type - ) WhereClause? - AssocItemList + Attr* Visibility? + 'default'? 'unsafe'? 'impl' 'const'? GenericParamList? + ('!'? target_trait:Type 'for')? target_type:Type + WhereClause? + AssocItemList ExternBlock = Attr* Abi ExternItemList -- cgit v1.2.3 From 22d295ceaaee76dbd555cdeedc0ed7578e66279d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 21:45:29 +0200 Subject: Rename DotDotPat -> RestPat --- xtask/src/codegen/rust.ungram | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 25d6f7a20..02f5aa732 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -498,7 +498,7 @@ TypeBound = Pat = IdentPat | BoxPat -| DotDotPat +| RestPat | LiteralPat | MacroPat | OrPat @@ -560,7 +560,7 @@ OrPat = BoxPat = 'box' Pat -DotDotPat = +RestPat = '..' MacroPat = -- cgit v1.2.3 From 675e86becfab2615528152487db49bce1b43bd60 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 21:56:52 +0200 Subject: Section headers --- xtask/src/codegen/rust.ungram | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 02f5aa732..b14801f24 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -1,3 +1,7 @@ +//*************************// +// Names, Paths and Macros // +//*************************// + Name = 'ident' @@ -50,6 +54,10 @@ MacroStmts = statements:Stmt* Expr? +//*************************// +// Items // +//*************************// + SourceFile = 'shebang'? Attr* @@ -245,6 +253,10 @@ Visibility = Attr = '#' '!'? '[' Path ('=' Literal | TokenTree)? ']' +//****************************// +// Statements and Expressions // +//****************************// + Stmt = ExprStmt | Item @@ -434,6 +446,10 @@ AwaitExpr = BoxExpr = Attr* 'box' Expr +//*************************// +// Types // +//*************************// + Type = ArrayType | DynTraitType @@ -495,6 +511,10 @@ TypeBound = 'lifetime' | '?'? Type +//************************// +// Patterns // +//************************// + Pat = IdentPat | BoxPat -- cgit v1.2.3 From b9c6aa9ec9e491160a6ad7c5ec66151bd67b0ecd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 21:58:36 +0200 Subject: Unify naming of tuple fields --- xtask/src/codegen/rust.ungram | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index b14801f24..aca23890c 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram @@ -359,7 +359,7 @@ IndexExpr = Attr* base:Expr '[' index:Expr ']' TupleExpr = - Attr* '(' Attr* (Expr (',' Expr)* ','?)? ')' + Attr* '(' Attr* fields:(Expr (',' Expr)* ','?)? ')' RecordExpr = Path RecordExprFieldList @@ -560,16 +560,16 @@ RecordPatField = Attr* (NameRef ':')? Pat TupleStructPat = - Path '(' args:(Pat (',' Pat)* ','?)? ')' + Path '(' fields:(Pat (',' Pat)* ','?)? ')' TuplePat = - '(' args:(Pat (',' Pat)* ','?)? ')' + '(' fields:(Pat (',' Pat)* ','?)? ')' ParenPat = '(' Pat ')' SlicePat = - '[' args:(Pat (',' Pat)* ','?)? ']' + '[' (Pat (',' Pat)* ','?)? ']' PathPat = Path -- cgit v1.2.3 From bff8dd094958f1abe2fcfe8fe9f15dc7a7e6b53e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 1 Aug 2020 13:47:19 +0200 Subject: Update grammar --- xtask/src/codegen/gen_syntax.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 4602ff1d7..cafad8070 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -10,7 +10,7 @@ use std::{ use proc_macro2::{Punct, Spacing}; use quote::{format_ident, quote}; -use ungrammar::{Grammar, Rule}; +use ungrammar::{rust_grammar, Grammar, Rule}; use crate::{ ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc, KINDS_SRC}, @@ -19,9 +19,7 @@ use crate::{ }; pub fn generate_syntax(mode: Mode) -> Result<()> { - let grammar = include_str!("rust.ungram") - .parse::() - .unwrap_or_else(|err| panic!("\n \x1b[91merror\x1b[0m: {}\n", err)); + let grammar = rust_grammar(); let ast = lower(&grammar); let syntax_kinds_file = project_root().join(codegen::SYNTAX_KINDS); @@ -538,6 +536,7 @@ fn lower_enum(grammar: &Grammar, rule: &Rule) -> Option> { for alternative in alternatives { match alternative { Rule::Node(it) => variants.push(grammar[*it].name.clone()), + Rule::Token(it) if grammar[*it].name == ";" => (), _ => return None, } } @@ -591,8 +590,8 @@ fn lower_rule(acc: &mut Vec, grammar: &Grammar, label: Option<&String>, r | "index" | "base" | "value" - | "target_type" - | "target_trait" + | "trait" + | "self_ty" ); if manually_implemented { return; -- cgit v1.2.3