From d5119133fc03694c6644cac9e307d1d496fc9bf2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Aug 2018 11:03:22 +0300 Subject: [PATCH] heck --- crates/libsyntax2/src/ast/generated.rs | 96 +++++++++++++++++++++ crates/libsyntax2/src/ast/generated.rs.tera | 2 +- crates/libsyntax2/src/grammar.ron | 20 +++++ crates/tools/Cargo.toml | 1 + crates/tools/src/main.rs | 18 ++-- 5 files changed, 124 insertions(+), 13 deletions(-) diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index c9b587ecbb..7a2a9c7d46 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -4,6 +4,30 @@ use { SyntaxKind::*, }; +#[derive(Debug, Clone, Copy)] +pub struct ConstItem> { + syntax: SyntaxNode, +} + +impl AstNode for ConstItem { + fn cast(syntax: SyntaxNode) -> Option { + match syntax.kind() { + CONST_ITEM => Some(ConstItem { syntax }), + _ => None, + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } +} + +impl ConstItem { + pub fn name(&self) -> Option> { + self.syntax() + .children() + .filter_map(Name::cast) + .next() + } +} + #[derive(Debug, Clone, Copy)] pub struct Enum> { syntax: SyntaxNode, @@ -75,6 +99,30 @@ impl Function { } } +#[derive(Debug, Clone, Copy)] +pub struct Module> { + syntax: SyntaxNode, +} + +impl AstNode for Module { + fn cast(syntax: SyntaxNode) -> Option { + match syntax.kind() { + MODULE => Some(Module { syntax }), + _ => None, + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } +} + +impl Module { + pub fn name(&self) -> Option> { + self.syntax() + .children() + .filter_map(Name::cast) + .next() + } +} + #[derive(Debug, Clone, Copy)] pub struct Name> { syntax: SyntaxNode, @@ -92,6 +140,30 @@ impl AstNode for Name { impl Name {} +#[derive(Debug, Clone, Copy)] +pub struct StaticItem> { + syntax: SyntaxNode, +} + +impl AstNode for StaticItem { + fn cast(syntax: SyntaxNode) -> Option { + match syntax.kind() { + STATIC_ITEM => Some(StaticItem { syntax }), + _ => None, + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } +} + +impl StaticItem { + pub fn name(&self) -> Option> { + self.syntax() + .children() + .filter_map(Name::cast) + .next() + } +} + #[derive(Debug, Clone, Copy)] pub struct Struct> { syntax: SyntaxNode, @@ -116,3 +188,27 @@ impl Struct { } } +#[derive(Debug, Clone, Copy)] +pub struct Trait> { + syntax: SyntaxNode, +} + +impl AstNode for Trait { + fn cast(syntax: SyntaxNode) -> Option { + match syntax.kind() { + TRAIT => Some(Trait { syntax }), + _ => None, + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } +} + +impl Trait { + pub fn name(&self) -> Option> { + self.syntax() + .children() + .filter_map(Name::cast) + .next() + } +} + diff --git a/crates/libsyntax2/src/ast/generated.rs.tera b/crates/libsyntax2/src/ast/generated.rs.tera index 09630e427a..86b8b05d1a 100644 --- a/crates/libsyntax2/src/ast/generated.rs.tera +++ b/crates/libsyntax2/src/ast/generated.rs.tera @@ -12,7 +12,7 @@ pub struct {{ node }}> { impl AstNode for {{ node }} { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { - {{ node | upper }} => Some({{ node }} { syntax }), + {{ node | SCREAM }} => Some({{ node }} { syntax }), _ => None, } } diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index a8916c5c72..c9470d4fad 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron @@ -234,6 +234,26 @@ Grammar( ["name", "Name"] ] ), + "Trait": ( + options: [ + ["name", "Name"] + ] + ), + "Module": ( + options: [ + ["name", "Name"] + ] + ), + "ConstItem": ( + options: [ + ["name", "Name"] + ] + ), + "StaticItem": ( + options: [ + ["name", "Name"] + ] + ), "Name": (), }, ) diff --git a/crates/tools/Cargo.toml b/crates/tools/Cargo.toml index 856f7d8cbd..32a6fbc916 100644 --- a/crates/tools/Cargo.toml +++ b/crates/tools/Cargo.toml @@ -12,3 +12,4 @@ tera = "0.11" clap = "2.32.0" failure = "0.1.1" commandspec = "0.10" +heck = "0.3.0" diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index d42d3ecb73..b56be141ab 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs @@ -7,8 +7,10 @@ extern crate tools; extern crate walkdir; #[macro_use] extern crate commandspec; +extern crate heck; use clap::{App, Arg, SubCommand}; +use heck::{CamelCase, ShoutySnakeCase}; use std::{ collections::HashMap, fs, @@ -87,18 +89,10 @@ fn render_template(template: &str) -> Result { .map_err(|e| format_err!("template error: {:?}", e))?; tera.register_global_function("concat", Box::new(concat)); tera.register_filter("camel", |arg, _| { - Ok(arg.as_str().unwrap() - .split("_") - .flat_map(|word| { - word.chars() - .next().unwrap() - .to_uppercase() - .chain( - word.chars().skip(1).flat_map(|c| c.to_lowercase()) - ) - }) - .collect::() - .into()) + Ok(arg.as_str().unwrap().to_camel_case().into()) + }); + tera.register_filter("SCREAM", |arg, _| { + Ok(arg.as_str().unwrap().to_shouty_snake_case().into()) }); let ret = tera .render("grammar", &grammar)