gardesk/tarmac-web / faa72e1

Browse files

add shell token definitions

Authored by espadonne
SHA
faa72e1979df048a59b7cb2e339ff0e726a9fdcf
Parents
2e1dc57
Tree
28ff048

1 changed file

StatusFile+-
A src/lib/shell-tokens.ts 49 0
src/lib/shell-tokens.tsadded
@@ -0,0 +1,49 @@
1
+/**
2
+ * Shell token databases for syntax highlighting.
3
+ */
4
+
5
+export const KEYWORDS = new Set([
6
+  "if", "then", "else", "elif", "fi",
7
+  "for", "in", "do", "done",
8
+  "while", "until",
9
+  "case", "esac",
10
+  "function", "select", "time",
11
+]);
12
+
13
+export const OPERATORS = [
14
+  "&&", "||", "|&", "|", ";;", ";;&", ";&", ";",
15
+  "&>>", "&>", ">>", ">|", ">", "<<-", "<<<", "<<", "<>", "<&", "<",
16
+  ">&", "&", "((", "))", "(", ")", "{", "}", "[[", "]]", "!",
17
+];
18
+
19
+export const PREFIX_COMMANDS = new Set([
20
+  "sudo", "su", "doas", "env", "nice", "nohup", "time",
21
+  "strace", "ltrace", "xargs", "exec", "command", "builtin",
22
+]);
23
+
24
+export function isKeyword(word: string): boolean {
25
+  return KEYWORDS.has(word);
26
+}
27
+
28
+export function matchOperator(str: string): string | null {
29
+  for (const op of OPERATORS) {
30
+    if (str.startsWith(op)) return op;
31
+  }
32
+  return null;
33
+}
34
+
35
+export function isOption(word: string): boolean {
36
+  return /^--?[a-zA-Z]/.test(word);
37
+}
38
+
39
+export function isPath(word: string): boolean {
40
+  return word.includes("/") && !word.startsWith("$");
41
+}
42
+
43
+export function isNumber(word: string): boolean {
44
+  return /^-?\d+(\.\d+)?$/.test(word);
45
+}
46
+
47
+export function isPrefixCommand(word: string): boolean {
48
+  return PREFIX_COMMANDS.has(word);
49
+}