Debian Codegraph @

codemirror-legacy-modes/mode/d.js

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