mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
use quote! macro to generate grammar
We already use syn"e elsewhere (transitively), so it make sense to cut down on the number of technologies and get rid of tera
This commit is contained in:
parent
d545a5c75c
commit
8cefdb5527
4 changed files with 181 additions and 40 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1362,6 +1362,7 @@ name = "ra_tools"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
|
@ -263,7 +263,7 @@ Grammar(
|
||||||
"SourceFile": (
|
"SourceFile": (
|
||||||
traits: [ "ModuleItemOwner", "FnDefOwner" ],
|
traits: [ "ModuleItemOwner", "FnDefOwner" ],
|
||||||
collections: [
|
collections: [
|
||||||
["modules", "Module"],
|
("modules", "Module"),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
"FnDef": (
|
"FnDef": (
|
||||||
|
@ -286,7 +286,7 @@ Grammar(
|
||||||
"DocCommentsOwner"
|
"DocCommentsOwner"
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
"NamedFieldDefList": (collections: [["fields", "NamedFieldDef"]]),
|
"NamedFieldDefList": (collections: [("fields", "NamedFieldDef")]),
|
||||||
"NamedFieldDef": (
|
"NamedFieldDef": (
|
||||||
traits: [
|
traits: [
|
||||||
"VisibilityOwner",
|
"VisibilityOwner",
|
||||||
|
@ -296,7 +296,7 @@ Grammar(
|
||||||
"TypeAscriptionOwner"
|
"TypeAscriptionOwner"
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
"PosFieldDefList": (collections: [["fields", "PosFieldDef"]]),
|
"PosFieldDefList": (collections: [("fields", "PosFieldDef")]),
|
||||||
"PosFieldDef": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]),
|
"PosFieldDef": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]),
|
||||||
"EnumDef": ( traits: [
|
"EnumDef": ( traits: [
|
||||||
"VisibilityOwner",
|
"VisibilityOwner",
|
||||||
|
@ -305,7 +305,7 @@ Grammar(
|
||||||
"AttrsOwner",
|
"AttrsOwner",
|
||||||
"DocCommentsOwner"
|
"DocCommentsOwner"
|
||||||
], options: [["variant_list", "EnumVariantList"]] ),
|
], options: [["variant_list", "EnumVariantList"]] ),
|
||||||
"EnumVariantList": ( collections: [["variants", "EnumVariant"]] ),
|
"EnumVariantList": ( collections: [("variants", "EnumVariant")] ),
|
||||||
"EnumVariant": ( traits: ["NameOwner", "DocCommentsOwner", "AttrsOwner"], options: ["Expr"] ),
|
"EnumVariant": ( traits: ["NameOwner", "DocCommentsOwner", "AttrsOwner"], options: ["Expr"] ),
|
||||||
"TraitDef": (
|
"TraitDef": (
|
||||||
traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner", "TypeParamsOwner", "TypeBoundsOwner"],
|
traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner", "TypeParamsOwner", "TypeBoundsOwner"],
|
||||||
|
@ -316,7 +316,7 @@ Grammar(
|
||||||
options: [ "ItemList" ]
|
options: [ "ItemList" ]
|
||||||
),
|
),
|
||||||
"ItemList": (
|
"ItemList": (
|
||||||
collections: [["impl_items", "ImplItem"]],
|
collections: [("impl_items", "ImplItem")],
|
||||||
traits: [ "FnDefOwner", "ModuleItemOwner" ],
|
traits: [ "FnDefOwner", "ModuleItemOwner" ],
|
||||||
),
|
),
|
||||||
"ConstDef": (
|
"ConstDef": (
|
||||||
|
@ -355,7 +355,7 @@ Grammar(
|
||||||
"ImplBlock": (options: ["ItemList"], traits: ["TypeParamsOwner", "AttrsOwner"]),
|
"ImplBlock": (options: ["ItemList"], traits: ["TypeParamsOwner", "AttrsOwner"]),
|
||||||
|
|
||||||
"ParenType": (options: ["TypeRef"]),
|
"ParenType": (options: ["TypeRef"]),
|
||||||
"TupleType": ( collections: [["fields", "TypeRef"]] ),
|
"TupleType": ( collections: [("fields", "TypeRef")] ),
|
||||||
"NeverType": (),
|
"NeverType": (),
|
||||||
"PathType": (options: ["Path"]),
|
"PathType": (options: ["Path"]),
|
||||||
"PointerType": (options: ["TypeRef"]),
|
"PointerType": (options: ["TypeRef"]),
|
||||||
|
@ -405,10 +405,10 @@ Grammar(
|
||||||
),
|
),
|
||||||
|
|
||||||
"TupleExpr": (
|
"TupleExpr": (
|
||||||
collections: [["exprs", "Expr"]]
|
collections: [("exprs", "Expr")]
|
||||||
),
|
),
|
||||||
"ArrayExpr": (
|
"ArrayExpr": (
|
||||||
collections: [["exprs", "Expr"]]
|
collections: [("exprs", "Expr")]
|
||||||
),
|
),
|
||||||
"ParenExpr": (options: ["Expr"]),
|
"ParenExpr": (options: ["Expr"]),
|
||||||
"PathExpr": (options: ["Path"]),
|
"PathExpr": (options: ["Path"]),
|
||||||
|
@ -449,7 +449,7 @@ Grammar(
|
||||||
options: [ "Expr", "MatchArmList" ],
|
options: [ "Expr", "MatchArmList" ],
|
||||||
),
|
),
|
||||||
"MatchArmList": (
|
"MatchArmList": (
|
||||||
collections: [ ["arms", "MatchArm"] ],
|
collections: [ ("arms", "MatchArm") ],
|
||||||
traits: [ "AttrsOwner" ]
|
traits: [ "AttrsOwner" ]
|
||||||
),
|
),
|
||||||
"MatchArm": (
|
"MatchArm": (
|
||||||
|
@ -457,13 +457,13 @@ Grammar(
|
||||||
[ "guard", "MatchGuard" ],
|
[ "guard", "MatchGuard" ],
|
||||||
"Expr",
|
"Expr",
|
||||||
],
|
],
|
||||||
collections: [ [ "pats", "Pat" ] ],
|
collections: [ ("pats", "Pat") ],
|
||||||
traits: [ "AttrsOwner" ]
|
traits: [ "AttrsOwner" ]
|
||||||
),
|
),
|
||||||
"MatchGuard": (options: ["Expr"]),
|
"MatchGuard": (options: ["Expr"]),
|
||||||
"StructLit": (options: ["Path", "NamedFieldList"]),
|
"StructLit": (options: ["Path", "NamedFieldList"]),
|
||||||
"NamedFieldList": (
|
"NamedFieldList": (
|
||||||
collections: [ ["fields", "NamedField"] ],
|
collections: [ ("fields", "NamedField") ],
|
||||||
options: [["spread", "Expr"]]
|
options: [["spread", "Expr"]]
|
||||||
),
|
),
|
||||||
"NamedField": (options: ["NameRef", "Expr"]),
|
"NamedField": (options: ["NameRef", "Expr"]),
|
||||||
|
@ -532,8 +532,8 @@ Grammar(
|
||||||
"StructPat": ( options: ["FieldPatList", "Path"] ),
|
"StructPat": ( options: ["FieldPatList", "Path"] ),
|
||||||
"FieldPatList": (
|
"FieldPatList": (
|
||||||
collections: [
|
collections: [
|
||||||
["field_pats", "FieldPat"],
|
("field_pats", "FieldPat"),
|
||||||
["bind_pats", "BindPat"],
|
("bind_pats", "BindPat"),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
"FieldPat": (
|
"FieldPat": (
|
||||||
|
@ -542,9 +542,9 @@ Grammar(
|
||||||
),
|
),
|
||||||
"TupleStructPat": (
|
"TupleStructPat": (
|
||||||
options: ["Path"],
|
options: ["Path"],
|
||||||
collections: [["args", "Pat"]],
|
collections: [("args", "Pat")],
|
||||||
),
|
),
|
||||||
"TuplePat": ( collections: [["args", "Pat"]] ),
|
"TuplePat": ( collections: [("args", "Pat")] ),
|
||||||
"SlicePat": (),
|
"SlicePat": (),
|
||||||
"RangePat": (),
|
"RangePat": (),
|
||||||
"LiteralPat": (options: ["Literal"]),
|
"LiteralPat": (options: ["Literal"]),
|
||||||
|
@ -575,8 +575,8 @@ Grammar(
|
||||||
"TokenTree": (),
|
"TokenTree": (),
|
||||||
"TypeParamList": (
|
"TypeParamList": (
|
||||||
collections: [
|
collections: [
|
||||||
["type_params", "TypeParam" ],
|
("type_params", "TypeParam" ),
|
||||||
["lifetime_params", "LifetimeParam" ],
|
("lifetime_params", "LifetimeParam" ),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
"TypeParam": ( traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner", "DefaultTypeParamOwner"] ),
|
"TypeParam": ( traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner", "DefaultTypeParamOwner"] ),
|
||||||
|
@ -590,7 +590,7 @@ Grammar(
|
||||||
),
|
),
|
||||||
"TypeBoundList": (
|
"TypeBoundList": (
|
||||||
collections: [
|
collections: [
|
||||||
["bounds", "TypeBound"],
|
("bounds", "TypeBound"),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
"WherePred": (
|
"WherePred": (
|
||||||
|
@ -603,7 +603,7 @@ Grammar(
|
||||||
),
|
),
|
||||||
"WhereClause": (
|
"WhereClause": (
|
||||||
collections: [
|
collections: [
|
||||||
["predicates", "WherePred"],
|
("predicates", "WherePred"),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
"ExprStmt": (
|
"ExprStmt": (
|
||||||
|
@ -627,7 +627,7 @@ Grammar(
|
||||||
"Block": (
|
"Block": (
|
||||||
options: [ "Expr" ],
|
options: [ "Expr" ],
|
||||||
collections: [
|
collections: [
|
||||||
["statements", "Stmt"],
|
("statements", "Stmt"),
|
||||||
],
|
],
|
||||||
traits: [
|
traits: [
|
||||||
"AttrsOwner",
|
"AttrsOwner",
|
||||||
|
@ -636,7 +636,7 @@ Grammar(
|
||||||
"ParamList": (
|
"ParamList": (
|
||||||
options: [ "SelfParam" ],
|
options: [ "SelfParam" ],
|
||||||
collections: [
|
collections: [
|
||||||
["params", "Param"]
|
("params", "Param"),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
"SelfParam": (
|
"SelfParam": (
|
||||||
|
@ -663,14 +663,14 @@ Grammar(
|
||||||
traits: ["NameOwner"],
|
traits: ["NameOwner"],
|
||||||
),
|
),
|
||||||
"UseTreeList": (
|
"UseTreeList": (
|
||||||
collections: [["use_trees", "UseTree"]]
|
collections: [("use_trees", "UseTree")]
|
||||||
),
|
),
|
||||||
"ExternCrateItem": (
|
"ExternCrateItem": (
|
||||||
options: ["NameRef", "Alias"],
|
options: ["NameRef", "Alias"],
|
||||||
),
|
),
|
||||||
"ArgList": (
|
"ArgList": (
|
||||||
collections: [
|
collections: [
|
||||||
["args", "Expr"]
|
("args", "Expr"),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
"Path": (
|
"Path": (
|
||||||
|
@ -683,9 +683,9 @@ Grammar(
|
||||||
options: [ "NameRef", "TypeArgList" ]
|
options: [ "NameRef", "TypeArgList" ]
|
||||||
),
|
),
|
||||||
"TypeArgList": (collections: [
|
"TypeArgList": (collections: [
|
||||||
["type_args", "TypeArg"],
|
("type_args", "TypeArg"),
|
||||||
["lifetime_args", "LifetimeArg"],
|
("lifetime_args", "LifetimeArg"),
|
||||||
["assoc_type_args", "AssocTypeArg"],
|
("assoc_type_args", "AssocTypeArg"),
|
||||||
]),
|
]),
|
||||||
"TypeArg": (options: ["TypeRef"]),
|
"TypeArg": (options: ["TypeRef"]),
|
||||||
"AssocTypeArg": (options: ["NameRef", "TypeRef"]),
|
"AssocTypeArg": (options: ["NameRef", "TypeRef"]),
|
||||||
|
@ -698,7 +698,7 @@ Grammar(
|
||||||
"MacroStmts" : (
|
"MacroStmts" : (
|
||||||
options: [ "Expr" ],
|
options: [ "Expr" ],
|
||||||
collections: [
|
collections: [
|
||||||
["statements", "Stmt"],
|
("statements", "Stmt"),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
|
@ -12,4 +12,5 @@ itertools = "0.8.0"
|
||||||
clap = "2.32.0"
|
clap = "2.32.0"
|
||||||
quote = "1.0.2"
|
quote = "1.0.2"
|
||||||
ron = "0.5.1"
|
ron = "0.5.1"
|
||||||
|
heck = "0.3.0"
|
||||||
serde = { version = "1.0.0", features = ["derive"] }
|
serde = { version = "1.0.0", features = ["derive"] }
|
||||||
|
|
|
@ -1,27 +1,162 @@
|
||||||
use std::{collections::BTreeMap, fs, path::Path};
|
use std::{
|
||||||
|
collections::BTreeMap,
|
||||||
|
fs,
|
||||||
|
io::Write,
|
||||||
|
path::Path,
|
||||||
|
process::{Command, Stdio},
|
||||||
|
};
|
||||||
|
|
||||||
use quote::quote;
|
use heck::{ShoutySnakeCase, SnakeCase};
|
||||||
|
use quote::{format_ident, quote};
|
||||||
use ron;
|
use ron;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{project_root, Mode, Result, AST, GRAMMAR};
|
use crate::{project_root, Mode, Result, AST, GRAMMAR, SYNTAX_KINDS};
|
||||||
|
|
||||||
pub fn generate(mode: Mode) -> Result<()> {
|
pub fn generate(mode: Mode) -> Result<()> {
|
||||||
let grammar = project_root().join(GRAMMAR);
|
let grammar = project_root().join(GRAMMAR);
|
||||||
// let syntax_kinds = project_root().join(SYNTAX_KINDS);
|
let grammar: Grammar = {
|
||||||
let ast = project_root().join(AST);
|
let text = fs::read_to_string(grammar)?;
|
||||||
generate_ast(&grammar, &ast, mode)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_ast(grammar_src: &Path, dst: &Path, mode: Mode) -> Result<()> {
|
|
||||||
let src: Grammar = {
|
|
||||||
let text = fs::read_to_string(grammar_src)?;
|
|
||||||
ron::de::from_str(&text)?
|
ron::de::from_str(&text)?
|
||||||
};
|
};
|
||||||
eprintln!("{:#?}", src);
|
|
||||||
|
let _syntax_kinds = project_root().join(SYNTAX_KINDS);
|
||||||
|
let _ast = project_root().join(AST);
|
||||||
|
|
||||||
|
let ast = generate_ast(&grammar)?;
|
||||||
|
println!("{}", ast);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn generate_ast(grammar: &Grammar) -> Result<String> {
|
||||||
|
let nodes = grammar.ast.iter().map(|(name, ast_node)| {
|
||||||
|
let variants =
|
||||||
|
ast_node.variants.iter().map(|var| format_ident!("{}", var)).collect::<Vec<_>>();
|
||||||
|
let name = format_ident!("{}", name);
|
||||||
|
|
||||||
|
let kinds = if variants.is_empty() { vec![name.clone()] } else { variants.clone() }
|
||||||
|
.into_iter()
|
||||||
|
.map(|name| format_ident!("{}", name.to_string().to_shouty_snake_case()))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let variants = if variants.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
let kind_enum = format_ident!("{}Kind", name);
|
||||||
|
Some(quote!(
|
||||||
|
pub enum #kind_enum {
|
||||||
|
#(#variants(#variants),)*
|
||||||
|
}
|
||||||
|
|
||||||
|
#(
|
||||||
|
impl From<#variants> for #name {
|
||||||
|
fn from(node: #variants) -> #name {
|
||||||
|
#name { syntax: node.syntax }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
|
||||||
|
impl #name {
|
||||||
|
pub fn kind(&self) -> #kind_enum {
|
||||||
|
let syntax = self.syntax.clone();
|
||||||
|
match syntax.kind() {
|
||||||
|
#(
|
||||||
|
#kinds =>
|
||||||
|
#kind_enum::#variants(#variants { syntax }),
|
||||||
|
)*
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
))
|
||||||
|
};
|
||||||
|
|
||||||
|
let traits = ast_node.traits.iter().map(|trait_name| {
|
||||||
|
let trait_name = format_ident!("{}", trait_name);
|
||||||
|
quote!(impl ast::#trait_name for #name {})
|
||||||
|
});
|
||||||
|
|
||||||
|
let collections = ast_node.collections.iter().map(|(name, kind)| {
|
||||||
|
let method_name = format_ident!("{}", name);
|
||||||
|
let kind = format_ident!("{}", kind);
|
||||||
|
quote! {
|
||||||
|
pub fn #method_name(&self) -> AstChildren<#kind> {
|
||||||
|
AstChildren::new(&self.syntax)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let options = ast_node.options.iter().map(|attr| {
|
||||||
|
let method_name = match attr {
|
||||||
|
Attr::Type(t) => format_ident!("{}", t.to_snake_case()),
|
||||||
|
Attr::NameType(n, _) => format_ident!("{}", n),
|
||||||
|
};
|
||||||
|
let ty = match attr {
|
||||||
|
Attr::Type(t) | Attr::NameType(_, t) => format_ident!("{}", t),
|
||||||
|
};
|
||||||
|
quote! {
|
||||||
|
pub fn #method_name(&self) -> Option<#ty> {
|
||||||
|
AstChildren::new(&self.syntax).next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct #name {
|
||||||
|
pub(crate) syntax: SyntaxNode,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AstNode for #name {
|
||||||
|
fn can_cast(kind: SyntaxKind) -> bool {
|
||||||
|
match kind {
|
||||||
|
#(#kinds)|* => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||||
|
if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None }
|
||||||
|
}
|
||||||
|
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||||
|
}
|
||||||
|
|
||||||
|
#variants
|
||||||
|
|
||||||
|
#(#traits)*
|
||||||
|
|
||||||
|
impl #name {
|
||||||
|
#(#collections)*
|
||||||
|
#(#options)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let ast = quote! {
|
||||||
|
use crate::{
|
||||||
|
SyntaxNode, SyntaxKind::{self, *},
|
||||||
|
ast::{self, AstNode, AstChildren},
|
||||||
|
};
|
||||||
|
|
||||||
|
#(#nodes)*
|
||||||
|
};
|
||||||
|
|
||||||
|
let pretty = reformat(ast)?;
|
||||||
|
Ok(pretty)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reformat(text: impl std::fmt::Display) -> Result<String> {
|
||||||
|
let mut rustfmt = Command::new("rustfmt")
|
||||||
|
.arg("--config-path")
|
||||||
|
.arg(project_root().join("rustfmt.toml"))
|
||||||
|
.stdin(Stdio::piped())
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.spawn()?;
|
||||||
|
write!(rustfmt.stdin.take().unwrap(), "{}", text)?;
|
||||||
|
let output = rustfmt.wait_with_output()?;
|
||||||
|
let stdout = String::from_utf8(output.stdout)?;
|
||||||
|
Ok(stdout)
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct Grammar {
|
struct Grammar {
|
||||||
single_byte_tokens: Vec<(String, String)>,
|
single_byte_tokens: Vec<(String, String)>,
|
||||||
|
@ -35,10 +170,14 @@ struct Grammar {
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct AstNode {
|
struct AstNode {
|
||||||
|
#[serde(default)]
|
||||||
|
#[serde(rename = "enum")]
|
||||||
|
variants: Vec<String>,
|
||||||
|
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
traits: Vec<String>,
|
traits: Vec<String>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
collections: Vec<Attr>,
|
collections: Vec<(String, String)>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
options: Vec<Attr>,
|
options: Vec<Attr>,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue