shell.js 4.1 KB

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