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 = /[+\-*&%=<>!?|\/]/;
14 function tokenBase(stream, state) {
15 var ch = stream.next();
17 var result = hooks[ch](stream, state);
18 if (result !== false) return result;
20 if (ch == '"' || ch == "'" || ch == "`") {
21 state.tokenize = tokenString(ch);
22 return state.tokenize(stream, state);
24 if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
29 stream.eatWhile(/[\w\.]/);
33 if (stream.eat("+")) {
34 state.tokenize = tokenComment;
35 return tokenNestedComment(stream, state);
37 if (stream.eat("*")) {
38 state.tokenize = tokenComment;
39 return tokenComment(stream, state);
41 if (stream.eat("/")) {
46 if (isOperatorChar.test(ch)) {
47 stream.eatWhile(isOperatorChar);
50 stream.eatWhile(/[\w\$_]/);
51 var cur = stream.current();
52 if (keywords.propertyIsEnumerable(cur)) {
53 if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
56 if (builtin.propertyIsEnumerable(cur)) {
57 if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
60 if (atoms.propertyIsEnumerable(cur)) return "atom";
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 == "\\";
71 if (end || !(escaped || multiLineStrings))
72 state.tokenize = null;
77 function tokenComment(stream, state) {
78 var maybeEnd = false, ch;
79 while (ch = stream.next()) {
80 if (ch == "/" && maybeEnd) {
81 state.tokenize = null;
84 maybeEnd = (ch == "*");
89 function tokenNestedComment(stream, state) {
90 var maybeEnd = false, ch;
91 while (ch = stream.next()) {
92 if (ch == "/" && maybeEnd) {
93 state.tokenize = null;
96 maybeEnd = (ch == "+");
101 function Context(indented, column, type, align, prev) {
102 this.indented = indented;
103 this.column = column;
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);
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;
124 startState: function(basecolumn) {
127 context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
133 token: function(stream, state) {
134 var ctx = state.context;
136 if (ctx.align == null) ctx.align = false;
137 state.indented = stream.indentation();
138 state.startOfLine = true;
140 if (stream.eatSpace()) return 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;
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);
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;
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);
177 function words(str) {
178 var obj = {}, words = str.split(" ");
179 for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
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";
186 CodeMirror.defineMIME("text/x-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 " +
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"),
199 "@": function(stream, _state) {
200 stream.eatWhile(/[\w\$_]/);