forked from haskell/haskell-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
haskell-c2hs.el
207 lines (193 loc) · 7.65 KB
/
haskell-c2hs.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
;; haskell-c2hs.el --- -*- lexical-binding: t; -*-
;; Copyright (C) 2016 Sergey Vinokurov
;;
;; Author: Sergey Vinokurov <[email protected]>
;; Created: Monday, 7 March 2016
;; 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/>.
;;; Commentary:
;; This mode is mostly intended for highlighting {#...#} hooks.
;;
;; Quick setup:
;; (autoload 'haskell-c2hs-mode "haskell-c2hs-mode" nil t)
;; (add-to-list 'auto-mode-alist '("\\.chs\\'" . haskell-c2hs-mode))
;;
(require 'haskell-mode)
(require 'haskell-font-lock)
(require 'haskell-utils)
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.chs\\'" . haskell-c2hs-mode))
(defface haskell-c2hs-hook-pair-face
'((t (:inherit 'font-lock-preprocessor-face)))
"Face for highlighting {#...#} pairs."
:group 'haskell)
(defface haskell-c2hs-hook-name-face
'((t (:inherit 'font-lock-keyword-face)))
"Face for highlighting c2hs hook names."
:group 'haskell)
(defvar haskell-c2hs-font-lock-keywords
`((,(eval-when-compile
(let* ((ws '(any ?\s ?\t ?\n ?\r))
(anychar '(or (not (any ?#))
(seq "#"
(not (any ?\})))))
(any-nonquote '(or (not (any ?# ?\"))
(seq "#"
(not (any ?\} ?\")))))
(cid '(seq (any (?a . ?z) (?A . ?Z) ?_)
(* (any (?a . ?z) (?A . ?Z) (?0 . ?9) ?_))))
(hsid-type '(seq (? "'")
(any (?A . ?Z))
(* (any (?a . ?z) (?A . ?Z) (?0 . ?9) ?_ ?'))))
(equals-str-val `(seq (* ,ws)
"="
(* ,ws)
"\""
(* ,any-nonquote)
"\"")))
(eval
`(rx
(seq
(group-n 1 "{#")
(* ,ws)
(or (seq (group-n 2
"import"
(opt (+ ,ws)
"qualified"))
(+ ,ws))
(seq (group-n 2
"context")
(opt (+ ,ws)
(group-n 3
"lib")
,equals-str-val)
(opt (+ ,ws)
(group-n 4
"prefix")
,equals-str-val)
(opt (+ ,ws)
(group-n 5
"add"
(+ ,ws)
"prefix")
,equals-str-val))
(seq (group-n 2
"type")
(+ ,ws)
,cid)
(seq (group-n 2
"sizeof")
(+ ,ws)
,cid)
(seq (group-n 2
"enum"
(+ ,ws)
"define")
(+ ,ws)
,cid)
;; TODO: vanilla enum fontification is incomplete
(seq (group-n 2
"enum")
(+ ,ws)
,cid
(opt (+ ,ws)
(group-n 3
"as")))
;; TODO: fun hook highlighting is incompelete
(seq (group-n 2
(or "call"
"fun")
(opt (+ ,ws)
"pure")
(opt (+ ,ws)
"unsafe"))
(+ ,ws)
,cid
(opt (+ ,ws)
(group-n 3
"as")
(opt (+ ,ws)
(group-n 8
"^"))))
(group-n 2
"get")
(group-n 2
"set")
(seq (group-n 2
"pointer")
(or (seq (* ,ws)
(group-n 3 "*")
(* ,ws))
(+ ,ws))
,cid
(opt (+ ,ws)
(group-n 4 "as")
(+ ,ws)
,hsid-type)
(opt (+ ,ws)
(group-n 5
(or "foreign"
"stable")))
(opt
(or (seq (+ ,ws)
(group-n 6
"newtype"))
(seq (* ,ws)
"->"
(* ,ws)
,hsid-type)))
(opt (+ ,ws)
(group-n 7
"nocode")))
(group-n 2
"class")
(group-n 2
"alignof")
(group-n 2
"offsetof")
(seq (group-n 2
"const")
(+ ,ws)
,cid)
(seq (group-n 2
"typedef")
(+ ,ws)
,cid
(+ ,ws)
,hsid-type)
(group-n 2
"nonGNU")
;; TODO: default hook not implemented
)
(* ,anychar)
(group-n 9 "#}"))))))
;; Override highlighting for pairs in order to always distinguish them.
(1 'haskell-c2hs-hook-pair-face t)
(2 'haskell-c2hs-hook-name-face)
;; Make matches lax, i.e. do not signal error if nothing
;; matched.
(3 'haskell-c2hs-hook-name-face nil t)
(4 'haskell-c2hs-hook-name-face nil t)
(5 'haskell-c2hs-hook-name-face nil t)
(6 'haskell-c2hs-hook-name-face nil t)
(7 'haskell-c2hs-hook-name-face nil t)
(8 'font-lock-negation-char-face nil t)
;; Override highlighting for pairs in order to always distinguish them.
(9 'haskell-c2hs-hook-pair-face t))
,@(haskell-font-lock-keywords)))
;;;###autoload
(define-derived-mode haskell-c2hs-mode haskell-mode "C2HS"
"Mode for editing *.chs files of the c2hs haskell tool."
(setq-local font-lock-defaults
(cons 'haskell-c2hs-font-lock-keywords
(cdr font-lock-defaults))))
(provide 'haskell-c2hs)
;; haskell-c2hs.el ends here