-
Notifications
You must be signed in to change notification settings - Fork 8
/
idris2-syntax.el
273 lines (233 loc) · 10.3 KB
/
idris2-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
;; idris2-syntax.el --- idris2 syntax highlighting -*- lexical-binding: t -*-
;;
;; Copyright (C) 2013 tim dixon, David Raymond Christiansen and Hannes Mehnert
;;
;; Authors: tim dixon <[email protected]>,
;; David Raymond Christiansen <[email protected]>,
;; Hannes Mehnert <[email protected]>
;;
;; 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 'idris2-core)
(require 'idris2-common-utils)
(require 'cl-lib)
(defgroup idris2-faces nil
"Fonts and colors for Idris2 code.
Because Idris2'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 'idris2 :group 'idris2)
(defface idris2-identifier-face
'((t (:inherit default)))
"The face to highlight Idris2 identifiers with."
:group 'idris2-faces)
(defface idris2-hole-face
'((t (:inherit idris2-identifier-face)))
"The face to highlight Idris2 holes with."
:group 'idris2-faces)
(defface idris2-keyword-face
'((t (:inherit font-lock-keyword-face)))
"The face to highlight Idris2 keywords with."
:group 'idris2-faces)
(defface idris2-module-face
'((t (:inherit font-lock-variable-name-face)))
"The face to highlight Idris2 module names with."
:group 'idris2-faces)
(defface idris2-directive-face
'((t (:inherit font-lock-keyword-face)))
"The face to highlight Idris2 compiler directives."
:group 'idris2-faces)
(defface idris2-directive-argument-face
'((t (:inherit font-lock-preprocessor-face)))
"The face to highlight arguments to Idris2 directives."
:group 'idris2-faces)
(defface idris2-definition-face
'((t (:inherit font-lock-function-name-face)))
"The face to highlight things being defined in."
:group 'idris2-faces)
(defface idris2-parameter-face
'((t (:inherit font-lock-constant-face)))
"The face to highlight formal parameters to function definitions with."
:group 'idris2-faces)
(defface idris2-colon-face
'((t (:inherit font-lock-variable-name-face)))
"The face to highlight ':' in type annotations with."
:group 'idris2-faces)
(defface idris2-equals-face
'((t (:inherit font-lock-variable-name-face)))
"The face to highlight '=' in definitions with."
:group 'idris2-faces)
(defface idris2-operator-face
'((t (:inherit font-lock-variable-name-face)))
"The face to highlight operators with."
:group 'idris2-faces)
(defface idris2-char-face
'((t (:inherit font-lock-string-face)))
"The face used to highlight character literals in Idris2"
:group 'idris2-faces)
(defface idris2-unsafe-face
'((t (:inherit font-lock-warning-face)))
"The face used to highlight unsafe Idris2 features, such as %assert_total"
:group 'idris2-faces)
(defvar idris2-definition-keywords
'("data" "codata" "constructor" "interface" "record" "postulate")
"Keywords that introduce some identifier.")
(defvar idris2-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 Idris2 operator.")
(defconst idris2-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)
;; Idris2 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 idris2-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 idris2-special-char-regexp
(let ((idris2-special-chars
(cl-loop for code
across "0abfnrtv\"'\\"
collecting (concat "'\\" (string code) "'")))
(idris2-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 idris2-ascii-escapes idris2-special-chars)))))
(defun idris2-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 idris2-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 idris2-font-lock-keyword-regexp
(regexp-opt (append idris2-definition-keywords
idris2-keywords)
'symbols)
"A regexp for matching Idris2 keywords")
(defun idris2-font-lock-literate-search (regexp lidr limit)
"Find REGEXP in Idris2 source between point and LIMIT, where 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
(idris2-font-lock-literate-search regexp lidr limit))
nil))
;; This should be a function so that it evaluates `idris2-lidr-p' at the correct time
(defun idris2-font-lock-defaults ()
(cl-flet ((line-start (regexp)
(if (idris2-lidr-p)
(concat "^>" regexp)
(concat "^" regexp))))
`('(
;; Imports
(,(line-start "\\(import\\)\\s-+\\(public\\)")
(1 'idris2-keyword-face)
(2 'idris2-keyword-face))
;; Documentation comments.
(,(line-start "\\s-*\\(|||\\)\\(.*\\)$")
(1 font-lock-comment-delimiter-face)
(2 'idris2-inline-doc-face))
(,(apply-partially #'idris2-font-lock-literate-search "\\s-*\\(|||\\)\\(.*\\)$" (idris2-lidr-p))
(1 font-lock-comment-delimiter-face)
(2 'idris2-inline-doc-face))
(,(line-start "\\s-*\\(|||\\)\\s-*\\(@\\)\\s-*\\(\\sw+\\)")
(1 font-lock-comment-delimiter-face t)
(2 font-lock-comment-delimiter-face t)
(3 'idris2-parameter-face t))
;; %assert_total
("%assert_total" . 'idris2-unsafe-face)
;; Expression-like directives: %runElab and %unify_log
(,(apply-partially #'idris2-font-lock-literate-search
(regexp-opt '("%runElab" "%unify_log"))
(idris2-lidr-p))
(0 'idris2-directive-face))
;; `%access`, `%default`, etc
(,(line-start "\\s-*\\(%\\w+\\)\\s-*\\(.*\\)")
(1 'idris2-directive-face)
(2 'idris2-directive-argument-face))
;; Keywords
(,(apply-partially #'idris2-font-lock-literate-search idris2-font-lock-keyword-regexp (idris2-lidr-p))
(1 'idris2-keyword-face))
;; Operators
(,(apply-partially #'idris2-font-lock-literate-search idris2-operator-regexp (idris2-lidr-p)) . 'idris2-operator-face)
;; Holes
(,(apply-partially #'idris2-font-lock-literate-search "\\?[a-zA-Z_]\\w*" (idris2-lidr-p)) . 'idris2-hole-face)
;; Identifiers
(,(apply-partially #'idris2-font-lock-literate-search "[a-zA-Z_]\\w*" (idris2-lidr-p)) . 'idris2-identifier-face)
;; Scary stuff
(,(apply-partially #'idris2-font-lock-literate-search
(regexp-opt '("believe_me" "really_believe_me" "assert_total" "assert_smaller" "prim__believe_me"))
(idris2-lidr-p))
0 'idris2-unsafe-face t)
))))
(provide 'idris2-syntax)