browser/devtools/sourceeditor/codemirror/clike.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/sourceeditor/codemirror/clike.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,437 @@
     1.4 +(function(mod) {
     1.5 +  if (typeof exports == "object" && typeof module == "object") // CommonJS
     1.6 +    mod(require("../../lib/codemirror"));
     1.7 +  else if (typeof define == "function" && define.amd) // AMD
     1.8 +    define(["../../lib/codemirror"], mod);
     1.9 +  else // Plain browser env
    1.10 +    mod(CodeMirror);
    1.11 +})(function(CodeMirror) {
    1.12 +"use strict";
    1.13 +
    1.14 +CodeMirror.defineMode("clike", function(config, parserConfig) {
    1.15 +  var indentUnit = config.indentUnit,
    1.16 +      statementIndentUnit = parserConfig.statementIndentUnit || indentUnit,
    1.17 +      dontAlignCalls = parserConfig.dontAlignCalls,
    1.18 +      keywords = parserConfig.keywords || {},
    1.19 +      builtin = parserConfig.builtin || {},
    1.20 +      blockKeywords = parserConfig.blockKeywords || {},
    1.21 +      atoms = parserConfig.atoms || {},
    1.22 +      hooks = parserConfig.hooks || {},
    1.23 +      multiLineStrings = parserConfig.multiLineStrings;
    1.24 +  var isOperatorChar = /[+\-*&%=<>!?|\/]/;
    1.25 +
    1.26 +  var curPunc;
    1.27 +
    1.28 +  function tokenBase(stream, state) {
    1.29 +    var ch = stream.next();
    1.30 +    if (hooks[ch]) {
    1.31 +      var result = hooks[ch](stream, state);
    1.32 +      if (result !== false) return result;
    1.33 +    }
    1.34 +    if (ch == '"' || ch == "'") {
    1.35 +      state.tokenize = tokenString(ch);
    1.36 +      return state.tokenize(stream, state);
    1.37 +    }
    1.38 +    if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
    1.39 +      curPunc = ch;
    1.40 +      return null;
    1.41 +    }
    1.42 +    if (/\d/.test(ch)) {
    1.43 +      stream.eatWhile(/[\w\.]/);
    1.44 +      return "number";
    1.45 +    }
    1.46 +    if (ch == "/") {
    1.47 +      if (stream.eat("*")) {
    1.48 +        state.tokenize = tokenComment;
    1.49 +        return tokenComment(stream, state);
    1.50 +      }
    1.51 +      if (stream.eat("/")) {
    1.52 +        stream.skipToEnd();
    1.53 +        return "comment";
    1.54 +      }
    1.55 +    }
    1.56 +    if (isOperatorChar.test(ch)) {
    1.57 +      stream.eatWhile(isOperatorChar);
    1.58 +      return "operator";
    1.59 +    }
    1.60 +    stream.eatWhile(/[\w\$_]/);
    1.61 +    var cur = stream.current();
    1.62 +    if (keywords.propertyIsEnumerable(cur)) {
    1.63 +      if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
    1.64 +      return "keyword";
    1.65 +    }
    1.66 +    if (builtin.propertyIsEnumerable(cur)) {
    1.67 +      if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
    1.68 +      return "builtin";
    1.69 +    }
    1.70 +    if (atoms.propertyIsEnumerable(cur)) return "atom";
    1.71 +    return "variable";
    1.72 +  }
    1.73 +
    1.74 +  function tokenString(quote) {
    1.75 +    return function(stream, state) {
    1.76 +      var escaped = false, next, end = false;
    1.77 +      while ((next = stream.next()) != null) {
    1.78 +        if (next == quote && !escaped) {end = true; break;}
    1.79 +        escaped = !escaped && next == "\\";
    1.80 +      }
    1.81 +      if (end || !(escaped || multiLineStrings))
    1.82 +        state.tokenize = null;
    1.83 +      return "string";
    1.84 +    };
    1.85 +  }
    1.86 +
    1.87 +  function tokenComment(stream, state) {
    1.88 +    var maybeEnd = false, ch;
    1.89 +    while (ch = stream.next()) {
    1.90 +      if (ch == "/" && maybeEnd) {
    1.91 +        state.tokenize = null;
    1.92 +        break;
    1.93 +      }
    1.94 +      maybeEnd = (ch == "*");
    1.95 +    }
    1.96 +    return "comment";
    1.97 +  }
    1.98 +
    1.99 +  function Context(indented, column, type, align, prev) {
   1.100 +    this.indented = indented;
   1.101 +    this.column = column;
   1.102 +    this.type = type;
   1.103 +    this.align = align;
   1.104 +    this.prev = prev;
   1.105 +  }
   1.106 +  function pushContext(state, col, type) {
   1.107 +    var indent = state.indented;
   1.108 +    if (state.context && state.context.type == "statement")
   1.109 +      indent = state.context.indented;
   1.110 +    return state.context = new Context(indent, col, type, null, state.context);
   1.111 +  }
   1.112 +  function popContext(state) {
   1.113 +    var t = state.context.type;
   1.114 +    if (t == ")" || t == "]" || t == "}")
   1.115 +      state.indented = state.context.indented;
   1.116 +    return state.context = state.context.prev;
   1.117 +  }
   1.118 +
   1.119 +  // Interface
   1.120 +
   1.121 +  return {
   1.122 +    startState: function(basecolumn) {
   1.123 +      return {
   1.124 +        tokenize: null,
   1.125 +        context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
   1.126 +        indented: 0,
   1.127 +        startOfLine: true
   1.128 +      };
   1.129 +    },
   1.130 +
   1.131 +    token: function(stream, state) {
   1.132 +      var ctx = state.context;
   1.133 +      if (stream.sol()) {
   1.134 +        if (ctx.align == null) ctx.align = false;
   1.135 +        state.indented = stream.indentation();
   1.136 +        state.startOfLine = true;
   1.137 +      }
   1.138 +      if (stream.eatSpace()) return null;
   1.139 +      curPunc = null;
   1.140 +      var style = (state.tokenize || tokenBase)(stream, state);
   1.141 +      if (style == "comment" || style == "meta") return style;
   1.142 +      if (ctx.align == null) ctx.align = true;
   1.143 +
   1.144 +      if ((curPunc == ";" || curPunc == ":" || curPunc == ",") && ctx.type == "statement") popContext(state);
   1.145 +      else if (curPunc == "{") pushContext(state, stream.column(), "}");
   1.146 +      else if (curPunc == "[") pushContext(state, stream.column(), "]");
   1.147 +      else if (curPunc == "(") pushContext(state, stream.column(), ")");
   1.148 +      else if (curPunc == "}") {
   1.149 +        while (ctx.type == "statement") ctx = popContext(state);
   1.150 +        if (ctx.type == "}") ctx = popContext(state);
   1.151 +        while (ctx.type == "statement") ctx = popContext(state);
   1.152 +      }
   1.153 +      else if (curPunc == ctx.type) popContext(state);
   1.154 +      else if (((ctx.type == "}" || ctx.type == "top") && curPunc != ';') || (ctx.type == "statement" && curPunc == "newstatement"))
   1.155 +        pushContext(state, stream.column(), "statement");
   1.156 +      state.startOfLine = false;
   1.157 +      return style;
   1.158 +    },
   1.159 +
   1.160 +    indent: function(state, textAfter) {
   1.161 +      if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass;
   1.162 +      var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
   1.163 +      if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev;
   1.164 +      var closing = firstChar == ctx.type;
   1.165 +      if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : statementIndentUnit);
   1.166 +      else if (ctx.align && (!dontAlignCalls || ctx.type != ")")) return ctx.column + (closing ? 0 : 1);
   1.167 +      else if (ctx.type == ")" && !closing) return ctx.indented + statementIndentUnit;
   1.168 +      else return ctx.indented + (closing ? 0 : indentUnit);
   1.169 +    },
   1.170 +
   1.171 +    electricChars: "{}",
   1.172 +    blockCommentStart: "/*",
   1.173 +    blockCommentEnd: "*/",
   1.174 +    lineComment: "//",
   1.175 +    fold: "brace"
   1.176 +  };
   1.177 +});
   1.178 +
   1.179 +(function() {
   1.180 +  function words(str) {
   1.181 +    var obj = {}, words = str.split(" ");
   1.182 +    for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
   1.183 +    return obj;
   1.184 +  }
   1.185 +  var cKeywords = "auto if break int case long char register continue return default short do sizeof " +
   1.186 +    "double static else struct entry switch extern typedef float union for unsigned " +
   1.187 +    "goto while enum void const signed volatile";
   1.188 +
   1.189 +  function cppHook(stream, state) {
   1.190 +    if (!state.startOfLine) return false;
   1.191 +    for (;;) {
   1.192 +      if (stream.skipTo("\\")) {
   1.193 +        stream.next();
   1.194 +        if (stream.eol()) {
   1.195 +          state.tokenize = cppHook;
   1.196 +          break;
   1.197 +        }
   1.198 +      } else {
   1.199 +        stream.skipToEnd();
   1.200 +        state.tokenize = null;
   1.201 +        break;
   1.202 +      }
   1.203 +    }
   1.204 +    return "meta";
   1.205 +  }
   1.206 +
   1.207 +  function cpp11StringHook(stream, state) {
   1.208 +    stream.backUp(1);
   1.209 +    // Raw strings.
   1.210 +    if (stream.match(/(R|u8R|uR|UR|LR)/)) {
   1.211 +      var match = stream.match(/"(.{0,16})\(/);
   1.212 +      if (!match) {
   1.213 +        return false;
   1.214 +      }
   1.215 +      state.cpp11RawStringDelim = match[1];
   1.216 +      state.tokenize = tokenRawString;
   1.217 +      return tokenRawString(stream, state);
   1.218 +    }
   1.219 +    // Unicode strings/chars.
   1.220 +    if (stream.match(/(u8|u|U|L)/)) {
   1.221 +      if (stream.match(/["']/, /* eat */ false)) {
   1.222 +        return "string";
   1.223 +      }
   1.224 +      return false;
   1.225 +    }
   1.226 +    // Ignore this hook.
   1.227 +    stream.next();
   1.228 +    return false;
   1.229 +  }
   1.230 +
   1.231 +  // C#-style strings where "" escapes a quote.
   1.232 +  function tokenAtString(stream, state) {
   1.233 +    var next;
   1.234 +    while ((next = stream.next()) != null) {
   1.235 +      if (next == '"' && !stream.eat('"')) {
   1.236 +        state.tokenize = null;
   1.237 +        break;
   1.238 +      }
   1.239 +    }
   1.240 +    return "string";
   1.241 +  }
   1.242 +
   1.243 +  // C++11 raw string literal is <prefix>"<delim>( anything )<delim>", where
   1.244 +  // <delim> can be a string up to 16 characters long.
   1.245 +  function tokenRawString(stream, state) {
   1.246 +    var closingSequence = new RegExp(".*?\\)" + state.cpp11RawStringDelim + '"');
   1.247 +    var match = stream.match(closingSequence);
   1.248 +    if (match) {
   1.249 +      state.tokenize = null;
   1.250 +    } else {
   1.251 +      stream.skipToEnd();
   1.252 +    }
   1.253 +    return "string";
   1.254 +  }
   1.255 +
   1.256 +  function def(mimes, mode) {
   1.257 +    var words = [];
   1.258 +    function add(obj) {
   1.259 +      if (obj) for (var prop in obj) if (obj.hasOwnProperty(prop))
   1.260 +        words.push(prop);
   1.261 +    }
   1.262 +    add(mode.keywords);
   1.263 +    add(mode.builtin);
   1.264 +    add(mode.atoms);
   1.265 +    if (words.length) {
   1.266 +      mode.helperType = mimes[0];
   1.267 +      CodeMirror.registerHelper("hintWords", mimes[0], words);
   1.268 +    }
   1.269 +
   1.270 +    for (var i = 0; i < mimes.length; ++i)
   1.271 +      CodeMirror.defineMIME(mimes[i], mode);
   1.272 +  }
   1.273 +
   1.274 +  def(["text/x-csrc", "text/x-c", "text/x-chdr"], {
   1.275 +    name: "clike",
   1.276 +    keywords: words(cKeywords),
   1.277 +    blockKeywords: words("case do else for if switch while struct"),
   1.278 +    atoms: words("null"),
   1.279 +    hooks: {"#": cppHook},
   1.280 +    modeProps: {fold: ["brace", "include"]}
   1.281 +  });
   1.282 +
   1.283 +  def(["text/x-c++src", "text/x-c++hdr"], {
   1.284 +    name: "clike",
   1.285 +    keywords: words(cKeywords + " asm dynamic_cast namespace reinterpret_cast try bool explicit new " +
   1.286 +                    "static_cast typeid catch operator template typename class friend private " +
   1.287 +                    "this using const_cast inline public throw virtual delete mutable protected " +
   1.288 +                    "wchar_t alignas alignof constexpr decltype nullptr noexcept thread_local final " +
   1.289 +                    "static_assert override"),
   1.290 +    blockKeywords: words("catch class do else finally for if struct switch try while"),
   1.291 +    atoms: words("true false null"),
   1.292 +    hooks: {
   1.293 +      "#": cppHook,
   1.294 +      "u": cpp11StringHook,
   1.295 +      "U": cpp11StringHook,
   1.296 +      "L": cpp11StringHook,
   1.297 +      "R": cpp11StringHook
   1.298 +    },
   1.299 +    modeProps: {fold: ["brace", "include"]}
   1.300 +  });
   1.301 +  CodeMirror.defineMIME("text/x-java", {
   1.302 +    name: "clike",
   1.303 +    keywords: words("abstract assert boolean break byte case catch char class const continue default " +
   1.304 +                    "do double else enum extends final finally float for goto if implements import " +
   1.305 +                    "instanceof int interface long native new package private protected public " +
   1.306 +                    "return short static strictfp super switch synchronized this throw throws transient " +
   1.307 +                    "try void volatile while"),
   1.308 +    blockKeywords: words("catch class do else finally for if switch try while"),
   1.309 +    atoms: words("true false null"),
   1.310 +    hooks: {
   1.311 +      "@": function(stream) {
   1.312 +        stream.eatWhile(/[\w\$_]/);
   1.313 +        return "meta";
   1.314 +      }
   1.315 +    },
   1.316 +    modeProps: {fold: ["brace", "import"]}
   1.317 +  });
   1.318 +  CodeMirror.defineMIME("text/x-csharp", {
   1.319 +    name: "clike",
   1.320 +    keywords: words("abstract as base break case catch checked class const continue" +
   1.321 +                    " default delegate do else enum event explicit extern finally fixed for" +
   1.322 +                    " foreach goto if implicit in interface internal is lock namespace new" +
   1.323 +                    " operator out override params private protected public readonly ref return sealed" +
   1.324 +                    " sizeof stackalloc static struct switch this throw try typeof unchecked" +
   1.325 +                    " unsafe using virtual void volatile while add alias ascending descending dynamic from get" +
   1.326 +                    " global group into join let orderby partial remove select set value var yield"),
   1.327 +    blockKeywords: words("catch class do else finally for foreach if struct switch try while"),
   1.328 +    builtin: words("Boolean Byte Char DateTime DateTimeOffset Decimal Double" +
   1.329 +                    " Guid Int16 Int32 Int64 Object SByte Single String TimeSpan UInt16 UInt32" +
   1.330 +                    " UInt64 bool byte char decimal double short int long object"  +
   1.331 +                    " sbyte float string ushort uint ulong"),
   1.332 +    atoms: words("true false null"),
   1.333 +    hooks: {
   1.334 +      "@": function(stream, state) {
   1.335 +        if (stream.eat('"')) {
   1.336 +          state.tokenize = tokenAtString;
   1.337 +          return tokenAtString(stream, state);
   1.338 +        }
   1.339 +        stream.eatWhile(/[\w\$_]/);
   1.340 +        return "meta";
   1.341 +      }
   1.342 +    }
   1.343 +  });
   1.344 +  CodeMirror.defineMIME("text/x-scala", {
   1.345 +    name: "clike",
   1.346 +    keywords: words(
   1.347 +
   1.348 +      /* scala */
   1.349 +      "abstract case catch class def do else extends false final finally for forSome if " +
   1.350 +      "implicit import lazy match new null object override package private protected return " +
   1.351 +      "sealed super this throw trait try trye type val var while with yield _ : = => <- <: " +
   1.352 +      "<% >: # @ " +
   1.353 +
   1.354 +      /* package scala */
   1.355 +      "assert assume require print println printf readLine readBoolean readByte readShort " +
   1.356 +      "readChar readInt readLong readFloat readDouble " +
   1.357 +
   1.358 +      "AnyVal App Application Array BufferedIterator BigDecimal BigInt Char Console Either " +
   1.359 +      "Enumeration Equiv Error Exception Fractional Function IndexedSeq Integral Iterable " +
   1.360 +      "Iterator List Map Numeric Nil NotNull Option Ordered Ordering PartialFunction PartialOrdering " +
   1.361 +      "Product Proxy Range Responder Seq Serializable Set Specializable Stream StringBuilder " +
   1.362 +      "StringContext Symbol Throwable Traversable TraversableOnce Tuple Unit Vector :: #:: " +
   1.363 +
   1.364 +      /* package java.lang */
   1.365 +      "Boolean Byte Character CharSequence Class ClassLoader Cloneable Comparable " +
   1.366 +      "Compiler Double Exception Float Integer Long Math Number Object Package Pair Process " +
   1.367 +      "Runtime Runnable SecurityManager Short StackTraceElement StrictMath String " +
   1.368 +      "StringBuffer System Thread ThreadGroup ThreadLocal Throwable Triple Void"
   1.369 +
   1.370 +
   1.371 +    ),
   1.372 +    blockKeywords: words("catch class do else finally for forSome if match switch try while"),
   1.373 +    atoms: words("true false null"),
   1.374 +    hooks: {
   1.375 +      "@": function(stream) {
   1.376 +        stream.eatWhile(/[\w\$_]/);
   1.377 +        return "meta";
   1.378 +      }
   1.379 +    }
   1.380 +  });
   1.381 +  def(["x-shader/x-vertex", "x-shader/x-fragment"], {
   1.382 +    name: "clike",
   1.383 +    keywords: words("float int bool void " +
   1.384 +                    "vec2 vec3 vec4 ivec2 ivec3 ivec4 bvec2 bvec3 bvec4 " +
   1.385 +                    "mat2 mat3 mat4 " +
   1.386 +                    "sampler1D sampler2D sampler3D samplerCube " +
   1.387 +                    "sampler1DShadow sampler2DShadow" +
   1.388 +                    "const attribute uniform varying " +
   1.389 +                    "break continue discard return " +
   1.390 +                    "for while do if else struct " +
   1.391 +                    "in out inout"),
   1.392 +    blockKeywords: words("for while do if else struct"),
   1.393 +    builtin: words("radians degrees sin cos tan asin acos atan " +
   1.394 +                    "pow exp log exp2 sqrt inversesqrt " +
   1.395 +                    "abs sign floor ceil fract mod min max clamp mix step smootstep " +
   1.396 +                    "length distance dot cross normalize ftransform faceforward " +
   1.397 +                    "reflect refract matrixCompMult " +
   1.398 +                    "lessThan lessThanEqual greaterThan greaterThanEqual " +
   1.399 +                    "equal notEqual any all not " +
   1.400 +                    "texture1D texture1DProj texture1DLod texture1DProjLod " +
   1.401 +                    "texture2D texture2DProj texture2DLod texture2DProjLod " +
   1.402 +                    "texture3D texture3DProj texture3DLod texture3DProjLod " +
   1.403 +                    "textureCube textureCubeLod " +
   1.404 +                    "shadow1D shadow2D shadow1DProj shadow2DProj " +
   1.405 +                    "shadow1DLod shadow2DLod shadow1DProjLod shadow2DProjLod " +
   1.406 +                    "dFdx dFdy fwidth " +
   1.407 +                    "noise1 noise2 noise3 noise4"),
   1.408 +    atoms: words("true false " +
   1.409 +                "gl_FragColor gl_SecondaryColor gl_Normal gl_Vertex " +
   1.410 +                "gl_MultiTexCoord0 gl_MultiTexCoord1 gl_MultiTexCoord2 gl_MultiTexCoord3 " +
   1.411 +                "gl_MultiTexCoord4 gl_MultiTexCoord5 gl_MultiTexCoord6 gl_MultiTexCoord7 " +
   1.412 +                "gl_FogCoord " +
   1.413 +                "gl_Position gl_PointSize gl_ClipVertex " +
   1.414 +                "gl_FrontColor gl_BackColor gl_FrontSecondaryColor gl_BackSecondaryColor " +
   1.415 +                "gl_TexCoord gl_FogFragCoord " +
   1.416 +                "gl_FragCoord gl_FrontFacing " +
   1.417 +                "gl_FragColor gl_FragData gl_FragDepth " +
   1.418 +                "gl_ModelViewMatrix gl_ProjectionMatrix gl_ModelViewProjectionMatrix " +
   1.419 +                "gl_TextureMatrix gl_NormalMatrix gl_ModelViewMatrixInverse " +
   1.420 +                "gl_ProjectionMatrixInverse gl_ModelViewProjectionMatrixInverse " +
   1.421 +                "gl_TexureMatrixTranspose gl_ModelViewMatrixInverseTranspose " +
   1.422 +                "gl_ProjectionMatrixInverseTranspose " +
   1.423 +                "gl_ModelViewProjectionMatrixInverseTranspose " +
   1.424 +                "gl_TextureMatrixInverseTranspose " +
   1.425 +                "gl_NormalScale gl_DepthRange gl_ClipPlane " +
   1.426 +                "gl_Point gl_FrontMaterial gl_BackMaterial gl_LightSource gl_LightModel " +
   1.427 +                "gl_FrontLightModelProduct gl_BackLightModelProduct " +
   1.428 +                "gl_TextureColor gl_EyePlaneS gl_EyePlaneT gl_EyePlaneR gl_EyePlaneQ " +
   1.429 +                "gl_FogParameters " +
   1.430 +                "gl_MaxLights gl_MaxClipPlanes gl_MaxTextureUnits gl_MaxTextureCoords " +
   1.431 +                "gl_MaxVertexAttribs gl_MaxVertexUniformComponents gl_MaxVaryingFloats " +
   1.432 +                "gl_MaxVertexTextureImageUnits gl_MaxTextureImageUnits " +
   1.433 +                "gl_MaxFragmentUniformComponents gl_MaxCombineTextureImageUnits " +
   1.434 +                "gl_MaxDrawBuffers"),
   1.435 +    hooks: {"#": cppHook},
   1.436 +    modeProps: {fold: ["brace", "include"]}
   1.437 +  });
   1.438 +}());
   1.439 +
   1.440 +});

mercurial