forked from idris-hackers/idris-mode
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidris-syntax.el
238 lines (203 loc) · 8.35 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
;;; idris-syntax.el --- idris 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 'idris-core)
(defgroup idris-faces nil "Idris highlighting" :prefix 'idris :group 'idris)
(defface idris-identifier-face
'((t (:inherit default)))
"The face to highlight Idris identifiers with."
:group 'idris-faces)
(defface idris-metavariable-face
'((t (:inherit idris-identifier-face)))
"The face to highlight Idris metavariables 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-bottom-face
'((t (:inherit idris-identifier-face)))
"The face used to highlight _|_ in Idris"
: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" "class" "codata" "record")
"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 (standard-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 ?\- "_ 123" st)
(modify-syntax-entry ?\n ">" st)
;; ' and _ can be names
(modify-syntax-entry ?' "w" st)
(modify-syntax-entry ?_ "w" st)
;; Idris operator chars
(mapc #'(lambda (ch) (modify-syntax-entry ch "_" 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" "attack" "case" "compute" "covering" "do" "dsl" "else" "exact" "focus" "if"
"implicit" "import" "in" "infix" "infixl" "infixr" "instance" "intros" "module"
"mutual" "namespace" "of" "let" "parameters" "partial" "pattern" "prefix"
"private" "public" "refine" "rewrite" "solve" "syntax" "term" "then" "total"
"trivial" "try" "using" "where" "with"))
(defconst idris-font-lock-defaults
`('(
;; {- Block comments -}
("\\({-\\)\\(.*\\)\\(-}\\)"
(1 font-lock-comment-delimiter-face)
(2 font-lock-comment-face)
(3 font-lock-comment-delimiter-face))
;; TODO: this doesn't let you do newlines
;; Documentation comments.
("^\\s-*\\(|||\\)\\(.+\\)$"
(1 font-lock-comment-delimiter-face)
(2 font-lock-doc-face))
("^\\s-*\\(|||\\)\\s-*\\(@\\)\\s-*\\(\\sw+\\)"
(1 font-lock-comment-delimiter-face t)
(2 font-lock-comment-delimiter-face t)
(3 'idris-parameter-face t))
;; Ordinary comments.
("\\(--\\)\s*\\(.*\\)"
(1 font-lock-comment-delimiter-face)
(2 font-lock-comment-face))
;; %assert_total
("%assert_total" . 'idris-unsafe-face)
;; `%access`, `%default`, etc
("^\\s-*\\(%\\w+\\)\\s-*\\(.*\\)"
(1 'idris-directive-face)
(2 'idris-directive-argument-face))
;; Definitions with keywords.
(,(format "\\(%s\\) \\(\\w+\\)" (regexp-opt idris-definition-keywords))
(1 'idris-keyword-face)
(2 'idris-definition-face))
;; Type declarations
;; TODO: this won't match, e.g. f:a
("^\\s-*\\(\\w+\\|(\\s_+)\\)\\s-+\\(:\\)\\s-+"
(1 'idris-definition-face)
(2 'idris-colon-face))
("^\\s-*\\(total\\|partial\\)\\s-+\\(\\w+\\)\\s-+\\(:\\)\\s-+"
(1 'idris-keyword-face)
(2 'idris-definition-face)
(3 'idris-colon-face))
;; "where"-blocks
("^\\s-+\\(where\\)\\s-+\\(\\w+\\)\s-*\\(.?*\\)\\(=\\)"
(1 'idris-keyword-face)
(2 'idris-definition-face)
(3 'idris-parameter-face)
(4 'idris-equals-face))
("^\\s-+\\(where\\)\\s-+\\(\\w+\\|(\\s_+)\\)\s-*\\(:\\)\\s-*"
(1 'idris-keyword-face)
(2 'idris-definition-face)
(3 'idris-colon-face))
("^\\s-+\\(where\\)\\s-+\\(total\\|partial\\)\\s-+\\(\\w+\\)\s-*\\(:\\)\\s-*"
(1 'idris-keyword-face)
(2 'idris-keyword-face)
(3 'idris-definition-face)
(4 'idris-colon-face))
;; Vanilla definitions with = (and optionally let ... in ...)
("^\\s-*\\(\\w+\\|(\\s_+)\\)\s-*\\(.*?\\)\\(=\\)"
(1 'idris-definition-face)
(2 'idris-parameter-face)
(3 'idris-equals-face))
("^\\s-*\\(\\w+\\|(\\s_+)\\)\\s-+\\(.*?\\)\\s-*\\(impossible\\)"
(1 'idris-definition-face)
(2 'idris-parameter-face)
(3 'idris-keyword-face))
;; Definitions using "with"
("^\\s-*\\(\\w+\\)\s-*\\(.?*\\)\\(with\\)\\(.?*\\)"
(1 'idris-definition-face)
(2 'idris-parameter-face)
(3 'idris-keyword-face)
(4 'idris-parameter-face))
;; Character literals
("'\\(?:[^']\\|\\\\.\\)'" . 'idris-char-face)
;; Bottom
("_|_" . 'idris-bottom-face)
;; Other keywords
(,(regexp-opt idris-keywords 'words) . 'idris-keyword-face)
;; Operators
(,idris-operator-regexp . 'idris-operator-face)
;; Metavariables
("\\?[a-zA-Z_]\\w*" . 'idris-metavariable-face)
;; Identifiers
("[a-zA-Z_]\\w*" . 'idris-identifier-face)
;; Scary stuff
(,(regexp-opt '("believe_me" "really_believe_me" "assert_total" "assert_smaller" "prim__believe_me"))
0 'idris-unsafe-face t)
;; TODO: operator definitions.
;; TODO: let ... in ...
)))
(provide 'idris-syntax)