Debian Codegraph @

codemirror-legacy-modes/mode/oz.js

raw
1 function wordRegexp(words) {
2 return new RegExp("^((" + words.join(")|(") + "))\\b");
3 }
4
5 var singleOperators = /[\^@!\|<>#~\.\*\-\+\\/,=]/;
6 var doubleOperators = /(<-)|(:=)|(=<)|(>=)|(<=)|(<:)|(>:)|(=:)|(\\=)|(\\=:)|(!!)|(==)|(::)/;
7 var tripleOperators = /(:::)|(\.\.\.)|(=<:)|(>=:)/;
8
9 var middle = ["in", "then", "else", "of", "elseof", "elsecase", "elseif", "catch",
10 "finally", "with", "require", "prepare", "import", "export", "define", "do"];
11 var end = ["end"];
12
13 var atoms = wordRegexp(["true", "false", "nil", "unit"]);
14 var commonKeywords = wordRegexp(["andthen", "at", "attr", "declare", "feat", "from", "lex",
15 "mod", "div", "mode", "orelse", "parser", "prod", "prop", "scanner", "self", "syn", "token"]);
16 var openingKeywords = wordRegexp(["local", "proc", "fun", "case", "class", "if", "cond", "or", "dis",
17 "choice", "not", "thread", "try", "raise", "lock", "for", "suchthat", "meth", "functor"]);
18 var middleKeywords = wordRegexp(middle);
19 var endKeywords = wordRegexp(end);
20
21 // Tokenizers
22 function tokenBase(stream, state) {
23 if (stream.eatSpace()) {
24 return null;
25 }
26
27 // Brackets
28 if(stream.match(/[{}]/)) {
29 return "bracket";
30 }
31
32 // Special [] keyword
33 if (stream.match('[]')) {
34 return "keyword"
35 }
36
37 // Operators
38 if (stream.match(tripleOperators) || stream.match(doubleOperators)) {
39 return "operator";
40 }
41
42 // Atoms
43 if(stream.match(atoms)) {
44 return 'atom';
45 }
46
47 // Opening keywords
48 var matched = stream.match(openingKeywords);
49 if (matched) {
50 if (!state.doInCurrentLine)
51 state.currentIndent++;
52 else
53 state.doInCurrentLine = false;
54
55 // Special matching for signatures
56 if(matched[0] == "proc" || matched[0] == "fun")
57 state.tokenize = tokenFunProc;
58 else if(matched[0] == "class")
59 state.tokenize = tokenClass;
60 else if(matched[0] == "meth")
61 state.tokenize = tokenMeth;
62
63 return 'keyword';
64 }
65
66 // Middle and other keywords
67 if (stream.match(middleKeywords) || stream.match(commonKeywords)) {
68 return "keyword"
69 }
70
71 // End keywords
72 if (stream.match(endKeywords)) {
73 state.currentIndent--;
74 return 'keyword';
75 }
76
77 // Eat the next char for next comparisons
78 var ch = stream.next();
79
80 // Strings
81 if (ch == '"' || ch == "'") {
82 state.tokenize = tokenString(ch);
83 return state.tokenize(stream, state);
84 }
85
86 // Numbers
87 if (/[~\d]/.test(ch)) {
88 if (ch == "~") {
89 if(! /^[0-9]/.test(stream.peek()))
90 return null;
91 else if (( stream.next() == "0" && stream.match(/^[xX][0-9a-fA-F]+/)) || stream.match(/^[0-9]*(\.[0-9]+)?([eE][~+]?[0-9]+)?/))
92 return "number";
93 }
94
95 if ((ch == "0" && stream.match(/^[xX][0-9a-fA-F]+/)) || stream.match(/^[0-9]*(\.[0-9]+)?([eE][~+]?[0-9]+)?/))
96 return "number";
97
98 return null;
99 }
100
101 // Comments
102 if (ch == "%") {
103 stream.skipToEnd();
104 return 'comment';
105 }
106 else if (ch == "/") {
107 if (stream.eat("*")) {
108 state.tokenize = tokenComment;
109 return tokenComment(stream, state);
110 }
111 }
112
113 // Single operators
114 if(singleOperators.test(ch)) {
115 return "operator";
116 }
117
118 // If nothing match, we skip the entire alphanumerical block
119 stream.eatWhile(/\w/);
120
121 return "variable";
122 }
123
124 function tokenClass(stream, state) {
125 if (stream.eatSpace()) {
126 return null;
127 }
128 stream.match(/([A-Z][A-Za-z0-9_]*)|(`.+`)/);
129 state.tokenize = tokenBase;
130 return "type"
131 }
132
133 function tokenMeth(stream, state) {
134 if (stream.eatSpace()) {
135 return null;
136 }
137 stream.match(/([a-zA-Z][A-Za-z0-9_]*)|(`.+`)/);
138 state.tokenize = tokenBase;
139 return "def"
140 }
141
142 function tokenFunProc(stream, state) {
143 if (stream.eatSpace()) {
144 return null;
145 }
146
147 if(!state.hasPassedFirstStage && stream.eat("{")) {
148 state.hasPassedFirstStage = true;
149 return "bracket";
150 }
151 else if(state.hasPassedFirstStage) {
152 stream.match(/([A-Z][A-Za-z0-9_]*)|(`.+`)|\$/);
153 state.hasPassedFirstStage = false;
154 state.tokenize = tokenBase;
155 return "def"
156 }
157 else {
158 state.tokenize = tokenBase;
159 return null;
160 }
161 }
162
163 function tokenComment(stream, state) {
164 var maybeEnd = false, ch;
165 while (ch = stream.next()) {
166 if (ch == "/" && maybeEnd) {
167 state.tokenize = tokenBase;
168 break;
169 }
170 maybeEnd = (ch == "*");
171 }
172 return "comment";
173 }
174
175 function tokenString(quote) {
176 return function (stream, state) {
177 var escaped = false, next, end = false;
178 while ((next = stream.next()) != null) {
179 if (next == quote && !escaped) {
180 end = true;
181 break;
182 }
183 escaped = !escaped && next == "\\";
184 }
185 if (end || !escaped)
186 state.tokenize = tokenBase;
187 return "string";
188 };
189 }
190
191 function buildElectricInputRegEx() {
192 // Reindentation should occur on [] or on a match of any of
193 // the block closing keywords, at the end of a line.
194 var allClosings = middle.concat(end);
195 return new RegExp("[\\[\\]]|(" + allClosings.join("|") + ")$");
196 }
197
198 export const oz = {
199 name: "oz",
200
201 startState: function () {
202 return {
203 tokenize: tokenBase,
204 currentIndent: 0,
205 doInCurrentLine: false,
206 hasPassedFirstStage: false
207 };
208 },
209
210 token: function (stream, state) {
211 if (stream.sol())
212 state.doInCurrentLine = 0;
213
214 return state.tokenize(stream, state);
215 },
216
217 indent: function (state, textAfter, cx) {
218 var trueText = textAfter.replace(/^\s+|\s+$/g, '');
219
220 if (trueText.match(endKeywords) || trueText.match(middleKeywords) || trueText.match(/(\[])/))
221 return cx.unit * (state.currentIndent - 1);
222
223 if (state.currentIndent < 0)
224 return 0;
225
226 return state.currentIndent * cx.unit
227 },
228
229 languageData: {
230 indentOnInut: buildElectricInputRegEx(),
231 commentTokens: {line: "%", block: {open: "/*", close: "*/"}}
232 }
233 };