libabbs 0.1.8

Library for aosc-os-abbs maintenance
Documentation
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! Lossless syntax tree representation of APML.
//!
//! This LST is designed to correspond to the source file
//! byte by byte in order to obtain a lossless reverse
//! conversion capability to the source file.
//!
//! To parse a source string into LST, see [`ApmlLst::parse`] and [`parser`][super::parser].
//!
//! The root of LST is [`ApmlLst`], which is made up by a list of [tokens](Token).
//!
//! A token may be a [`VariableDefinition`], a space-like character, or a newline.
//! For example, `TEST=value\n` can be parsed into a [`VariableDefinition`] token and a newline token.
//!
//! <div class="warning">
//! The LST structure poses few of limitations and validations.
//! It is your duty to make sure the generated LST is valid, or else
//! the serialized APML may be invalid.
//! </div>

use std::{
	borrow::Cow,
	fmt::{Debug, Display, Write},
	sync::Arc,
};

use super::{
	parser::{ParseError, apml_lst},
	pattern::BashPattern,
};

/// A APML parse-tree, consisting of a list of tokens.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ApmlLst<'a>(pub Vec<Token<'a>>);

impl Display for ApmlLst<'_> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		for token in &self.0 {
			Display::fmt(token, f)?;
		}
		Ok(())
	}
}

impl<'a> ApmlLst<'a> {
	/// Parses a APML source string into a lossless syntax tree.
	///
	/// This is a wrapper calling [`apml_lst`] parser combinator,
	/// while errors produced by the parser are converted into [`ParseError`],
	/// and [`ParseError::UnexpectedSource`] is produced when
	/// there are some unparsable texts in the input.
	pub fn parse(src: &'a str) -> Result<Self, ParseError> {
		let (out, tree) = apml_lst(src)?;
		if !out.is_empty() {
			return Err(ParseError::UnexpectedSource {
				pos: nom::Offset::offset(src, out) + 1,
			});
		}
		Ok(tree)
	}
}

/// A token in the LST.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Token<'a> {
	/// A space-like character (`'<char>'`).
	///
	/// This currently includes:
	/// - Space (`' '`)
	/// - Tab (`'\t'`)
	Spacy(char),
	/// A newline character (`'\n'`, ASCII code 0x0A).
	Newline,
	/// A comment (`"#<text>"`).
	Comment(Cow<'a, str>),
	/// A variable definition.
	Variable(VariableDefinition<'a>),
}

impl Token<'_> {
	/// Returns if the token is a space or a newline.
	pub fn is_empty(&self) -> bool {
		matches!(&self, Token::Newline | Token::Spacy(_))
	}
}

impl Display for Token<'_> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Token::Spacy(ch) => f.write_char(*ch),
			Token::Newline => f.write_char('\n'),
			Token::Comment(text) => f.write_fmt(format_args!("#{text}")),
			Token::Variable(def) => Display::fmt(def, f),
		}
	}
}

/// A variable definition (`"<name>=<value>"`).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VariableDefinition<'a> {
	/// Name of the variable.
	pub name: Cow<'a, str>,
	/// Binary operator.
	pub op: VariableOp,
	/// Value of the variable.
	pub value: VariableValue<'a>,
}

impl Display for VariableDefinition<'_> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.write_str(&self.name)?;
		Display::fmt(&self.op, f)?;
		Display::fmt(&self.value, f)?;
		Ok(())
	}
}

/// A variable operator.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum VariableOp {
	/// Value assignment (`'='`).
	Assignment,
	/// Appending (`"+="`).
	Append,
}

impl Display for VariableOp {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			VariableOp::Assignment => f.write_char('='),
			VariableOp::Append => f.write_str("+="),
		}
	}
}

/// Value of a variable.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum VariableValue<'a> {
	/// A string value (`"<text>"`).
	String(Arc<Text<'a>>),
	/// A array value (`"(<tokens>)"`)
	Array(Vec<ArrayToken<'a>>),
}

impl Display for VariableValue<'_> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			VariableValue::String(text) => Display::fmt(text, f),
			VariableValue::Array(tokens) => {
				f.write_char('(')?;
				for token in tokens {
					Display::fmt(token, f)?;
				}
				f.write_char(')')?;
				Ok(())
			}
		}
	}
}

/// A section of text.
///
/// Text is made up of several text units.
/// For example:
/// - `abc'123'` is made up of an unquoted unit `abc` and a single-quoted unit `123`.
/// - `"abc$0"` is made up of one double-quoted unit.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Text<'a>(pub Vec<TextUnit<'a>>);

impl Display for Text<'_> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		for unit in &self.0 {
			Display::fmt(unit, f)?;
		}
		Ok(())
	}
}

/// A unit of text.
///
/// See [Text] and [Word] for more documentation.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TextUnit<'a> {
	/// An unquoted text unit (`"<words>"`).
	Unquoted(Vec<Word<'a>>),
	/// A single-quoted text unit (`"'<text>'"`).
	SingleQuote(Cow<'a, str>),
	/// A double-quoted text unit (`"\"<words>\""`).
	DoubleQuote(Vec<Word<'a>>),
}

impl Display for TextUnit<'_> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			TextUnit::Unquoted(words) => {
				for word in words {
					Display::fmt(word, f)?;
				}
				Ok(())
			}
			TextUnit::SingleQuote(text) => {
				f.write_fmt(format_args!("'{text}'"))
			}
			TextUnit::DoubleQuote(words) => {
				f.write_char('"')?;
				for word in words {
					Display::fmt(word, f)?;
				}
				f.write_char('"')?;
				Ok(())
			}
		}
	}
}

/// A word is a part of a text unit.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Word<'a> {
	/// A literal string (`"<parts>"`).
	Literal(Vec<LiteralPart<'a>>),
	/// An unbraced variable expansion (`"$<var>"`).
	UnbracedVariable(Cow<'a, str>),
	/// A braced variable expansion (`"${<expansion>}"`).
	BracedVariable(BracedExpansion<'a>),
	/// A sub-command expansion (`"$(<tokens>)"`).
	Subcommand(Vec<ArrayToken<'a>>),
}

impl Display for Word<'_> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Word::Literal(parts) => {
				for part in parts {
					Display::fmt(part, f)?;
				}
				Ok(())
			}
			Word::UnbracedVariable(name) => {
				f.write_fmt(format_args!("${name}"))
			}
			Word::BracedVariable(exp) => {
				f.write_fmt(format_args!("${{{exp}}}"))
			}
			Word::Subcommand(tokens) => {
				f.write_str("$(")?;
				for token in tokens {
					Display::fmt(token, f)?;
				}
				f.write_str(")")?;
				Ok(())
			}
		}
	}
}

/// A element of literal words.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum LiteralPart<'a> {
	/// A string (`"<text>"`).
	String(Cow<'a, str>),
	/// An escaped character (`"\\<char>"`).
	Escaped(char),
	/// A tag for discard newlines (`"\\\n"`).
	LineContinuation,
}

impl Display for LiteralPart<'_> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			LiteralPart::String(text) => f.write_str(text),
			LiteralPart::Escaped(ch) => f.write_fmt(format_args!("\\{ch}")),
			LiteralPart::LineContinuation => f.write_str("\\\n"),
		}
	}
}

impl LiteralPart<'_> {
	/// Returns if a character should be escaped when used in double-quoted words.
	pub fn should_escape(ch: char) -> bool {
		matches!(ch, '$' | '"' | '\\')
	}

	/// Produces a list of literal part, escaping characters that need to be
	/// escaped when used in double-quoted words.
	///
	/// Note that all escaped strings will be cloned,
	/// so this function needs more allocations.
	pub fn escape<S: AsRef<str>>(text: S) -> Vec<Self> {
		let mut result = Vec::new();
		let mut buffer = String::new();
		for ch in text.as_ref().chars() {
			if Self::should_escape(ch) {
				if !buffer.is_empty() {
					result.push(LiteralPart::String(Cow::Owned(buffer)));
					buffer = String::new();
				}
				result.push(LiteralPart::Escaped(ch));
			} else {
				buffer.push(ch);
			}
		}
		if !buffer.is_empty() {
			result.push(LiteralPart::String(Cow::Owned(buffer)));
		}
		result
	}
}

/// A braced variable expansion (`"<name>[modifier]"`).
///
/// Note that for [ExpansionModifier::Length], the format is `"#<name>"`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BracedExpansion<'a> {
	/// Name of the variable.
	pub name: Cow<'a, str>,
	/// Modifier to apply to the expanded value.
	pub modifier: Option<ExpansionModifier<'a>>,
}

impl Display for BracedExpansion<'_> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match &self.modifier {
			Some(ExpansionModifier::Length) => {
				f.write_fmt(format_args!("#{}", self.name))
			}
			None => f.write_str(&self.name),
			Some(modifier) => {
				f.write_fmt(format_args!("{}{}", self.name, modifier))
			}
		}
	}
}

/// A modifier in the braced variable expansion.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ExpansionModifier<'a> {
	/// Reference to a substring (`":offset"` or `":offset:length"`).
	///
	/// The range is [offset, (offset+length)) (indexing from zero).
	Substring {
		/// Offset.
		offset: Cow<'a, str>,
		/// Length.
		length: Option<Cow<'a, str>>,
	},
	/// Stripping the shortest matching prefix (`"#<pattern>"`).
	StripShortestPrefix(Arc<BashPattern<'a>>),
	/// Stripping the longest matching prefix (`"##<pattern>"`).
	StripLongestPrefix(Arc<BashPattern<'a>>),
	/// Stripping the shortest matching suffix (`"%<pattern>"`).
	StripShortestSuffix(Arc<BashPattern<'a>>),
	/// Stripping the longest matching suffix (`"%%<pattern>"`).
	StripLongestSuffix(Arc<BashPattern<'a>>),
	/// Replacing the first match of a pattern with a text (`"/<pattern>[/<string>]"`).
	///
	/// `string` can be omitted, leaving `"/<pattern>"` structure,
	/// which removes the first match of the pattern.
	ReplaceOnce {
		pattern: Arc<BashPattern<'a>>,
		string: Option<Arc<Text<'a>>>,
	},
	/// Replacing the all matches of a pattern with a text (`"//<pattern>[/<string>]"`).
	///
	/// `string` can be omitted.
	ReplaceAll {
		pattern: Arc<BashPattern<'a>>,
		string: Option<Arc<Text<'a>>>,
	},
	/// Replacing the prefix of a pattern with a text (`"/#<pattern>[/<string>]"`).
	///
	/// `string` can be omitted.
	ReplacePrefix {
		pattern: Arc<BashPattern<'a>>,
		string: Option<Arc<Text<'a>>>,
	},
	/// Replacing the suffix of a pattern with a text (`"/%<pattern>[/<string>]"`).
	///
	/// `string` can be omitted.
	ReplaceSuffix {
		pattern: Arc<BashPattern<'a>>,
		string: Option<Arc<Text<'a>>>,
	},
	/// Upper-casify the first match of a pattern (`"^<pattern>"`).
	UpperOnce(Arc<BashPattern<'a>>),
	/// Upper-casify the all matches of a pattern (`"^^<pattern>"`).
	UpperAll(Arc<BashPattern<'a>>),
	/// Lower-casify the first match of a pattern (`",<pattern>"`).
	LowerOnce(Arc<BashPattern<'a>>),
	/// Lower-casify the all matches of a pattern (`",,<pattern>"`).
	LowerAll(Arc<BashPattern<'a>>),
	/// Producing errors when the variable is unset or null (`":?<text>"`).
	ErrorOnUnset(Arc<Text<'a>>),
	/// Returning the length of the variable.
	///
	/// Note that this modifier uses a special format, see [BracedExpansion].
	Length,
	/// Returning a text when the variable is unset or null (`":-<text>"`).
	WhenUnset(Arc<Text<'a>>),
	/// Returning a text when the variable is set (`":+<text>"`).
	WhenSet(Arc<Text<'a>>),
	/// Expands to array elements (`"[@]"`).
	ArrayElements,
	/// Expands to a string of array elements concatenated with space (`"[*]"`).
	SingleWordElements,
}

impl Display for ExpansionModifier<'_> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			ExpansionModifier::Substring { offset, length } => match length {
				None => f.write_fmt(format_args!(":{offset}")),
				Some(length) => f.write_fmt(format_args!(":{offset}:{length}")),
			},
			ExpansionModifier::StripShortestPrefix(pattern) => {
				f.write_fmt(format_args!("#{pattern}"))
			}
			ExpansionModifier::StripLongestPrefix(pattern) => {
				f.write_fmt(format_args!("##{pattern}"))
			}
			ExpansionModifier::StripShortestSuffix(pattern) => {
				f.write_fmt(format_args!("%{pattern}"))
			}
			ExpansionModifier::StripLongestSuffix(pattern) => {
				f.write_fmt(format_args!("%%{pattern}"))
			}
			ExpansionModifier::ReplaceOnce { pattern, string } => {
				match string {
					Some(string) => {
						f.write_fmt(format_args!("/{pattern}/{string}"))
					}
					None => f.write_fmt(format_args!("/{pattern}")),
				}
			}
			ExpansionModifier::ReplaceAll { pattern, string } => match string {
				Some(string) => {
					f.write_fmt(format_args!("//{pattern}/{string}"))
				}
				None => f.write_fmt(format_args!("//{pattern}")),
			},
			ExpansionModifier::ReplacePrefix { pattern, string } => {
				match string {
					Some(string) => {
						f.write_fmt(format_args!("/#{pattern}/{string}"))
					}
					None => f.write_fmt(format_args!("/#{pattern}")),
				}
			}
			ExpansionModifier::ReplaceSuffix { pattern, string } => {
				match string {
					Some(string) => {
						f.write_fmt(format_args!("/%{pattern}/{string}"))
					}
					None => f.write_fmt(format_args!("/%{pattern}")),
				}
			}
			ExpansionModifier::UpperOnce(pattern) => {
				f.write_fmt(format_args!("^{pattern}"))
			}
			ExpansionModifier::UpperAll(pattern) => {
				f.write_fmt(format_args!("^^{pattern}"))
			}
			ExpansionModifier::LowerOnce(pattern) => {
				f.write_fmt(format_args!(",{pattern}"))
			}
			ExpansionModifier::LowerAll(pattern) => {
				f.write_fmt(format_args!(",,{pattern}"))
			}
			ExpansionModifier::ErrorOnUnset(text) => {
				f.write_fmt(format_args!(":?{text}"))
			}
			ExpansionModifier::Length => f.write_char('#'),
			ExpansionModifier::WhenUnset(text) => {
				f.write_fmt(format_args!(":-{text}"))
			}
			ExpansionModifier::WhenSet(text) => {
				f.write_fmt(format_args!(":+{text}"))
			}
			ExpansionModifier::ArrayElements => f.write_str("[@]"),
			ExpansionModifier::SingleWordElements => f.write_str("[*]"),
		}
	}
}

/// A token in an array variable value.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ArrayToken<'a> {
	/// A space-like character (`'<char>'`).
	///
	/// See [Token::Spacy] for more.
	Spacy(char),
	/// A newline character (`'\n'`, ASCII code 0x0A).
	Newline,
	/// A comment (`"#<text>"`).
	Comment(Cow<'a, str>),
	/// A array element (`"<text>"`).
	Element(Arc<Text<'a>>),
}

impl Display for ArrayToken<'_> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			ArrayToken::Spacy(ch) => f.write_char(*ch),
			ArrayToken::Newline => f.write_char('\n'),
			ArrayToken::Comment(text) => {
				f.write_char('#')?;
				f.write_str(text)?;
				Ok(())
			}
			ArrayToken::Element(text) => Display::fmt(text, f),
		}
	}
}
#[cfg(test)]
mod test {
	use super::*;

	#[test]
	fn test_apml_parse() {
		let tree = ApmlLst::parse(r##"# Test APML"##).unwrap();
		dbg!(&tree);
		let tree = ApmlLst::parse(r##"aaa"##).unwrap_err();
		dbg!(&tree);
	}

	#[test]
	fn test_token() {
		assert!(Token::Newline.is_empty());
		assert!(Token::Spacy(' ').is_empty());
		assert!(Token::Spacy('\t').is_empty());
		assert!(!Token::Comment(Cow::Borrowed("Test")).is_empty());
		assert!(
			!Token::Variable(VariableDefinition {
				name: Cow::Borrowed("Test"),
				op: VariableOp::Assignment,
				value: VariableValue::String(Arc::new(Text(vec![])))
			})
			.is_empty()
		);
	}

	#[test]
	fn test_literal_part_escape() {
		assert!(LiteralPart::should_escape('$'));
		assert!(LiteralPart::should_escape('"'));
		assert!(LiteralPart::should_escape('\\'));
		assert!(!LiteralPart::should_escape('a'));
		assert!(!LiteralPart::should_escape(' '));
		assert_eq!(
			LiteralPart::escape("asdf"),
			vec![LiteralPart::String("asdf".into())]
		);
		assert_eq!(
			LiteralPart::escape("asd$$f\ng\\"),
			vec![
				LiteralPart::String("asd".into()),
				LiteralPart::Escaped('$'),
				LiteralPart::Escaped('$'),
				LiteralPart::String("f\ng".into()),
				LiteralPart::Escaped('\\'),
			]
		);
	}
}