openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

shell.js (4188B)


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