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
201
#![allow(dead_code)]
pub mod asm;
pub mod lua;
pub mod rust;
pub mod shell;
pub mod sql;

use std::collections::BTreeSet;
use std::hash::{Hash, Hasher};

pub const SEPARATORS: [char; 1] = ['_'];
pub const QUOTES: [char; 3] = ['\'', '"', '`'];

type MultiLine = bool;
type Float = bool;

#[derive(Default, Clone, Copy, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum TokenType {
    Comment(MultiLine),
    Function,
    Keyword,
    Literal,
    Numeric(Float),
    Punctuation(char),
    Special,
    Str(char),
    Type,
    Whitespace(char),
    #[default]
    Unknown,
}
impl std::fmt::Debug for TokenType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut name = String::new();
        match &self {
            TokenType::Comment(multiline) => {
                name.push_str("Comment");
                {
                    if *multiline {
                        name.push_str(" MultiLine");
                    } else {
                        name.push_str(" SingleLine");
                    }
                }
            }
            TokenType::Function => name.push_str("Function"),
            TokenType::Keyword => name.push_str("Keyword"),
            TokenType::Literal => name.push_str("Literal"),
            TokenType::Numeric(float) => {
                name.push_str("Numeric");
                if *float {
                    name.push_str(" Float");
                } else {
                    name.push_str(" Integer");
                }
            }
            TokenType::Punctuation(_) => name.push_str("Punctuation"),
            TokenType::Special => name.push_str("Special"),
            TokenType::Str(quote) => {
                name.push_str("Str ");
                name.push(*quote);
            }
            TokenType::Type => name.push_str("Type"),
            TokenType::Whitespace(c) => {
                name.push_str("Whitespace");
                match c {
                    ' ' => name.push_str(" Space"),
                    '\t' => name.push_str(" Tab"),
                    '\n' => name.push_str(" New Line"),
                    _ => (),
                };
            }
            TokenType::Unknown => name.push_str("Unknown"),
        };
        write!(f, "{name}")
    }
}
impl From<char> for TokenType {
    fn from(c: char) -> Self {
        match c {
            c if c.is_whitespace() => TokenType::Whitespace(c),
            c if QUOTES.contains(&c) => TokenType::Str(c),
            c if c.is_numeric() => TokenType::Numeric(false),
            c if c.is_alphabetic() || SEPARATORS.contains(&c) => TokenType::Literal,
            c if c.is_ascii_punctuation() => TokenType::Punctuation(c),
            _ => TokenType::Unknown,
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
/// Rules for highlighting.
pub struct Syntax {
    pub language: &'static str,
    pub case_sensitive: bool,
    pub comment: &'static str,
    pub comment_multiline: [&'static str; 2],
    pub keywords: BTreeSet<&'static str>,
    pub types: BTreeSet<&'static str>,
    pub special: BTreeSet<&'static str>,
}
impl Default for Syntax {
    fn default() -> Self {
        Syntax::rust()
    }
}
impl Hash for Syntax {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.language.hash(state);
    }
}
impl Syntax {
    pub fn new(language: &'static str) -> Self {
        Syntax {
            language,
            ..Default::default()
        }
    }
    pub fn with_case_sensitive(self, case_sensitive: bool) -> Self {
        Syntax {
            case_sensitive,
            ..self
        }
    }
    pub fn with_comment(self, comment: &'static str) -> Self {
        Syntax { comment, ..self }
    }
    pub fn with_comment_multiline(self, comment_multiline: [&'static str; 2]) -> Self {
        Syntax {
            comment_multiline,
            ..self
        }
    }
    pub fn with_keywords<T: Into<BTreeSet<&'static str>>>(self, keywords: T) -> Self {
        Syntax {
            keywords: keywords.into(),
            ..self
        }
    }
    pub fn with_types<T: Into<BTreeSet<&'static str>>>(self, types: T) -> Self {
        Syntax {
            types: types.into(),
            ..self
        }
    }
    pub fn with_special<T: Into<BTreeSet<&'static str>>>(self, special: T) -> Self {
        Syntax {
            special: special.into(),
            ..self
        }
    }

    #[must_use]
    pub fn language(&self) -> &str {
        self.language
    }
    #[must_use]
    pub fn comment(&self) -> &str {
        self.comment
    }
    #[must_use]
    pub fn is_keyword(&self, word: &str) -> bool {
        if self.case_sensitive {
            self.keywords.contains(&word)
        } else {
            self.keywords.contains(word.to_ascii_uppercase().as_str())
        }
    }
    #[must_use]
    pub fn is_type(&self, word: &str) -> bool {
        if self.case_sensitive {
            self.types.contains(&word)
        } else {
            self.types.contains(word.to_ascii_uppercase().as_str())
        }
    }
    #[must_use]
    pub fn is_special(&self, word: &str) -> bool {
        if self.case_sensitive {
            self.special.contains(&word)
        } else {
            self.special.contains(word.to_ascii_uppercase().as_str())
        }
    }
}

impl Syntax {
    #[must_use]
    pub fn simple(comment: &'static str) -> Self {
        Syntax {
            language: "",
            case_sensitive: false,
            comment,
            comment_multiline: [comment; 2],
            keywords: BTreeSet::new(),
            types: BTreeSet::new(),
            special: BTreeSet::new(),
        }
    }
}