Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / ide / enscript / sml_fancy.st
CommitLineData
7f918cf1
CE
1/**
2 * Name: sml_fancy
3 * Description: Standard ML programming language.
4 * Author: Matthew Fluet <mfluet@acm.org>
5 */
6
7/*
8builtin_face ---
9comment_face --- comments
10function_name_face --- modules keywords
11highlight_face ---
12keyword_face --- core keywords
13reference_face --- special constants (nums)
14string_face --- strings
15type_face --- type bindings
16variable_name_face --- constructor bindings
17*/
18
19require_state (sml_verbose);
20
21TRUE = 1;
22FALSE = 0;
23
24sml_typctxt = -1;
25sml_typctxt_expected_eqs = -1;
26SML_DATBIND_UNKNOWN = 1;
27SML_DATBIND_REPL = 2;
28SML_DATBIND_DECL = 3;
29sml_datbind = FALSE;
30sml_exbind = FALSE;
31sml_conbind = FALSE;
32
33SML_AND_NUL = -1;
34SML_AND_VALBIND = 1;
35SML_AND_TYPBIND = 2;
36SML_AND_DATBIND = 3;
37SML_AND_EXBIND = 4;
38SML_AND_STRBIND = 5;
39SML_AND_SIGBIND = 6;
40SML_AND_FUNBIND = 7;
41SML_AND_WHERETYPE = 8;
42sml_andbind = list (SML_AND_NUL);
43
44SML_CORE_LEVEL = 0;
45SML_MODULES_LEVEL = 1;
46
47sml_endmatch = list (SML_MODULES_LEVEL);
48
49sml_last_keyword = "";
50sub sml_keyword (s, lvl) {
51 sml_last_keyword = s;
52 if (lvl == SML_CORE_LEVEL)
53 keyword_face (true);
54 else if (lvl = SML_MODULES_LEVEL)
55 function_name_face (true);
56 language_print (s);
57 if (lvl == SML_CORE_LEVEL)
58 keyword_face (false);
59 else if (lvl = SML_MODULES_LEVEL)
60 function_name_face (false);
61}
62
63sml_scopes = list (list ("",SML_MODULES_LEVEL));
64sml_scope = 0;
65sub sml_enter_scope (s, lvl) {
66 sml_andbind = list(SML_AND_NUL, sml_andbind);
67 sml_endmatch = list(-1, sml_endmatch);
68 sml_scopes = list (list (s,lvl), sml_scopes);
69 sml_scope++;
70 return;
71}
72sub sml_leave_scope () {
73 sml_scope--;
74 sml_scopes = sml_scopes[1];
75 sml_endmatch = sml_endmatch[1];
76 sml_andbind = sml_andbind[1];
77 return;
78}
79sub sml_let_level () {
80 local scopes = sml_scopes;
81 if (sml_andbind[0] == SML_AND_STRBIND ||
82 sml_andbind[0] == SML_AND_FUNBIND) {
83 return TRUE;
84 }
85 if ((strcmp(scopes[0][0],"let") == 0 ||
86 strcmp(scopes[0][0],"(") == 0) &&
87 scopes[0][1] == SML_MODULES_LEVEL)
88 return TRUE;
89 return FALSE;
90}
91sub sml_local_level () {
92 local scopes = sml_scopes;
93
94 if ((strcmp(scopes[0][0],"let") == 0 ||
95 strcmp(scopes[0][0],"(") == 0) &&
96 scopes[0][1] == SML_MODULES_LEVEL)
97 return TRUE;
98 while (length(scopes) == 2) {
99 if (strcmp(scopes[0][0],"local") == 0 &&
100 scopes[0][1] == SML_MODULES_LEVEL)
101 scopes = scopes[1];
102 else if (strcmp(scopes[0][0],"let") == 0 &&
103 scopes[0][1] == SML_MODULES_LEVEL)
104 return TRUE;
105 else
106 return FALSE;
107 }
108 return TRUE;
109}
110
111sub sml_start_typctxt (eqs) {
112 type_face(true);
113 sml_typctxt = sml_scope;
114 sml_typctxt_expected_eqs = eqs;
115 return;
116}
117sub sml_start_typbind () {
118 sml_start_typctxt (1);
119 sml_datbind = FALSE;
120 sml_exbind = FALSE;
121 sml_conbind = FALSE;
122}
123sub sml_start_eqtyp () {
124 sml_start_typctxt (0);
125 sml_datbind = FALSE;
126 sml_exbind = FALSE;
127 sml_conbind = FALSE;
128}
129sub sml_start_wheretyp () {
130 sml_start_typctxt (1);
131 sml_datbind = FALSE;
132 sml_exbind = FALSE;
133 sml_conbind = FALSE;
134}
135sub sml_start_sharingtyp () {
136 sml_start_typctxt (-1);
137 sml_datbind = FALSE;
138 sml_exbind = FALSE;
139 sml_conbind = FALSE;
140}
141sub sml_start_datbind () {
142 sml_start_typctxt (1);
143 sml_datbind = SML_DATBIND_UNKNOWN;
144 sml_exbind = FALSE;
145 sml_conbind = FALSE;
146}
147sub sml_start_exbind () {
148 sml_start_typctxt (1);
149 sml_datbind = FALSE;
150 sml_exbind = TRUE;
151 sml_conbind = TRUE;
152}
153sub sml_finish_typctxt () {
154 if (sml_typctxt == sml_scope) {
155 sml_typctxt = -1;
156 sml_typctxt_expected_eqs = -1;
157 if (sml_datbind) {sml_datbind = FALSE; sml_conbind = FALSE;}
158 if (sml_exbind) {sml_exbind = FALSE; sml_conbind = FALSE;}
159 type_face (false);
160 }
161 return;
162}
163
164state sml_fancy extends sml_verbose
165{
166 END {
167 sml_finish_typctxt ();
168 }
169
170 /*
171 * Keywords
172 */
173 /(:|,|_|->)/ {
174 language_print ($0);
175 }
176 /[\({[]/ {
177 sml_enter_scope ($0,sml_scopes[0][1]);
178 language_print ($0);
179 }
180 /[]}\)]/ {
181 sml_finish_typctxt ();
182 language_print ($0);
183 sml_leave_scope ();
184 }
185 /(\.\.\.|;|=>)/ {
186 sml_finish_typctxt ();
187 sml_andbind[0] = SML_AND_NUL;
188 language_print ($0);
189 }
190 /\|/ {
191 if (sml_datbind == SML_DATBIND_DECL) {
192 type_face (false);
193 language_print ($0);
194 sml_conbind = TRUE;
195 type_face (true);
196 } else {
197 language_print ($0);
198 }
199 }
200 /=/ {
201 if (sml_typctxt != -1) {
202 type_face (false);
203 language_print ($0);
204 type_face (true);
205 if (sml_typctxt_expected_eqs == 0) {
206 sml_finish_typctxt ();
207 } else {
208 sml_typctxt_expected_eqs--;
209 if (sml_datbind == SML_DATBIND_UNKNOWN) {
210 sml_conbind = TRUE;
211 }
212 if (sml_exbind) {
213 sml_conbind = TRUE;
214 }
215 }
216 } else {
217 language_print ($0);
218 }
219 }
220 sml_sel_re {
221 language_print ($0);
222 }
223 /\b(abstype)\b/ {
224 sml_finish_typctxt ();
225 sml_enter_scope ($0,SML_CORE_LEVEL);
226 sml_keyword ($0, SML_CORE_LEVEL);
227 sml_andbind[0] = SML_AND_DATBIND;
228 sml_endmatch[0] = SML_CORE_LEVEL;
229 sml_start_datbind ();
230 }
231 /\b(and)\b/ {
232 sml_finish_typctxt ();
233 if (sml_andbind[0] == SML_AND_VALBIND) {
234 sml_keyword ($0, SML_CORE_LEVEL);
235 } else if (sml_andbind[0] == SML_AND_TYPBIND) {
236 sml_keyword ($0, SML_CORE_LEVEL);
237 sml_start_typbind ();
238 } else if (sml_andbind[0] == SML_AND_DATBIND) {
239 sml_keyword ($0, SML_CORE_LEVEL);
240 sml_start_datbind ();
241 } else if (sml_andbind[0] == SML_AND_EXBIND) {
242 sml_keyword ($0, SML_CORE_LEVEL);
243 sml_start_exbind ();
244 } else if (sml_andbind[0] == SML_AND_STRBIND) {
245 sml_keyword ($0, SML_MODULES_LEVEL);
246 } else if (sml_andbind[0] == SML_AND_SIGBIND) {
247 sml_keyword ($0, SML_MODULES_LEVEL);
248 } else if (sml_andbind[0] == SML_AND_FUNBIND) {
249 sml_keyword ($0, SML_MODULES_LEVEL);
250 } else if (sml_andbind[0] == SML_AND_WHERETYPE) {
251 sml_keyword ($0, SML_MODULES_LEVEL);
252 sml_last_keyword = "where";
253 }
254
255 }
256 /\b(andalso)\b/ {
257 sml_keyword ($0, SML_CORE_LEVEL);
258 }
259 /\b(as)\b/ {
260 sml_keyword ($0, SML_CORE_LEVEL);
261 }
262 /\b(case)\b/ {
263 sml_keyword ($0, SML_CORE_LEVEL);
264 }
265 /\b(datatype)\b/ {
266 if (sml_datbind == SML_DATBIND_UNKNOWN) {
267 sml_datbind = SML_DATBIND_REPL;
268 sml_conbind = FALSE;
269 sml_keyword ($0, SML_CORE_LEVEL);
270 } else {
271 sml_finish_typctxt ();
272 sml_keyword ($0, SML_CORE_LEVEL);
273 sml_andbind[0] = SML_AND_DATBIND;
274 sml_start_datbind ();
275 }
276 }
277 /\b(do)\b/ {
278 sml_keyword ($0, SML_CORE_LEVEL);
279 }
280 /\b(else)\b/ {
281 sml_keyword ($0, SML_CORE_LEVEL);
282 }
283 /\b(end)\b/ {
284 sml_finish_typctxt ();
285 sml_keyword ($0, sml_endmatch[0]);
286 sml_leave_scope ();
287 }
288 /\b(eqtype)\b/ {
289 sml_finish_typctxt ();
290 sml_keyword ($0, SML_CORE_LEVEL);
291 sml_start_eqtyp ();
292 }
293 /\b(exception)\b/ {
294 sml_finish_typctxt ();
295 sml_keyword ($0, SML_CORE_LEVEL);
296 sml_andbind[0] = SML_AND_EXBIND;
297 sml_start_exbind ();
298 }
299 /\b(fn)\b/ {
300 sml_keyword ($0, SML_CORE_LEVEL);
301 }
302 sml_fun_with_tyvar_re {
303 sml_finish_typctxt ();
304 sml_keyword ($1, SML_CORE_LEVEL);
305 language_print ($2);
306 type_face(true);
307 language_print ($3);
308 type_face(false);
309 sml_andbind[0] = SML_AND_VALBIND;
310 }
311 sml_fun_with_tyvarseq_re {
312 sml_finish_typctxt ();
313 sml_keyword ($1, SML_CORE_LEVEL);
314 language_print ($2);
315 language_print ($3);
316 language_print ($4);
317 type_face(true);
318 language_print ($5);
319 type_face(false);
320 call (sml_tyvarseq);
321 sml_andbind[0] = SML_AND_VALBIND;
322 }
323 /\b(fun)\b/ {
324 sml_finish_typctxt ();
325 sml_keyword ($0, SML_CORE_LEVEL);
326 sml_andbind[0] = SML_AND_VALBIND;
327 }
328 /\b(functor)\b/ {
329 sml_finish_typctxt ();
330 sml_keyword ($0, SML_MODULES_LEVEL);
331 sml_andbind[0] = SML_AND_FUNBIND;
332 }
333 /\b(handle)\b/ {
334 sml_keyword ($0, SML_CORE_LEVEL);
335 }
336 /\b(if)\b/ {
337 sml_keyword ($0, SML_CORE_LEVEL);
338 }
339 /\b(in)\b/ {
340 sml_finish_typctxt ();
341 sml_andbind[0] = SML_AND_NUL;
342 sml_keyword ($0, sml_endmatch[0]);
343 }
344 /\b(include)\b/ {
345 sml_finish_typctxt ();
346 sml_keyword ($0, SML_MODULES_LEVEL);
347 }
348 sml_infix_re {
349 sml_finish_typctxt ();
350 sml_keyword ($1, SML_CORE_LEVEL);
351 language_print ($3);
352 language_print ($4);
353 }
354 /\b(let)\b/ {
355 sml_finish_typctxt ();
356 if (sml_let_level()) {
357 sml_enter_scope ($0,SML_MODULES_LEVEL);
358 sml_keyword ($0, SML_MODULES_LEVEL);
359 sml_endmatch[0] = SML_MODULES_LEVEL;
360 } else {
361 sml_enter_scope ($0,SML_CORE_LEVEL);
362 sml_keyword ($0, SML_CORE_LEVEL);
363 sml_endmatch[0] = SML_CORE_LEVEL;
364 }
365 }
366 /\b(local)\b/ {
367 sml_finish_typctxt ();
368 if (sml_local_level ()) {
369 sml_enter_scope ($0, SML_MODULES_LEVEL);
370 sml_keyword ($0, SML_MODULES_LEVEL);
371 sml_endmatch[0] = SML_MODULES_LEVEL;
372 } else {
373 sml_enter_scope ($0, SML_CORE_LEVEL);
374 sml_keyword ($0, SML_CORE_LEVEL);
375 sml_endmatch[0] = SML_CORE_LEVEL;
376 }
377 }
378 /\b(nonfix)\b/ {
379 sml_finish_typctxt ();
380 sml_keyword ($0, SML_CORE_LEVEL);
381 }
382 /\b(of)\b/ {
383 sml_keyword ($0, SML_CORE_LEVEL);
384 }
385 /\b(op)\b/ {
386 sml_keyword ($0, SML_CORE_LEVEL);
387 }
388 /\b(open)\b/ {
389 sml_finish_typctxt ();
390 sml_keyword ($0, SML_MODULES_LEVEL);
391 }
392 /\b(orelse)\b/ {
393 sml_keyword ($0, SML_CORE_LEVEL);
394 }
395 /\b(raise)\b/ {
396 sml_keyword ($0, SML_CORE_LEVEL);
397 }
398 /\b(rec)\b/ {
399 sml_keyword ($0, SML_CORE_LEVEL);
400 }
401 /\b(sharing)\b/ {
402 sml_finish_typctxt ();
403 sml_keyword ($0, SML_MODULES_LEVEL);
404 }
405 /\b(sig)\b/ {
406 sml_enter_scope ($0, SML_CORE_LEVEL);
407 sml_keyword ($0, SML_MODULES_LEVEL);
408 sml_endmatch[0] = SML_MODULES_LEVEL;
409 }
410 /\b(signature)\b/ {
411 sml_finish_typctxt ();
412 sml_keyword ($0, SML_MODULES_LEVEL);
413 sml_andbind[0] = SML_AND_SIGBIND;
414 }
415 /\b(struct)\b/ {
416 sml_enter_scope ($0, SML_CORE_LEVEL);
417 sml_keyword ($0, SML_MODULES_LEVEL);
418 sml_endmatch[0] = SML_MODULES_LEVEL;
419 }
420 /\b(structure)\b/ {
421 sml_finish_typctxt ();
422 sml_keyword ($0, SML_MODULES_LEVEL);
423 sml_andbind[0] = SML_AND_STRBIND;
424 }
425 /\b(then)\b/ {
426 sml_keyword ($0, SML_CORE_LEVEL);
427 }
428 /\b(type)\b/ {
429 if (strcmp(sml_last_keyword,"where") == 0) {
430 sml_keyword ($0, SML_MODULES_LEVEL);
431 sml_last_keyword = "where type";
432 sml_andbind[0] = SML_AND_WHERETYPE;
433 sml_start_wheretyp ();
434 } else if (strcmp(sml_last_keyword,"sharing") == 0) {
435 sml_keyword ($0, SML_MODULES_LEVEL);
436 sml_last_keyword = "sharing type";
437 sml_andbind[0] = SML_AND_NUL;
438 sml_start_sharingtyp ();
439 } else {
440 sml_finish_typctxt ();
441 sml_keyword ($0, SML_CORE_LEVEL);
442 sml_andbind[0] = SML_AND_TYPBIND;
443 sml_start_typbind ();
444 }
445 }
446 sml_val_with_tyvar_re {
447 sml_finish_typctxt ();
448 sml_keyword ($1, SML_CORE_LEVEL);
449 language_print ($2);
450 type_face(true);
451 language_print ($3);
452 type_face(false);
453 sml_andbind[0] = SML_AND_VALBIND;
454 }
455 sml_val_with_tyvarseq_re {
456 sml_finish_typctxt ();
457 sml_keyword ($1, SML_CORE_LEVEL);
458 language_print ($2);
459 language_print ($3);
460 language_print ($4);
461 type_face(true);
462 language_print ($5);
463 type_face(false);
464 call (sml_tyvarseq);
465 sml_andbind[0] = SML_AND_VALBIND;
466 }
467 /\b(val)\b/ {
468 sml_finish_typctxt ();
469 sml_keyword ($0, SML_CORE_LEVEL);
470 sml_andbind[0] = SML_AND_VALBIND;
471 }
472 /\b(where)\b/ {
473 sml_finish_typctxt ();
474 sml_keyword ($0, SML_MODULES_LEVEL);
475 }
476 /\b(while)\b/ {
477 sml_keyword ($0, SML_CORE_LEVEL);
478 }
479 /\b(with)\b/ {
480 sml_finish_typctxt ();
481 sml_keyword ($0, SML_CORE_LEVEL);
482 }
483 /\b(withtype)\b/ {
484 sml_finish_typctxt ();
485 sml_keyword ($0, SML_CORE_LEVEL);
486 sml_andbind[0] = SML_AND_TYPBIND;
487 sml_start_typbind ();
488 }
489 sml_longid_re {
490 if (sml_conbind) {
491 sml_conbind = FALSE;
492 variable_name_face (true);
493 language_print ($0);
494 variable_name_face (false);
495 } else {
496 language_print ($0);
497 }
498 }
499 sml_id_re {
500 if (sml_conbind) {
501 if (sml_datbind == SML_DATBIND_UNKNOWN)
502 sml_datbind = SML_DATBIND_DECL;
503 sml_conbind = FALSE;
504 variable_name_face (true);
505 language_print ($0);
506 variable_name_face (false);
507 } else {
508 language_print ($0);
509 }
510 }
511
512}
513
514/*
515 * Binding tyvar seqs
516 */
517state sml_tyvarseq extends Highlight
518{
519 /,/ {
520 language_print($0);
521 }
522 sml_tyvar_re {
523 type_face(true);
524 language_print($0);
525 type_face(false);
526 }
527 /\)/ {
528 language_print($0);
529 return;
530 }
531}
532
533
534\f
535/*
536Local variables:
537mode: c
538End:
539*/