Debian Codegraph @

codemirror-legacy-modes/mode/apl.js

raw
1 var builtInFuncs = {
2 "+": ["conjugate", "add"],
3 "−": ["negate", "subtract"],
4 "×": ["signOf", "multiply"],
5 "÷": ["reciprocal", "divide"],
6 "⌈": ["ceiling", "greaterOf"],
7 "⌊": ["floor", "lesserOf"],
8 "∣": ["absolute", "residue"],
9 "⍳": ["indexGenerate", "indexOf"],
10 "?": ["roll", "deal"],
11 "⋆": ["exponentiate", "toThePowerOf"],
12 "⍟": ["naturalLog", "logToTheBase"],
13 "○": ["piTimes", "circularFuncs"],
14 "!": ["factorial", "binomial"],
15 "⌹": ["matrixInverse", "matrixDivide"],
16 "<": [null, "lessThan"],
17 "≤": [null, "lessThanOrEqual"],
18 "=": [null, "equals"],
19 ">": [null, "greaterThan"],
20 "≥": [null, "greaterThanOrEqual"],
21 "≠": [null, "notEqual"],
22 "≡": ["depth", "match"],
23 "≢": [null, "notMatch"],
24 "∈": ["enlist", "membership"],
25 "⍷": [null, "find"],
26 "∪": ["unique", "union"],
27 "∩": [null, "intersection"],
28 "∼": ["not", "without"],
29 "∨": [null, "or"],
30 "∧": [null, "and"],
31 "⍱": [null, "nor"],
32 "⍲": [null, "nand"],
33 "⍴": ["shapeOf", "reshape"],
34 ",": ["ravel", "catenate"],
35 "⍪": [null, "firstAxisCatenate"],
36 "⌽": ["reverse", "rotate"],
37 "⊖": ["axis1Reverse", "axis1Rotate"],
38 "⍉": ["transpose", null],
39 "↑": ["first", "take"],
40 "↓": [null, "drop"],
41 "⊂": ["enclose", "partitionWithAxis"],
42 "⊃": ["diclose", "pick"],
43 "⌷": [null, "index"],
44 "⍋": ["gradeUp", null],
45 "⍒": ["gradeDown", null],
46 "⊤": ["encode", null],
47 "⊥": ["decode", null],
48 "⍕": ["format", "formatByExample"],
49 "⍎": ["execute", null],
50 "⊣": ["stop", "left"],
51 "⊢": ["pass", "right"]
52 };
53
54 var isOperator = /[\.\/⌿⍀¨⍣]/;
55 var isNiladic = /⍬/;
56 var isFunction = /[\+−×÷⌈⌊∣⍳\?⋆⍟○!⌹<≤=>≥≠≡≢∈⍷∪∩∼∨∧⍱⍲⍴,⍪⌽⊖⍉↑↓⊂⊃⌷⍋⍒⊤⊥⍕⍎⊣⊢]/;
57 var isArrow = /←/;
58 var isComment = /[⍝#].*$/;
59
60 var stringEater = function(type) {
61 var prev;
62 prev = false;
63 return function(c) {
64 prev = c;
65 if (c === type) {
66 return prev === "\\";
67 }
68 return true;
69 };
70 };
71
72 export const apl = {
73 name: "apl",
74 startState: function() {
75 return {
76 prev: false,
77 func: false,
78 op: false,
79 string: false,
80 escape: false
81 };
82 },
83 token: function(stream, state) {
84 var ch;
85 if (stream.eatSpace()) {
86 return null;
87 }
88 ch = stream.next();
89 if (ch === '"' || ch === "'") {
90 stream.eatWhile(stringEater(ch));
91 stream.next();
92 state.prev = true;
93 return "string";
94 }
95 if (/[\[{\(]/.test(ch)) {
96 state.prev = false;
97 return null;
98 }
99 if (/[\]}\)]/.test(ch)) {
100 state.prev = true;
101 return null;
102 }
103 if (isNiladic.test(ch)) {
104 state.prev = false;
105 return "atom";
106 }
107 if (/[¯\d]/.test(ch)) {
108 if (state.func) {
109 state.func = false;
110 state.prev = false;
111 } else {
112 state.prev = true;
113 }
114 stream.eatWhile(/[\w\.]/);
115 return "number";
116 }
117 if (isOperator.test(ch)) {
118 return "operator"
119 }
120 if (isArrow.test(ch)) {
121 return "operator";
122 }
123 if (isFunction.test(ch)) {
124 state.func = true;
125 state.prev = false;
126 return builtInFuncs[ch] ? "variableName.function.standard" : "variableName.function"
127 }
128 if (isComment.test(ch)) {
129 stream.skipToEnd();
130 return "comment";
131 }
132 if (ch === "∘" && stream.peek() === ".") {
133 stream.next();
134 return "variableName.function";
135 }
136 stream.eatWhile(/[\w\$_]/);
137 state.prev = true;
138 return "keyword";
139 }
140 }