browser/devtools/sourceeditor/codemirror/clike.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 (function(mod) {
michael@0 2 if (typeof exports == "object" && typeof module == "object") // CommonJS
michael@0 3 mod(require("../../lib/codemirror"));
michael@0 4 else if (typeof define == "function" && define.amd) // AMD
michael@0 5 define(["../../lib/codemirror"], mod);
michael@0 6 else // Plain browser env
michael@0 7 mod(CodeMirror);
michael@0 8 })(function(CodeMirror) {
michael@0 9 "use strict";
michael@0 10
michael@0 11 CodeMirror.defineMode("clike", function(config, parserConfig) {
michael@0 12 var indentUnit = config.indentUnit,
michael@0 13 statementIndentUnit = parserConfig.statementIndentUnit || indentUnit,
michael@0 14 dontAlignCalls = parserConfig.dontAlignCalls,
michael@0 15 keywords = parserConfig.keywords || {},
michael@0 16 builtin = parserConfig.builtin || {},
michael@0 17 blockKeywords = parserConfig.blockKeywords || {},
michael@0 18 atoms = parserConfig.atoms || {},
michael@0 19 hooks = parserConfig.hooks || {},
michael@0 20 multiLineStrings = parserConfig.multiLineStrings;
michael@0 21 var isOperatorChar = /[+\-*&%=<>!?|\/]/;
michael@0 22
michael@0 23 var curPunc;
michael@0 24
michael@0 25 function tokenBase(stream, state) {
michael@0 26 var ch = stream.next();
michael@0 27 if (hooks[ch]) {
michael@0 28 var result = hooks[ch](stream, state);
michael@0 29 if (result !== false) return result;
michael@0 30 }
michael@0 31 if (ch == '"' || ch == "'") {
michael@0 32 state.tokenize = tokenString(ch);
michael@0 33 return state.tokenize(stream, state);
michael@0 34 }
michael@0 35 if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
michael@0 36 curPunc = ch;
michael@0 37 return null;
michael@0 38 }
michael@0 39 if (/\d/.test(ch)) {
michael@0 40 stream.eatWhile(/[\w\.]/);
michael@0 41 return "number";
michael@0 42 }
michael@0 43 if (ch == "/") {
michael@0 44 if (stream.eat("*")) {
michael@0 45 state.tokenize = tokenComment;
michael@0 46 return tokenComment(stream, state);
michael@0 47 }
michael@0 48 if (stream.eat("/")) {
michael@0 49 stream.skipToEnd();
michael@0 50 return "comment";
michael@0 51 }
michael@0 52 }
michael@0 53 if (isOperatorChar.test(ch)) {
michael@0 54 stream.eatWhile(isOperatorChar);
michael@0 55 return "operator";
michael@0 56 }
michael@0 57 stream.eatWhile(/[\w\$_]/);
michael@0 58 var cur = stream.current();
michael@0 59 if (keywords.propertyIsEnumerable(cur)) {
michael@0 60 if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
michael@0 61 return "keyword";
michael@0 62 }
michael@0 63 if (builtin.propertyIsEnumerable(cur)) {
michael@0 64 if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
michael@0 65 return "builtin";
michael@0 66 }
michael@0 67 if (atoms.propertyIsEnumerable(cur)) return "atom";
michael@0 68 return "variable";
michael@0 69 }
michael@0 70
michael@0 71 function tokenString(quote) {
michael@0 72 return function(stream, state) {
michael@0 73 var escaped = false, next, end = false;
michael@0 74 while ((next = stream.next()) != null) {
michael@0 75 if (next == quote && !escaped) {end = true; break;}
michael@0 76 escaped = !escaped && next == "\\";
michael@0 77 }
michael@0 78 if (end || !(escaped || multiLineStrings))
michael@0 79 state.tokenize = null;
michael@0 80 return "string";
michael@0 81 };
michael@0 82 }
michael@0 83
michael@0 84 function tokenComment(stream, state) {
michael@0 85 var maybeEnd = false, ch;
michael@0 86 while (ch = stream.next()) {
michael@0 87 if (ch == "/" && maybeEnd) {
michael@0 88 state.tokenize = null;
michael@0 89 break;
michael@0 90 }
michael@0 91 maybeEnd = (ch == "*");
michael@0 92 }
michael@0 93 return "comment";
michael@0 94 }
michael@0 95
michael@0 96 function Context(indented, column, type, align, prev) {
michael@0 97 this.indented = indented;
michael@0 98 this.column = column;
michael@0 99 this.type = type;
michael@0 100 this.align = align;
michael@0 101 this.prev = prev;
michael@0 102 }
michael@0 103 function pushContext(state, col, type) {
michael@0 104 var indent = state.indented;
michael@0 105 if (state.context && state.context.type == "statement")
michael@0 106 indent = state.context.indented;
michael@0 107 return state.context = new Context(indent, col, type, null, state.context);
michael@0 108 }
michael@0 109 function popContext(state) {
michael@0 110 var t = state.context.type;
michael@0 111 if (t == ")" || t == "]" || t == "}")
michael@0 112 state.indented = state.context.indented;
michael@0 113 return state.context = state.context.prev;
michael@0 114 }
michael@0 115
michael@0 116 // Interface
michael@0 117
michael@0 118 return {
michael@0 119 startState: function(basecolumn) {
michael@0 120 return {
michael@0 121 tokenize: null,
michael@0 122 context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
michael@0 123 indented: 0,
michael@0 124 startOfLine: true
michael@0 125 };
michael@0 126 },
michael@0 127
michael@0 128 token: function(stream, state) {
michael@0 129 var ctx = state.context;
michael@0 130 if (stream.sol()) {
michael@0 131 if (ctx.align == null) ctx.align = false;
michael@0 132 state.indented = stream.indentation();
michael@0 133 state.startOfLine = true;
michael@0 134 }
michael@0 135 if (stream.eatSpace()) return null;
michael@0 136 curPunc = null;
michael@0 137 var style = (state.tokenize || tokenBase)(stream, state);
michael@0 138 if (style == "comment" || style == "meta") return style;
michael@0 139 if (ctx.align == null) ctx.align = true;
michael@0 140
michael@0 141 if ((curPunc == ";" || curPunc == ":" || curPunc == ",") && ctx.type == "statement") popContext(state);
michael@0 142 else if (curPunc == "{") pushContext(state, stream.column(), "}");
michael@0 143 else if (curPunc == "[") pushContext(state, stream.column(), "]");
michael@0 144 else if (curPunc == "(") pushContext(state, stream.column(), ")");
michael@0 145 else if (curPunc == "}") {
michael@0 146 while (ctx.type == "statement") ctx = popContext(state);
michael@0 147 if (ctx.type == "}") ctx = popContext(state);
michael@0 148 while (ctx.type == "statement") ctx = popContext(state);
michael@0 149 }
michael@0 150 else if (curPunc == ctx.type) popContext(state);
michael@0 151 else if (((ctx.type == "}" || ctx.type == "top") && curPunc != ';') || (ctx.type == "statement" && curPunc == "newstatement"))
michael@0 152 pushContext(state, stream.column(), "statement");
michael@0 153 state.startOfLine = false;
michael@0 154 return style;
michael@0 155 },
michael@0 156
michael@0 157 indent: function(state, textAfter) {
michael@0 158 if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass;
michael@0 159 var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
michael@0 160 if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev;
michael@0 161 var closing = firstChar == ctx.type;
michael@0 162 if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : statementIndentUnit);
michael@0 163 else if (ctx.align && (!dontAlignCalls || ctx.type != ")")) return ctx.column + (closing ? 0 : 1);
michael@0 164 else if (ctx.type == ")" && !closing) return ctx.indented + statementIndentUnit;
michael@0 165 else return ctx.indented + (closing ? 0 : indentUnit);
michael@0 166 },
michael@0 167
michael@0 168 electricChars: "{}",
michael@0 169 blockCommentStart: "/*",
michael@0 170 blockCommentEnd: "*/",
michael@0 171 lineComment: "//",
michael@0 172 fold: "brace"
michael@0 173 };
michael@0 174 });
michael@0 175
michael@0 176 (function() {
michael@0 177 function words(str) {
michael@0 178 var obj = {}, words = str.split(" ");
michael@0 179 for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
michael@0 180 return obj;
michael@0 181 }
michael@0 182 var cKeywords = "auto if break int case long char register continue return default short do sizeof " +
michael@0 183 "double static else struct entry switch extern typedef float union for unsigned " +
michael@0 184 "goto while enum void const signed volatile";
michael@0 185
michael@0 186 function cppHook(stream, state) {
michael@0 187 if (!state.startOfLine) return false;
michael@0 188 for (;;) {
michael@0 189 if (stream.skipTo("\\")) {
michael@0 190 stream.next();
michael@0 191 if (stream.eol()) {
michael@0 192 state.tokenize = cppHook;
michael@0 193 break;
michael@0 194 }
michael@0 195 } else {
michael@0 196 stream.skipToEnd();
michael@0 197 state.tokenize = null;
michael@0 198 break;
michael@0 199 }
michael@0 200 }
michael@0 201 return "meta";
michael@0 202 }
michael@0 203
michael@0 204 function cpp11StringHook(stream, state) {
michael@0 205 stream.backUp(1);
michael@0 206 // Raw strings.
michael@0 207 if (stream.match(/(R|u8R|uR|UR|LR)/)) {
michael@0 208 var match = stream.match(/"(.{0,16})\(/);
michael@0 209 if (!match) {
michael@0 210 return false;
michael@0 211 }
michael@0 212 state.cpp11RawStringDelim = match[1];
michael@0 213 state.tokenize = tokenRawString;
michael@0 214 return tokenRawString(stream, state);
michael@0 215 }
michael@0 216 // Unicode strings/chars.
michael@0 217 if (stream.match(/(u8|u|U|L)/)) {
michael@0 218 if (stream.match(/["']/, /* eat */ false)) {
michael@0 219 return "string";
michael@0 220 }
michael@0 221 return false;
michael@0 222 }
michael@0 223 // Ignore this hook.
michael@0 224 stream.next();
michael@0 225 return false;
michael@0 226 }
michael@0 227
michael@0 228 // C#-style strings where "" escapes a quote.
michael@0 229 function tokenAtString(stream, state) {
michael@0 230 var next;
michael@0 231 while ((next = stream.next()) != null) {
michael@0 232 if (next == '"' && !stream.eat('"')) {
michael@0 233 state.tokenize = null;
michael@0 234 break;
michael@0 235 }
michael@0 236 }
michael@0 237 return "string";
michael@0 238 }
michael@0 239
michael@0 240 // C++11 raw string literal is <prefix>"<delim>( anything )<delim>", where
michael@0 241 // <delim> can be a string up to 16 characters long.
michael@0 242 function tokenRawString(stream, state) {
michael@0 243 var closingSequence = new RegExp(".*?\\)" + state.cpp11RawStringDelim + '"');
michael@0 244 var match = stream.match(closingSequence);
michael@0 245 if (match) {
michael@0 246 state.tokenize = null;
michael@0 247 } else {
michael@0 248 stream.skipToEnd();
michael@0 249 }
michael@0 250 return "string";
michael@0 251 }
michael@0 252
michael@0 253 function def(mimes, mode) {
michael@0 254 var words = [];
michael@0 255 function add(obj) {
michael@0 256 if (obj) for (var prop in obj) if (obj.hasOwnProperty(prop))
michael@0 257 words.push(prop);
michael@0 258 }
michael@0 259 add(mode.keywords);
michael@0 260 add(mode.builtin);
michael@0 261 add(mode.atoms);
michael@0 262 if (words.length) {
michael@0 263 mode.helperType = mimes[0];
michael@0 264 CodeMirror.registerHelper("hintWords", mimes[0], words);
michael@0 265 }
michael@0 266
michael@0 267 for (var i = 0; i < mimes.length; ++i)
michael@0 268 CodeMirror.defineMIME(mimes[i], mode);
michael@0 269 }
michael@0 270
michael@0 271 def(["text/x-csrc", "text/x-c", "text/x-chdr"], {
michael@0 272 name: "clike",
michael@0 273 keywords: words(cKeywords),
michael@0 274 blockKeywords: words("case do else for if switch while struct"),
michael@0 275 atoms: words("null"),
michael@0 276 hooks: {"#": cppHook},
michael@0 277 modeProps: {fold: ["brace", "include"]}
michael@0 278 });
michael@0 279
michael@0 280 def(["text/x-c++src", "text/x-c++hdr"], {
michael@0 281 name: "clike",
michael@0 282 keywords: words(cKeywords + " asm dynamic_cast namespace reinterpret_cast try bool explicit new " +
michael@0 283 "static_cast typeid catch operator template typename class friend private " +
michael@0 284 "this using const_cast inline public throw virtual delete mutable protected " +
michael@0 285 "wchar_t alignas alignof constexpr decltype nullptr noexcept thread_local final " +
michael@0 286 "static_assert override"),
michael@0 287 blockKeywords: words("catch class do else finally for if struct switch try while"),
michael@0 288 atoms: words("true false null"),
michael@0 289 hooks: {
michael@0 290 "#": cppHook,
michael@0 291 "u": cpp11StringHook,
michael@0 292 "U": cpp11StringHook,
michael@0 293 "L": cpp11StringHook,
michael@0 294 "R": cpp11StringHook
michael@0 295 },
michael@0 296 modeProps: {fold: ["brace", "include"]}
michael@0 297 });
michael@0 298 CodeMirror.defineMIME("text/x-java", {
michael@0 299 name: "clike",
michael@0 300 keywords: words("abstract assert boolean break byte case catch char class const continue default " +
michael@0 301 "do double else enum extends final finally float for goto if implements import " +
michael@0 302 "instanceof int interface long native new package private protected public " +
michael@0 303 "return short static strictfp super switch synchronized this throw throws transient " +
michael@0 304 "try void volatile while"),
michael@0 305 blockKeywords: words("catch class do else finally for if switch try while"),
michael@0 306 atoms: words("true false null"),
michael@0 307 hooks: {
michael@0 308 "@": function(stream) {
michael@0 309 stream.eatWhile(/[\w\$_]/);
michael@0 310 return "meta";
michael@0 311 }
michael@0 312 },
michael@0 313 modeProps: {fold: ["brace", "import"]}
michael@0 314 });
michael@0 315 CodeMirror.defineMIME("text/x-csharp", {
michael@0 316 name: "clike",
michael@0 317 keywords: words("abstract as base break case catch checked class const continue" +
michael@0 318 " default delegate do else enum event explicit extern finally fixed for" +
michael@0 319 " foreach goto if implicit in interface internal is lock namespace new" +
michael@0 320 " operator out override params private protected public readonly ref return sealed" +
michael@0 321 " sizeof stackalloc static struct switch this throw try typeof unchecked" +
michael@0 322 " unsafe using virtual void volatile while add alias ascending descending dynamic from get" +
michael@0 323 " global group into join let orderby partial remove select set value var yield"),
michael@0 324 blockKeywords: words("catch class do else finally for foreach if struct switch try while"),
michael@0 325 builtin: words("Boolean Byte Char DateTime DateTimeOffset Decimal Double" +
michael@0 326 " Guid Int16 Int32 Int64 Object SByte Single String TimeSpan UInt16 UInt32" +
michael@0 327 " UInt64 bool byte char decimal double short int long object" +
michael@0 328 " sbyte float string ushort uint ulong"),
michael@0 329 atoms: words("true false null"),
michael@0 330 hooks: {
michael@0 331 "@": function(stream, state) {
michael@0 332 if (stream.eat('"')) {
michael@0 333 state.tokenize = tokenAtString;
michael@0 334 return tokenAtString(stream, state);
michael@0 335 }
michael@0 336 stream.eatWhile(/[\w\$_]/);
michael@0 337 return "meta";
michael@0 338 }
michael@0 339 }
michael@0 340 });
michael@0 341 CodeMirror.defineMIME("text/x-scala", {
michael@0 342 name: "clike",
michael@0 343 keywords: words(
michael@0 344
michael@0 345 /* scala */
michael@0 346 "abstract case catch class def do else extends false final finally for forSome if " +
michael@0 347 "implicit import lazy match new null object override package private protected return " +
michael@0 348 "sealed super this throw trait try trye type val var while with yield _ : = => <- <: " +
michael@0 349 "<% >: # @ " +
michael@0 350
michael@0 351 /* package scala */
michael@0 352 "assert assume require print println printf readLine readBoolean readByte readShort " +
michael@0 353 "readChar readInt readLong readFloat readDouble " +
michael@0 354
michael@0 355 "AnyVal App Application Array BufferedIterator BigDecimal BigInt Char Console Either " +
michael@0 356 "Enumeration Equiv Error Exception Fractional Function IndexedSeq Integral Iterable " +
michael@0 357 "Iterator List Map Numeric Nil NotNull Option Ordered Ordering PartialFunction PartialOrdering " +
michael@0 358 "Product Proxy Range Responder Seq Serializable Set Specializable Stream StringBuilder " +
michael@0 359 "StringContext Symbol Throwable Traversable TraversableOnce Tuple Unit Vector :: #:: " +
michael@0 360
michael@0 361 /* package java.lang */
michael@0 362 "Boolean Byte Character CharSequence Class ClassLoader Cloneable Comparable " +
michael@0 363 "Compiler Double Exception Float Integer Long Math Number Object Package Pair Process " +
michael@0 364 "Runtime Runnable SecurityManager Short StackTraceElement StrictMath String " +
michael@0 365 "StringBuffer System Thread ThreadGroup ThreadLocal Throwable Triple Void"
michael@0 366
michael@0 367
michael@0 368 ),
michael@0 369 blockKeywords: words("catch class do else finally for forSome if match switch try while"),
michael@0 370 atoms: words("true false null"),
michael@0 371 hooks: {
michael@0 372 "@": function(stream) {
michael@0 373 stream.eatWhile(/[\w\$_]/);
michael@0 374 return "meta";
michael@0 375 }
michael@0 376 }
michael@0 377 });
michael@0 378 def(["x-shader/x-vertex", "x-shader/x-fragment"], {
michael@0 379 name: "clike",
michael@0 380 keywords: words("float int bool void " +
michael@0 381 "vec2 vec3 vec4 ivec2 ivec3 ivec4 bvec2 bvec3 bvec4 " +
michael@0 382 "mat2 mat3 mat4 " +
michael@0 383 "sampler1D sampler2D sampler3D samplerCube " +
michael@0 384 "sampler1DShadow sampler2DShadow" +
michael@0 385 "const attribute uniform varying " +
michael@0 386 "break continue discard return " +
michael@0 387 "for while do if else struct " +
michael@0 388 "in out inout"),
michael@0 389 blockKeywords: words("for while do if else struct"),
michael@0 390 builtin: words("radians degrees sin cos tan asin acos atan " +
michael@0 391 "pow exp log exp2 sqrt inversesqrt " +
michael@0 392 "abs sign floor ceil fract mod min max clamp mix step smootstep " +
michael@0 393 "length distance dot cross normalize ftransform faceforward " +
michael@0 394 "reflect refract matrixCompMult " +
michael@0 395 "lessThan lessThanEqual greaterThan greaterThanEqual " +
michael@0 396 "equal notEqual any all not " +
michael@0 397 "texture1D texture1DProj texture1DLod texture1DProjLod " +
michael@0 398 "texture2D texture2DProj texture2DLod texture2DProjLod " +
michael@0 399 "texture3D texture3DProj texture3DLod texture3DProjLod " +
michael@0 400 "textureCube textureCubeLod " +
michael@0 401 "shadow1D shadow2D shadow1DProj shadow2DProj " +
michael@0 402 "shadow1DLod shadow2DLod shadow1DProjLod shadow2DProjLod " +
michael@0 403 "dFdx dFdy fwidth " +
michael@0 404 "noise1 noise2 noise3 noise4"),
michael@0 405 atoms: words("true false " +
michael@0 406 "gl_FragColor gl_SecondaryColor gl_Normal gl_Vertex " +
michael@0 407 "gl_MultiTexCoord0 gl_MultiTexCoord1 gl_MultiTexCoord2 gl_MultiTexCoord3 " +
michael@0 408 "gl_MultiTexCoord4 gl_MultiTexCoord5 gl_MultiTexCoord6 gl_MultiTexCoord7 " +
michael@0 409 "gl_FogCoord " +
michael@0 410 "gl_Position gl_PointSize gl_ClipVertex " +
michael@0 411 "gl_FrontColor gl_BackColor gl_FrontSecondaryColor gl_BackSecondaryColor " +
michael@0 412 "gl_TexCoord gl_FogFragCoord " +
michael@0 413 "gl_FragCoord gl_FrontFacing " +
michael@0 414 "gl_FragColor gl_FragData gl_FragDepth " +
michael@0 415 "gl_ModelViewMatrix gl_ProjectionMatrix gl_ModelViewProjectionMatrix " +
michael@0 416 "gl_TextureMatrix gl_NormalMatrix gl_ModelViewMatrixInverse " +
michael@0 417 "gl_ProjectionMatrixInverse gl_ModelViewProjectionMatrixInverse " +
michael@0 418 "gl_TexureMatrixTranspose gl_ModelViewMatrixInverseTranspose " +
michael@0 419 "gl_ProjectionMatrixInverseTranspose " +
michael@0 420 "gl_ModelViewProjectionMatrixInverseTranspose " +
michael@0 421 "gl_TextureMatrixInverseTranspose " +
michael@0 422 "gl_NormalScale gl_DepthRange gl_ClipPlane " +
michael@0 423 "gl_Point gl_FrontMaterial gl_BackMaterial gl_LightSource gl_LightModel " +
michael@0 424 "gl_FrontLightModelProduct gl_BackLightModelProduct " +
michael@0 425 "gl_TextureColor gl_EyePlaneS gl_EyePlaneT gl_EyePlaneR gl_EyePlaneQ " +
michael@0 426 "gl_FogParameters " +
michael@0 427 "gl_MaxLights gl_MaxClipPlanes gl_MaxTextureUnits gl_MaxTextureCoords " +
michael@0 428 "gl_MaxVertexAttribs gl_MaxVertexUniformComponents gl_MaxVaryingFloats " +
michael@0 429 "gl_MaxVertexTextureImageUnits gl_MaxTextureImageUnits " +
michael@0 430 "gl_MaxFragmentUniformComponents gl_MaxCombineTextureImageUnits " +
michael@0 431 "gl_MaxDrawBuffers"),
michael@0 432 hooks: {"#": cppHook},
michael@0 433 modeProps: {fold: ["brace", "include"]}
michael@0 434 });
michael@0 435 }());
michael@0 436
michael@0 437 });

mercurial