You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
5.3 KiB

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