dew/src/main/resources/org/apidesign/bck2brwsr/dew/js/codemirror/mode/clike.js
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Jan 2013 13:18:46 +0100
branchdew
changeset 544 08ffdc3938e7
parent 460 launcher/src/main/resources/org/apidesign/bck2brwsr/dew/js/codemirror/mode/clike.js@c0f1788183dd
permissions -rw-r--r--
Moving Development Environment for Web to own project
phrebejk@460
     1
CodeMirror.defineMode("clike", function(config, parserConfig) {
phrebejk@460
     2
  var indentUnit = config.indentUnit,
phrebejk@460
     3
      statementIndentUnit = parserConfig.statementIndentUnit || indentUnit,
phrebejk@460
     4
      keywords = parserConfig.keywords || {},
phrebejk@460
     5
      builtin = parserConfig.builtin || {},
phrebejk@460
     6
      blockKeywords = parserConfig.blockKeywords || {},
phrebejk@460
     7
      atoms = parserConfig.atoms || {},
phrebejk@460
     8
      hooks = parserConfig.hooks || {},
phrebejk@460
     9
      multiLineStrings = parserConfig.multiLineStrings;
phrebejk@460
    10
  var isOperatorChar = /[+\-*&%=<>!?|\/]/;
phrebejk@460
    11
phrebejk@460
    12
  var curPunc;
phrebejk@460
    13
phrebejk@460
    14
  function tokenBase(stream, state) {
phrebejk@460
    15
    var ch = stream.next();
phrebejk@460
    16
    if (hooks[ch]) {
phrebejk@460
    17
      var result = hooks[ch](stream, state);
phrebejk@460
    18
      if (result !== false) return result;
phrebejk@460
    19
    }
phrebejk@460
    20
    if (ch == '"' || ch == "'") {
phrebejk@460
    21
      state.tokenize = tokenString(ch);
phrebejk@460
    22
      return state.tokenize(stream, state);
phrebejk@460
    23
    }
phrebejk@460
    24
    if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
phrebejk@460
    25
      curPunc = ch;
phrebejk@460
    26
      return null;
phrebejk@460
    27
    }
phrebejk@460
    28
    if (/\d/.test(ch)) {
phrebejk@460
    29
      stream.eatWhile(/[\w\.]/);
phrebejk@460
    30
      return "number";
phrebejk@460
    31
    }
phrebejk@460
    32
    if (ch == "/") {
phrebejk@460
    33
      if (stream.eat("*")) {
phrebejk@460
    34
        state.tokenize = tokenComment;
phrebejk@460
    35
        return tokenComment(stream, state);
phrebejk@460
    36
      }
phrebejk@460
    37
      if (stream.eat("/")) {
phrebejk@460
    38
        stream.skipToEnd();
phrebejk@460
    39
        return "comment";
phrebejk@460
    40
      }
phrebejk@460
    41
    }
phrebejk@460
    42
    if (isOperatorChar.test(ch)) {
phrebejk@460
    43
      stream.eatWhile(isOperatorChar);
phrebejk@460
    44
      return "operator";
phrebejk@460
    45
    }
phrebejk@460
    46
    stream.eatWhile(/[\w\$_]/);
phrebejk@460
    47
    var cur = stream.current();
phrebejk@460
    48
    if (keywords.propertyIsEnumerable(cur)) {
phrebejk@460
    49
      if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
phrebejk@460
    50
      return "keyword";
phrebejk@460
    51
    }
phrebejk@460
    52
    if (builtin.propertyIsEnumerable(cur)) {
phrebejk@460
    53
      if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
phrebejk@460
    54
      return "builtin";
phrebejk@460
    55
    }
phrebejk@460
    56
    if (atoms.propertyIsEnumerable(cur)) return "atom";
phrebejk@460
    57
    return "variable";
phrebejk@460
    58
  }
phrebejk@460
    59
phrebejk@460
    60
  function tokenString(quote) {
phrebejk@460
    61
    return function(stream, state) {
phrebejk@460
    62
      var escaped = false, next, end = false;
phrebejk@460
    63
      while ((next = stream.next()) != null) {
phrebejk@460
    64
        if (next == quote && !escaped) {end = true; break;}
phrebejk@460
    65
        escaped = !escaped && next == "\\";
phrebejk@460
    66
      }
phrebejk@460
    67
      if (end || !(escaped || multiLineStrings))
phrebejk@460
    68
        state.tokenize = null;
phrebejk@460
    69
      return "string";
phrebejk@460
    70
    };
phrebejk@460
    71
  }
phrebejk@460
    72
phrebejk@460
    73
  function tokenComment(stream, state) {
phrebejk@460
    74
    var maybeEnd = false, ch;
phrebejk@460
    75
    while (ch = stream.next()) {
phrebejk@460
    76
      if (ch == "/" && maybeEnd) {
phrebejk@460
    77
        state.tokenize = null;
phrebejk@460
    78
        break;
phrebejk@460
    79
      }
phrebejk@460
    80
      maybeEnd = (ch == "*");
phrebejk@460
    81
    }
phrebejk@460
    82
    return "comment";
phrebejk@460
    83
  }
phrebejk@460
    84
phrebejk@460
    85
  function Context(indented, column, type, align, prev) {
phrebejk@460
    86
    this.indented = indented;
phrebejk@460
    87
    this.column = column;
phrebejk@460
    88
    this.type = type;
phrebejk@460
    89
    this.align = align;
phrebejk@460
    90
    this.prev = prev;
phrebejk@460
    91
  }
phrebejk@460
    92
  function pushContext(state, col, type) {
phrebejk@460
    93
    var indent = state.indented;
phrebejk@460
    94
    if (state.context && state.context.type == "statement")
phrebejk@460
    95
      indent = state.context.indented;
phrebejk@460
    96
    return state.context = new Context(indent, col, type, null, state.context);
phrebejk@460
    97
  }
phrebejk@460
    98
  function popContext(state) {
phrebejk@460
    99
    var t = state.context.type;
phrebejk@460
   100
    if (t == ")" || t == "]" || t == "}")
phrebejk@460
   101
      state.indented = state.context.indented;
phrebejk@460
   102
    return state.context = state.context.prev;
phrebejk@460
   103
  }
phrebejk@460
   104
phrebejk@460
   105
  // Interface
phrebejk@460
   106
phrebejk@460
   107
  return {
phrebejk@460
   108
    startState: function(basecolumn) {
phrebejk@460
   109
      return {
phrebejk@460
   110
        tokenize: null,
phrebejk@460
   111
        context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
phrebejk@460
   112
        indented: 0,
phrebejk@460
   113
        startOfLine: true
phrebejk@460
   114
      };
phrebejk@460
   115
    },
phrebejk@460
   116
phrebejk@460
   117
    token: function(stream, state) {
phrebejk@460
   118
      var ctx = state.context;
phrebejk@460
   119
      if (stream.sol()) {
phrebejk@460
   120
        if (ctx.align == null) ctx.align = false;
phrebejk@460
   121
        state.indented = stream.indentation();
phrebejk@460
   122
        state.startOfLine = true;
phrebejk@460
   123
      }
phrebejk@460
   124
      if (stream.eatSpace()) return null;
phrebejk@460
   125
      curPunc = null;
phrebejk@460
   126
      var style = (state.tokenize || tokenBase)(stream, state);
phrebejk@460
   127
      if (style == "comment" || style == "meta") return style;
phrebejk@460
   128
      if (ctx.align == null) ctx.align = true;
phrebejk@460
   129
phrebejk@460
   130
      if ((curPunc == ";" || curPunc == ":" || curPunc == ",") && ctx.type == "statement") popContext(state);
phrebejk@460
   131
      else if (curPunc == "{") pushContext(state, stream.column(), "}");
phrebejk@460
   132
      else if (curPunc == "[") pushContext(state, stream.column(), "]");
phrebejk@460
   133
      else if (curPunc == "(") pushContext(state, stream.column(), ")");
phrebejk@460
   134
      else if (curPunc == "}") {
phrebejk@460
   135
        while (ctx.type == "statement") ctx = popContext(state);
phrebejk@460
   136
        if (ctx.type == "}") ctx = popContext(state);
phrebejk@460
   137
        while (ctx.type == "statement") ctx = popContext(state);
phrebejk@460
   138
      }
phrebejk@460
   139
      else if (curPunc == ctx.type) popContext(state);
phrebejk@460
   140
      else if (((ctx.type == "}" || ctx.type == "top") && curPunc != ';') || (ctx.type == "statement" && curPunc == "newstatement"))
phrebejk@460
   141
        pushContext(state, stream.column(), "statement");
phrebejk@460
   142
      state.startOfLine = false;
phrebejk@460
   143
      return style;
phrebejk@460
   144
    },
phrebejk@460
   145
phrebejk@460
   146
    indent: function(state, textAfter) {
phrebejk@460
   147
      if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass;
phrebejk@460
   148
      var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
phrebejk@460
   149
      if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev;
phrebejk@460
   150
      var closing = firstChar == ctx.type;
phrebejk@460
   151
      if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : statementIndentUnit);
phrebejk@460
   152
      else if (ctx.align) return ctx.column + (closing ? 0 : 1);
phrebejk@460
   153
      else return ctx.indented + (closing ? 0 : indentUnit);
phrebejk@460
   154
    },
phrebejk@460
   155
phrebejk@460
   156
    electricChars: "{}"
phrebejk@460
   157
  };
phrebejk@460
   158
});
phrebejk@460
   159
phrebejk@460
   160
(function() {
phrebejk@460
   161
  function words(str) {
phrebejk@460
   162
    var obj = {}, words = str.split(" ");
phrebejk@460
   163
    for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
phrebejk@460
   164
    return obj;
phrebejk@460
   165
  }
phrebejk@460
   166
  var cKeywords = "auto if break int case long char register continue return default short do sizeof " +
phrebejk@460
   167
    "double static else struct entry switch extern typedef float union for unsigned " +
phrebejk@460
   168
    "goto while enum void const signed volatile";
phrebejk@460
   169
phrebejk@460
   170
  function cppHook(stream, state) {
phrebejk@460
   171
    if (!state.startOfLine) return false;
phrebejk@460
   172
    for (;;) {
phrebejk@460
   173
      if (stream.skipTo("\\")) {
phrebejk@460
   174
        stream.next();
phrebejk@460
   175
        if (stream.eol()) {
phrebejk@460
   176
          state.tokenize = cppHook;
phrebejk@460
   177
          break;
phrebejk@460
   178
        }
phrebejk@460
   179
      } else {
phrebejk@460
   180
        stream.skipToEnd();
phrebejk@460
   181
        state.tokenize = null;
phrebejk@460
   182
        break;
phrebejk@460
   183
      }
phrebejk@460
   184
    }
phrebejk@460
   185
    return "meta";
phrebejk@460
   186
  }
phrebejk@460
   187
phrebejk@460
   188
  // C#-style strings where "" escapes a quote.
phrebejk@460
   189
  function tokenAtString(stream, state) {
phrebejk@460
   190
    var next;
phrebejk@460
   191
    while ((next = stream.next()) != null) {
phrebejk@460
   192
      if (next == '"' && !stream.eat('"')) {
phrebejk@460
   193
        state.tokenize = null;
phrebejk@460
   194
        break;
phrebejk@460
   195
      }
phrebejk@460
   196
    }
phrebejk@460
   197
    return "string";
phrebejk@460
   198
  }
phrebejk@460
   199
phrebejk@460
   200
  function mimes(ms, mode) {
phrebejk@460
   201
    for (var i = 0; i < ms.length; ++i) CodeMirror.defineMIME(ms[i], mode);
phrebejk@460
   202
  }
phrebejk@460
   203
phrebejk@460
   204
  mimes(["text/x-csrc", "text/x-c", "text/x-chdr"], {
phrebejk@460
   205
    name: "clike",
phrebejk@460
   206
    keywords: words(cKeywords),
phrebejk@460
   207
    blockKeywords: words("case do else for if switch while struct"),
phrebejk@460
   208
    atoms: words("null"),
phrebejk@460
   209
    hooks: {"#": cppHook}
phrebejk@460
   210
  });
phrebejk@460
   211
  mimes(["text/x-c++src", "text/x-c++hdr"], {
phrebejk@460
   212
    name: "clike",
phrebejk@460
   213
    keywords: words(cKeywords + " asm dynamic_cast namespace reinterpret_cast try bool explicit new " +
phrebejk@460
   214
                    "static_cast typeid catch operator template typename class friend private " +
phrebejk@460
   215
                    "this using const_cast inline public throw virtual delete mutable protected " +
phrebejk@460
   216
                    "wchar_t"),
phrebejk@460
   217
    blockKeywords: words("catch class do else finally for if struct switch try while"),
phrebejk@460
   218
    atoms: words("true false null"),
phrebejk@460
   219
    hooks: {"#": cppHook}
phrebejk@460
   220
  });
phrebejk@460
   221
  CodeMirror.defineMIME("text/x-java", {
phrebejk@460
   222
    name: "clike",
phrebejk@460
   223
    keywords: words("abstract assert boolean break byte case catch char class const continue default " + 
phrebejk@460
   224
                    "do double else enum extends final finally float for goto if implements import " +
phrebejk@460
   225
                    "instanceof int interface long native new package private protected public " +
phrebejk@460
   226
                    "return short static strictfp super switch synchronized this throw throws transient " +
phrebejk@460
   227
                    "try void volatile while"),
phrebejk@460
   228
    blockKeywords: words("catch class do else finally for if switch try while"),
phrebejk@460
   229
    atoms: words("true false null"),
phrebejk@460
   230
    hooks: {
phrebejk@460
   231
      "@": function(stream) {
phrebejk@460
   232
        stream.eatWhile(/[\w\$_]/);
phrebejk@460
   233
        return "meta";
phrebejk@460
   234
      }
phrebejk@460
   235
    }
phrebejk@460
   236
  });
phrebejk@460
   237
  CodeMirror.defineMIME("text/x-csharp", {
phrebejk@460
   238
    name: "clike",
phrebejk@460
   239
    keywords: words("abstract as base break case catch checked class const continue" + 
phrebejk@460
   240
                    " default delegate do else enum event explicit extern finally fixed for" + 
phrebejk@460
   241
                    " foreach goto if implicit in interface internal is lock namespace new" + 
phrebejk@460
   242
                    " operator out override params private protected public readonly ref return sealed" + 
phrebejk@460
   243
                    " sizeof stackalloc static struct switch this throw try typeof unchecked" + 
phrebejk@460
   244
                    " unsafe using virtual void volatile while add alias ascending descending dynamic from get" + 
phrebejk@460
   245
                    " global group into join let orderby partial remove select set value var yield"),
phrebejk@460
   246
    blockKeywords: words("catch class do else finally for foreach if struct switch try while"),
phrebejk@460
   247
    builtin: words("Boolean Byte Char DateTime DateTimeOffset Decimal Double" +
phrebejk@460
   248
                    " Guid Int16 Int32 Int64 Object SByte Single String TimeSpan UInt16 UInt32" +
phrebejk@460
   249
                    " UInt64 bool byte char decimal double short int long object"  +
phrebejk@460
   250
                    " sbyte float string ushort uint ulong"),
phrebejk@460
   251
    atoms: words("true false null"),
phrebejk@460
   252
    hooks: {
phrebejk@460
   253
      "@": function(stream, state) {
phrebejk@460
   254
        if (stream.eat('"')) {
phrebejk@460
   255
          state.tokenize = tokenAtString;
phrebejk@460
   256
          return tokenAtString(stream, state);
phrebejk@460
   257
        }
phrebejk@460
   258
        stream.eatWhile(/[\w\$_]/);
phrebejk@460
   259
        return "meta";
phrebejk@460
   260
      }
phrebejk@460
   261
    }
phrebejk@460
   262
  });
phrebejk@460
   263
  CodeMirror.defineMIME("text/x-scala", {
phrebejk@460
   264
    name: "clike",
phrebejk@460
   265
    keywords: words(
phrebejk@460
   266
      
phrebejk@460
   267
      /* scala */
phrebejk@460
   268
      "abstract case catch class def do else extends false final finally for forSome if " +
phrebejk@460
   269
      "implicit import lazy match new null object override package private protected return " +
phrebejk@460
   270
      "sealed super this throw trait try trye type val var while with yield _ : = => <- <: " +
phrebejk@460
   271
      "<% >: # @ " +
phrebejk@460
   272
                    
phrebejk@460
   273
      /* package scala */
phrebejk@460
   274
      "assert assume require print println printf readLine readBoolean readByte readShort " +
phrebejk@460
   275
      "readChar readInt readLong readFloat readDouble " +
phrebejk@460
   276
      
phrebejk@460
   277
      "AnyVal App Application Array BufferedIterator BigDecimal BigInt Char Console Either " +
phrebejk@460
   278
      "Enumeration Equiv Error Exception Fractional Function IndexedSeq Integral Iterable " +
phrebejk@460
   279
      "Iterator List Map Numeric Nil NotNull Option Ordered Ordering PartialFunction PartialOrdering " +
phrebejk@460
   280
      "Product Proxy Range Responder Seq Serializable Set Specializable Stream StringBuilder " +
phrebejk@460
   281
      "StringContext Symbol Throwable Traversable TraversableOnce Tuple Unit Vector :: #:: " +
phrebejk@460
   282
      
phrebejk@460
   283
      /* package java.lang */            
phrebejk@460
   284
      "Boolean Byte Character CharSequence Class ClassLoader Cloneable Comparable " +
phrebejk@460
   285
      "Compiler Double Exception Float Integer Long Math Number Object Package Pair Process " +
phrebejk@460
   286
      "Runtime Runnable SecurityManager Short StackTraceElement StrictMath String " +
phrebejk@460
   287
      "StringBuffer System Thread ThreadGroup ThreadLocal Throwable Triple Void"
phrebejk@460
   288
      
phrebejk@460
   289
      
phrebejk@460
   290
    ),
phrebejk@460
   291
    blockKeywords: words("catch class do else finally for forSome if match switch try while"),
phrebejk@460
   292
    atoms: words("true false null"),
phrebejk@460
   293
    hooks: {
phrebejk@460
   294
      "@": function(stream) {
phrebejk@460
   295
        stream.eatWhile(/[\w\$_]/);
phrebejk@460
   296
        return "meta";
phrebejk@460
   297
      }
phrebejk@460
   298
    }
phrebejk@460
   299
  });
phrebejk@460
   300
}());