This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
/
fsharp-mode-font.el
260 lines (227 loc) · 10.5 KB
/
fsharp-mode-font.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
;;; fsharp-mode-font.el --- Syntax highlighting for F#
;; Copyright (C) 1997 INRIA
;; Author: 1993-1997 Xavier Leroy, Jacques Garrigue and Ian T Zimmerman
;; 2010-2011 Laurent Le Brun <laurent@le-brun.eu>
;; Maintainer: Robin Neatherway <robin.neatherway@gmail.com>
;; Keywords: languages
;; This file is not part of GNU Emacs.
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;; useful colors
(cond
((x-display-color-p)
(require 'font-lock)
(cond
((not (boundp 'font-lock-type-face))
;; make the necessary faces
(make-face 'Firebrick)
(set-face-foreground 'Firebrick "firebrick")
(make-face 'RosyBrown)
(set-face-foreground 'RosyBrown "RosyBrown")
(make-face 'Purple)
(set-face-foreground 'Purple "Purple")
(make-face 'MidnightBlue)
(set-face-foreground 'MidnightBlue "MidnightBlue")
(make-face 'DarkGoldenRod)
(set-face-foreground 'DarkGoldenRod "DarkGoldenRod")
(make-face 'DarkOliveGreen)
(set-face-foreground 'DarkOliveGreen "DarkOliveGreen4")
(make-face 'CadetBlue)
(set-face-foreground 'CadetBlue "CadetBlue")
; assign them as standard faces
(setq font-lock-comment-face 'Firebrick)
(setq font-lock-string-face 'RosyBrown)
(setq font-lock-keyword-face 'Purple)
(setq font-lock-function-name-face 'MidnightBlue)
(setq font-lock-variable-name-face 'DarkGoldenRod)
(setq font-lock-type-face 'DarkOliveGreen)
(setq font-lock-constant-face 'CadetBlue)))
; extra faces for documention
(make-face 'Stop)
(set-face-foreground 'Stop "White")
(set-face-background 'Stop "Red")
(make-face 'Doc)
(set-face-foreground 'Doc "Red")
))
(defconst fsharp-access-control-regexp
"\\(?:private\\s-+\\|internal\\s-+\\|public\\s-+\\)*")
(defconst fsharp-function-def-regexp
"\\<\\(?:let\\|and\\|with\\)\\s-+\\(?:\\(?:inline\\|rec\\)\\s-+\\)?\\([A-Za-z0-9_']+\\)\\(?:\\s-+[A-Za-z_]\\|\\s-*(\\)")
(defconst fsharp-pattern-function-regexp
"\\<\\(?:let\\|and\\)\\s-+\\(?:\\(?:inline\\|rec\\)\\s-+\\)?\\([A-Za-z0-9_']+\\)\\s-*=\\s-*function")
(defconst fsharp-active-pattern-regexp
"\\<\\(?:let\\|and\\)\\s-+\\(?:\\(?:inline\\|rec\\)\\s-+\\)?(\\(|[A-Za-z0-9_'|]+|\\))\\(?:\\s-+[A-Za-z_]\\|\\s-*(\\)")
(defconst fsharp-member-function-regexp
"\\<\\(?:override\\|member\\|abstract\\)\\s-+\\(?:\\(?:inline\\|rec\\)\\s-+\\)?\\(?:[A-Za-z0-9_']+\\.\\)?\\([A-Za-z0-9_']+\\)")
(defconst fsharp-overload-operator-regexp
"\\<\\(?:override\\|member\\|abstract\\)\\s-+\\(?:\\(?:inline\\|rec\\)\\s-+\\)?\\(([!%&*+-./<=>?@^|~]+)\\)")
(defconst fsharp-constructor-regexp "^\\s-*\\<\\(new\\) *(.*)[^=]*=")
(defconst fsharp-type-def-regexp
(format "^\\s-*\\<\\(?:type\\|inherit\\)\\s-+%s\\([A-Za-z0-9_'.]+\\)"
fsharp-access-control-regexp))
(defconst fsharp-var-or-arg-regexp "\\_<\\([A-Za-z_][A-Za-z0-9_']*\\)\\_>")
(defconst fsharp-explicit-field-regexp
(format "^\\s-*\\(?:val\\|abstract\\)\\s-*\\(?:mutable\\s-+\\)?%s\\([A-Za-z_][A-Za-z0-9_']*\\)\\s-*:\\s-*\\([A-Za-z_][A-Za-z0-9_'<> \t]*\\)" fsharp-access-control-regexp))
(defvar fsharp-imenu-generic-expression
`((nil ,(concat "^\\s-*" fsharp-function-def-regexp) 1)
(nil ,(concat "^\\s-*" fsharp-pattern-function-regexp) 1)
(nil ,(concat "^\\s-*" fsharp-active-pattern-regexp) 1)
(nil ,(concat "^\\s-*" fsharp-member-function-regexp) 1)
(nil ,(concat "^\\s-*" fsharp-overload-operator-regexp) 1)
(nil ,fsharp-constructor-regexp 1)
(nil ,fsharp-type-def-regexp 1)
))
(defvar fsharp-var-pre-form
(lambda ()
(save-excursion
(re-search-forward "\\(:\\s-*\\w[^)]*\\)?=")
(match-beginning 0))))
(defvar fsharp-fun-pre-form
(lambda ()
(save-excursion
(search-forward "->"))))
(defconst fsharp-font-lock-keywords
(list
(cons (regexp-opt
'(;; Sections in brackets refer to the F# 3.0 specification
;; Preprocessor directives (3.3)
"#if" "#else" "#endif"
;; Compiler directives (12.4)
"#nowarn" "#load" "#r" "#reference" "#I"
"#Include" "#q" "#quit" "#time" "#help"
;; Lexical matters (18.4)
"#indent"
;; Line Directives (3.9)
"#line"
;; Identifier replacements (3.11)
"__SOURCE_DIRECTORY__" "__SOURCE_FILE__" "__LINE__"
;; F# keywords (3.4)
"abstract" "and" "as" "assert" "base" "begin"
"class" "default" "delegate" "do" "done"
"downcast" "downto" "elif" "else" "end"
"exception" "extern" "false" "finally" "for" "fun"
"function" "global" "if" "in" "inherit" "inline"
"interface" "internal" "lazy" "let" "let!"
"match" "member" "module" "mutable" "namespace"
"new" "not" "null" "of" "open" "or" "override"
"private" "public" "rec" "return" "return!"
"select" "static" "struct" "then" "to" "true"
"try" "type" "upcast" "use" "use!" "val" "void"
"when" "while" "with" "yield" "yield!"
;; "Reserved because they are reserved in OCaml"
"asr" "land" "lor" "lsl" "lsr" "lxor" "mod" "sig"
;; F# reserved words for future use
"atomic" "break" "checked" "component" "const"
"constraint" "constructor" "continue" "eager"
"event" "external" "fixed" "functor" "include"
"method" "mixin" "object" "parallel" "process"
"protected" "pure" "sealed" "tailcall" "trait"
"virtual" "volatile"
;; Workflows not yet handled by fsautocomplete
;; but async always present
"async"
) 'symbols)
'font-lock-keyword-face)
;blocking
;; '("\\<\\(begin\\|end\\|module\\|namespace\\|object\\|sig\\|struct\\)\\>"
;; . font-lock-keyword-face)
;control
;; attributes
'("\\[<[A-Za-z0-9_]+>\\]" . font-lock-preprocessor-face)
;; type defines
`(,fsharp-type-def-regexp 1 font-lock-type-face)
`(,fsharp-function-def-regexp 1 font-lock-function-name-face)
`(,fsharp-pattern-function-regexp 1 font-lock-function-name-face)
`(,fsharp-active-pattern-regexp 1 font-lock-function-name-face)
`(,fsharp-member-function-regexp 1 font-lock-function-name-face)
`(,fsharp-overload-operator-regexp 1 font-lock-function-name-face)
;; `(,fsharp-constructor-regexp 1 font-lock-function-name-face)
`("[^:]:\\s-*\\(\\<[A-Za-z_'][^,)=<-]*\\)\\s-*\\(<[^>]*>\\)?"
(1 font-lock-type-face) ; type annotations
;; HACK: font-lock-negation-char-face is usually the same as
;; 'default'. use this to prevent generic type arguments from
;; being rendered in variable face
(2 font-lock-negation-char-face nil t))
`(,(format "^\\s-*\\<\\(let\\|use\\|override\\|member\\|and\\|\\(?:%snew\\)\\)\\_>"
fsharp-access-control-regexp)
(0 font-lock-keyword-face) ; let binding and function arguments
(,fsharp-var-or-arg-regexp
(,fsharp-var-pre-form) nil
(1 font-lock-variable-name-face nil t)))
`("\\<fun\\>"
(0 font-lock-keyword-face) ; lambda function arguments
(,fsharp-var-or-arg-regexp
(,fsharp-fun-pre-form) nil
(1 font-lock-variable-name-face nil t)))
`(,fsharp-type-def-regexp
(0 font-lock-keyword-face) ; implicit constructor arguments
(,fsharp-var-or-arg-regexp
(,fsharp-var-pre-form) nil
(1 font-lock-variable-name-face nil t)))
`(,fsharp-explicit-field-regexp
(1 font-lock-variable-name-face)
(2 font-lock-type-face))
;; open namespace
'("\\<open\s\\([A-Za-z0-9_.]+\\)" 1 font-lock-type-face)
;; module/namespace
'("\\_<\\(?:module\\|namespace\\)\s\\([A-Za-z0-9_.]+\\)" 1 font-lock-type-face)
;labels (and open)
'("\\_<\\(assert\\|open\\|include\\|module\\|namespace\\|extern\\|void\\)\\_>\\|[~][ (]*[a-z][a-zA-Z0-9_']*"
. font-lock-variable-name-face)
;; (cons (concat
;; "\\<\\(asr\\|false\\|land\\|lor\\|lsl\\|lsr\\|lxor"
;; "\\|mod\\|new\\|null\\|object\\|or\\|sig\\|true\\)\\>"
;; "\\|\|\\|->\\|&\\|#")
;; 'font-lock-constant-face)
))
(defun fsharp--syntax-propertize-function (start end)
; (message "Called with (%d,%d)" start end)
(goto-char start)
(fsharp--syntax-string end)
(funcall (syntax-propertize-rules
("\\(@\\)\"" (1 (prog1 "|" (fsharp--syntax-string end)))) ; verbatim string
("\\(\"\\)\"\"" (1 (prog1 "|" (fsharp--syntax-string end)))) ; triple-quoted string
("\\('\\)\\(?:[^\n\t\r\b\a\f\v\\\\]\\|\\\\[\"'ntrbafv\\\\]\\|\\\\u[0-9A-Fa-f]\\{4\\}\\|\\\\[0-9]\\{3\\}\\)\\('\\)"
(1 "|") (2 "|")) ; character literal
("\\((\\)/" (1 "()"))
("\\(/\\)\\*" (1 ".")))
start end))
(defun fsharp--syntax-string (end)
(let* ((pst (syntax-ppss))
(instr (nth 3 pst))
(start (nth 8 pst)))
(when (eq t instr) ; Then we are in a custom string
;(message "In custom string")
(cond
((eq ?@ (char-after start)) ; Then we are in a verbatim string
;(message "verbatim")
(while
(when (re-search-forward "\"\"?" end 'move)
(if (> (- (match-end 0) (match-beginning 0)) 1)
t ;; Skip this "" and keep looking further.
(put-text-property (- (match-beginning 0) 1) (- (match-end 0) 1)
'syntax-table (string-to-syntax "."))
(put-text-property (match-beginning 0) (match-end 0)
'syntax-table (string-to-syntax "|"))
nil)))
)
(t ; Then we are in a triple-quoted string
;(message "triple-quoted")
(when (re-search-forward "\"\"\"" end 'move)
(put-text-property (- (match-beginning 0) 1) (match-beginning 0)
'syntax-table (string-to-syntax "."))
(put-text-property (match-beginning 0) (match-end 0)
'syntax-table (string-to-syntax "|")))
)))))
(provide 'fsharp-mode-font)
;;; fsharp-mode-font.el ends here