shell.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/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 (/\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. function tokenString(quote, style) {
  76. var close = quote == "(" ? ")" : quote == "{" ? "}" : quote
  77. return function(stream, state) {
  78. var next, escaped = false;
  79. while ((next = stream.next()) != null) {
  80. if (next === close && !escaped) {
  81. state.tokens.shift();
  82. break;
  83. } else if (next === '$' && !escaped && quote !== "'" && stream.peek() != close) {
  84. escaped = true;
  85. stream.backUp(1);
  86. state.tokens.unshift(tokenDollar);
  87. break;
  88. } else if (!escaped && quote !== close && next === quote) {
  89. state.tokens.unshift(tokenString(quote, style))
  90. return tokenize(stream, state)
  91. } else if (!escaped && /['"]/.test(next) && !/['"]/.test(quote)) {
  92. state.tokens.unshift(tokenStringStart(next, "string"));
  93. stream.backUp(1);
  94. break;
  95. }
  96. escaped = !escaped && next === '\\';
  97. }
  98. return style;
  99. };
  100. };
  101. function tokenStringStart(quote, style) {
  102. return function(stream, state) {
  103. state.tokens[0] = tokenString(quote, style)
  104. stream.next()
  105. return tokenize(stream, state)
  106. }
  107. }
  108. var tokenDollar = function(stream, state) {
  109. if (state.tokens.length > 1) stream.eat('$');
  110. var ch = stream.next()
  111. if (/['"({]/.test(ch)) {
  112. state.tokens[0] = tokenString(ch, ch == "(" ? "quote" : ch == "{" ? "def" : "string");
  113. return tokenize(stream, state);
  114. }
  115. if (!/\d/.test(ch)) stream.eatWhile(/\w/);
  116. state.tokens.shift();
  117. return 'def';
  118. };
  119. function tokenize(stream, state) {
  120. return (state.tokens[0] || tokenBase) (stream, state);
  121. };
  122. return {
  123. startState: function() {return {tokens:[]};},
  124. token: function(stream, state) {
  125. return tokenize(stream, state);
  126. },
  127. closeBrackets: "()[]{}''\"\"``",
  128. lineComment: '#',
  129. fold: "brace"
  130. };
  131. });
  132. CodeMirror.defineMIME('text/x-sh', 'shell');
  133. // Apache uses a slightly different Media Type for Shell scripts
  134. // http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
  135. CodeMirror.defineMIME('application/x-sh', 'shell');
  136. });