dew/src/main/resources/org/apidesign/bck2brwsr/dew/js/codemirror/mode/clike.js
branchdew
changeset 544 08ffdc3938e7
parent 460 c0f1788183dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dew/src/main/resources/org/apidesign/bck2brwsr/dew/js/codemirror/mode/clike.js	Wed Jan 23 13:18:46 2013 +0100
     1.3 @@ -0,0 +1,300 @@
     1.4 +CodeMirror.defineMode("clike", function(config, parserConfig) {
     1.5 +  var indentUnit = config.indentUnit,
     1.6 +      statementIndentUnit = parserConfig.statementIndentUnit || indentUnit,
     1.7 +      keywords = parserConfig.keywords || {},
     1.8 +      builtin = parserConfig.builtin || {},
     1.9 +      blockKeywords = parserConfig.blockKeywords || {},
    1.10 +      atoms = parserConfig.atoms || {},
    1.11 +      hooks = parserConfig.hooks || {},
    1.12 +      multiLineStrings = parserConfig.multiLineStrings;
    1.13 +  var isOperatorChar = /[+\-*&%=<>!?|\/]/;
    1.14 +
    1.15 +  var curPunc;
    1.16 +
    1.17 +  function tokenBase(stream, state) {
    1.18 +    var ch = stream.next();
    1.19 +    if (hooks[ch]) {
    1.20 +      var result = hooks[ch](stream, state);
    1.21 +      if (result !== false) return result;
    1.22 +    }
    1.23 +    if (ch == '"' || ch == "'") {
    1.24 +      state.tokenize = tokenString(ch);
    1.25 +      return state.tokenize(stream, state);
    1.26 +    }
    1.27 +    if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
    1.28 +      curPunc = ch;
    1.29 +      return null;
    1.30 +    }
    1.31 +    if (/\d/.test(ch)) {
    1.32 +      stream.eatWhile(/[\w\.]/);
    1.33 +      return "number";
    1.34 +    }
    1.35 +    if (ch == "/") {
    1.36 +      if (stream.eat("*")) {
    1.37 +        state.tokenize = tokenComment;
    1.38 +        return tokenComment(stream, state);
    1.39 +      }
    1.40 +      if (stream.eat("/")) {
    1.41 +        stream.skipToEnd();
    1.42 +        return "comment";
    1.43 +      }
    1.44 +    }
    1.45 +    if (isOperatorChar.test(ch)) {
    1.46 +      stream.eatWhile(isOperatorChar);
    1.47 +      return "operator";
    1.48 +    }
    1.49 +    stream.eatWhile(/[\w\$_]/);
    1.50 +    var cur = stream.current();
    1.51 +    if (keywords.propertyIsEnumerable(cur)) {
    1.52 +      if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
    1.53 +      return "keyword";
    1.54 +    }
    1.55 +    if (builtin.propertyIsEnumerable(cur)) {
    1.56 +      if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
    1.57 +      return "builtin";
    1.58 +    }
    1.59 +    if (atoms.propertyIsEnumerable(cur)) return "atom";
    1.60 +    return "variable";
    1.61 +  }
    1.62 +
    1.63 +  function tokenString(quote) {
    1.64 +    return function(stream, state) {
    1.65 +      var escaped = false, next, end = false;
    1.66 +      while ((next = stream.next()) != null) {
    1.67 +        if (next == quote && !escaped) {end = true; break;}
    1.68 +        escaped = !escaped && next == "\\";
    1.69 +      }
    1.70 +      if (end || !(escaped || multiLineStrings))
    1.71 +        state.tokenize = null;
    1.72 +      return "string";
    1.73 +    };
    1.74 +  }
    1.75 +
    1.76 +  function tokenComment(stream, state) {
    1.77 +    var maybeEnd = false, ch;
    1.78 +    while (ch = stream.next()) {
    1.79 +      if (ch == "/" && maybeEnd) {
    1.80 +        state.tokenize = null;
    1.81 +        break;
    1.82 +      }
    1.83 +      maybeEnd = (ch == "*");
    1.84 +    }
    1.85 +    return "comment";
    1.86 +  }
    1.87 +
    1.88 +  function Context(indented, column, type, align, prev) {
    1.89 +    this.indented = indented;
    1.90 +    this.column = column;
    1.91 +    this.type = type;
    1.92 +    this.align = align;
    1.93 +    this.prev = prev;
    1.94 +  }
    1.95 +  function pushContext(state, col, type) {
    1.96 +    var indent = state.indented;
    1.97 +    if (state.context && state.context.type == "statement")
    1.98 +      indent = state.context.indented;
    1.99 +    return state.context = new Context(indent, col, type, null, state.context);
   1.100 +  }
   1.101 +  function popContext(state) {
   1.102 +    var t = state.context.type;
   1.103 +    if (t == ")" || t == "]" || t == "}")
   1.104 +      state.indented = state.context.indented;
   1.105 +    return state.context = state.context.prev;
   1.106 +  }
   1.107 +
   1.108 +  // Interface
   1.109 +
   1.110 +  return {
   1.111 +    startState: function(basecolumn) {
   1.112 +      return {
   1.113 +        tokenize: null,
   1.114 +        context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
   1.115 +        indented: 0,
   1.116 +        startOfLine: true
   1.117 +      };
   1.118 +    },
   1.119 +
   1.120 +    token: function(stream, state) {
   1.121 +      var ctx = state.context;
   1.122 +      if (stream.sol()) {
   1.123 +        if (ctx.align == null) ctx.align = false;
   1.124 +        state.indented = stream.indentation();
   1.125 +        state.startOfLine = true;
   1.126 +      }
   1.127 +      if (stream.eatSpace()) return null;
   1.128 +      curPunc = null;
   1.129 +      var style = (state.tokenize || tokenBase)(stream, state);
   1.130 +      if (style == "comment" || style == "meta") return style;
   1.131 +      if (ctx.align == null) ctx.align = true;
   1.132 +
   1.133 +      if ((curPunc == ";" || curPunc == ":" || curPunc == ",") && ctx.type == "statement") popContext(state);
   1.134 +      else if (curPunc == "{") pushContext(state, stream.column(), "}");
   1.135 +      else if (curPunc == "[") pushContext(state, stream.column(), "]");
   1.136 +      else if (curPunc == "(") pushContext(state, stream.column(), ")");
   1.137 +      else if (curPunc == "}") {
   1.138 +        while (ctx.type == "statement") ctx = popContext(state);
   1.139 +        if (ctx.type == "}") ctx = popContext(state);
   1.140 +        while (ctx.type == "statement") ctx = popContext(state);
   1.141 +      }
   1.142 +      else if (curPunc == ctx.type) popContext(state);
   1.143 +      else if (((ctx.type == "}" || ctx.type == "top") && curPunc != ';') || (ctx.type == "statement" && curPunc == "newstatement"))
   1.144 +        pushContext(state, stream.column(), "statement");
   1.145 +      state.startOfLine = false;
   1.146 +      return style;
   1.147 +    },
   1.148 +
   1.149 +    indent: function(state, textAfter) {
   1.150 +      if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass;
   1.151 +      var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
   1.152 +      if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev;
   1.153 +      var closing = firstChar == ctx.type;
   1.154 +      if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : statementIndentUnit);
   1.155 +      else if (ctx.align) return ctx.column + (closing ? 0 : 1);
   1.156 +      else return ctx.indented + (closing ? 0 : indentUnit);
   1.157 +    },
   1.158 +
   1.159 +    electricChars: "{}"
   1.160 +  };
   1.161 +});
   1.162 +
   1.163 +(function() {
   1.164 +  function words(str) {
   1.165 +    var obj = {}, words = str.split(" ");
   1.166 +    for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
   1.167 +    return obj;
   1.168 +  }
   1.169 +  var cKeywords = "auto if break int case long char register continue return default short do sizeof " +
   1.170 +    "double static else struct entry switch extern typedef float union for unsigned " +
   1.171 +    "goto while enum void const signed volatile";
   1.172 +
   1.173 +  function cppHook(stream, state) {
   1.174 +    if (!state.startOfLine) return false;
   1.175 +    for (;;) {
   1.176 +      if (stream.skipTo("\\")) {
   1.177 +        stream.next();
   1.178 +        if (stream.eol()) {
   1.179 +          state.tokenize = cppHook;
   1.180 +          break;
   1.181 +        }
   1.182 +      } else {
   1.183 +        stream.skipToEnd();
   1.184 +        state.tokenize = null;
   1.185 +        break;
   1.186 +      }
   1.187 +    }
   1.188 +    return "meta";
   1.189 +  }
   1.190 +
   1.191 +  // C#-style strings where "" escapes a quote.
   1.192 +  function tokenAtString(stream, state) {
   1.193 +    var next;
   1.194 +    while ((next = stream.next()) != null) {
   1.195 +      if (next == '"' && !stream.eat('"')) {
   1.196 +        state.tokenize = null;
   1.197 +        break;
   1.198 +      }
   1.199 +    }
   1.200 +    return "string";
   1.201 +  }
   1.202 +
   1.203 +  function mimes(ms, mode) {
   1.204 +    for (var i = 0; i < ms.length; ++i) CodeMirror.defineMIME(ms[i], mode);
   1.205 +  }
   1.206 +
   1.207 +  mimes(["text/x-csrc", "text/x-c", "text/x-chdr"], {
   1.208 +    name: "clike",
   1.209 +    keywords: words(cKeywords),
   1.210 +    blockKeywords: words("case do else for if switch while struct"),
   1.211 +    atoms: words("null"),
   1.212 +    hooks: {"#": cppHook}
   1.213 +  });
   1.214 +  mimes(["text/x-c++src", "text/x-c++hdr"], {
   1.215 +    name: "clike",
   1.216 +    keywords: words(cKeywords + " asm dynamic_cast namespace reinterpret_cast try bool explicit new " +
   1.217 +                    "static_cast typeid catch operator template typename class friend private " +
   1.218 +                    "this using const_cast inline public throw virtual delete mutable protected " +
   1.219 +                    "wchar_t"),
   1.220 +    blockKeywords: words("catch class do else finally for if struct switch try while"),
   1.221 +    atoms: words("true false null"),
   1.222 +    hooks: {"#": cppHook}
   1.223 +  });
   1.224 +  CodeMirror.defineMIME("text/x-java", {
   1.225 +    name: "clike",
   1.226 +    keywords: words("abstract assert boolean break byte case catch char class const continue default " + 
   1.227 +                    "do double else enum extends final finally float for goto if implements import " +
   1.228 +                    "instanceof int interface long native new package private protected public " +
   1.229 +                    "return short static strictfp super switch synchronized this throw throws transient " +
   1.230 +                    "try void volatile while"),
   1.231 +    blockKeywords: words("catch class do else finally for if switch try while"),
   1.232 +    atoms: words("true false null"),
   1.233 +    hooks: {
   1.234 +      "@": function(stream) {
   1.235 +        stream.eatWhile(/[\w\$_]/);
   1.236 +        return "meta";
   1.237 +      }
   1.238 +    }
   1.239 +  });
   1.240 +  CodeMirror.defineMIME("text/x-csharp", {
   1.241 +    name: "clike",
   1.242 +    keywords: words("abstract as base break case catch checked class const continue" + 
   1.243 +                    " default delegate do else enum event explicit extern finally fixed for" + 
   1.244 +                    " foreach goto if implicit in interface internal is lock namespace new" + 
   1.245 +                    " operator out override params private protected public readonly ref return sealed" + 
   1.246 +                    " sizeof stackalloc static struct switch this throw try typeof unchecked" + 
   1.247 +                    " unsafe using virtual void volatile while add alias ascending descending dynamic from get" + 
   1.248 +                    " global group into join let orderby partial remove select set value var yield"),
   1.249 +    blockKeywords: words("catch class do else finally for foreach if struct switch try while"),
   1.250 +    builtin: words("Boolean Byte Char DateTime DateTimeOffset Decimal Double" +
   1.251 +                    " Guid Int16 Int32 Int64 Object SByte Single String TimeSpan UInt16 UInt32" +
   1.252 +                    " UInt64 bool byte char decimal double short int long object"  +
   1.253 +                    " sbyte float string ushort uint ulong"),
   1.254 +    atoms: words("true false null"),
   1.255 +    hooks: {
   1.256 +      "@": function(stream, state) {
   1.257 +        if (stream.eat('"')) {
   1.258 +          state.tokenize = tokenAtString;
   1.259 +          return tokenAtString(stream, state);
   1.260 +        }
   1.261 +        stream.eatWhile(/[\w\$_]/);
   1.262 +        return "meta";
   1.263 +      }
   1.264 +    }
   1.265 +  });
   1.266 +  CodeMirror.defineMIME("text/x-scala", {
   1.267 +    name: "clike",
   1.268 +    keywords: words(
   1.269 +      
   1.270 +      /* scala */
   1.271 +      "abstract case catch class def do else extends false final finally for forSome if " +
   1.272 +      "implicit import lazy match new null object override package private protected return " +
   1.273 +      "sealed super this throw trait try trye type val var while with yield _ : = => <- <: " +
   1.274 +      "<% >: # @ " +
   1.275 +                    
   1.276 +      /* package scala */
   1.277 +      "assert assume require print println printf readLine readBoolean readByte readShort " +
   1.278 +      "readChar readInt readLong readFloat readDouble " +
   1.279 +      
   1.280 +      "AnyVal App Application Array BufferedIterator BigDecimal BigInt Char Console Either " +
   1.281 +      "Enumeration Equiv Error Exception Fractional Function IndexedSeq Integral Iterable " +
   1.282 +      "Iterator List Map Numeric Nil NotNull Option Ordered Ordering PartialFunction PartialOrdering " +
   1.283 +      "Product Proxy Range Responder Seq Serializable Set Specializable Stream StringBuilder " +
   1.284 +      "StringContext Symbol Throwable Traversable TraversableOnce Tuple Unit Vector :: #:: " +
   1.285 +      
   1.286 +      /* package java.lang */            
   1.287 +      "Boolean Byte Character CharSequence Class ClassLoader Cloneable Comparable " +
   1.288 +      "Compiler Double Exception Float Integer Long Math Number Object Package Pair Process " +
   1.289 +      "Runtime Runnable SecurityManager Short StackTraceElement StrictMath String " +
   1.290 +      "StringBuffer System Thread ThreadGroup ThreadLocal Throwable Triple Void"
   1.291 +      
   1.292 +      
   1.293 +    ),
   1.294 +    blockKeywords: words("catch class do else finally for forSome if match switch try while"),
   1.295 +    atoms: words("true false null"),
   1.296 +    hooks: {
   1.297 +      "@": function(stream) {
   1.298 +        stream.eatWhile(/[\w\$_]/);
   1.299 +        return "meta";
   1.300 +      }
   1.301 +    }
   1.302 +  });
   1.303 +}());