OLD | NEW |
(Empty) | |
| 1 CodeMirror.defineMode('shell', function() { |
| 2 |
| 3 var words = {}; |
| 4 function define(style, string) { |
| 5 var split = string.split(' '); |
| 6 for(var i = 0; i < split.length; i++) { |
| 7 words[split[i]] = style; |
| 8 } |
| 9 }; |
| 10 |
| 11 // Atoms |
| 12 define('atom', 'true false'); |
| 13 |
| 14 // Keywords |
| 15 define('keyword', 'if then do else elif while until for in esac fi fin ' + |
| 16 'fil done exit set unset export function'); |
| 17 |
| 18 // Commands |
| 19 define('builtin', 'ab awk bash beep cat cc cd chown chmod chroot clear cp ' + |
| 20 'curl cut diff echo find gawk gcc get git grep kill killall ln ls make ' + |
| 21 'mkdir openssl mv nc node npm ping ps restart rm rmdir sed service sh ' + |
| 22 'shopt shred source sort sleep ssh start stop su sudo tee telnet top ' + |
| 23 'touch vi vim wall wc wget who write yes zsh'); |
| 24 |
| 25 function tokenBase(stream, state) { |
| 26 |
| 27 var sol = stream.sol(); |
| 28 var ch = stream.next(); |
| 29 |
| 30 if (ch === '\'' || ch === '"' || ch === '`') { |
| 31 state.tokens.unshift(tokenString(ch)); |
| 32 return tokenize(stream, state); |
| 33 } |
| 34 if (ch === '#') { |
| 35 if (sol && stream.eat('!')) { |
| 36 stream.skipToEnd(); |
| 37 return 'meta'; // 'comment'? |
| 38 } |
| 39 stream.skipToEnd(); |
| 40 return 'comment'; |
| 41 } |
| 42 if (ch === '$') { |
| 43 state.tokens.unshift(tokenDollar); |
| 44 return tokenize(stream, state); |
| 45 } |
| 46 if (ch === '+' || ch === '=') { |
| 47 return 'operator'; |
| 48 } |
| 49 if (ch === '-') { |
| 50 stream.eat('-'); |
| 51 stream.eatWhile(/\w/); |
| 52 return 'attribute'; |
| 53 } |
| 54 if (/\d/.test(ch)) { |
| 55 stream.eatWhile(/\d/); |
| 56 if(!/\w/.test(stream.peek())) { |
| 57 return 'number'; |
| 58 } |
| 59 } |
| 60 stream.eatWhile(/[\w-]/); |
| 61 var cur = stream.current(); |
| 62 if (stream.peek() === '=' && /\w+/.test(cur)) return 'def'; |
| 63 return words.hasOwnProperty(cur) ? words[cur] : null; |
| 64 } |
| 65 |
| 66 function tokenString(quote) { |
| 67 return function(stream, state) { |
| 68 var next, end = false, escaped = false; |
| 69 while ((next = stream.next()) != null) { |
| 70 if (next === quote && !escaped) { |
| 71 end = true; |
| 72 break; |
| 73 } |
| 74 if (next === '$' && !escaped && quote !== '\'') { |
| 75 escaped = true; |
| 76 stream.backUp(1); |
| 77 state.tokens.unshift(tokenDollar); |
| 78 break; |
| 79 } |
| 80 escaped = !escaped && next === '\\'; |
| 81 } |
| 82 if (end || !escaped) { |
| 83 state.tokens.shift(); |
| 84 } |
| 85 return (quote === '`' || quote === ')' ? 'quote' : 'string'); |
| 86 }; |
| 87 }; |
| 88 |
| 89 var tokenDollar = function(stream, state) { |
| 90 if (state.tokens.length > 1) stream.eat('$'); |
| 91 var ch = stream.next(), hungry = /\w/; |
| 92 if (ch === '{') hungry = /[^}]/; |
| 93 if (ch === '(') { |
| 94 state.tokens[0] = tokenString(')'); |
| 95 return tokenize(stream, state); |
| 96 } |
| 97 if (!/\d/.test(ch)) { |
| 98 stream.eatWhile(hungry); |
| 99 stream.eat('}'); |
| 100 } |
| 101 state.tokens.shift(); |
| 102 return 'def'; |
| 103 }; |
| 104 |
| 105 function tokenize(stream, state) { |
| 106 return (state.tokens[0] || tokenBase) (stream, state); |
| 107 }; |
| 108 |
| 109 return { |
| 110 startState: function() {return {tokens:[]};}, |
| 111 token: function(stream, state) { |
| 112 if (stream.eatSpace()) return null; |
| 113 return tokenize(stream, state); |
| 114 } |
| 115 }; |
| 116 }); |
| 117 |
| 118 CodeMirror.defineMIME('text/x-sh', 'shell'); |
OLD | NEW |