shell.js 4.4 KB

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