replace heck with just ascii transformation

This commit is contained in:
Aleksey Kladov 2019-08-19 15:08:36 +03:00
parent 8704a74cd5
commit 58e9d7ef6f
3 changed files with 33 additions and 8 deletions

1
Cargo.lock generated
View file

@ -1290,7 +1290,6 @@ name = "ra_tools"
version = "0.1.0"
dependencies = [
"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)",
"proc-macro2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -12,5 +12,4 @@ clap = "2.32.0"
quote = "1.0.2"
proc-macro2 = "1.0.1"
ron = "0.5.1"
heck = "0.3.0"
serde = { version = "1.0.0", features = ["derive"] }

View file

@ -5,7 +5,6 @@ use std::{
process::{Command, Stdio},
};
use heck::{ShoutySnakeCase, SnakeCase};
use proc_macro2::{Punct, Spacing};
use quote::{format_ident, quote};
use ron;
@ -38,7 +37,7 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
let name = format_ident!("{}", name);
let adt = if variants.is_empty() {
let kind = format_ident!("{}", name.to_string().to_shouty_snake_case());
let kind = format_ident!("{}", to_upper_snake_case(&name.to_string()));
quote! {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct #name {
@ -61,7 +60,7 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
} else {
let kinds = variants
.iter()
.map(|name| format_ident!("{}", name.to_string().to_shouty_snake_case()))
.map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))
.collect::<Vec<_>>();
quote! {
@ -122,7 +121,7 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
let options = ast_node.options.iter().map(|attr| {
let method_name = match attr {
Attr::Type(t) => format_ident!("{}", t.to_snake_case()),
Attr::Type(t) => format_ident!("{}", to_lower_snake_case(&t)),
Attr::NameType(n, _) => format_ident!("{}", n),
};
let ty = match attr {
@ -189,14 +188,14 @@ fn generate_syntax_kinds(grammar: &Grammar) -> Result<String> {
let full_keywords_values = &grammar.keywords;
let full_keywords =
full_keywords_values.iter().map(|kw| format_ident!("{}_KW", kw.to_shouty_snake_case()));
full_keywords_values.iter().map(|kw| format_ident!("{}_KW", to_upper_snake_case(&kw)));
let all_keywords_values =
grammar.keywords.iter().chain(grammar.contextual_keywords.iter()).collect::<Vec<_>>();
let all_keywords_idents = all_keywords_values.iter().map(|kw| format_ident!("{}", kw));
let all_keywords = all_keywords_values
.iter()
.map(|name| format_ident!("{}_KW", name.to_shouty_snake_case()))
.map(|name| format_ident!("{}_KW", to_upper_snake_case(&name)))
.collect::<Vec<_>>();
let literals =
@ -325,3 +324,31 @@ enum Attr {
Type(String),
NameType(String, String),
}
fn to_upper_snake_case(s: &str) -> String {
let mut buf = String::with_capacity(s.len());
let mut prev_is_upper = None;
for c in s.chars() {
if c.is_ascii_uppercase() && prev_is_upper == Some(false) {
buf.push('_')
}
prev_is_upper = Some(c.is_ascii_uppercase());
buf.push(c.to_ascii_uppercase());
}
buf
}
fn to_lower_snake_case(s: &str) -> String {
let mut buf = String::with_capacity(s.len());
let mut prev_is_upper = None;
for c in s.chars() {
if c.is_ascii_uppercase() && prev_is_upper == Some(false) {
buf.push('_')
}
prev_is_upper = Some(c.is_ascii_uppercase());
buf.push(c.to_ascii_lowercase());
}
buf
}