Debian Codegraph @

codemirror-legacy-modes/mode/erlang.js

raw
1 /////////////////////////////////////////////////////////////////////////////
2 // constants
3
4 var typeWords = [
5 "-type", "-spec", "-export_type", "-opaque"];
6
7 var keywordWords = [
8 "after","begin","catch","case","cond","end","fun","if",
9 "let","of","query","receive","try","when"];
10
11 var separatorRE = /[\->,;]/;
12 var separatorWords = [
13 "->",";",","];
14
15 var operatorAtomWords = [
16 "and","andalso","band","bnot","bor","bsl","bsr","bxor",
17 "div","not","or","orelse","rem","xor"];
18
19 var operatorSymbolRE = /[\+\-\*\/<>=\|:!]/;
20 var operatorSymbolWords = [
21 "=","+","-","*","/",">",">=","<","=<","=:=","==","=/=","/=","||","<-","!"];
22
23 var openParenRE = /[<\(\[\{]/;
24 var openParenWords = [
25 "<<","(","[","{"];
26
27 var closeParenRE = /[>\)\]\}]/;
28 var closeParenWords = [
29 "}","]",")",">>"];
30
31 var guardWords = [
32 "is_atom","is_binary","is_bitstring","is_boolean","is_float",
33 "is_function","is_integer","is_list","is_number","is_pid",
34 "is_port","is_record","is_reference","is_tuple",
35 "atom","binary","bitstring","boolean","function","integer","list",
36 "number","pid","port","record","reference","tuple"];
37
38 var bifWords = [
39 "abs","adler32","adler32_combine","alive","apply","atom_to_binary",
40 "atom_to_list","binary_to_atom","binary_to_existing_atom",
41 "binary_to_list","binary_to_term","bit_size","bitstring_to_list",
42 "byte_size","check_process_code","contact_binary","crc32",
43 "crc32_combine","date","decode_packet","delete_module",
44 "disconnect_node","element","erase","exit","float","float_to_list",
45 "garbage_collect","get","get_keys","group_leader","halt","hd",
46 "integer_to_list","internal_bif","iolist_size","iolist_to_binary",
47 "is_alive","is_atom","is_binary","is_bitstring","is_boolean",
48 "is_float","is_function","is_integer","is_list","is_number","is_pid",
49 "is_port","is_process_alive","is_record","is_reference","is_tuple",
50 "length","link","list_to_atom","list_to_binary","list_to_bitstring",
51 "list_to_existing_atom","list_to_float","list_to_integer",
52 "list_to_pid","list_to_tuple","load_module","make_ref","module_loaded",
53 "monitor_node","node","node_link","node_unlink","nodes","notalive",
54 "now","open_port","pid_to_list","port_close","port_command",
55 "port_connect","port_control","pre_loaded","process_flag",
56 "process_info","processes","purge_module","put","register",
57 "registered","round","self","setelement","size","spawn","spawn_link",
58 "spawn_monitor","spawn_opt","split_binary","statistics",
59 "term_to_binary","time","throw","tl","trunc","tuple_size",
60 "tuple_to_list","unlink","unregister","whereis"];
61
62 // upper case: [A-Z] [Ø-Þ] [À-Ö]
63 // lower case: [a-z] [ß-ö] [ø-ÿ]
64 var anumRE = /[\w@Ø-ÞÀ-Öß-öø-ÿ]/;
65 var escapesRE =
66 /[0-7]{1,3}|[bdefnrstv\\"']|\^[a-zA-Z]|x[0-9a-zA-Z]{2}|x{[0-9a-zA-Z]+}/;
67
68 /////////////////////////////////////////////////////////////////////////////
69 // tokenizer
70
71 function tokenizer(stream,state) {
72 // in multi-line string
73 if (state.in_string) {
74 state.in_string = (!doubleQuote(stream));
75 return rval(state,stream,"string");
76 }
77
78 // in multi-line atom
79 if (state.in_atom) {
80 state.in_atom = (!singleQuote(stream));
81 return rval(state,stream,"atom");
82 }
83
84 // whitespace
85 if (stream.eatSpace()) {
86 return rval(state,stream,"whitespace");
87 }
88
89 // attributes and type specs
90 if (!peekToken(state) &&
91 stream.match(/-\s*[a-zß-öø-ÿ][\wØ-ÞÀ-Öß-öø-ÿ]*/)) {
92 if (is_member(stream.current(),typeWords)) {
93 return rval(state,stream,"type");
94 }else{
95 return rval(state,stream,"attribute");
96 }
97 }
98
99 var ch = stream.next();
100
101 // comment
102 if (ch == '%') {
103 stream.skipToEnd();
104 return rval(state,stream,"comment");
105 }
106
107 // colon
108 if (ch == ":") {
109 return rval(state,stream,"colon");
110 }
111
112 // macro
113 if (ch == '?') {
114 stream.eatSpace();
115 stream.eatWhile(anumRE);
116 return rval(state,stream,"macro");
117 }
118
119 // record
120 if (ch == "#") {
121 stream.eatSpace();
122 stream.eatWhile(anumRE);
123 return rval(state,stream,"record");
124 }
125
126 // dollar escape
127 if (ch == "$") {
128 if (stream.next() == "\\" && !stream.match(escapesRE)) {
129 return rval(state,stream,"error");
130 }
131 return rval(state,stream,"number");
132 }
133
134 // dot
135 if (ch == ".") {
136 return rval(state,stream,"dot");
137 }
138
139 // quoted atom
140 if (ch == '\'') {
141 if (!(state.in_atom = (!singleQuote(stream)))) {
142 if (stream.match(/\s*\/\s*[0-9]/,false)) {
143 stream.match(/\s*\/\s*[0-9]/,true);
144 return rval(state,stream,"fun"); // 'f'/0 style fun
145 }
146 if (stream.match(/\s*\(/,false) || stream.match(/\s*:/,false)) {
147 return rval(state,stream,"function");
148 }
149 }
150 return rval(state,stream,"atom");
151 }
152
153 // string
154 if (ch == '"') {
155 state.in_string = (!doubleQuote(stream));
156 return rval(state,stream,"string");
157 }
158
159 // variable
160 if (/[A-Z_Ø-ÞÀ-Ö]/.test(ch)) {
161 stream.eatWhile(anumRE);
162 return rval(state,stream,"variable");
163 }
164
165 // atom/keyword/BIF/function
166 if (/[a-z_ß-öø-ÿ]/.test(ch)) {
167 stream.eatWhile(anumRE);
168
169 if (stream.match(/\s*\/\s*[0-9]/,false)) {
170 stream.match(/\s*\/\s*[0-9]/,true);
171 return rval(state,stream,"fun"); // f/0 style fun
172 }
173
174 var w = stream.current();
175
176 if (is_member(w,keywordWords)) {
177 return rval(state,stream,"keyword");
178 }else if (is_member(w,operatorAtomWords)) {
179 return rval(state,stream,"operator");
180 }else if (stream.match(/\s*\(/,false)) {
181 // 'put' and 'erlang:put' are bifs, 'foo:put' is not
182 if (is_member(w,bifWords) &&
183 ((peekToken(state).token != ":") ||
184 (peekToken(state,2).token == "erlang"))) {
185 return rval(state,stream,"builtin");
186 }else if (is_member(w,guardWords)) {
187 return rval(state,stream,"guard");
188 }else{
189 return rval(state,stream,"function");
190 }
191 }else if (lookahead(stream) == ":") {
192 if (w == "erlang") {
193 return rval(state,stream,"builtin");
194 } else {
195 return rval(state,stream,"function");
196 }
197 }else if (is_member(w,["true","false"])) {
198 return rval(state,stream,"boolean");
199 }else{
200 return rval(state,stream,"atom");
201 }
202 }
203
204 // number
205 var digitRE = /[0-9]/;
206 var radixRE = /[0-9a-zA-Z]/; // 36#zZ style int
207 if (digitRE.test(ch)) {
208 stream.eatWhile(digitRE);
209 if (stream.eat('#')) { // 36#aZ style integer
210 if (!stream.eatWhile(radixRE)) {
211 stream.backUp(1); //"36#" - syntax error
212 }
213 } else if (stream.eat('.')) { // float
214 if (!stream.eatWhile(digitRE)) {
215 stream.backUp(1); // "3." - probably end of function
216 } else {
217 if (stream.eat(/[eE]/)) { // float with exponent
218 if (stream.eat(/[-+]/)) {
219 if (!stream.eatWhile(digitRE)) {
220 stream.backUp(2); // "2e-" - syntax error
221 }
222 } else {
223 if (!stream.eatWhile(digitRE)) {
224 stream.backUp(1); // "2e" - syntax error
225 }
226 }
227 }
228 }
229 }
230 return rval(state,stream,"number"); // normal integer
231 }
232
233 // open parens
234 if (nongreedy(stream,openParenRE,openParenWords)) {
235 return rval(state,stream,"open_paren");
236 }
237
238 // close parens
239 if (nongreedy(stream,closeParenRE,closeParenWords)) {
240 return rval(state,stream,"close_paren");
241 }
242
243 // separators
244 if (greedy(stream,separatorRE,separatorWords)) {
245 return rval(state,stream,"separator");
246 }
247
248 // operators
249 if (greedy(stream,operatorSymbolRE,operatorSymbolWords)) {
250 return rval(state,stream,"operator");
251 }
252
253 return rval(state,stream,null);
254 }
255
256 /////////////////////////////////////////////////////////////////////////////
257 // utilities
258 function nongreedy(stream,re,words) {
259 if (stream.current().length == 1 && re.test(stream.current())) {
260 stream.backUp(1);
261 while (re.test(stream.peek())) {
262 stream.next();
263 if (is_member(stream.current(),words)) {
264 return true;
265 }
266 }
267 stream.backUp(stream.current().length-1);
268 }
269 return false;
270 }
271
272 function greedy(stream,re,words) {
273 if (stream.current().length == 1 && re.test(stream.current())) {
274 while (re.test(stream.peek())) {
275 stream.next();
276 }
277 while (0 < stream.current().length) {
278 if (is_member(stream.current(),words)) {
279 return true;
280 }else{
281 stream.backUp(1);
282 }
283 }
284 stream.next();
285 }
286 return false;
287 }
288
289 function doubleQuote(stream) {
290 return quote(stream, '"', '\\');
291 }
292
293 function singleQuote(stream) {
294 return quote(stream,'\'','\\');
295 }
296
297 function quote(stream,quoteChar,escapeChar) {
298 while (!stream.eol()) {
299 var ch = stream.next();
300 if (ch == quoteChar) {
301 return true;
302 }else if (ch == escapeChar) {
303 stream.next();
304 }
305 }
306 return false;
307 }
308
309 function lookahead(stream) {
310 var m = stream.match(/^\s*([^\s%])/, false)
311 return m ? m[1] : "";
312 }
313
314 function is_member(element,list) {
315 return (-1 < list.indexOf(element));
316 }
317
318 function rval(state,stream,type) {
319
320 // parse stack
321 pushToken(state,realToken(type,stream));
322
323 // map erlang token type to CodeMirror style class
324 // erlang -> CodeMirror tag
325 switch (type) {
326 case "atom": return "atom";
327 case "attribute": return "attribute";
328 case "boolean": return "atom";
329 case "builtin": return "builtin";
330 case "close_paren": return null;
331 case "colon": return null;
332 case "comment": return "comment";
333 case "dot": return null;
334 case "error": return "error";
335 case "fun": return "meta";
336 case "function": return "tag";
337 case "guard": return "property";
338 case "keyword": return "keyword";
339 case "macro": return "macroName";
340 case "number": return "number";
341 case "open_paren": return null;
342 case "operator": return "operator";
343 case "record": return "bracket";
344 case "separator": return null;
345 case "string": return "string";
346 case "type": return "def";
347 case "variable": return "variable";
348 default: return null;
349 }
350 }
351
352 function aToken(tok,col,ind,typ) {
353 return {token: tok,
354 column: col,
355 indent: ind,
356 type: typ};
357 }
358
359 function realToken(type,stream) {
360 return aToken(stream.current(),
361 stream.column(),
362 stream.indentation(),
363 type);
364 }
365
366 function fakeToken(type) {
367 return aToken(type,0,0,type);
368 }
369
370 function peekToken(state,depth) {
371 var len = state.tokenStack.length;
372 var dep = (depth ? depth : 1);
373
374 if (len < dep) {
375 return false;
376 }else{
377 return state.tokenStack[len-dep];
378 }
379 }
380
381 function pushToken(state,token) {
382
383 if (!(token.type == "comment" || token.type == "whitespace")) {
384 state.tokenStack = maybe_drop_pre(state.tokenStack,token);
385 state.tokenStack = maybe_drop_post(state.tokenStack);
386 }
387 }
388
389 function maybe_drop_pre(s,token) {
390 var last = s.length-1;
391
392 if (0 < last && s[last].type === "record" && token.type === "dot") {
393 s.pop();
394 }else if (0 < last && s[last].type === "group") {
395 s.pop();
396 s.push(token);
397 }else{
398 s.push(token);
399 }
400 return s;
401 }
402
403 function maybe_drop_post(s) {
404 if (!s.length) return s
405 var last = s.length-1;
406
407 if (s[last].type === "dot") {
408 return [];
409 }
410 if (last > 1 && s[last].type === "fun" && s[last-1].token === "fun") {
411 return s.slice(0,last-1);
412 }
413 switch (s[last].token) {
414 case "}": return d(s,{g:["{"]});
415 case "]": return d(s,{i:["["]});
416 case ")": return d(s,{i:["("]});
417 case ">>": return d(s,{i:["<<"]});
418 case "end": return d(s,{i:["begin","case","fun","if","receive","try"]});
419 case ",": return d(s,{e:["begin","try","when","->",
420 ",","(","[","{","<<"]});
421 case "->": return d(s,{r:["when"],
422 m:["try","if","case","receive"]});
423 case ";": return d(s,{E:["case","fun","if","receive","try","when"]});
424 case "catch":return d(s,{e:["try"]});
425 case "of": return d(s,{e:["case"]});
426 case "after":return d(s,{e:["receive","try"]});
427 default: return s;
428 }
429 }
430
431 function d(stack,tt) {
432 // stack is a stack of Token objects.
433 // tt is an object; {type:tokens}
434 // type is a char, tokens is a list of token strings.
435 // The function returns (possibly truncated) stack.
436 // It will descend the stack, looking for a Token such that Token.token
437 // is a member of tokens. If it does not find that, it will normally (but
438 // see "E" below) return stack. If it does find a match, it will remove
439 // all the Tokens between the top and the matched Token.
440 // If type is "m", that is all it does.
441 // If type is "i", it will also remove the matched Token and the top Token.
442 // If type is "g", like "i", but add a fake "group" token at the top.
443 // If type is "r", it will remove the matched Token, but not the top Token.
444 // If type is "e", it will keep the matched Token but not the top Token.
445 // If type is "E", it behaves as for type "e", except if there is no match,
446 // in which case it will return an empty stack.
447
448 for (var type in tt) {
449 var len = stack.length-1;
450 var tokens = tt[type];
451 for (var i = len-1; -1 < i ; i--) {
452 if (is_member(stack[i].token,tokens)) {
453 var ss = stack.slice(0,i);
454 switch (type) {
455 case "m": return ss.concat(stack[i]).concat(stack[len]);
456 case "r": return ss.concat(stack[len]);
457 case "i": return ss;
458 case "g": return ss.concat(fakeToken("group"));
459 case "E": return ss.concat(stack[i]);
460 case "e": return ss.concat(stack[i]);
461 }
462 }
463 }
464 }
465 return (type == "E" ? [] : stack);
466 }
467
468 /////////////////////////////////////////////////////////////////////////////
469 // indenter
470
471 function indenter(state, textAfter, cx) {
472 var t;
473 var wordAfter = wordafter(textAfter);
474 var currT = peekToken(state,1);
475 var prevT = peekToken(state,2);
476
477 if (state.in_string || state.in_atom) {
478 return null;
479 }else if (!prevT) {
480 return 0;
481 }else if (currT.token == "when") {
482 return currT.column + cx.unit;
483 }else if (wordAfter === "when" && prevT.type === "function") {
484 return prevT.indent+cx.unit;
485 }else if (wordAfter === "(" && currT.token === "fun") {
486 return currT.column+3;
487 }else if (wordAfter === "catch" && (t = getToken(state,["try"]))) {
488 return t.column;
489 }else if (is_member(wordAfter,["end","after","of"])) {
490 t = getToken(state,["begin","case","fun","if","receive","try"]);
491 return t ? t.column : null;
492 }else if (is_member(wordAfter,closeParenWords)) {
493 t = getToken(state,openParenWords);
494 return t ? t.column : null;
495 }else if (is_member(currT.token,[",","|","||"]) ||
496 is_member(wordAfter,[",","|","||"])) {
497 t = postcommaToken(state);
498 return t ? t.column+t.token.length : cx.unit;
499 }else if (currT.token == "->") {
500 if (is_member(prevT.token, ["receive","case","if","try"])) {
501 return prevT.column+cx.unit+cx.unit;
502 }else{
503 return prevT.column+cx.unit;
504 }
505 }else if (is_member(currT.token,openParenWords)) {
506 return currT.column+currT.token.length;
507 }else{
508 t = defaultToken(state);
509 return truthy(t) ? t.column+cx.unit : 0;
510 }
511 }
512
513 function wordafter(str) {
514 var m = str.match(/,|[a-z]+|\}|\]|\)|>>|\|+|\(/);
515
516 return truthy(m) && (m.index === 0) ? m[0] : "";
517 }
518
519 function postcommaToken(state) {
520 var objs = state.tokenStack.slice(0,-1);
521 var i = getTokenIndex(objs,"type",["open_paren"]);
522
523 return truthy(objs[i]) ? objs[i] : false;
524 }
525
526 function defaultToken(state) {
527 var objs = state.tokenStack;
528 var stop = getTokenIndex(objs,"type",["open_paren","separator","keyword"]);
529 var oper = getTokenIndex(objs,"type",["operator"]);
530
531 if (truthy(stop) && truthy(oper) && stop < oper) {
532 return objs[stop+1];
533 } else if (truthy(stop)) {
534 return objs[stop];
535 } else {
536 return false;
537 }
538 }
539
540 function getToken(state,tokens) {
541 var objs = state.tokenStack;
542 var i = getTokenIndex(objs,"token",tokens);
543
544 return truthy(objs[i]) ? objs[i] : false;
545 }
546
547 function getTokenIndex(objs,propname,propvals) {
548
549 for (var i = objs.length-1; -1 < i ; i--) {
550 if (is_member(objs[i][propname],propvals)) {
551 return i;
552 }
553 }
554 return false;
555 }
556
557 function truthy(x) {
558 return (x !== false) && (x != null);
559 }
560
561 /////////////////////////////////////////////////////////////////////////////
562 // this object defines the mode
563
564 export const erlang = {
565 name: "erlang",
566 startState() {
567 return {tokenStack: [],
568 in_string: false,
569 in_atom: false};
570 },
571
572 token: tokenizer,
573
574 indent: indenter,
575
576 languageData: {
577 commentTokens: {line: "%"}
578 }
579 };
580