Echo Writes Code

syntax.rs

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
enum Language {
	Cmake,
	Cpp,
	Css,
	Html,
	Json,
	Lua,
	Markdown,
	Rust,
}

pub(crate) fn highlight(filename: &str, content: &str) -> String {
	// Our strategy is basically to determine the language through a heuristic, then highlight it if
	// we find a match.
	// If there's no match, or we're not sure, we don't highlight to be safe.

	match detect_language(filename, content) {
		Some(Cmake) => highlight_cmake(content),
		Some(Cpp) => highlight_cpp(content),
		Some(Css) => highlight_css(content),
		Some(Html) => highlight_html(content),
		Some(Json) => highlight_json(content),
		Some(Lua) => highlight_lua(content),
		Some(Markdown) => highlight_markdown(content),
		Some(Rust) => highlight_rust(content),
		None => content.to_string(),
	}
}

fn detect_language(filename: &str, content: &str) -> Option<Language> {
	if is_cmake(filename, content) {
		Some(Language::Cmake)
	} else if is_cpp(filename, content) {
		Some(Language::Cpp)
	} else if is_css(filename, content) {
		Some(Language::Css)
	} else if is_html(filename, content) {
		Some(Language::Html)
	} else if is_json(filename, content) {
		Some(Language::Json)
	} else if is_lua(filename, content) {
		Some(Language::Lua)
	} else if is_markdown(filename, content) {
		Some(Language::Markdown)
	} else if is_rust(filename, content) {
		Some(Language::Rust)
	} else {
		None
	}
}

fn is_cmake(filename: &str, _content: &str) -> bool {
	filename == "CMakeLists.txt" || filename.ends_with(".cmake")
}

fn is_cpp(filename: &str, _content: &str) -> bool {
	&[".hpp", ".inl", ".cpp"]
		.iter()
		.any(|&extension| filename.ends_with(extension))
}

fn is_css(filename: &str, _content: &str) -> bool {
	filename.ends_with(".css")
}

fn is_html(filename: &str, _content: &str) -> bool {
	filename.ends_with(".html")
}

fn is_json(filename: &str, _content: &str) -> bool {
	filename.ends_with(".json")
}

fn is_lua(filename: &str, _content: &str) -> bool {
	filename.ends_with(".lua")
}

fn is_markdown(filename: &str, _content: &str) -> bool {
	&[".md", ".markdown"]
		.iter()
		.any(|&extension| filename.ends_with(extension))
}

fn is_rust(filename: &str, _content: &str) -> bool {
	filename.ends_with(".rs")
}

fn highlight_cmake(content: &str) -> String {
	content.to_string()
}

fn highlight_cpp(content: &str) -> String {
	content.to_string()
}

fn highlight_css(content: &str) -> String {
	content.to_string()
}

fn highlight_html(content: &str) -> String {
	content.to_string()
}

fn highlight_json(content: &str) -> String {
	content.to_string()
}

fn highlight_lua(content: &str) -> String {
	content.to_string()
}

fn highlight_markdown(content: &str) -> String {
	content.to_string()
}

fn highlight_rust(content: &str) -> String {
	let keywords = vec![
		// Strict keywords
		"as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn", "for",
		"if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref", "return",
		"self", "Self", "static", "struct", "super", "trait", "true", "type", "unsafe", "use", "where",
		"while",

		// Strict keywords (2018)
		"async", "await", "dyn",

		// Reserved keywords
		"abstract", "become", "box", "do", "final", "macro", "override", "priv", "typeof", "unsized",
		"virtual", "yield",

		// Reserved keywords (2018)
		"try",

		// Weak keywords
		"macro_rules", "union", "'static",
	];

	let keyword_characters = {
		let mut buffer = ascii::ascii_uppercase();
		buffer.append(ascii::ascii_lowercase());
		buffer.append(ascii::ascii_digits());
		buffer.push(ascii::characters::LOW_LINE);
		buffer
	};

	let strings = vec![
		KscRegion { open: "\"", close: "\"", skip: Some("\\\"") },
	];

	let comments = vec![
		KscRegion { open: "/*", close: "*/", skip: None },
		KscRegion { open: "//", close: "\n", skip: None },
		KscRegion { open: "//", close: "\r\n", skip: None },
	];

	highlight_ksc(content, keywords, keyword_characters, strings, comments)
}

struct KscRegion {
	open: String,
	close: String,
	skip: Option<String>,
}

enum KscState {
	CopyBytes,
	InKeyword(Vec<u8>),
	InString(Vec<u8>),
	InComment(Vec<u8>),
}

// ksc - keywords, strings, comments
fn highlight_ksc(content: &str, keywords: &[&str], keyword_characters: &[u8], strings: &[Region], comments: &[Region]) -> String {
	let mut result = Vec::new();
	let mut index = 0;
	let mut state = KscState::CopyBytes;

	loop {
		let byte = content.as_bytes()[index];

		match state {
			KscState::CopyBytes => {
				let maybe_keyword = keyword_characters.contains(byte);
				let maybe_string = strings.iter().any(|r| r.

				if maybe_keyword {
					state = KscState::InKeyword(vec![byte]);
				} else
			},
			KscState::InKeyword => {
			},
			KscState::InString => {
			},
			KscState::InComment {
			},
		}
	}
}