-
Notifications
You must be signed in to change notification settings - Fork 72
/
idris-syntax.el
274 lines (234 loc) · 10.1 KB
/
idris-syntax.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
;; idris-syntax.el --- idris syntax highlighting -*- lexical-binding: t -*-
;;
;; Copyright (C) 2013 tim dixon, David Raymond Christiansen and Hannes Mehnert
;;
;; Authors: tim dixon <tdixon51793@gmail.com>,
;; David Raymond Christiansen <drc@itu.dk>,
;; Hannes Mehnert <hame@itu.dk>
;;
;; This program 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 of the License, or
;; (at your option) any later version.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
(require 'idris-core)
(require 'idris-common-utils)
(require 'cl-lib)
(defgroup idris-faces nil
"Fonts and colors for Idris code.
Because Idris's highlighting is semantic rather than syntactic,
there aren't really very good defaults to appeal to from
font-lock. You may need to change these settings to work well
with your favorite theme. If you do so, please consider
contributing the settings upstream to the theme maintainer."
:prefix 'idris :group 'idris)
(defface idris-identifier-face
'((t (:inherit default)))
"The face to highlight Idris identifiers with."
:group 'idris-faces)
(defface idris-hole-face
'((t (:inherit idris-identifier-face)))
"The face to highlight Idris holes with."
:group 'idris-faces)
(defface idris-keyword-face
'((t (:inherit font-lock-keyword-face)))
"The face to highlight Idris keywords with."
:group 'idris-faces)
(defface idris-module-face
'((t (:inherit font-lock-variable-name-face)))
"The face to highlight Idris module names with."
:group 'idris-faces)
(defface idris-directive-face
'((t (:inherit font-lock-keyword-face)))
"The face to highlight Idris compiler directives."
:group 'idris-faces)
(defface idris-directive-argument-face
'((t (:inherit font-lock-preprocessor-face)))
"The face to highlight arguments to Idris directives."
:group 'idris-faces)
(defface idris-definition-face
'((t (:inherit font-lock-function-name-face)))
"The face to highlight things being defined in."
:group 'idris-faces)
(defface idris-parameter-face
'((t (:inherit font-lock-constant-face)))
"The face to highlight formal parameters to function definitions with."
:group 'idris-faces)
(defface idris-colon-face
'((t (:inherit font-lock-variable-name-face)))
"The face to highlight ':' in type annotations with."
:group 'idris-faces)
(defface idris-equals-face
'((t (:inherit font-lock-variable-name-face)))
"The face to highlight '=' in definitions with."
:group 'idris-faces)
(defface idris-operator-face
'((t (:inherit font-lock-variable-name-face)))
"The face to highlight operators with."
:group 'idris-faces)
(defface idris-char-face
'((t (:inherit font-lock-string-face)))
"The face used to highlight character literals in Idris"
:group 'idris-faces)
(defface idris-unsafe-face
'((t (:inherit font-lock-warning-face)))
"The face used to highlight unsafe Idris features, such as %assert_total."
:group 'idris-faces)
(defvar idris-definition-keywords
'("data" "codata" "constructor" "interface" "record" "postulate")
"Keywords that introduce some identifier.")
(defvar idris-operator-regexp
(let ((op "-!#$%&*+./<=>@\\\\^|~:"))
(concat "\\?[" op "]+" ; starts with ?, has at least one more opchar
"\\|" "["op"][" op "?]*")) ; doesn't start with ?
"A regular expression matching an Idris operator.")
(defconst idris-syntax-table
(let ((st (make-syntax-table)))
;; Matching parens
(modify-syntax-entry ?\( "()" st)
(modify-syntax-entry ?\) ")(" st)
(modify-syntax-entry ?\[ "(]" st)
(modify-syntax-entry ?\] ")[" st)
;; Matching {}, but with nested comments
(modify-syntax-entry ?\{ "(} 1bn" st)
(modify-syntax-entry ?\} "){ 4bn" st)
(modify-syntax-entry ?\n ">" st)
;; ' and _ can be part of names, so give them symbol constituent syntax
(modify-syntax-entry ?' "_" st)
(modify-syntax-entry ?_ "_" st)
;; Idris operator chars get punctuation syntax
(mapc #'(lambda (ch) (modify-syntax-entry ch "." st))
"!#$%&*+./<=>@^|~:")
;; - is an operator char but may also be 1st or 2nd char of comment starter
;; -- and the 1st char of comment end -}
(modify-syntax-entry ?\- ". 123" st)
;; Whitespace is whitespace
(modify-syntax-entry ?\ " " st)
(modify-syntax-entry ?\t " " st)
;; ;; Strings
(modify-syntax-entry ?\" "\"" st)
(modify-syntax-entry ?\\ "/" st)
st))
(defconst idris-keywords
'("abstract" "case" "covering" "default" "do" "dsl" "else" "export" "if"
"implementation" "implicit" "import" "in" "infix" "infixl" "infixr"
"module" "mutual" "namespace" "of" "let" "parameters" "partial"
"pattern" "prefix" "private" "proof" "public" "rewrite" "syntax"
"tactics" "then" "total" "using" "where" "with"))
(defconst idris-special-char-regexp
(let ((idris-special-chars
(cl-loop for code
across "0abfnrtv\"'\\"
collecting (concat "'\\" (string code) "'")))
(idris-ascii-escapes
(cl-loop for esc
in '("NUL" "SOH" "STX" "ETX" "EOT" "ENQ"
"ACK" "BEL" "BS" "HT" "LF" "VT" "FF"
"CR" "SO" "SI" "DLE" "DC1" "DC2"
"DC3" "DC4" "NAK" "SYN" "ETB" "CAN"
"EM" "SUB" "ESC" "FS" "GS" "RS" "US"
"SP" "DEL")
collecting (concat "'\\" esc "'"))))
(concat "\\(?:'\"'\\)"
"\\|"
"\\(?:'\\\\[0-9]+'\\)"
"\\|"
"\\(?:'\\\\o[0-7]+'\\)"
"\\|"
"\\(?:'\\\\x[0-9a-fA-F]+'\\)"
"\\|"
"\\(?:'[^'\\]'\\)"
"\\|"
(regexp-opt (append idris-ascii-escapes idris-special-chars)))))
(defun idris-syntax-propertize-function (begin end)
"Add syntax properties to a region of the buffer that the
syntax table won't support, such as characters."
(save-excursion
(goto-char begin)
(while (re-search-forward idris-special-char-regexp end t)
(let ((open (match-beginning 0))
(close (match-end 0)))
(add-text-properties open (1+ open) '(syntax-table (7 . ?\')))
(add-text-properties (1- close) close '(syntax-table (7 . ?\')))))
;; Backslash \ is not escaping in \(x, y) -> x + y.
(goto-char begin)
(while (re-search-forward "\\\\(" end t)
(let ((open (match-beginning 0)))
(add-text-properties open (1+ open) '(syntax-table (1 . nil)))))))
(defconst idris-font-lock-keyword-regexp
(regexp-opt (append idris-definition-keywords
idris-keywords)
'words)
"A regexp for matching Idris keywords.")
(defun idris-font-lock-literate-search (regexp lidr limit)
"Find REGEXP in Idris source between point and LIMIT.
LIDR is non-nil for literate files.
See the documentation for search-based fontification,
esp. `font-lock-defaults', for details."
(if (re-search-forward regexp limit t)
(if (or (and (not lidr) ;; normal syntax - ensure not in docs
(save-excursion
(move-beginning-of-line nil)
(not (looking-at-p "^\\s-*|||"))))
(and lidr
(save-excursion ;; LIDR syntax - ensure in code, not in docs
(move-beginning-of-line nil)
(and (looking-at-p "^> ")
(not (looking-at-p "^>\\s-*|||"))))))
t
(idris-font-lock-literate-search regexp lidr limit))
nil))
;; This should be a function so that it evaluates `idris-lidr-p' at the correct time
(defun idris-font-lock-defaults ()
(cl-flet ((line-start (regexp)
(if (idris-lidr-p)
(concat "^>" regexp)
(concat "^" regexp))))
`('(
;; Imports
(,(line-start "\\(import\\)\\s-+\\(public\\)")
(1 'idris-keyword-face)
(2 'idris-keyword-face))
;; Documentation comments.
(,(line-start "\\s-*\\(|||\\)\\(.*\\)$")
(1 font-lock-comment-delimiter-face)
(2 'idris-inline-doc-face))
(,(apply-partially #'idris-font-lock-literate-search "\\s-*\\(|||\\)\\(.*\\)$" (idris-lidr-p))
(1 font-lock-comment-delimiter-face)
(2 'idris-inline-doc-face))
(,(line-start "\\s-*\\(|||\\)\\s-*\\(@\\)\\s-*\\(\\sw+\\)")
(1 font-lock-comment-delimiter-face t)
(2 font-lock-comment-delimiter-face t)
(3 'idris-parameter-face t))
;; %assert_total
("%assert_total" . 'idris-unsafe-face)
;; Expression-like directives: %runElab and %unify_log
(,(apply-partially #'idris-font-lock-literate-search
(regexp-opt '("%runElab" "%unify_log"))
(idris-lidr-p))
(0 'idris-directive-face))
;; `%access`, `%default`, etc
(,(line-start "\\s-*\\(%\\w+\\)\\s-*\\(.*\\)")
(1 'idris-directive-face)
(2 'idris-directive-argument-face))
;; Keywords
(,(apply-partially #'idris-font-lock-literate-search idris-font-lock-keyword-regexp (idris-lidr-p))
(1 'idris-keyword-face))
;; Operators
(,(apply-partially #'idris-font-lock-literate-search idris-operator-regexp (idris-lidr-p)) . 'idris-operator-face)
;; Holes
(,(apply-partially #'idris-font-lock-literate-search "\\?[a-zA-Z_]\\w*" (idris-lidr-p)) . 'idris-hole-face)
;; Identifiers
(,(apply-partially #'idris-font-lock-literate-search "[a-zA-Z_]\\w*" (idris-lidr-p)) . 'idris-identifier-face)
;; Scary stuff
(,(apply-partially #'idris-font-lock-literate-search
(regexp-opt '("believe_me" "really_believe_me" "assert_total" "assert_smaller" "prim__believe_me"))
(idris-lidr-p))
0 'idris-unsafe-face t)
))))
(provide 'idris-syntax)