|
| 1 | +CodeMirror.defineMode("d", function(config, parserConfig) { |
| 2 | + var indentUnit = config.indentUnit, |
| 3 | + statementIndentUnit = parserConfig.statementIndentUnit || indentUnit, |
| 4 | + keywords = parserConfig.keywords || {}, |
| 5 | + builtin = parserConfig.builtin || {}, |
| 6 | + blockKeywords = parserConfig.blockKeywords || {}, |
| 7 | + atoms = parserConfig.atoms || {}, |
| 8 | + hooks = parserConfig.hooks || {}, |
| 9 | + multiLineStrings = parserConfig.multiLineStrings; |
| 10 | + var isOperatorChar = /[+\-*&%=<>!?|\/]/; |
| 11 | + |
| 12 | + var curPunc; |
| 13 | + |
| 14 | + function tokenBase(stream, state) { |
| 15 | + var ch = stream.next(); |
| 16 | + if (hooks[ch]) { |
| 17 | + var result = hooks[ch](stream, state); |
| 18 | + if (result !== false) return result; |
| 19 | + } |
| 20 | + if (ch == '"' || ch == "'" || ch == "`") { |
| 21 | + state.tokenize = tokenString(ch); |
| 22 | + return state.tokenize(stream, state); |
| 23 | + } |
| 24 | + if (/[\[\]{}\(\),;\:\.]/.test(ch)) { |
| 25 | + curPunc = ch; |
| 26 | + return null; |
| 27 | + } |
| 28 | + if (/\d/.test(ch)) { |
| 29 | + stream.eatWhile(/[\w\.]/); |
| 30 | + return "number"; |
| 31 | + } |
| 32 | + if (ch == "/") { |
| 33 | + if (stream.eat("+")) { |
| 34 | + state.tokenize = tokenComment; |
| 35 | + return tokenNestedComment(stream, state); |
| 36 | + } |
| 37 | + if (stream.eat("*")) { |
| 38 | + state.tokenize = tokenComment; |
| 39 | + return tokenComment(stream, state); |
| 40 | + } |
| 41 | + if (stream.eat("/")) { |
| 42 | + stream.skipToEnd(); |
| 43 | + return "comment"; |
| 44 | + } |
| 45 | + } |
| 46 | + if (isOperatorChar.test(ch)) { |
| 47 | + stream.eatWhile(isOperatorChar); |
| 48 | + return "operator"; |
| 49 | + } |
| 50 | + stream.eatWhile(/[\w\$_]/); |
| 51 | + var cur = stream.current(); |
| 52 | + if (keywords.propertyIsEnumerable(cur)) { |
| 53 | + if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement"; |
| 54 | + return "keyword"; |
| 55 | + } |
| 56 | + if (builtin.propertyIsEnumerable(cur)) { |
| 57 | + if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement"; |
| 58 | + return "builtin"; |
| 59 | + } |
| 60 | + if (atoms.propertyIsEnumerable(cur)) return "atom"; |
| 61 | + return "variable"; |
| 62 | + } |
| 63 | + |
| 64 | + function tokenString(quote) { |
| 65 | + return function(stream, state) { |
| 66 | + var escaped = false, next, end = false; |
| 67 | + while ((next = stream.next()) != null) { |
| 68 | + if (next == quote && !escaped) {end = true; break;} |
| 69 | + escaped = !escaped && next == "\\"; |
| 70 | + } |
| 71 | + if (end || !(escaped || multiLineStrings)) |
| 72 | + state.tokenize = null; |
| 73 | + return "string"; |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + function tokenComment(stream, state) { |
| 78 | + var maybeEnd = false, ch; |
| 79 | + while (ch = stream.next()) { |
| 80 | + if (ch == "/" && maybeEnd) { |
| 81 | + state.tokenize = null; |
| 82 | + break; |
| 83 | + } |
| 84 | + maybeEnd = (ch == "*"); |
| 85 | + } |
| 86 | + return "comment"; |
| 87 | + } |
| 88 | + |
| 89 | + function tokenNestedComment(stream, state) { |
| 90 | + var maybeEnd = false, ch; |
| 91 | + while (ch = stream.next()) { |
| 92 | + if (ch == "/" && maybeEnd) { |
| 93 | + state.tokenize = null; |
| 94 | + break; |
| 95 | + } |
| 96 | + maybeEnd = (ch == "+"); |
| 97 | + } |
| 98 | + return "comment"; |
| 99 | + } |
| 100 | + |
| 101 | + function Context(indented, column, type, align, prev) { |
| 102 | + this.indented = indented; |
| 103 | + this.column = column; |
| 104 | + this.type = type; |
| 105 | + this.align = align; |
| 106 | + this.prev = prev; |
| 107 | + } |
| 108 | + function pushContext(state, col, type) { |
| 109 | + var indent = state.indented; |
| 110 | + if (state.context && state.context.type == "statement") |
| 111 | + indent = state.context.indented; |
| 112 | + return state.context = new Context(indent, col, type, null, state.context); |
| 113 | + } |
| 114 | + function popContext(state) { |
| 115 | + var t = state.context.type; |
| 116 | + if (t == ")" || t == "]" || t == "}") |
| 117 | + state.indented = state.context.indented; |
| 118 | + return state.context = state.context.prev; |
| 119 | + } |
| 120 | + |
| 121 | + // Interface |
| 122 | + |
| 123 | + return { |
| 124 | + startState: function(basecolumn) { |
| 125 | + return { |
| 126 | + tokenize: null, |
| 127 | + context: new Context((basecolumn || 0) - indentUnit, 0, "top", false), |
| 128 | + indented: 0, |
| 129 | + startOfLine: true |
| 130 | + }; |
| 131 | + }, |
| 132 | + |
| 133 | + token: function(stream, state) { |
| 134 | + var ctx = state.context; |
| 135 | + if (stream.sol()) { |
| 136 | + if (ctx.align == null) ctx.align = false; |
| 137 | + state.indented = stream.indentation(); |
| 138 | + state.startOfLine = true; |
| 139 | + } |
| 140 | + if (stream.eatSpace()) return null; |
| 141 | + curPunc = null; |
| 142 | + var style = (state.tokenize || tokenBase)(stream, state); |
| 143 | + if (style == "comment" || style == "meta") return style; |
| 144 | + if (ctx.align == null) ctx.align = true; |
| 145 | + |
| 146 | + if ((curPunc == ";" || curPunc == ":" || curPunc == ",") && ctx.type == "statement") popContext(state); |
| 147 | + else if (curPunc == "{") pushContext(state, stream.column(), "}"); |
| 148 | + else if (curPunc == "[") pushContext(state, stream.column(), "]"); |
| 149 | + else if (curPunc == "(") pushContext(state, stream.column(), ")"); |
| 150 | + else if (curPunc == "}") { |
| 151 | + while (ctx.type == "statement") ctx = popContext(state); |
| 152 | + if (ctx.type == "}") ctx = popContext(state); |
| 153 | + while (ctx.type == "statement") ctx = popContext(state); |
| 154 | + } |
| 155 | + else if (curPunc == ctx.type) popContext(state); |
| 156 | + else if (((ctx.type == "}" || ctx.type == "top") && curPunc != ';') || (ctx.type == "statement" && curPunc == "newstatement")) |
| 157 | + pushContext(state, stream.column(), "statement"); |
| 158 | + state.startOfLine = false; |
| 159 | + return style; |
| 160 | + }, |
| 161 | + |
| 162 | + indent: function(state, textAfter) { |
| 163 | + if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass; |
| 164 | + var ctx = state.context, firstChar = textAfter && textAfter.charAt(0); |
| 165 | + if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev; |
| 166 | + var closing = firstChar == ctx.type; |
| 167 | + if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : statementIndentUnit); |
| 168 | + else if (ctx.align) return ctx.column + (closing ? 0 : 1); |
| 169 | + else return ctx.indented + (closing ? 0 : indentUnit); |
| 170 | + }, |
| 171 | + |
| 172 | + electricChars: "{}" |
| 173 | + }; |
| 174 | +}); |
| 175 | + |
| 176 | +(function() { |
| 177 | + function words(str) { |
| 178 | + var obj = {}, words = str.split(" "); |
| 179 | + for (var i = 0; i < words.length; ++i) obj[words[i]] = true; |
| 180 | + return obj; |
| 181 | + } |
| 182 | + |
| 183 | + var blockKeywords = "body catch class do else enum for foreach foreach_reverse if in interface mixin " + |
| 184 | + "out scope struct switch try union unittest version while with"; |
| 185 | + |
| 186 | + CodeMirror.defineMIME("text/x-d", { |
| 187 | + name: "d", |
| 188 | + keywords: words("abstract alias align asm assert auto break case cast cdouble cent cfloat const continue " + |
| 189 | + "debug default delegate delete deprecated export extern final finally function goto immutable " + |
| 190 | + "import inout invariant is lazy macro module new nothrow override package pragma private " + |
| 191 | + "protected public pure ref return shared short static super synchronized template this " + |
| 192 | + "throw typedef typeid typeof volatile __FILE__ __LINE__ __gshared __traits __vector __parameters " + |
| 193 | + blockKeywords), |
| 194 | + blockKeywords: words(blockKeywords), |
| 195 | + builtin: words("bool byte char creal dchar double float idouble ifloat int ireal long real short ubyte " + |
| 196 | + "ucent uint ulong ushort wchar wstring void size_t sizediff_t"), |
| 197 | + atoms: words("exit failure success true false null"), |
| 198 | + hooks: { |
| 199 | + "@": function(stream, _state) { |
| 200 | + stream.eatWhile(/[\w\$_]/); |
| 201 | + return "meta"; |
| 202 | + } |
| 203 | + } |
| 204 | + }); |
| 205 | +}()); |
0 commit comments