forked from dylan-lang/dylan-emacs-support
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dylan.el
1504 lines (1324 loc) · 60 KB
/
dylan.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; dylan.el --- Dylan editing modes -*- lexical-binding: t -*-
;; Lineage: CMU Gwydion Project, Harlequin/Functional Objects, Open Dylan
;; Copyright (C) 1994, 1995, 1996, 1997 Carnegie Mellon University (Robert Stockton)
;; Copyright (C) 1995, 1996, 1998 Harlequin (David N. Gray)
;; Copyright (C) 1999 Eric Kidd
;; Copyright (C) 2003 Stefan Plantikow
;; Copyright (C) 2004, 2005, 2007, 2010 Chris Page
;; Copyright (C) 2011, 2012, 2013 Hannes Mehnert
;; Copyright (C) 2013 Erik Charlebois
;; Copyright (C) 2016 Alfredo Beaumont
;; Copyright (C) 2018, 2019, 2020 Carl Gay
;; Copyright (C) 2021 Lassi Kortela
;; SPDX-License-Identifier: GPL-2.0-or-later
;; URL: https://opendylan.org/
;; Package-Version: 3.0
;; Package-Requires: ((emacs "25.1"))
;;; Commentary:
;; Dylan mode is a major mode for editing Dylan programs. It provides
;; indenting and syntax highlighting support.
;; Testing
;;
;; dylan-test.dylan contains Dylan code that is indented in the preferred way. One
;; way to test this code is to open that file and press Tab on each line, or on the
;; specific lines you're trying to affect.
;; Debugging
;;
;; So far I (cgay) have found it easiest to debug by adding calls to
;; (message ...) all over the place and keep the *Messages* window
;; visible, since tracing doesn't say the names of the arguments or
;; return values. If someone else knows a better way please comment.
;;
;; With so many cascading defvars and the fact that eval-buffer doesn't reset
;; their values, frequently the easiest way to test changes is to start a new
;; Emacs.
;;; Code:
;;; Customization:
(defgroup dylan nil
"Major mode for editing Dylan source code."
:group 'languages)
(defcustom dylan-indent 2
"*Number of spaces to indent each sub-block."
:type 'integer
:group 'dylan)
(defcustom dylan-continuation-indent 2
"*Number of additional spaces to indent each continued line.
A continued line may be, for example, a method parameter list
started on the line following the 'define method', the 2nd and
subsequent lines of a method call, the 2nd and subsequent lines
of a module 'use' statement, etc."
:type 'integer
:group 'dylan)
(defcustom dylan-highlight-function-calls nil
"*Whether to highlight function calls in Font Lock mode.
Applies only in Font Lock decoration level 2 or higher.
This uses a very simple regular expression that highlights just
about anything adjacent to a left-parenthesis."
:type 'boolean
:group 'dylan)
;;; Faces:
(defface dylan-header-background
'((t :inherit header))
"Background face for Dylan interchange file headers.
This is designed to apply background attributes to the entire
header, with other faces applied on top."
:group 'dylan)
(defface dylan-header-separator
'((t))
"Face for the last line of Dylan interchange file headers."
:group 'dylan)
(defface dylan-header-keyword
'((t :inherit font-lock-keyword-face))
"Face for Dylan interchange file header keywords."
:group 'dylan)
(defface dylan-header-value
'((t))
"Face for Dylan interchange file header values."
:group 'dylan)
(defface dylan-header-module-name
'((t :inherit font-lock-function-name-face))
"Face for the `module:' name in Dylan interchange file headers."
:group 'dylan)
(defface dylan-header-error
'((t :inherit font-lock-warning-face))
"Face for invalid lines in Dylan interchange file headers.
Valid lines begin with a keyword or a value continuation
whitespace prefix."
:group 'dylan)
;;; Regular expressions -- See https://opendylan.org/books/drm/Lexical_Grammar
(defconst dylan-graphic-character "!&*<>|^$%@_")
(defconst dylan-special-character "-+~?/=") ; code may assume '-' comes first
;;; For Dylan `name` BNF special care has to be taken to handle leading
;;; numerics and leading graphics.
(defconst dylan-name-pattern
(format
"\\([a-zA-Z]\\|[0-9][a-zA-Z][a-zA-Z]\\|[%s][a-zA-Z]\\)[%s%sa-zA-Z0-9]*"
dylan-graphic-character
dylan-special-character ; intentionally follows '[' in regex
dylan-graphic-character))
(defconst dylan-keyword-symbol-pattern
(format "%s:" dylan-name-pattern))
(defvar dylan-unnamed-definition-words
'(;; Melange/C-FFI
"interface")
"Words that introduce unnamed definitions like \"define interface\".")
(defvar dylan-named-definition-words
'(;; Dylan
"module" "library" "macro"
;; C-FFI
"C-struct" "C-union" "C-function"
"C-callable-wrapper")
"Words that introduce simple named definitions like \"define library\".")
(defvar dylan-type-parameterized-definition-words
'(;; Dylan
"class"
;; C-FFI
"C-subtype" "C-mapped-subtype")
;; TODO(cgay): It seems like a lie that these are parameterized
;; "like 'define method'".
;; The superclass is a type spec only, without a variable name.
"Words that introduce type definitions like \"define class\".
These are also parameterized like \"define method\" and are
appended to `dylan-other-parameterized-definition-words'.")
(defvar dylan-other-parameterized-definition-words
'(;; Dylan
"method" "function"
;; Testworks
"test" "benchmark" "suite"
;; C-FFI
"C-variable" "C-address")
"Words that introduce trickier definitions like \"define method\".
These require special definitions to be added to
`dylan-body-start-expressions'.")
(defvar dylan-constant-simple-definition-words
'(;; Dylan
"constant")
"Words that introduce module constant definitions.
These must also be simple definitions and are appended to
`dylan-other-simple-definition-words'.")
(defvar dylan-variable-simple-definition-words
'(;; Dylan
"variable")
"Words that introduce module variable definitions.
These must also be simple definitions and are appended to
`dylan-other-simple-definition-words'.")
(defvar dylan-other-simple-definition-words
'(;; Dylan
"generic" "domain"
;; C-FFI
"C-pointer-type"
;; Extensions
"table")
"Other words that introduce simple definitions (without implicit bodies).")
(defvar dylan-statement-words
'(;; Dylan
"if" "block" "begin" "method" "case" "for" "select" "when" "unless"
"until" "while"
;; Extensions
"iterate" "profiling")
"Words that begin statements with implicit bodies.")
;; Names beginning "with-", "without-", and "...ing-" (e.g., printing-object)
;; are commonly used as statement macros.
(defvar dylan-with-statement-prefix
"\\(with\\|without\\|[a-zA-Z]+ing\\)-")
(defvar dylan-statement-prefixes
(concat "\\|\\_<" dylan-with-statement-prefix "[-_a-zA-Z?!*@<>$%]+"))
(defvar dylan-separator-words
'("afterwards" "cleanup" "exception" "else" "elseif" "finally")
"These are the patterns that act as separators in compound statements.
This may include any general pattern that must be indented
specially.")
(defvar dylan-other-words
'(;; Dylan
"above" "below" "by" "from"
"handler" "in" "instance\\?" "let" "local" "otherwise"
"slot" "subclass" "then" "to"
;; Extensions
"keyed-by" "virtual")
"Keywords that do not require special indentation handling.
But which should be highlighted anyway.")
;;; See also dylan-skip-star-comment-backward and -forward.
(defvar dylan-comment-pattern "//.*$"
"Internal pattern for finding comments in Dylan code.
Currently only handles `//' comments.")
(defun dylan-make-pattern (start &rest list)
"Build a search pattern matching any of the patterns in LIST.
Makes sure that it doesn't match partial words. START is one word
that appears as a prefix of all potential matches."
(let ((str (concat "\\_<" start "\\_>")))
(while list
(setq str (concat str "\\|\\_<" (car list) "\\_>"))
(setq list (cdr list)))
str))
(defvar dylan-body-start-expressions '()
"Patterns matching the part of a compound statement before the body.
This is used to determine where the first statement begins for
indentation purposes.
Contains a list of patterns, each of which is either a regular
expression or a list of regular expressions. A set of balanced
parens will be matched between each list element.")
(defvar dylan-font-lock-header-keywords
;; Many of these regexp patterns are order-dependent, assuming the
;; preceding patterns have already been matched and fontified as
;; appropriate, preventing following patterns from being used to
;; fontify the same text.
;;
;; Most of these patterns match up to the end of buffer, so that
;; highlighting occurs while entering header text in a new Dylan
;; file. Notably, the pattern for invalid header lines does not, so
;; that it doesn't mark incomplete lines as invalid while the user
;; is still entering them. (It does mark even temporarily invalid
;; lines that aren't at the end of buffer, though.)
`(
;; The "module:" header line. Highlight the module name.
(,(concat "^"
"module:" ; keyword
"[ \t]*" ; space
"\\(\\("
"[-_a-zA-Z0-9?!*@<>$%]+" ; module name...
"\\)\\|"
"[^ \t\n][^\n]*?" ; ...or invalid value
"\\)"
"[ \t]*\\(\n\\|\\'\\)") ; tail space
(1 (if (match-beginning 2)
'dylan-header-module-name
'dylan-header-error)))
;; The "language:" header line. Highlight the language name. This
;; is a bit of pedantry on my part -- this header is rarely used,
;; except perhaps in very old files -- so I'm just using the same
;; face as for the module name, rather than defining a separate
;; face (or renaming the module name face to be more generic).
;; "infix-dylan" is the only portable value, so let's warn about
;; other values.
(,(concat "^"
"language:" ; keyword
"[ \t]*" ; space
"\\(\\("
"infix-dylan" ; language name...
"\\)\\|"
"[^ \t\n][^\n]*?" ; ...or invalid value
"\\)"
"[ \t]*\\(\n\\|\\'\\)") ; tail space
(1 (if (match-beginning 2)
'dylan-header-module-name
'dylan-header-error)))
;; Header lines with keywords, and lines with value continuations.
(,(concat "^"
"\\(?:\\("
"[a-zA-Z][-a-zA-Z0-9]*:" ; keyword...
"\\)\\|"
"[ \t]" ; ...or continuation prefix
"\\)"
"[ \t]*" ; space
"\\("
;; Can keyword lines have empty values?
"[^ \t\n][^\n]*?" ; value
"\\)"
"[ \t]*\\(\n\\|\\'\\)") ; tail space
(1 'dylan-header-keyword nil t)
(2 'dylan-header-value))
;; Invalid header lines. This pattern assumes we've already tried the
;; pattern for header lines with keywords and it didn't match.
;;
;; Note: Ideally, we'd mark any subsequent continuation lines invalid,
;; too. Look into a way to do that.
(,(concat "^"
"[^ \t\n]" ; any invalid prefix character
"[^\n]*\n") ; rest of line
. 'dylan-header-error)
;; Mark all lines in the header with the header background face
;; (except for the final, blank line).
(,(concat "^"
"[ \t]*" ; possible continuation prefix
"[^ \t\n]+" ; any non-whitespace in line
"[^\n]*\n") ; rest of line
(0 'dylan-header-background append))
;; Mark the final, blank line with the header separator face.
(,(concat "^"
"[ \t]*\n") ; tail space
. 'dylan-header-separator))
"Font-lock keywords for Dylan interchange file headers in Dylan Mode.
See `font-lock-keywords'.")
(defvar dylan-font-lock-keywords nil
"Keywords for Dylan Mode in Font Lock decoration level 0.
See `font-lock-keywords'.")
(defvar dylan-font-lock-keywords-1 nil
"Keywords for Dylan Mode in Font Lock decoration level 1.
See `font-lock-keywords'.")
(defvar dylan-font-lock-keywords-2 nil
"Keywords for Dylan Mode in Font Lock decoration level 2.
See `font-lock-keywords'.")
(defconst dylan-define-pattern
(format "define\\([[:blank:]]+%s\\)*[[:blank:]]+" dylan-name-pattern)
"Pattern that matches `define' and adjectives.
A sub-pattern designed to be followed by patterns that match the
define word or other parts of the definition macro call.")
(defvar dylan-other-definition-words
(append dylan-unnamed-definition-words
dylan-named-definition-words
dylan-other-parameterized-definition-words))
(defvar dylan-definition-words
(append dylan-type-parameterized-definition-words
dylan-other-definition-words))
(defvar dylan-type-definition-pattern
(regexp-opt dylan-type-parameterized-definition-words 'symbols))
(defvar dylan-other-definition-pattern
(regexp-opt dylan-other-definition-words 'symbols))
(defvar dylan-definition-pattern
(regexp-opt dylan-definition-words 'symbols))
(defvar dylan-named-definition-pattern
(regexp-opt dylan-named-definition-words 'symbols))
(defvar dylan-unnamed-definition-pattern
(regexp-opt dylan-unnamed-definition-words 'symbols))
(defvar dylan-type-parameterized-definition-pattern
(regexp-opt dylan-type-parameterized-definition-words 'symbols))
(defvar dylan-parameterized-definition-pattern
(regexp-opt (append dylan-type-parameterized-definition-words
dylan-other-parameterized-definition-words) 'symbols))
(defvar dylan-separator-word-pattern
(regexp-opt dylan-separator-words 'symbols)
;; Try to find a way to make these context-sensitive, so they are
;; only highlighted within the appropriate statements.
;; Unfortunately, doing this robustly may require knowing the syntax
;; of all statement macros and parsing them so we don't highlight
;; separators when they're within nested statements. On the other
;; hand, we may get a lot of mileage out of just handling all the
;; iteration words in for statements.
"Separator words in statement macros.")
(defvar dylan-constant-simple-definition-pattern
(regexp-opt dylan-constant-simple-definition-words 'symbols))
(defvar dylan-variable-simple-definition-pattern
(regexp-opt dylan-variable-simple-definition-words 'symbols))
(defvar dylan-other-simple-definition-pattern
(regexp-opt dylan-other-simple-definition-words 'symbols))
(defvar dylan-simple-definition-pattern
(regexp-opt (append dylan-constant-simple-definition-words
dylan-variable-simple-definition-words
dylan-other-simple-definition-words) 'symbols))
(defvar dylan-end-keyword-pattern nil)
(defvar dylan-other-pattern nil)
(defvar dylan-keyword-pattern nil)
(defvar dylan-find-keyword-pattern nil)
(defvar dylan-beginning-of-form-pattern nil)
(defun dylan-mode-init-keyword-patterns ()
"Build Dylan Mode keyword patterns.
Used for indenting and highlighting. The patterns are built from
the various keyword list variables."
;; Define regular expression patterns using the word lists.
(setq dylan-keyword-pattern
;; We disallow newlines in "define foo" patterns because it allows the
;; actual keyword to be confused for a qualifier if another definition
;; follows closely.
(concat
(apply 'dylan-make-pattern
(concat dylan-define-pattern dylan-definition-pattern)
dylan-statement-words)
dylan-statement-prefixes))
(setq dylan-end-keyword-pattern
;; We intentionally disallow newlines in "end foo" constructs, because
;; doing so makes it very difficult to deal with the keyword "end" in
;; comments.
(concat "\\_<end\\_>[ \t]*\\("
(apply 'dylan-make-pattern
(append dylan-definition-words dylan-statement-words))
dylan-statement-prefixes
"\\)?"))
(setq dylan-other-pattern
(apply 'dylan-make-pattern
(concat "define\\([ \t\n]+\\w+\\)*[ \t\n]+"
dylan-simple-definition-pattern)
dylan-other-words))
(setq dylan-body-start-expressions
;; cpage 2007-04-06: Why are these listed here? Shouldn't we
;; build these patterns from dylan-statement-words?
`(("if[ \t\n]*" "")
("block[ \t\n]*" "")
("for[ \t\n]*" "")
("select[ \t\n]*" "")
("when[ \t\n]*" "")
("unless[ \t\n]*" "")
("until[ \t\n]*" "")
("while[ \t\n]*" "")
("iterate[ \t\n]+\\w+[ \t\n]*" "")
("profiling[ \t\n]*" "")
;; Special patterns for "define method" and "define function", which
;; have a return value spec.
;; TODO(cgay): comments may appear before/in/after the signature.
(,(concat "\\(" dylan-define-pattern "\\)?"
"\\(method\\|function\\)[ \t\n]+[^( ]*[ \t\n]*")
"[ \t\n]*=>[^;)]+)?;?")
(,(concat "\\(" dylan-define-pattern "\\)?"
"\\(method\\|function\\)[ \t\n]+[^( ]*[ \t\n]*")
"[ \t\n]*;")
,(concat "define[ \t]+" dylan-named-definition-pattern
"[ \t\n]+[^ \t\n]+")
,(concat "define[ \t]+" dylan-unnamed-definition-pattern)
(,(concat "\\(" dylan-define-pattern "\\)?"
dylan-parameterized-definition-pattern
"[ \t\n]+[^( ]*[ \t\n]*")
"")
"begin"
"case"
;; Since we don't know the syntax of all the "with(out)-" macros,
;; just assume that the user has already split the line at
;; the end of the header.
,(concat dylan-with-statement-prefix "[^\n]*")
"[[({]"))
(setq dylan-find-keyword-pattern (concat "[][)(}{\"']\\|\\_<define\\_>\\|"
dylan-end-keyword-pattern
"\\|" dylan-keyword-pattern))
(setq dylan-beginning-of-form-pattern
(concat "[;,]\\|=>\\|"
dylan-find-keyword-pattern
"\\|" dylan-separator-word-pattern)))
(defun dylan-mode-init-font-lock-keywords ()
"Build Dylan Mode font-lock keyword variables.
Uses the values of the various pattern variables."
;; Decoration level 0: Don't highlight anything, currently -- mostly useful
;; for testing. Think about how to best differentiate between 0 and 1 by
;; moving some keyword patterns from 1 to 0, or by moving 2 to 3 and moving
;; some from 1 to 2.
;; TODO(cgay): For me personally, I don't want most Dylan reserved
;; words, like "define", "method", "macro", to be highlighted
;; specially. Those are the necessary background noise of the
;; language and don't need calling out.
;; I like
;; * Newly introduced bindings
;; - let bindings
;; - define function|method|class|etc names
;; - parameter names, maybe
;; - macro variable bindings, "x" in ?x:name
;; * return, signal, and error
;; * keyword symbols (foo: but not #"foo")
;; * strings, maybe
;; * comments
;; Maybe that could be level 0.
(setq dylan-font-lock-keywords nil)
;; Decoration level 1: Most Dylan keywords
(setq dylan-font-lock-keywords-1
(append dylan-font-lock-keywords
`(,dylan-end-keyword-pattern
,dylan-keyword-pattern
,dylan-separator-word-pattern
,dylan-keyword-symbol-pattern
;; Symbols with string syntax
;;
;; Is there a better way to fontify these symbols? Using
;; font-lock syntactic keywords, perhaps?
("\\(#\\)\"[^\"]*\"?" 1 font-lock-string-face)
;; Logical negation operator
("\\W\\(~\\)" 1 font-lock-negation-char-face)
;; Function signature keywords
;;
;; "#" does not have symbol or word syntax, so we can't
;; match for "\\<" at the start of #-words. Match for "not
;; a word constituent" instead. This highlights some
;; patterns that aren't valid Dylan, but it's close
;; enough. (e.g., it highlights "#key" within "##key".)
(,(concat "\\W"
(regexp-opt
'("#rest" "#key" "#all-keys" "#next")
t)
"\\>")
1 font-lock-keyword-face)
,dylan-other-pattern
;; Condition signaling function calls
(,(concat (regexp-opt
'("signal" "error" "cerror"
"break" "check-type" "abort")
'words)
"[ \t]*(")
1 font-lock-warning-face)
;; Definition starts
(,(concat
"\\_<\\(" dylan-define-pattern
"\\(" dylan-constant-simple-definition-pattern "\\|"
dylan-variable-simple-definition-pattern "\\|"
dylan-other-simple-definition-pattern "\\)"
"\\)\\_>[ \t]+\\(\\(\\s_\\|\\w\\)+\\)")
(7 (cond ((match-beginning 4)
'font-lock-constant-face)
((match-beginning 5)
'font-lock-variable-name-face)
(t
'font-lock-function-name-face))))
(,(concat "\\_<\\(" dylan-define-pattern
dylan-definition-pattern "\\)")
1 font-lock-keyword-face)
(,(concat "\\_<\\(" dylan-define-pattern
"\\(" dylan-type-definition-pattern "\\|"
dylan-other-definition-pattern "\\)"
"\\)\\_>[ \t]+\\(\\(\\s_\\|\\w\\)+\\)")
(6 (cond ((match-beginning 4) 'font-lock-type-face)
(t 'font-lock-function-name-face))))
;; Local methods
("method[ \t\n]+\\(\\(\\s_\\|\\sw\\)+\\)"
1 font-lock-function-name-face)
("\\(\\_<\\(\\s_\\|\\sw\\)+\\)\\_>\\s-+::"
1 font-lock-variable-name-face)
("::\\s-+\\(\\_<\\(\\s_\\|\\sw\\)+\\)\\_>"
1 font-lock-type-face)
;; Definition ends
(,(concat "\\_<end[ \t]+\\("
dylan-type-definition-pattern
"\\|\\w*\\)\\_>[ \t]+\\(\\(\\s_\\|\\w\\)+\\)")
(3 (cond ((match-beginning 2) 'font-lock-type-face)
(t 'font-lock-function-name-face)))))))
;; Decoration level 2: Highlight all function and local variable
;; definitions, and, optionally, all function calls.
(setq dylan-font-lock-keywords-2
(append
dylan-font-lock-keywords-1
'(("slot[ \t\n]+\\(\\(\\sw\\|\\s_\\)+\\)"
1 font-lock-variable-name-face)
("block[ \t\n]+(\\([^)]+\\)"
1 font-lock-function-name-face)
("let[ \t\n]+\\(\\(\\sw\\|\\s_\\)+\\)"
1 font-lock-variable-name-face)
;; This highlights commas and whitespace separating the
;; variable names. Try to find a way to highlight only the
;; variable names.
("let[ \t\n]+(\\([^)]+\\)" 1 font-lock-variable-name-face))))
(when dylan-highlight-function-calls
(setq dylan-font-lock-keywords-2
(append dylan-font-lock-keywords-2
;; Function calls
'(("\\_<\\(\\(\\s_\\|\\w\\)+\\)("
1 font-lock-function-name-face))))))
(defun dylan-look-back (regexp)
"Attempt to find a match for REGEXP right before point.
Returns t if a match was found, nil otherwise. In order for this
to work properly, the search string must end with \"$\". Also
note that this will only work within the current line."
(save-excursion
(save-restriction
(let ((dot (point)))
(beginning-of-line)
(narrow-to-region dot (point))
(re-search-forward regexp nil t)))))
(defvar dylan-find-keyword-pattern nil
"Pattern to match the beginning and end of various \"blocks\".
Including parenthesized expressions.")
(defvar dylan-beginning-of-form-pattern nil
"Like `dylan-find-keyword-pattern' but also matches statement terminators.")
(defun dylan-header-end ()
"Get the position of the end of the interchange file header.
Dylan interchange file headers end at the first empty line in the
buffer, containing no whitespace. Note that a well-formed header
would match
\"\\`\\([a-zA-Z][-a-zA-Z0-9]*:.*\n\\([ \t]+.+\n\\)*\\)*\n\"
but this function is only meant to partition the file into header and body
so we can handle them separately, whether they are well-formed or not."
(save-excursion
(save-restriction
(widen)
(goto-char 1)
(or (and (re-search-forward "^[ \t]*\\(\n\\|\\'\\)" nil t)
;; in Emacs 18, the search just returns `t', not point.
(point))
(point-max)))))
(defun dylan-backward-find-keyword
(&optional match-statement-end in-case no-commas _start)
"Find the previous keyword.
Move point backward to the beginning of the innermost enclosing
compound statement or set of parentheses. Return t on success and
nil otherwise.
TODO: Document MATCH-STATEMENT-END, IN-CASE, NO-COMMAS, START."
;; don't go back into the interchange file header
(let ((header-end (dylan-header-end))
(result 'not-found))
(while (and (>= (point) header-end) (eq result 'not-found))
;; cpage 2007-04-14: This could handle block comments better.
;; The re-search-backward pattern doesn't skip over them, and
;; dylan-skip-whitespace-backward can't skip over a block
;; comment if point is inside it.
(setq
result
(if (re-search-backward (if match-statement-end
dylan-beginning-of-form-pattern
dylan-find-keyword-pattern)
header-end t)
(cond (;; Skip backwards over eol comments.
(and (dylan-look-back dylan-comment-pattern)
(not (save-excursion
(dylan-look-back
(concat "\"[^\"]*" dylan-comment-pattern)))))
(goto-char (match-beginning 0))
'not-found)
;; If point is inside a block comment, keep searching. Since
;; we've just tested (above) for an eol comment, if the text
;; has the comment face applied it must be the interior of a
;; block comment. This isn't a complete solution for handling
;; block comments, but it provides much better behavior than
;; not performing this test at all -- in which case, the
;; interior of block comments are treated like code.
((equal (get-text-property (point) 'face)
font-lock-comment-face)
'not-found)
;; Skip backwards over balanced parens.
((looking-at "[])}'\"]")
(condition-case nil
(progn
(forward-char 1)
(backward-sexp 1)
'not-found)
(error nil)))
;; At start of unrecognized definition. Stop searching.
((and (looking-at "define") ; non-nesting top level form
(not (looking-at dylan-keyword-pattern)))
nil)
;; Skip backward over blocks/statements that end with "end".
((or (looking-at "end") ; Point is either before or
(and (dylan-look-back "\\_<end[ \t]*$") ; after "end".
(backward-word 1)))
;; Search for the start of the block.
(dylan-backward-find-keyword)
;; cpage 2007-05-17: Why does this check for "method" and
;; "define"? Should it also check for "define...function"?
;; What about "define...class", etc.?
(if (or (and (looking-at "method")
(dylan-look-back
"define\\([ \t\n]+\\w+\\)*[ \t]+$"))
(looking-at "define"))
nil
'not-found))
;; cpage 2007-05-17: What is this for? Does it handle
;; `until:' and `while:' within `for' iteration
;; clauses? Shouldn't it test for the keywords with
;; the colon?
;;
;; hack for overloaded uses of "while" and "until" words
((or (looking-at "until") (looking-at "while"))
(if (save-excursion
(condition-case nil
(progn
(backward-up-list 1)
(backward-sexp 1)
(looking-at "for\\_<")) (error nil)))
(backward-up-list 1))
t)
;; Statement macro separator words.
((and (looking-at dylan-separator-word-pattern)
(not match-statement-end))
'not-found)
;; cpage 2007-05-17: What do the following three clauses look
;; for?
((and (looking-at ";")
(not match-statement-end))
'not-found)
((and (looking-at ",")
(or (not match-statement-end) no-commas))
'not-found)
((and (looking-at "=>")
(not (and match-statement-end in-case)))
'not-found)
(t t))
(goto-char (point-min))
nil)))
(and (equal t result)
(>= (point) header-end))))
(defun dylan-find-end (&optional match-statement-end in-case no-commas)
"Move point forward to the end of the enclosing construct.
Find the end of the innermost compound statement or set of
parentheses. Returns t on success and nil otherwise.
TODO: Document MATCH-STATEMENT-END, IN-CASE, NO-COMMAS."
(let ((result 'not-found))
(while (eq result 'not-found)
(setq
result
(if (re-search-forward (if match-statement-end
dylan-beginning-of-form-pattern
dylan-find-keyword-pattern) nil t)
(let ((match-start (match-beginning 0)))
(cond ((dylan-look-back dylan-comment-pattern)
(forward-line)
'not-found)
((dylan-look-back "[[({'\"]$")
(condition-case nil
(progn
(backward-char 1)
(forward-sexp 1)
'not-found)
(error nil)))
((dylan-look-back "[])}]$") t)
((dylan-look-back "define$") ; top-level form special case
(dylan-find-end t nil nil)
nil)
((dylan-look-back "\\_<end\\([ \t]+\\w+\\)?$")
(if (and (not (looking-at "[ \t]+\\(end\\|=>\\)\\_>"))
(looking-at "[ \t]+\\w+"))
(goto-char (match-end 0)))
t)
;; hack for overloaded uses of "while" and "until" reserved
;; words
((dylan-look-back "until$\\|while$")
(if (save-excursion
(condition-case nil
(progn
(backward-up-list 1)
(backward-sexp 1)
(looking-at "for\\_<")) (error nil)))
(up-list 1))
t)
((save-excursion (goto-char match-start)
(looking-at dylan-separator-word-pattern))
t)
((dylan-look-back ";$")
(if (not match-statement-end)
'not-found
t))
((dylan-look-back ",$")
(if (or (not match-statement-end) no-commas)
'not-found
t))
((dylan-look-back "=>$")
(if (not (and match-statement-end in-case))
'not-found
t))
(t ; start compound statement
(if (save-excursion (goto-char match-start)
(looking-at "define"))
(progn (dylan-find-end) nil)
(dylan-find-end)
'not-found))))
(goto-char (point-max))
nil)))
result))
(defun dylan-skip-star-comment-backward ()
"Helper for `dylan-skip-whitespace-backward'.
Find beginning of enclosing block comment. Deals properly with
nested block comments and with comments inside strings."
(re-search-backward "/\\*\\|\\*/")
(while (cond ((dylan-look-back dylan-comment-pattern)
(goto-char (match-beginning 0)))
((looking-at "\\*/")
(dylan-skip-star-comment-backward))
(t nil))
(re-search-backward "/\\*\\|\\*/" nil t))
t)
(defun dylan-skip-star-comment-forward ()
"Helper for `dylan-skip-whitespace-forward'.
Find end of enclosing block comment. Deals properly with nested
block comments and comments inside strings."
(re-search-forward "/\\*\\|\\*/")
(while (cond ((dylan-look-back dylan-comment-pattern)
(end-of-line))
((dylan-look-back "/\\*$")
(dylan-skip-star-comment-forward))
(t nil))
(re-search-forward "/\\*\\|\\*/" nil t))
t)
(defun dylan-skip-whitespace-backward ()
"Skips over both varieties of comments and other whitespace characters."
;; don't go back into the interchange file header
(let ((header-end (dylan-header-end)))
(unless (< (point) header-end)
;; skip syntactic whitespace
(skip-syntax-backward " >" header-end)
;; skip comments
(while (cond ((dylan-look-back dylan-comment-pattern)
(goto-char (match-beginning 0)))
((dylan-look-back "\\*/$")
(goto-char (match-beginning 0))
(dylan-skip-star-comment-backward))
(t nil))
(skip-syntax-backward " >" header-end)))))
(defun dylan-skip-whitespace-forward ()
"Skips over both varieties of comments and other whitespace characters."
;; skip syntactic whitespace
(skip-syntax-forward " >")
;; skip comments
(while (cond ((looking-at dylan-comment-pattern)
(goto-char (match-end 0))
t)
((looking-at "/\\*")
(goto-char (match-end 0))
(dylan-skip-star-comment-forward))
(t nil))
(skip-syntax-forward " >")))
(defun dylan-aux-find-body-start (clauses)
"Helper for `dylan-find-body-start' to find one of CLAUSES."
(save-excursion
(cond ((null clauses) (point))
((looking-at (car clauses))
(if (null (cdr clauses))
(match-end 0)
(goto-char (match-end 0))
(and (looking-at "[[({]")
(condition-case nil (forward-list) (error nil))
(dylan-aux-find-body-start (cdr clauses))))))))
(defun dylan-find-body-start (exprs)
"Helper to find the innermost enclosing statement belonging to EXPRS.
When passed `dylan-body-start-expressions', processes it to find
the beginning of the first statement in the compound statement
that starts at the current point."
(let ((start (cond ((null exprs) (point-max))
((listp (car exprs))
(or (dylan-aux-find-body-start (car exprs))
(dylan-find-body-start (cdr exprs))))
(t (if (looking-at (car exprs))
(match-end 0)
(dylan-find-body-start (cdr exprs)))))))
;; Skip end-of-line comments that occur immediately after a start
;; expression.
;;
;; Example: method foo () => () // here
;; Example: method foo () // here
;; => ()
(save-excursion
(goto-char start)
(when (looking-at (concat "[ \t]*" dylan-comment-pattern))
(end-of-line))
(point))))
(defun dylan-backward-statement (&optional in-case no-commas)
"Move point between the previous statement and the current one.
The precise point is undefined. If we are already between
statements, move back one more.
TODO: Document IN-CASE, NO-COMMAS."
;; don't go back into the interchange file header
(let ((header-end (dylan-header-end)))
(unless (< (point) header-end)
(dylan-skip-whitespace-backward)
(let* ((dot (point)))
;; Skip over words like "else" that separate statements inside a body.
(when (save-excursion
(and (re-search-backward dylan-separator-word-pattern
header-end t)
(if (not (looking-at "exception\\|elseif"))
(forward-word 1)
(goto-char (match-end 0))
(condition-case nil (forward-list 1)
(error nil))
t)
(>= (point) dot)))
(re-search-backward dylan-separator-word-pattern header-end t)
(dylan-skip-whitespace-backward))
(if (dylan-look-back "[,;]$\\|=>$")
(backward-char)) ; TODO(cgay): should go back 2 for "=>" ?
(cond ((not (dylan-backward-find-keyword t in-case no-commas))
(if (dylan-look-back "\\(define\\|local\\)[ \t]+") ; hack
(goto-char (match-beginning 0))))
((looking-at dylan-separator-word-pattern)
(let ((start (point)))
(cond ((looking-at "\\(exception\\|elseif\\)[ \t\n]*(")
(goto-char (match-end 1))
(condition-case nil (forward-list 1)
(error nil)))
(t (forward-word 1)))
(if (>= (point) dot)
(progn (goto-char start)
(dylan-backward-statement in-case no-commas)))))
((looking-at "[;,]\\|=>")
(goto-char (match-end 0)))
(t
;; check whether we were already at the first "form" in an
;; enclosing block
(let ((first (dylan-find-body-start
dylan-body-start-expressions)))
(if (< first dot)
(goto-char first)
(if (dylan-look-back "\\(define\\|local\\)[ \t]+") ; hack
(goto-char (match-beginning 0)))))))))))
(defun dylan-beginning-of-form ()
"Find the beginning of the innermost statement.
That contains or terminates at the current point."
(interactive)
(dylan-backward-statement)
(dylan-skip-whitespace-forward))
(defun dylan-forward-statement (&optional in-case no-commas)
"Move the cursor between this statement and the next one.
The precise point is undefined. If we are already between
statements, move forward one more.
TODO: Document IN-CASE, NO-COMMAS."
(dylan-skip-whitespace-forward)