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