|
| 1 | +// CodeMirror, copyright (c) by Marijn Haverbeke and others |
| 2 | +// Distributed under an MIT license: http://codemirror.net/LICENSE |
| 3 | + |
| 4 | +(function(mod) { |
| 5 | + if (typeof exports == "object" && typeof module == "object") // CommonJS |
| 6 | + mod(require("../../lib/codemirror")); |
| 7 | + else if (typeof define == "function" && define.amd) // AMD |
| 8 | + define(["../../lib/codemirror"], mod); |
| 9 | + else // Plain browser env |
| 10 | + mod(CodeMirror); |
| 11 | +})(function(CodeMirror) { |
| 12 | +"use strict"; |
| 13 | + |
| 14 | +function wordRegexp(words) { |
| 15 | + return new RegExp("^((" + words.join(")|(") + "))\\b"); |
| 16 | +}; |
| 17 | + |
| 18 | +var builtinArray = [ |
| 19 | + "Clamp", |
| 20 | + "Constructor", |
| 21 | + "EnforceRange", |
| 22 | + "Exposed", |
| 23 | + "ImplicitThis", |
| 24 | + "Global", "PrimaryGlobal", |
| 25 | + "LegacyArrayClass", |
| 26 | + "LegacyUnenumerableNamedProperties", |
| 27 | + "LenientThis", |
| 28 | + "NamedConstructor", |
| 29 | + "NewObject", |
| 30 | + "NoInterfaceObject", |
| 31 | + "OverrideBuiltins", |
| 32 | + "PutForwards", |
| 33 | + "Replaceable", |
| 34 | + "SameObject", |
| 35 | + "TreatNonObjectAsNull", |
| 36 | + "TreatNullAs", |
| 37 | + "EmptyString", |
| 38 | + "Unforgeable", |
| 39 | + "Unscopeable" |
| 40 | +]; |
| 41 | +var builtins = wordRegexp(builtinArray); |
| 42 | + |
| 43 | +var typeArray = [ |
| 44 | + "unsigned", "short", "long", // UnsignedIntegerType |
| 45 | + "unrestricted", "float", "double", // UnrestrictedFloatType |
| 46 | + "boolean", "byte", "octet", // Rest of PrimitiveType |
| 47 | + "Promise", // PromiseType |
| 48 | + "ArrayBuffer", "DataView", "Int8Array", "Int16Array", "Int32Array", |
| 49 | + "Uint8Array", "Uint16Array", "Uint32Array", "Uint8ClampedArray", |
| 50 | + "Float32Array", "Float64Array", // BufferRelatedType |
| 51 | + "ByteString", "DOMString", "USVString", "sequence", "object", "RegExp", |
| 52 | + "Error", "DOMException", "FrozenArray", // Rest of NonAnyType |
| 53 | + "any", // Rest of SingleType |
| 54 | + "void" // Rest of ReturnType |
| 55 | +]; |
| 56 | +var types = wordRegexp(typeArray); |
| 57 | + |
| 58 | +var keywordArray = [ |
| 59 | + "attribute", "callback", "const", "deleter", "dictionary", "enum", "getter", |
| 60 | + "implements", "inherit", "interface", "iterable", "legacycaller", "maplike", |
| 61 | + "partial", "required", "serializer", "setlike", "setter", "static", |
| 62 | + "stringifier", "typedef", // ArgumentNameKeyword except |
| 63 | + // "unrestricted" |
| 64 | + "optional", "readonly", "or" |
| 65 | +]; |
| 66 | +var keywords = wordRegexp(keywordArray); |
| 67 | + |
| 68 | +var atomArray = [ |
| 69 | + "true", "false", // BooleanLiteral |
| 70 | + "Infinity", "NaN", // FloatLiteral |
| 71 | + "null" // Rest of ConstValue |
| 72 | +]; |
| 73 | +var atoms = wordRegexp(atomArray); |
| 74 | + |
| 75 | +CodeMirror.registerHelper("hintWords", "webidl", |
| 76 | + builtinArray.concat(typeArray).concat(keywordArray).concat(atomArray)); |
| 77 | + |
| 78 | +var startDefArray = ["callback", "dictionary", "enum", "interface"]; |
| 79 | +var startDefs = wordRegexp(startDefArray); |
| 80 | + |
| 81 | +var endDefArray = ["typedef"]; |
| 82 | +var endDefs = wordRegexp(endDefArray); |
| 83 | + |
| 84 | +var singleOperators = /^[:<=>?]/; |
| 85 | +var integers = /^-?([1-9][0-9]*|0[Xx][0-9A-Fa-f]+|0[0-7]*)/; |
| 86 | +var floats = /^-?(([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)([Ee][+-]?[0-9]+)?|[0-9]+[Ee][+-]?[0-9]+)/; |
| 87 | +var identifiers = /^_?[A-Za-z][0-9A-Z_a-z-]*/; |
| 88 | +var strings = /^"[^"]*"/; |
| 89 | +var multilineComments = /^\/\*.*?\*\//; |
| 90 | +var multilineCommentsStart = /^\/\*.*/; |
| 91 | +var multilineCommentsEnd = /^.*?\*\//; |
| 92 | + |
| 93 | +function readToken(stream, state) { |
| 94 | + // whitespace |
| 95 | + if (stream.eatSpace()) return null; |
| 96 | + |
| 97 | + // comment |
| 98 | + if (state.inComment) { |
| 99 | + if (stream.match(multilineCommentsEnd)) { |
| 100 | + state.inComment = false; |
| 101 | + return "comment"; |
| 102 | + } |
| 103 | + stream.skipToEnd(); |
| 104 | + return "comment"; |
| 105 | + } |
| 106 | + if (stream.match("//")) { |
| 107 | + stream.skipToEnd(); |
| 108 | + return "comment"; |
| 109 | + } |
| 110 | + if (stream.match(multilineComments)) return "comment"; |
| 111 | + if (stream.match(multilineCommentsStart)) { |
| 112 | + state.inComment = true; |
| 113 | + return "comment"; |
| 114 | + } |
| 115 | + |
| 116 | + // integer and float |
| 117 | + if (stream.match(/^-?[0-9\.]/, false)) { |
| 118 | + if (stream.match(integers) || stream.match(floats)) return "number"; |
| 119 | + } |
| 120 | + |
| 121 | + // string |
| 122 | + if (stream.match(strings)) return "string"; |
| 123 | + |
| 124 | + // identifier |
| 125 | + var pos = stream.pos; |
| 126 | + if (stream.match(identifiers)) { |
| 127 | + if (state.startDef) return "def"; |
| 128 | + if (state.endDef && stream.match(/^\s*;/, false)) { |
| 129 | + state.endDef = false; |
| 130 | + return "def"; |
| 131 | + } |
| 132 | + stream.pos = pos; |
| 133 | + } |
| 134 | + |
| 135 | + if (stream.match(keywords)) return "keyword"; |
| 136 | + |
| 137 | + if (stream.match(types)) { |
| 138 | + var lastToken = state.lastToken; |
| 139 | + var nextToken = (stream.match(/^\s*(.+?)\b/, false) || [])[1]; |
| 140 | + |
| 141 | + if (lastToken === ":" || lastToken === "implements" || |
| 142 | + nextToken === "implements" || nextToken === "=") { |
| 143 | + // Used as identifier |
| 144 | + return "builtin"; |
| 145 | + } else { |
| 146 | + // Used as type |
| 147 | + return "variable-3"; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if (stream.match(builtins)) return "builtin"; |
| 152 | + if (stream.match(atoms)) return "atom"; |
| 153 | + if (stream.match(identifiers)) return "variable"; |
| 154 | + |
| 155 | + // other |
| 156 | + if (stream.match(singleOperators)) return "operator"; |
| 157 | + |
| 158 | + // unrecognized |
| 159 | + stream.next(); |
| 160 | + return null; |
| 161 | +}; |
| 162 | + |
| 163 | +CodeMirror.defineMode("webidl", function() { |
| 164 | + return { |
| 165 | + startState: function() { |
| 166 | + return { |
| 167 | + // Is in multiline comment |
| 168 | + inComment: false, |
| 169 | + // Last non-whitespace, matched token |
| 170 | + lastToken: "", |
| 171 | + // Next token is a definition |
| 172 | + startDef: false, |
| 173 | + // Last token of the statement is a definition |
| 174 | + endDef: false |
| 175 | + }; |
| 176 | + }, |
| 177 | + token: function(stream, state) { |
| 178 | + var style = readToken(stream, state); |
| 179 | + |
| 180 | + if (style) { |
| 181 | + var cur = stream.current(); |
| 182 | + state.lastToken = cur; |
| 183 | + if (style === "keyword") { |
| 184 | + state.startDef = startDefs.test(cur); |
| 185 | + state.endDef = state.endDef || endDefs.test(cur); |
| 186 | + } else { |
| 187 | + state.startDef = false; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + return style; |
| 192 | + } |
| 193 | + }; |
| 194 | +}); |
| 195 | + |
| 196 | +CodeMirror.defineMIME("text/x-webidl", "webidl"); |
| 197 | +}); |
0 commit comments