1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! This module provides functionality for generating variable names and type names so that they
//! adhere to the Rust naming conventions.

const KEYWORDS: &[&str; 52] = &[
    "abstract", "as", "async", "await", "become", "box", "break", "const", "continue", "crate",
    "do", "dyn", "else", "enum", "extern", "false", "final", "fn", "for", "if", "impl", "in",
    "let", "loop", "macro", "match", "mod", "move", "mut", "override", "priv", "pub", "ref",
    "return", "Self", "self", "static", "struct", "super", "trait", "true", "try", "type",
    "typeof", "union", "unsafe", "unsized", "use", "virtual", "where", "while", "yield",
];

/// Struct used as namespace only
pub struct NamingHelper;

impl NamingHelper {
    /// Checks whether the given name is a reserved Rust keyword
    /// ```
    /// use parol::generators::NamingHelper as NmHlp;
    /// assert!(!NmHlp::is_rust_keyword("Type"));
    /// assert!(NmHlp::is_rust_keyword("type"));
    /// ```
    pub fn is_rust_keyword(name: &str) -> bool {
        KEYWORDS.iter().any(|kw| kw == &name)
    }

    /// Checks whether the symbol starts with "r#"
    ///
    /// ```
    /// use parol::generators::naming_helper::NamingHelper;
    ///
    /// assert!(NamingHelper::is_raw_identifier("r#let"));
    /// assert!(!NamingHelper::is_raw_identifier("_let"));
    /// ```
    pub fn is_raw_identifier(name: &str) -> bool {
        name.starts_with("r#")
    }

    /// If the given name is a reserved Rust keyword it is converted into a raw identifier
    /// ```
    /// use parol::generators::NamingHelper as NmHlp;
    /// assert_eq!("Type".to_string(), NmHlp::escape_rust_keyword("Type".to_string()));
    /// assert_eq!("r#type".to_string(), NmHlp::escape_rust_keyword("type".to_string()));
    /// ```
    pub fn escape_rust_keyword(name: String) -> String {
        if Self::is_rust_keyword(&name) {
            format!("r#{}", name)
        } else {
            name
        }
    }

    /// Returns an underscore string if the item is not used.
    pub fn item_unused_indicator(used: bool) -> &'static str {
        if used {
            ""
        } else {
            "_"
        }
    }

    /// Returns an underscore string if the item is not used.
    ///
    /// ```
    /// use parol::generators::naming_helper::NamingHelper;
    ///
    /// assert_eq!(NamingHelper::add_unused_indicator(false, "r#let"), "_let");
    /// assert_eq!(NamingHelper::add_unused_indicator(false, "x"), "_x");
    /// assert_eq!(NamingHelper::add_unused_indicator(true, "x"), "x");
    /// ```
    pub fn add_unused_indicator(used: bool, name: &str) -> String {
        if !used && !Self::is_raw_identifier(name) {
            format!("_{}", name)
        } else if !used {
            name.to_string().replace("r#", "_")
        } else {
            name.to_string()
        }
    }

    ///
    /// Produces a lower snake camel case version of the given name.
    /// Since these names are supposed to be used as identifiers a clash with rust keywords is
    /// detected and prevented.
    ///
    /// ```
    /// use parol::generators::NamingHelper as NmHlp;
    /// assert_eq!("prolog0", NmHlp::to_lower_snake_case("Prolog0"));
    /// assert_eq!("_prolog_0_", NmHlp::to_lower_snake_case("_prolog_0_"));
    /// assert_eq!("_prolog_0_1_3", NmHlp::to_lower_snake_case("_prolog_0_1__3"));
    /// assert_eq!("_", NmHlp::to_lower_snake_case("_____"));
    /// assert_eq!("calc_lst1_1", NmHlp::to_lower_snake_case("calc_lst1_1"));
    /// assert_eq!("nor_op_23", NmHlp::to_lower_snake_case("nor_op_23"));
    /// assert_eq!("r#type", NmHlp::to_lower_snake_case("type"));
    /// ```
    pub fn to_lower_snake_case(name: &str) -> String {
        let mut last_char = '.';
        Self::escape_rust_keyword(name.chars().fold(String::new(), |mut acc, c| {
            if acc.is_empty() {
                acc.push(c.to_lowercase().next().unwrap())
            } else if c == '_' {
                if !acc.ends_with('_') {
                    acc.push('_');
                }
            } else if c.is_ascii_digit() && last_char.is_ascii_alphabetic() {
                acc.push(c.to_lowercase().next().unwrap())
            } else if c.is_ascii_uppercase() {
                if !acc.ends_with('_') {
                    acc.push('_');
                }
                acc.push(c.to_lowercase().next().unwrap())
            } else {
                acc.push(c);
            }
            last_char = c;
            acc
        }))
    }

    ///
    /// Produces an upper camel case version of the given name.
    /// Separated numbers are kept separated.
    /// Camel case compliant input should be preserved.
    ///
    /// ```
    /// use parol::generators::NamingHelper as NmHlp;
    /// assert_eq!("Prolog0", NmHlp::to_upper_camel_case("_prolog_0"));
    /// assert_eq!("Prolog0", NmHlp::to_upper_camel_case("_prolog_0_"));
    /// assert_eq!("Prolog0", NmHlp::to_upper_camel_case("_prolog_0__"));
    /// assert_eq!("Prolog0_1", NmHlp::to_upper_camel_case("_prolog_0__1"));
    /// assert_eq!("Prolog0_1_10_20", NmHlp::to_upper_camel_case("_prolog_0__1_10___20__"));
    /// assert_eq!("Prolog0A", NmHlp::to_upper_camel_case("_prolog_0__a"));
    /// assert_eq!("PrologAA", NmHlp::to_upper_camel_case("_prolog_a_a"));
    /// assert_eq!("PrologItem", NmHlp::to_upper_camel_case("prolog_item"));
    /// assert_eq!("PrologItem", NmHlp::to_upper_camel_case("PrologItem"));
    /// assert_eq!("AA", NmHlp::to_upper_camel_case("_a_a_"));
    /// ```
    pub fn to_upper_camel_case(name: &str) -> String {
        let mut up = true;
        let mut last_char = '.';
        name.chars().fold(String::new(), |mut acc, c| {
            if c == '_' {
                up = true;
            } else if up {
                if last_char.is_ascii_digit() && c.is_ascii_digit() {
                    acc.push('_');
                }
                last_char = c.to_uppercase().next().unwrap();
                acc.push(last_char);
                up = false;
            } else {
                last_char = c;
                acc.push(last_char);
            }
            acc
        })
    }

    /// This is a very restrictive definition of allowed characters in identifiers `parol` allows.
    /// Invalid characters in terminal names, non-terminal names and module names are typically
    /// replaced by the underscore character and can later be removed during name generations using
    /// special casing rules like UpperCamelCase.
    ///
    /// ```
    /// use parol::generators::naming_helper::NamingHelper;
    ///
    /// assert!(NamingHelper::is_valid_name_character('a'));
    /// assert!(NamingHelper::is_valid_name_character('A'));
    /// assert!(NamingHelper::is_valid_name_character('1'));
    /// assert!(NamingHelper::is_valid_name_character('_'));
    ///
    /// assert!(!NamingHelper::is_valid_name_character('-'));
    /// assert!(!NamingHelper::is_valid_name_character(':'));
    /// assert!(!NamingHelper::is_valid_name_character('/'));
    /// ```
    pub fn is_valid_name_character(c: char) -> bool {
        c.is_alphanumeric() || c == '_'
    }

    /// Replaces all invalid characters from the given name with underscores. It is used to process
    /// user given names which are for instance given as command arguments.
    ///
    /// # Examples
    ///
    /// ```
    /// use parol::generators::naming_helper::NamingHelper;
    ///
    /// assert_eq!(NamingHelper::purge_name("test-module"), "test_module");
    /// ```
    pub fn purge_name(name: &str) -> String {
        let mut result = String::with_capacity(name.len());
        for c in name.chars() {
            result.push(if Self::is_valid_name_character(c) {
                c
            } else {
                '_'
            });
        }
        result
    }
}