shellharden 4.3.2

The corrective bash syntax highlighter
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * Copyright 2016 - 2021 Andreas Nordal
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

use crate::situation::Horizon;
use crate::situation::WhatNow;
use crate::situation::flush;
use crate::situation::if_needed;
use crate::situation::pop;
use crate::situation::push;
use crate::situation::COLOR_HERE;
use crate::situation::COLOR_KWD;
use crate::situation::COLOR_SQ;
use crate::situation::COLOR_VAR;
use crate::situation::COLOR_ESC;

use crate::microparsers::prefixlen;
use crate::microparsers::predlen;
use crate::microparsers::identifierlen;
use crate::microparsers::is_whitespace;
use crate::microparsers::is_word;

use crate::commonstrcmd::QuotingCtx;
use crate::commonstrcmd::CommonStrCmdResult;
use crate::commonstrcmd::common_str_cmd;

use crate::sitcase::SitCase;
use crate::sitdeclare::SitDeclare;
use crate::sitfor::SitFor;
use crate::sitcmd::SitNormal;
use crate::sitcmd::SitCmd;
use crate::sitcomment::SitComment;
use crate::sitextent::push_extent;
use crate::sitextent::push_replaceable;
use crate::sitmagic::push_magic;
use crate::sitrvalue::SitLvalue;
use crate::sitstrdq::SitStrDq;
use crate::sitstrphantom::SitStrPhantom;
use crate::sitstrsqesc::SitStrSqEsc;
use crate::sittest::SitTest;
use crate::situntilbyte::SitUntilByte;
use crate::sitvec::SitVec;

pub fn keyword_or_command(
	end_trigger :u16,
	horizon: Horizon,
	i: usize,
) -> WhatNow {
	if horizon.input[i] == b'#' {
		return push_comment(i);
	}
	if horizon.input[i] == b'\\' {
		return push_extent(COLOR_ESC, i, 2);
	}
	let (found, len) = find_lvalue(&horizon.input[i..]);
	if found == Tri::Maybe && (i > 0 || horizon.is_lengthenable) {
		return flush(i);
	}
	if found == Tri::Yes {
		return push((i, len, None), Box::new(SitLvalue { end_trigger }));
	}
	let len = predlen(is_word, &horizon.input[i..]);
	let len = if len != 0 { len } else { prefixlen(&horizon.input[i..], b"((") };
	if i + len == horizon.input.len() && (i > 0 || horizon.is_lengthenable) {
		return flush(i);
	}
	let word = &horizon.input[i..i+len];
	match word {
		b"(" => push(
			(i, 1, None),
			Box::new(SitNormal {
				end_trigger: u16::from(b')'),
				end_replace: None,
			}),
		),
		b"((" => push_magic(i, 1, b')'),
		b"[[" => push_magic(i, 1, b']'),
		b"case" => push((i, len, None), Box::new(SitCase {})),
		b"for" |
		b"select" => push((i, len, None), Box::new(SitFor {})),
		b"!" |
		b"do" |
		b"done" |
		b"elif" |
		b"else" |
		b"export" |
		b"fi" |
		b"function" |
		b"if" |
		b"then" |
		b"until" |
		b"while" |
		b"{" |
		b"}" => push_extent(COLOR_KWD, i, len),
		b"declare" |
		b"local" |
		b"readonly" => push((i, len, None), Box::new(SitDeclare { end_trigger })),
		b"[" |
		b"test" if predlen(|x| x == b' ', &horizon.input[i + len ..]) == 1 => {
			push((i, len + 1, None), Box::new(SitTest { end_trigger }))
		},
		_ => push((i, 0, None), Box::new(SitCmd { end_trigger })),
	}
}

pub fn common_arg(
	end_trigger :u16,
	horizon :Horizon,
	i :usize,
) -> Option<WhatNow> {
	if let Some(res) = find_command_enders(horizon, i) {
		return Some(res);
	}
	common_expr(end_trigger, horizon, i)
}

pub fn common_cmd(
	end_trigger :u16,
	horizon :Horizon,
	i :usize,
) -> Option<WhatNow> {
	if let Some(res) = find_command_enders(horizon, i) {
		return Some(res);
	}
	common_token(end_trigger, horizon, i)
}

pub fn common_expr(
	end_trigger :u16,
	horizon :Horizon,
	i :usize,
) -> Option<WhatNow> {
	if horizon.input[i] == b'#' {
		return Some(push_comment(i));
	}
	common_token(end_trigger, horizon, i)
}

pub fn common_token(
	end_trigger :u16,
	horizon :Horizon,
	i :usize,
) -> Option<WhatNow> {
	if let Some(res) = find_usual_suspects(end_trigger, horizon, i, true) {
		return Some(res);
	}
	match common_str_cmd(horizon, i, QuotingCtx::Need) {
		CommonStrCmdResult::None => None,
		CommonStrCmdResult::Some(x) => Some(x),
		CommonStrCmdResult::OnlyWithQuotes(_) => Some(push(
			(i, 0, Some(b"\"")),
			Box::new(SitStrPhantom {
				cmd_end_trigger: end_trigger,
			}),
		)),
	}
}

pub fn common_cmd_quoting_unneeded(
	end_trigger :u16,
	horizon :Horizon,
	i :usize,
) -> Option<WhatNow> {
	if let Some(res) = find_command_enders(horizon, i) {
		return Some(res);
	}
	common_token_quoting_unneeded(end_trigger, horizon, i)
}

pub fn common_expr_quoting_unneeded(
	end_trigger :u16,
	horizon :Horizon,
	i :usize,
) -> Option<WhatNow> {
	if horizon.input[i] == b'#' {
		return Some(push_comment(i));
	}
	common_token_quoting_unneeded(end_trigger, horizon, i)
}

pub fn common_token_quoting_unneeded(
	end_trigger :u16,
	horizon :Horizon,
	i :usize,
) -> Option<WhatNow> {
	if let Some(res) = find_usual_suspects(end_trigger, horizon, i, false) {
		return Some(res);
	}
	match common_str_cmd(horizon, i, QuotingCtx::Dontneed) {
		CommonStrCmdResult::None => None,
		CommonStrCmdResult::Some(x) => Some(x),
		CommonStrCmdResult::OnlyWithQuotes(x) => {
			let (_, len, alt) = x.transform;
			if let Some(replacement) = alt {
				if replacement.len() >= len {
					#[allow(clippy::collapsible_if)] // Could be expanded.
					if horizon.input[i] == b'`' {
						return Some(push(
							(i, 1, None),
							Box::new(SitNormal {
								end_trigger: u16::from(b'`'),
								end_replace: None,
							}),
						));
					}
				}
			}
			Some(x)
		}
	}
}

// Does not pop on eof → Callers must use flush_or_pop
fn find_command_enders(
	horizon :Horizon,
	i :usize,
) -> Option<WhatNow> {
	let plen = prefixlen(&horizon.input[i..], b">&");
	if plen == 2 {
		return Some(flush(i + 2));
	}
	if i + plen == horizon.input.len() && (i > 0 || horizon.is_lengthenable) {
		return Some(flush(i));
	}
	let a = horizon.input[i];
	if a == b'\n' || a == b';' || a == b'|' || a == b'&' {
		return Some(pop(i, 0, None));
	}
	None
}

fn find_usual_suspects(
	end_trigger :u16,
	horizon :Horizon,
	i :usize,
	quoting_needed : bool,
) -> Option<WhatNow> {
	let a = horizon.input[i];
	if u16::from(a) == end_trigger {
		return Some(pop(i, 0, None));
	}
	if a == b'\'' {
		return Some(push(
			(i, 1, None),
			Box::new(SitUntilByte {
				until: b'\'',
				color: COLOR_SQ,
			}),
		));
	}
	if a == b'\"' {
		return Some(push((i, 1, None), Box::new(SitStrDq::new())));
	}
	if a == b'$' {
		if i+1 >= horizon.input.len() {
			if i > 0 || horizon.is_lengthenable {
				return Some(flush(i));
			}
			return None;
		}
		let b = horizon.input[i+1];
		if b == b'\'' {
			return Some(push((i, 2, None), Box::new(SitStrSqEsc {})));
		} else if b == b'*' {
			// $* → "$@" but not "$*" → "$@"
			return Some(push_replaceable(COLOR_VAR, i, 2, if_needed(quoting_needed, b"\"$@\"")));
		}
	}
	let (ate, delimiter) = find_heredoc(&horizon.input[i ..]);
	if i + ate == horizon.input.len() {
		if i > 0 || horizon.is_lengthenable {
			return Some(flush(i));
		}
	} else if !delimiter.is_empty() {
		return Some(push(
			(i, ate, None),
			Box::new(SitVec {
				terminator: delimiter,
				color: COLOR_HERE,
			}),
		));
	} else if ate > 0 {
		return Some(flush(i + ate));
	}
	None
}

fn push_comment(pre: usize) -> WhatNow {
	push((pre, 1, None), Box::new(SitComment {}))
}

#[derive(PartialEq)]
#[derive(Clone)]
#[derive(Copy)]
pub enum Tri {
	No,
	Maybe,
	Yes,
}

pub fn find_lvalue(horizon: &[u8]) -> (Tri, usize) {
	let mut ate = identifierlen(horizon);
	if ate == 0 {
		return (Tri::No, ate);
	}

	#[derive(Clone)]
	#[derive(Copy)]
	enum Lex {
		Ident,
		Brack,
		Pluss,
	}
	let mut state = Lex::Ident;

	loop {
		if ate == horizon.len() {
			return (Tri::Maybe, ate);
		}
		let byte :u8 = horizon[ate];

		// Recursion: There is now an expression_tracker() if needed.
		match (state, byte) {
			(Lex::Ident, b'=') => return (Tri::Yes, ate),
			(Lex::Pluss, b'=') => return (Tri::Yes, ate - 1),
			(Lex::Ident, b'[') => state = Lex::Brack,
			(Lex::Brack, b']') => state = Lex::Ident,
			(Lex::Ident, b'+') => state = Lex::Pluss,
			(Lex::Ident, _) => return (Tri::No, ate),
			(Lex::Pluss, _) => return (Tri::No, ate),
			(Lex::Brack, _) => {}
		}
		ate += 1;
	}
}

fn find_heredoc(horizon: &[u8]) -> (usize, Vec<u8>) {
	let mut ate = predlen(|x| x == b'<', horizon);
	let mut found = Vec::<u8>::new();
	if ate != 2 {
		return (ate, found);
	}
	ate += predlen(|x| x == b'-', &horizon[ate ..]);
	ate += predlen(is_whitespace, &horizon[ate ..]);

	// Lex one word.
	let herein = &horizon[ate ..];
	found.reserve(herein.len());

	#[derive(Clone)]
	#[derive(Copy)]
	enum DelimiterSyntax {
		Word,
		WordEsc,
		Sq,
		Dq,
		DqEsc,
	}
	let mut state = DelimiterSyntax::Word;

	for byte_ref in herein {
		let byte: u8 = *byte_ref;
		state = match (state, byte) {
			(DelimiterSyntax::Word, b' ' ) => break,
			(DelimiterSyntax::Word, b'\n') => break,
			(DelimiterSyntax::Word, b'\t') => break,
			(DelimiterSyntax::Word, b'\\') => DelimiterSyntax::WordEsc,
			(DelimiterSyntax::Word, b'\'') => DelimiterSyntax::Sq,
			(DelimiterSyntax::Word, b'\"') => DelimiterSyntax::Dq,
			(DelimiterSyntax::Sq, b'\'') => DelimiterSyntax::Word,
			(DelimiterSyntax::Dq, b'\"') => DelimiterSyntax::Word,
			(DelimiterSyntax::Dq, b'\\') => DelimiterSyntax::DqEsc,
			(DelimiterSyntax::WordEsc, b'\n') => DelimiterSyntax::Word,
			(DelimiterSyntax::WordEsc, _) => {
				found.push(byte);
				DelimiterSyntax::Word
			}
			(DelimiterSyntax::DqEsc, b'\n') => DelimiterSyntax::Dq,
			(DelimiterSyntax::DqEsc, _) => {
				if byte != b'\"' && byte != b'\\' {
					found.push(b'\\');
				}
				found.push(byte);
				DelimiterSyntax::Dq
			}
			(_, _) => {
				found.push(byte);
				state
			}
		};
		ate += 1;
	}
	(ate, found)
}

#[test]
fn test_find_lvalue() {
	assert!(find_lvalue(b"") == (Tri::No, 0));
	assert!(find_lvalue(b"=") == (Tri::No, 0));
	assert!(find_lvalue(b"[]") == (Tri::No, 0));
	assert!(find_lvalue(b"esa") == (Tri::Maybe, 3));
	assert!(find_lvalue(b"esa+") == (Tri::Maybe, 4));
	assert!(find_lvalue(b"esa+  ") == (Tri::No, 4));
	assert!(find_lvalue(b"esa[]") == (Tri::Maybe, 5));
	assert!(find_lvalue(b"esa[]+") == (Tri::Maybe, 6));
	assert!(find_lvalue(b"esa ") == (Tri::No, 3));
	assert!(find_lvalue(b"esa]") == (Tri::No, 3));
	assert!(find_lvalue(b"esa=") == (Tri::Yes, 3));
	assert!(find_lvalue(b"esa+=") == (Tri::Yes, 3));
	assert!(find_lvalue(b"esa[]=") == (Tri::Yes, 5));
	assert!(find_lvalue(b"esa[]+=") == (Tri::Yes, 5));
}