Debian Codegraph @

codemirror-legacy-modes/mode/dylan.js

raw
1 function forEach(arr, f) {
2 for (var i = 0; i < arr.length; i++) f(arr[i], i)
3 }
4 function some(arr, f) {
5 for (var i = 0; i < arr.length; i++) if (f(arr[i], i)) return true
6 return false
7 }
8
9 // Words
10 var words = {
11 // Words that introduce unnamed definitions like "define interface"
12 unnamedDefinition: ["interface"],
13
14 // Words that introduce simple named definitions like "define library"
15 namedDefinition: ["module", "library", "macro",
16 "C-struct", "C-union",
17 "C-function", "C-callable-wrapper"
18 ],
19
20 // Words that introduce type definitions like "define class".
21 // These are also parameterized like "define method" and are
22 // appended to otherParameterizedDefinitionWords
23 typeParameterizedDefinition: ["class", "C-subtype", "C-mapped-subtype"],
24
25 // Words that introduce trickier definitions like "define method".
26 // These require special definitions to be added to startExpressions
27 otherParameterizedDefinition: ["method", "function",
28 "C-variable", "C-address"
29 ],
30
31 // Words that introduce module constant definitions.
32 // These must also be simple definitions and are
33 // appended to otherSimpleDefinitionWords
34 constantSimpleDefinition: ["constant"],
35
36 // Words that introduce module variable definitions.
37 // These must also be simple definitions and are
38 // appended to otherSimpleDefinitionWords
39 variableSimpleDefinition: ["variable"],
40
41 // Other words that introduce simple definitions
42 // (without implicit bodies).
43 otherSimpleDefinition: ["generic", "domain",
44 "C-pointer-type",
45 "table"
46 ],
47
48 // Words that begin statements with implicit bodies.
49 statement: ["if", "block", "begin", "method", "case",
50 "for", "select", "when", "unless", "until",
51 "while", "iterate", "profiling", "dynamic-bind"
52 ],
53
54 // Patterns that act as separators in compound statements.
55 // This may include any general pattern that must be indented
56 // specially.
57 separator: ["finally", "exception", "cleanup", "else",
58 "elseif", "afterwards"
59 ],
60
61 // Keywords that do not require special indentation handling,
62 // but which should be highlighted
63 other: ["above", "below", "by", "from", "handler", "in",
64 "instance", "let", "local", "otherwise", "slot",
65 "subclass", "then", "to", "keyed-by", "virtual"
66 ],
67
68 // Condition signaling function calls
69 signalingCalls: ["signal", "error", "cerror",
70 "break", "check-type", "abort"
71 ]
72 };
73
74 words["otherDefinition"] =
75 words["unnamedDefinition"]
76 .concat(words["namedDefinition"])
77 .concat(words["otherParameterizedDefinition"]);
78
79 words["definition"] =
80 words["typeParameterizedDefinition"]
81 .concat(words["otherDefinition"]);
82
83 words["parameterizedDefinition"] =
84 words["typeParameterizedDefinition"]
85 .concat(words["otherParameterizedDefinition"]);
86
87 words["simpleDefinition"] =
88 words["constantSimpleDefinition"]
89 .concat(words["variableSimpleDefinition"])
90 .concat(words["otherSimpleDefinition"]);
91
92 words["keyword"] =
93 words["statement"]
94 .concat(words["separator"])
95 .concat(words["other"]);
96
97 // Patterns
98 var symbolPattern = "[-_a-zA-Z?!*@<>$%]+";
99 var symbol = new RegExp("^" + symbolPattern);
100 var patterns = {
101 // Symbols with special syntax
102 symbolKeyword: symbolPattern + ":",
103 symbolClass: "<" + symbolPattern + ">",
104 symbolGlobal: "\\*" + symbolPattern + "\\*",
105 symbolConstant: "\\$" + symbolPattern
106 };
107 var patternStyles = {
108 symbolKeyword: "atom",
109 symbolClass: "tag",
110 symbolGlobal: "variableName.standard",
111 symbolConstant: "variableName.constant"
112 };
113
114 // Compile all patterns to regular expressions
115 for (var patternName in patterns)
116 if (patterns.hasOwnProperty(patternName))
117 patterns[patternName] = new RegExp("^" + patterns[patternName]);
118
119 // Names beginning "with-" and "without-" are commonly
120 // used as statement macro
121 patterns["keyword"] = [/^with(?:out)?-[-_a-zA-Z?!*@<>$%]+/];
122
123 var styles = {};
124 styles["keyword"] = "keyword";
125 styles["definition"] = "def";
126 styles["simpleDefinition"] = "def";
127 styles["signalingCalls"] = "builtin";
128
129 // protected words lookup table
130 var wordLookup = {};
131 var styleLookup = {};
132
133 forEach([
134 "keyword",
135 "definition",
136 "simpleDefinition",
137 "signalingCalls"
138 ], function(type) {
139 forEach(words[type], function(word) {
140 wordLookup[word] = type;
141 styleLookup[word] = styles[type];
142 });
143 });
144
145
146 function chain(stream, state, f) {
147 state.tokenize = f;
148 return f(stream, state);
149 }
150
151 function tokenBase(stream, state) {
152 // String
153 var ch = stream.peek();
154 if (ch == "'" || ch == '"') {
155 stream.next();
156 return chain(stream, state, tokenString(ch, "string"));
157 }
158 // Comment
159 else if (ch == "/") {
160 stream.next();
161 if (stream.eat("*")) {
162 return chain(stream, state, tokenComment);
163 } else if (stream.eat("/")) {
164 stream.skipToEnd();
165 return "comment";
166 }
167 stream.backUp(1);
168 }
169 // Decimal
170 else if (/[+\-\d\.]/.test(ch)) {
171 if (stream.match(/^[+-]?[0-9]*\.[0-9]*([esdx][+-]?[0-9]+)?/i) ||
172 stream.match(/^[+-]?[0-9]+([esdx][+-]?[0-9]+)/i) ||
173 stream.match(/^[+-]?\d+/)) {
174 return "number";
175 }
176 }
177 // Hash
178 else if (ch == "#") {
179 stream.next();
180 // Symbol with string syntax
181 ch = stream.peek();
182 if (ch == '"') {
183 stream.next();
184 return chain(stream, state, tokenString('"', "string"));
185 }
186 // Binary number
187 else if (ch == "b") {
188 stream.next();
189 stream.eatWhile(/[01]/);
190 return "number";
191 }
192 // Hex number
193 else if (ch == "x") {
194 stream.next();
195 stream.eatWhile(/[\da-f]/i);
196 return "number";
197 }
198 // Octal number
199 else if (ch == "o") {
200 stream.next();
201 stream.eatWhile(/[0-7]/);
202 return "number";
203 }
204 // Token concatenation in macros
205 else if (ch == '#') {
206 stream.next();
207 return "punctuation";
208 }
209 // Sequence literals
210 else if ((ch == '[') || (ch == '(')) {
211 stream.next();
212 return "bracket";
213 // Hash symbol
214 } else if (stream.match(/f|t|all-keys|include|key|next|rest/i)) {
215 return "atom";
216 } else {
217 stream.eatWhile(/[-a-zA-Z]/);
218 return "error";
219 }
220 } else if (ch == "~") {
221 stream.next();
222 ch = stream.peek();
223 if (ch == "=") {
224 stream.next();
225 ch = stream.peek();
226 if (ch == "=") {
227 stream.next();
228 return "operator";
229 }
230 return "operator";
231 }
232 return "operator";
233 } else if (ch == ":") {
234 stream.next();
235 ch = stream.peek();
236 if (ch == "=") {
237 stream.next();
238 return "operator";
239 } else if (ch == ":") {
240 stream.next();
241 return "punctuation";
242 }
243 } else if ("[](){}".indexOf(ch) != -1) {
244 stream.next();
245 return "bracket";
246 } else if (".,".indexOf(ch) != -1) {
247 stream.next();
248 return "punctuation";
249 } else if (stream.match("end")) {
250 return "keyword";
251 }
252 for (var name in patterns) {
253 if (patterns.hasOwnProperty(name)) {
254 var pattern = patterns[name];
255 if ((pattern instanceof Array && some(pattern, function(p) {
256 return stream.match(p);
257 })) || stream.match(pattern))
258 return patternStyles[name];
259 }
260 }
261 if (/[+\-*\/^=<>&|]/.test(ch)) {
262 stream.next();
263 return "operator";
264 }
265 if (stream.match("define")) {
266 return "def";
267 } else {
268 stream.eatWhile(/[\w\-]/);
269 // Keyword
270 if (wordLookup.hasOwnProperty(stream.current())) {
271 return styleLookup[stream.current()];
272 } else if (stream.current().match(symbol)) {
273 return "variable";
274 } else {
275 stream.next();
276 return "variableName.standard";
277 }
278 }
279 }
280
281 function tokenComment(stream, state) {
282 var maybeEnd = false, maybeNested = false, nestedCount = 0, ch;
283 while ((ch = stream.next())) {
284 if (ch == "/" && maybeEnd) {
285 if (nestedCount > 0) {
286 nestedCount--;
287 } else {
288 state.tokenize = tokenBase;
289 break;
290 }
291 } else if (ch == "*" && maybeNested) {
292 nestedCount++;
293 }
294 maybeEnd = (ch == "*");
295 maybeNested = (ch == "/");
296 }
297 return "comment";
298 }
299
300 function tokenString(quote, style) {
301 return function(stream, state) {
302 var escaped = false, next, end = false;
303 while ((next = stream.next()) != null) {
304 if (next == quote && !escaped) {
305 end = true;
306 break;
307 }
308 escaped = !escaped && next == "\\";
309 }
310 if (end || !escaped) {
311 state.tokenize = tokenBase;
312 }
313 return style;
314 };
315 }
316
317 // Interface
318 export const dylan = {
319 name: "dylan",
320 startState: function() {
321 return {
322 tokenize: tokenBase,
323 currentIndent: 0
324 };
325 },
326 token: function(stream, state) {
327 if (stream.eatSpace())
328 return null;
329 var style = state.tokenize(stream, state);
330 return style;
331 },
332 languageData: {
333 commentTokens: {block: {open: "/*", close: "*/"}}
334 }
335 };
336