5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
CodeMirror.defineMode("d", function(config, parserConfig) {
15
var indentUnit = config.indentUnit,
16
statementIndentUnit = parserConfig.statementIndentUnit || indentUnit,
17
keywords = parserConfig.keywords || {},
18
builtin = parserConfig.builtin || {},
19
blockKeywords = parserConfig.blockKeywords || {},
20
atoms = parserConfig.atoms || {},
21
hooks = parserConfig.hooks || {},
22
multiLineStrings = parserConfig.multiLineStrings;
23
var isOperatorChar = /[+\-*&%=<>!?|\/]/;
27
function tokenBase(stream, state) {
28
var ch = stream.next();
30
var result = hooks[ch](stream, state);
31
if (result !== false) return result;
33
if (ch == '"' || ch == "'" || ch == "`") {
34
state.tokenize = tokenString(ch);
35
return state.tokenize(stream, state);
37
if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
42
stream.eatWhile(/[\w\.]/);
46
if (stream.eat("+")) {
47
state.tokenize = tokenNestedComment;
48
return tokenNestedComment(stream, state);
50
if (stream.eat("*")) {
51
state.tokenize = tokenComment;
52
return tokenComment(stream, state);
54
if (stream.eat("/")) {
59
if (isOperatorChar.test(ch)) {
60
stream.eatWhile(isOperatorChar);
63
stream.eatWhile(/[\w\$_\xa1-\uffff]/);
64
var cur = stream.current();
65
if (keywords.propertyIsEnumerable(cur)) {
66
if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
69
if (builtin.propertyIsEnumerable(cur)) {
70
if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
73
if (atoms.propertyIsEnumerable(cur)) return "atom";
77
function tokenString(quote) {
78
return function(stream, state) {
79
var escaped = false, next, end = false;
80
while ((next = stream.next()) != null) {
81
if (next == quote && !escaped) {end = true; break;}
82
escaped = !escaped && next == "\\";
84
if (end || !(escaped || multiLineStrings))
85
state.tokenize = null;
90
function tokenComment(stream, state) {
91
var maybeEnd = false, ch;
92
while (ch = stream.next()) {
93
if (ch == "/" && maybeEnd) {
94
state.tokenize = null;
97
maybeEnd = (ch == "*");
102
function tokenNestedComment(stream, state) {
103
var maybeEnd = false, ch;
104
while (ch = stream.next()) {
105
if (ch == "/" && maybeEnd) {
106
state.tokenize = null;
109
maybeEnd = (ch == "+");
114
function Context(indented, column, type, align, prev) {
115
this.indented = indented;
116
this.column = column;
121
function pushContext(state, col, type) {
122
var indent = state.indented;
123
if (state.context && state.context.type == "statement")
124
indent = state.context.indented;
125
return state.context = new Context(indent, col, type, null, state.context);
127
function popContext(state) {
128
var t = state.context.type;
129
if (t == ")" || t == "]" || t == "}")
130
state.indented = state.context.indented;
131
return state.context = state.context.prev;
137
startState: function(basecolumn) {
140
context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
146
token: function(stream, state) {
147
var ctx = state.context;
149
if (ctx.align == null) ctx.align = false;
150
state.indented = stream.indentation();
151
state.startOfLine = true;
153
if (stream.eatSpace()) return null;
155
var style = (state.tokenize || tokenBase)(stream, state);
156
if (style == "comment" || style == "meta") return style;
157
if (ctx.align == null) ctx.align = true;
159
if ((curPunc == ";" || curPunc == ":" || curPunc == ",") && ctx.type == "statement") popContext(state);
160
else if (curPunc == "{") pushContext(state, stream.column(), "}");
161
else if (curPunc == "[") pushContext(state, stream.column(), "]");
162
else if (curPunc == "(") pushContext(state, stream.column(), ")");
163
else if (curPunc == "}") {
164
while (ctx.type == "statement") ctx = popContext(state);
165
if (ctx.type == "}") ctx = popContext(state);
166
while (ctx.type == "statement") ctx = popContext(state);
168
else if (curPunc == ctx.type) popContext(state);
169
else if (((ctx.type == "}" || ctx.type == "top") && curPunc != ';') || (ctx.type == "statement" && curPunc == "newstatement"))
170
pushContext(state, stream.column(), "statement");
171
state.startOfLine = false;
175
indent: function(state, textAfter) {
176
if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass;
177
var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
178
if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev;
179
var closing = firstChar == ctx.type;
180
if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : statementIndentUnit);
181
else if (ctx.align) return ctx.column + (closing ? 0 : 1);
182
else return ctx.indented + (closing ? 0 : indentUnit);
186
blockCommentStart: "/*",
187
blockCommentEnd: "*/",
188
blockCommentContinue: " * ",
194
function words(str) {
195
var obj = {}, words = str.split(" ");
196
for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
200
var blockKeywords = "body catch class do else enum for foreach foreach_reverse if in interface mixin " +
201
"out scope struct switch try union unittest version while with";
203
CodeMirror.defineMIME("text/x-d", {
205
keywords: words("abstract alias align asm assert auto break case cast cdouble cent cfloat const continue " +
206
"debug default delegate delete deprecated export extern final finally function goto immutable " +
207
"import inout invariant is lazy macro module new nothrow override package pragma private " +
208
"protected public pure ref return shared short static super synchronized template this " +
209
"throw typedef typeid typeof volatile __FILE__ __LINE__ __gshared __traits __vector __parameters " +
211
blockKeywords: words(blockKeywords),
212
builtin: words("bool byte char creal dchar double float idouble ifloat int ireal long real short ubyte " +
213
"ucent uint ulong ushort wchar wstring void size_t sizediff_t"),
214
atoms: words("exit failure success true false null"),
216
"@": function(stream, _state) {
217
stream.eatWhile(/[\w\$_]/);