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