1
0

shell.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 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 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));
  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) {
  75. return function(stream, state) {
  76. var next, end = false, escaped = false;
  77. while ((next = stream.next()) != null) {
  78. if (next === quote && !escaped) {
  79. end = true;
  80. break;
  81. }
  82. if (next === '$' && !escaped && quote !== '\'') {
  83. escaped = true;
  84. stream.backUp(1);
  85. state.tokens.unshift(tokenDollar);
  86. break;
  87. }
  88. escaped = !escaped && next === '\\';
  89. }
  90. if (end || !escaped) {
  91. state.tokens.shift();
  92. }
  93. return (quote === '`' || quote === ')' ? 'quote' : 'string');
  94. };
  95. };
  96. var tokenDollar = function(stream, state) {
  97. if (state.tokens.length > 1) stream.eat('$');
  98. var ch = stream.next(), hungry = /\w/;
  99. if (ch === '{') hungry = /[^}]/;
  100. if (ch === '(') {
  101. state.tokens[0] = tokenString(')');
  102. return tokenize(stream, state);
  103. }
  104. if (!/\d/.test(ch)) {
  105. stream.eatWhile(hungry);
  106. stream.eat('}');
  107. }
  108. state.tokens.shift();
  109. return 'def';
  110. };
  111. function tokenize(stream, state) {
  112. return (state.tokens[0] || tokenBase) (stream, state);
  113. };
  114. return {
  115. startState: function() {return {tokens:[]};},
  116. token: function(stream, state) {
  117. return tokenize(stream, state);
  118. },
  119. lineComment: '#',
  120. fold: "brace"
  121. };
  122. });
  123. CodeMirror.defineMIME('text/x-sh', 'shell');
  124. });