Debian Codegraph @

codemirror-legacy-modes/mode/shell.js

raw
1 var words = {};
2 function define(style, dict) {
3 for(var i = 0; i < dict.length; i++) {
4 words[dict[i]] = style;
5 }
6 };
7
8 var commonAtoms = ["true", "false"];
9 var commonKeywords = ["if", "then", "do", "else", "elif", "while", "until", "for", "in", "esac", "fi",
10 "fin", "fil", "done", "exit", "set", "unset", "export", "function"];
11 var commonCommands = ["ab", "awk", "bash", "beep", "cat", "cc", "cd", "chown", "chmod", "chroot", "clear",
12 "cp", "curl", "cut", "diff", "echo", "find", "gawk", "gcc", "get", "git", "grep", "hg", "kill", "killall",
13 "ln", "ls", "make", "mkdir", "openssl", "mv", "nc", "nl", "node", "npm", "ping", "ps", "restart", "rm",
14 "rmdir", "sed", "service", "sh", "shopt", "shred", "source", "sort", "sleep", "ssh", "start", "stop",
15 "su", "sudo", "svn", "tee", "telnet", "top", "touch", "vi", "vim", "wall", "wc", "wget", "who", "write",
16 "yes", "zsh"];
17
18 define('atom', commonAtoms);
19 define('keyword', commonKeywords);
20 define('builtin', commonCommands);
21
22 function tokenBase(stream, state) {
23 if (stream.eatSpace()) return null;
24
25 var sol = stream.sol();
26 var ch = stream.next();
27
28 if (ch === '\\') {
29 stream.next();
30 return null;
31 }
32 if (ch === '\'' || ch === '"' || ch === '`') {
33 state.tokens.unshift(tokenString(ch, ch === "`" ? "quote" : "string"));
34 return tokenize(stream, state);
35 }
36 if (ch === '#') {
37 if (sol && stream.eat('!')) {
38 stream.skipToEnd();
39 return 'meta'; // 'comment'?
40 }
41 stream.skipToEnd();
42 return 'comment';
43 }
44 if (ch === '$') {
45 state.tokens.unshift(tokenDollar);
46 return tokenize(stream, state);
47 }
48 if (ch === '+' || ch === '=') {
49 return 'operator';
50 }
51 if (ch === '-') {
52 stream.eat('-');
53 stream.eatWhile(/\w/);
54 return 'attribute';
55 }
56 if (ch == "<") {
57 if (stream.match("<<")) return "operator"
58 var heredoc = stream.match(/^<-?\s*(?:['"]([^'"]*)['"]|([^'"\s]*))/)
59 if (heredoc) {
60 state.tokens.unshift(tokenHeredoc(heredoc[1] || heredoc[2]))
61 return 'string.special'
62 }
63 }
64 if (/\d/.test(ch)) {
65 stream.eatWhile(/\d/);
66 if(stream.eol() || !/\w/.test(stream.peek())) {
67 return 'number';
68 }
69 }
70 stream.eatWhile(/[\w-]/);
71 var cur = stream.current();
72 if (stream.peek() === '=' && /\w+/.test(cur)) return 'def';
73 return words.hasOwnProperty(cur) ? words[cur] : null;
74 }
75
76 function tokenString(quote, style) {
77 var close = quote == "(" ? ")" : quote == "{" ? "}" : quote
78 return function(stream, state) {
79 var next, escaped = false;
80 while ((next = stream.next()) != null) {
81 if (next === close && !escaped) {
82 state.tokens.shift();
83 break;
84 } else if (next === '$' && !escaped && quote !== "'" && stream.peek() != close) {
85 escaped = true;
86 stream.backUp(1);
87 state.tokens.unshift(tokenDollar);
88 break;
89 } else if (!escaped && quote !== close && next === quote) {
90 state.tokens.unshift(tokenString(quote, style))
91 return tokenize(stream, state)
92 } else if (!escaped && /['"]/.test(next) && !/['"]/.test(quote)) {
93 state.tokens.unshift(tokenStringStart(next, "string"));
94 stream.backUp(1);
95 break;
96 }
97 escaped = !escaped && next === '\\';
98 }
99 return style;
100 };
101 };
102
103 function tokenStringStart(quote, style) {
104 return function(stream, state) {
105 state.tokens[0] = tokenString(quote, style)
106 stream.next()
107 return tokenize(stream, state)
108 }
109 }
110
111 var tokenDollar = function(stream, state) {
112 if (state.tokens.length > 1) stream.eat('$');
113 var ch = stream.next()
114 if (/['"({]/.test(ch)) {
115 state.tokens[0] = tokenString(ch, ch == "(" ? "quote" : ch == "{" ? "def" : "string");
116 return tokenize(stream, state);
117 }
118 if (!/\d/.test(ch)) stream.eatWhile(/\w/);
119 state.tokens.shift();
120 return 'def';
121 };
122
123 function tokenHeredoc(delim) {
124 return function(stream, state) {
125 if (stream.sol() && stream.string == delim) state.tokens.shift()
126 stream.skipToEnd()
127 return "string.special"
128 }
129 }
130
131 function tokenize(stream, state) {
132 return (state.tokens[0] || tokenBase) (stream, state);
133 };
134
135 export const shell = {
136 name: "shell",
137 startState: function() {return {tokens:[]};},
138 token: function(stream, state) {
139 return tokenize(stream, state);
140 },
141 languageData: {
142 autocomplete: commonAtoms.concat(commonKeywords, commonCommands),
143 closeBrackets: {brackets: ["(", "[", "{", "'", '"', "`"]},
144 commentTokens: {line: "#"}
145 }
146 };