@cplieger/deadset-ts 8.0.0-dev.18 → 8.0.0-dev.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,31 +1,125 @@
1
1
  /**
2
2
  * Layer 1 of the command reader: one command line, parsed by the vendored shell parser, as
3
- * its and-or lists, their pipelines and the words of each simple command. Nothing here
4
- * tokenizes, unquotes or splits text.
3
+ * its and-or lists, their pipelines and the words of each simple command, and the one place
4
+ * that decides which parts of a word the shell computes and which words a brace expansion
5
+ * stands for, for layer 3 too.
5
6
  */
6
7
  import { parse, } from "../vendor/unbash/parser.js";
7
- /** Whether one part of a word has a value the line states: a literal, quoted or escaped text. */
8
- function isStatic(part) {
8
+ /**
9
+ * The character that stands, in a word's text as layer 3 reads it, for a part only the run
10
+ * knows. It is a private-use character, so no path holds it, and the parser reads it as
11
+ * literal text.
12
+ */
13
+ export const PART = "\uE000";
14
+ /** A GitHub Actions expression, which the runner substitutes before any shell reads the line. */
15
+ const EXPRESSION = /\$\{\{[\s\S]*?\}\}/gu;
16
+ /** `text` with each `${{ }}` expression in it read as a part only the run knows. */
17
+ export function expressionsAsParts(text) {
18
+ return text.replace(EXPRESSION, PART);
19
+ }
20
+ /** One command line parsed, each `${{ }}` expression in it read as a part only the run knows. */
21
+ export function parseLine(source) {
22
+ return parse(expressionsAsParts(source));
23
+ }
24
+ /** The words a word's brace expansions stand for, at most this many. */
25
+ const ALTERNATIVES = 64;
26
+ /**
27
+ * The text one part of a word stands for: a literal's, a quoted text's or an escape's value,
28
+ * and {@link PART} for every part the shell computes, a glob among them.
29
+ */
30
+ function partText(part) {
9
31
  switch (part.type) {
10
32
  case "Literal":
11
33
  case "SingleQuoted":
12
34
  case "AnsiCQuoted":
13
- return true;
35
+ return part.value;
14
36
  case "DoubleQuoted":
15
- return part.parts.every((child) => child.type === "Literal");
37
+ return part.parts.map((child) => (child.type === "Literal" ? child.value : PART)).join("");
16
38
  default:
17
- return false;
39
+ return PART;
18
40
  }
19
41
  }
42
+ /** The texts the word `piece` of a brace expansion stands for, or a part where it is no one word. */
43
+ function alternativeTexts(piece) {
44
+ if (piece === "") {
45
+ return [""];
46
+ }
47
+ const script = parse(piece);
48
+ const [statement, ...more] = script.commands;
49
+ const command = statement?.command;
50
+ return (script.errors ?? []).length === 0 &&
51
+ more.length === 0 &&
52
+ command?.type === "Command" &&
53
+ command.name !== undefined &&
54
+ command.prefix.length === 0 &&
55
+ command.suffix.length === 0
56
+ ? heldTextsOf(command.name)
57
+ : [PART];
58
+ }
59
+ /**
60
+ * The texts a brace expansion stands for, its comma-separated alternatives, or a part where it
61
+ * has none, a sequence such as `{1..3}` among them.
62
+ */
63
+ function braceTexts(text) {
64
+ const inner = text.slice(1, -1);
65
+ const pieces = [];
66
+ let depth = 0;
67
+ let from = 0;
68
+ let quote = "";
69
+ for (let at = 0; at < inner.length; at += 1) {
70
+ const char = inner.charAt(at);
71
+ if (char === "\\") {
72
+ at += 1;
73
+ }
74
+ else if (quote !== "") {
75
+ quote = char === quote ? "" : quote;
76
+ }
77
+ else if (char === "'" || char === '"') {
78
+ quote = char;
79
+ }
80
+ else if (char === "{" || char === "}") {
81
+ depth += char === "{" ? 1 : -1;
82
+ }
83
+ else if (char === "," && depth === 0) {
84
+ pieces.push(inner.slice(from, at));
85
+ from = at + 1;
86
+ }
87
+ }
88
+ if (pieces.length === 0) {
89
+ return [PART];
90
+ }
91
+ pieces.push(inner.slice(from));
92
+ return pieces.flatMap(alternativeTexts);
93
+ }
20
94
  /**
21
- * One word as layer 1 reads it. A tilde that starts the word unquoted is the parser's literal
22
- * text but the shell's home directory, so it makes the word dynamic too.
95
+ * Each word the shell may expand `word` to, as layer 3 reads it: its quotes removed, each brace
96
+ * expansion read as its alternatives, past {@link ALTERNATIVES} words as a part, and each part
97
+ * it computes, a tilde that starts it unquoted among them, as {@link PART}.
23
98
  */
24
- function wordOf(word) {
25
- const [first] = word.parts ?? [];
26
- const lead = word.parts === undefined ? word.text : first?.type === "Literal" ? first.text : "";
27
- const known = (word.parts ?? []).every(isStatic) && !lead.startsWith("~");
28
- return { value: known ? word.value : undefined, text: word.text };
99
+ export function heldTextsOf(word) {
100
+ const { parts } = word;
101
+ const [first] = parts ?? [];
102
+ const tilde = parts === undefined
103
+ ? word.text.startsWith("~")
104
+ : first?.type === "Literal" && first.text.startsWith("~");
105
+ let texts = [parts === undefined ? word.value : ""];
106
+ for (const part of parts ?? []) {
107
+ const options = part.type === "BraceExpansion" ? braceTexts(part.text) : [partText(part)];
108
+ const next = texts.flatMap((text) => options.map((option) => `${text}${option}`));
109
+ texts = next.length > ALTERNATIVES ? texts.map((text) => `${text}${PART}`) : next;
110
+ }
111
+ return tilde ? texts.map((text) => `${PART}${text.slice(1)}`) : texts;
112
+ }
113
+ /** One word as layer 1 reads it: static only where the shell expands it to one text with no part. */
114
+ export function wordOf(word) {
115
+ const [only, ...more] = heldTextsOf(word);
116
+ const one = more.length === 0 ? only : undefined;
117
+ const known = one !== undefined && !one.includes(PART);
118
+ return {
119
+ value: known ? word.value : undefined,
120
+ text: word.text,
121
+ ...(one === undefined ? {} : { written: one }),
122
+ };
29
123
  }
30
124
  /**
31
125
  * Whether one member of a pipeline, run in the line's own shell, may change that shell's
@@ -61,7 +155,7 @@ function pipelineOf(node, operator) {
61
155
  * gives no words, and marks its pipeline where it may change the line's directory.
62
156
  */
63
157
  export function shellLine(source) {
64
- const script = parse(source);
158
+ const script = parseLine(source);
65
159
  if ((script.errors ?? []).length > 0) {
66
160
  return [];
67
161
  }