diff options
author | Aleksey Kladov <[email protected]> | 2018-08-11 09:03:22 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-11 09:03:22 +0100 |
commit | d5119133fc03694c6644cac9e307d1d496fc9bf2 (patch) | |
tree | c8498441aa91abc5af44ed9f978c98dd419495ab | |
parent | 78f41ea707cc8aeaa8d1ba8a7216cb8712f13e98 (diff) |
heck
-rw-r--r-- | crates/libsyntax2/src/ast/generated.rs | 96 | ||||
-rw-r--r-- | crates/libsyntax2/src/ast/generated.rs.tera | 2 | ||||
-rw-r--r-- | crates/libsyntax2/src/grammar.ron | 20 | ||||
-rw-r--r-- | crates/tools/Cargo.toml | 1 | ||||
-rw-r--r-- | 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 c9b587ecb..7a2a9c7d4 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs | |||
@@ -5,6 +5,30 @@ use { | |||
5 | }; | 5 | }; |
6 | 6 | ||
7 | #[derive(Debug, Clone, Copy)] | 7 | #[derive(Debug, Clone, Copy)] |
8 | pub struct ConstItem<R: TreeRoot = Arc<SyntaxRoot>> { | ||
9 | syntax: SyntaxNode<R>, | ||
10 | } | ||
11 | |||
12 | impl<R: TreeRoot> AstNode<R> for ConstItem<R> { | ||
13 | fn cast(syntax: SyntaxNode<R>) -> Option<Self> { | ||
14 | match syntax.kind() { | ||
15 | CONST_ITEM => Some(ConstItem { syntax }), | ||
16 | _ => None, | ||
17 | } | ||
18 | } | ||
19 | fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } | ||
20 | } | ||
21 | |||
22 | impl<R: TreeRoot> ConstItem<R> { | ||
23 | pub fn name(&self) -> Option<Name<R>> { | ||
24 | self.syntax() | ||
25 | .children() | ||
26 | .filter_map(Name::cast) | ||
27 | .next() | ||
28 | } | ||
29 | } | ||
30 | |||
31 | #[derive(Debug, Clone, Copy)] | ||
8 | pub struct Enum<R: TreeRoot = Arc<SyntaxRoot>> { | 32 | pub struct Enum<R: TreeRoot = Arc<SyntaxRoot>> { |
9 | syntax: SyntaxNode<R>, | 33 | syntax: SyntaxNode<R>, |
10 | } | 34 | } |
@@ -76,6 +100,30 @@ impl<R: TreeRoot> Function<R> { | |||
76 | } | 100 | } |
77 | 101 | ||
78 | #[derive(Debug, Clone, Copy)] | 102 | #[derive(Debug, Clone, Copy)] |
103 | pub struct Module<R: TreeRoot = Arc<SyntaxRoot>> { | ||
104 | syntax: SyntaxNode<R>, | ||
105 | } | ||
106 | |||
107 | impl<R: TreeRoot> AstNode<R> for Module<R> { | ||
108 | fn cast(syntax: SyntaxNode<R>) -> Option<Self> { | ||
109 | match syntax.kind() { | ||
110 | MODULE => Some(Module { syntax }), | ||
111 | _ => None, | ||
112 | } | ||
113 | } | ||
114 | fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } | ||
115 | } | ||
116 | |||
117 | impl<R: TreeRoot> Module<R> { | ||
118 | pub fn name(&self) -> Option<Name<R>> { | ||
119 | self.syntax() | ||
120 | .children() | ||
121 | .filter_map(Name::cast) | ||
122 | .next() | ||
123 | } | ||
124 | } | ||
125 | |||
126 | #[derive(Debug, Clone, Copy)] | ||
79 | pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> { | 127 | pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> { |
80 | syntax: SyntaxNode<R>, | 128 | syntax: SyntaxNode<R>, |
81 | } | 129 | } |
@@ -93,6 +141,30 @@ impl<R: TreeRoot> AstNode<R> for Name<R> { | |||
93 | impl<R: TreeRoot> Name<R> {} | 141 | impl<R: TreeRoot> Name<R> {} |
94 | 142 | ||
95 | #[derive(Debug, Clone, Copy)] | 143 | #[derive(Debug, Clone, Copy)] |
144 | pub struct StaticItem<R: TreeRoot = Arc<SyntaxRoot>> { | ||
145 | syntax: SyntaxNode<R>, | ||
146 | } | ||
147 | |||
148 | impl<R: TreeRoot> AstNode<R> for StaticItem<R> { | ||
149 | fn cast(syntax: SyntaxNode<R>) -> Option<Self> { | ||
150 | match syntax.kind() { | ||
151 | STATIC_ITEM => Some(StaticItem { syntax }), | ||
152 | _ => None, | ||
153 | } | ||
154 | } | ||
155 | fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } | ||
156 | } | ||
157 | |||
158 | impl<R: TreeRoot> StaticItem<R> { | ||
159 | pub fn name(&self) -> Option<Name<R>> { | ||
160 | self.syntax() | ||
161 | .children() | ||
162 | .filter_map(Name::cast) | ||
163 | .next() | ||
164 | } | ||
165 | } | ||
166 | |||
167 | #[derive(Debug, Clone, Copy)] | ||
96 | pub struct Struct<R: TreeRoot = Arc<SyntaxRoot>> { | 168 | pub struct Struct<R: TreeRoot = Arc<SyntaxRoot>> { |
97 | syntax: SyntaxNode<R>, | 169 | syntax: SyntaxNode<R>, |
98 | } | 170 | } |
@@ -116,3 +188,27 @@ impl<R: TreeRoot> Struct<R> { | |||
116 | } | 188 | } |
117 | } | 189 | } |
118 | 190 | ||
191 | #[derive(Debug, Clone, Copy)] | ||
192 | pub struct Trait<R: TreeRoot = Arc<SyntaxRoot>> { | ||
193 | syntax: SyntaxNode<R>, | ||
194 | } | ||
195 | |||
196 | impl<R: TreeRoot> AstNode<R> for Trait<R> { | ||
197 | fn cast(syntax: SyntaxNode<R>) -> Option<Self> { | ||
198 | match syntax.kind() { | ||
199 | TRAIT => Some(Trait { syntax }), | ||
200 | _ => None, | ||
201 | } | ||
202 | } | ||
203 | fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } | ||
204 | } | ||
205 | |||
206 | impl<R: TreeRoot> Trait<R> { | ||
207 | pub fn name(&self) -> Option<Name<R>> { | ||
208 | self.syntax() | ||
209 | .children() | ||
210 | .filter_map(Name::cast) | ||
211 | .next() | ||
212 | } | ||
213 | } | ||
214 | |||
diff --git a/crates/libsyntax2/src/ast/generated.rs.tera b/crates/libsyntax2/src/ast/generated.rs.tera index 09630e427..86b8b05d1 100644 --- a/crates/libsyntax2/src/ast/generated.rs.tera +++ b/crates/libsyntax2/src/ast/generated.rs.tera | |||
@@ -12,7 +12,7 @@ pub struct {{ node }}<R: TreeRoot = Arc<SyntaxRoot>> { | |||
12 | impl<R: TreeRoot> AstNode<R> for {{ node }}<R> { | 12 | impl<R: TreeRoot> AstNode<R> for {{ node }}<R> { |
13 | fn cast(syntax: SyntaxNode<R>) -> Option<Self> { | 13 | fn cast(syntax: SyntaxNode<R>) -> Option<Self> { |
14 | match syntax.kind() { | 14 | match syntax.kind() { |
15 | {{ node | upper }} => Some({{ node }} { syntax }), | 15 | {{ node | SCREAM }} => Some({{ node }} { syntax }), |
16 | _ => None, | 16 | _ => None, |
17 | } | 17 | } |
18 | } | 18 | } |
diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index a8916c5c7..c9470d4fa 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron | |||
@@ -234,6 +234,26 @@ Grammar( | |||
234 | ["name", "Name"] | 234 | ["name", "Name"] |
235 | ] | 235 | ] |
236 | ), | 236 | ), |
237 | "Trait": ( | ||
238 | options: [ | ||
239 | ["name", "Name"] | ||
240 | ] | ||
241 | ), | ||
242 | "Module": ( | ||
243 | options: [ | ||
244 | ["name", "Name"] | ||
245 | ] | ||
246 | ), | ||
247 | "ConstItem": ( | ||
248 | options: [ | ||
249 | ["name", "Name"] | ||
250 | ] | ||
251 | ), | ||
252 | "StaticItem": ( | ||
253 | options: [ | ||
254 | ["name", "Name"] | ||
255 | ] | ||
256 | ), | ||
237 | "Name": (), | 257 | "Name": (), |
238 | }, | 258 | }, |
239 | ) | 259 | ) |
diff --git a/crates/tools/Cargo.toml b/crates/tools/Cargo.toml index 856f7d8cb..32a6fbc91 100644 --- a/crates/tools/Cargo.toml +++ b/crates/tools/Cargo.toml | |||
@@ -12,3 +12,4 @@ tera = "0.11" | |||
12 | clap = "2.32.0" | 12 | clap = "2.32.0" |
13 | failure = "0.1.1" | 13 | failure = "0.1.1" |
14 | commandspec = "0.10" | 14 | commandspec = "0.10" |
15 | heck = "0.3.0" | ||
diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index d42d3ecb7..b56be141a 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs | |||
@@ -7,8 +7,10 @@ extern crate tools; | |||
7 | extern crate walkdir; | 7 | extern crate walkdir; |
8 | #[macro_use] | 8 | #[macro_use] |
9 | extern crate commandspec; | 9 | extern crate commandspec; |
10 | extern crate heck; | ||
10 | 11 | ||
11 | use clap::{App, Arg, SubCommand}; | 12 | use clap::{App, Arg, SubCommand}; |
13 | use heck::{CamelCase, ShoutySnakeCase}; | ||
12 | use std::{ | 14 | use std::{ |
13 | collections::HashMap, | 15 | collections::HashMap, |
14 | fs, | 16 | fs, |
@@ -87,18 +89,10 @@ fn render_template(template: &str) -> Result<String> { | |||
87 | .map_err(|e| format_err!("template error: {:?}", e))?; | 89 | .map_err(|e| format_err!("template error: {:?}", e))?; |
88 | tera.register_global_function("concat", Box::new(concat)); | 90 | tera.register_global_function("concat", Box::new(concat)); |
89 | tera.register_filter("camel", |arg, _| { | 91 | tera.register_filter("camel", |arg, _| { |
90 | Ok(arg.as_str().unwrap() | 92 | Ok(arg.as_str().unwrap().to_camel_case().into()) |
91 | .split("_") | 93 | }); |
92 | .flat_map(|word| { | 94 | tera.register_filter("SCREAM", |arg, _| { |
93 | word.chars() | 95 | Ok(arg.as_str().unwrap().to_shouty_snake_case().into()) |
94 | .next().unwrap() | ||
95 | .to_uppercase() | ||
96 | .chain( | ||
97 | word.chars().skip(1).flat_map(|c| c.to_lowercase()) | ||
98 | ) | ||
99 | }) | ||
100 | .collect::<String>() | ||
101 | .into()) | ||
102 | }); | 96 | }); |
103 | let ret = tera | 97 | let ret = tera |
104 | .render("grammar", &grammar) | 98 | .render("grammar", &grammar) |