-
Notifications
You must be signed in to change notification settings - Fork 63
/
saw-script.el
333 lines (278 loc) · 11.5 KB
/
saw-script.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
;;; saw-script.el --- A major mode for editing SAW script files -*- lexical-binding: t; -*-
;; Copyright (C) 2018 Galois, Inc
;; Author: David Thrane Christiansen <dtc@dtc.galois.com>
;; Keywords: languages
;; Package-Requires: ((emacs "24.4") (prop-menu "0.1") (cl-lib "0.5"))
;; Package-Version: 0.5
;; Homepage: https://github.com/GaloisInc/saw-script
;; 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 is a major mode for editing SAWScript files.
;;; Code:
(require 'compile)
(require 'cl-lib)
(require 'prop-menu)
;;; Configuration
(defgroup saw-script '()
"SAWScript"
:group 'languages
:tag "SAWScript")
(defcustom saw-script-command "saw"
"The command to run to execute saw."
:type 'string
:group 'saw-script)
(defface saw-script-keyword-face
'((t (:inherit font-lock-keyword-face)))
"How to highlight SAWScript keywords."
:group 'saw-script)
(defface saw-script-operator-face
'((t (:inherit font-lock-keyword-face)))
"How to highlight SAWScript build-in operators."
:group 'saw-script)
(defface saw-script-embedded-cryptol-brace-face
'((t (:inherit font-lock-preprocessor-face)))
"How to highlight the braces around embedded Cryptol in SAWScript."
:group 'saw-script)
(defface saw-script-embedded-cryptol-face
'((t (:inherit font-lock-builtin-face)))
"How to highlight embedded Cryptol in SAWScript."
:group 'saw-script)
(defface saw-script-embedded-cryptol-type-brace-face
'((t (:inherit font-lock-reference-face)))
"How to highlight the braces around embedded Cryptol types in SAWScript."
:group 'saw-script)
(defface saw-script-embedded-cryptol-type-face
'((t (:inherit font-lock-builtin-face)))
"How to highlight embedded Cryptol types in SAWScript."
:group 'saw-script)
;;; Syntax table
(defvar saw-script-mode-syntax-table
(let ((syntax-table (make-syntax-table)))
;; Set up // line comments and /* */ block comments
(modify-syntax-entry ?/ ". 124" syntax-table)
(modify-syntax-entry ?* ". 23bn" syntax-table)
(modify-syntax-entry ?\n ">" syntax-table)
syntax-table)
"Syntax table for `saw-script-mode'.")
;;; Highlighting
(defconst saw-script-keywords
'("import"
"include"
"and"
"as"
"hiding"
"let"
"rec"
"in"
"do"
"if"
"then"
"else"))
(defvar saw-script--keyword-regexp
(regexp-opt saw-script-keywords 'words)
"Regular expression for SAWScript keyword highlighting.")
(defconst saw-script-operators
'("<-" "=" "==")
"Operators to highlight in SAWScript.")
(defvar saw-script--operator-regexp
(regexp-opt saw-script-operators)
"Regular expression for SAWScript keyword highlighting.")
(defun saw-script--add-multiline (end face)
"Find the end of embedded Cryptol or type terminating with END, and add a text property for font-lock FACE."
(save-match-data
(save-excursion
(let ((start (point)))
(when (re-search-forward end nil t)
(with-silent-modifications
(put-text-property start (- (point) 2)
'face face)
(put-text-property start (- (point) 2)
'fontified t)
(put-text-property start (point)
'font-lock-multiline t)
(put-text-property start (point)
'cryptol t)))
(point)))))
(defun saw-script--extend-after-change-region-function (beg end _old-len)
"Find embedded Cryptol for refontification in the region described by BEG, END, and OLD-LEN."
(save-excursion
(save-restriction
(save-match-data
(let ((beginning-is-cryptol (get-text-property beg 'cryptol))
(end-is-cryptol (get-text-property end 'cryptol)))
(if (or beginning-is-cryptol end-is-cryptol)
(cons
(progn (goto-char beg)
(while (and (get-text-property (point) 'cryptol) (> (point) (point-min)))
(forward-char -1))
(point))
(progn (goto-char end)
(while (and (get-text-property (point) 'cryptol) (> (point) (point-max)))
(forward-char 1))
(point)))
nil))))))
(defvar saw-script-font-lock-defaults
`((
(,saw-script--keyword-regexp . 'saw-script-keyword-face)
("{{" (0 'saw-script-embedded-cryptol-brace-face)
("\\(.*\\)\\(}}\\)" (saw-script--add-multiline "}}" 'saw-script-embedded-cryptol-face) nil
(1 'saw-script-embedded-cryptol-face)
(2 'saw-script-embedded-cryptol-brace-face)))
("{|" (0 'saw-script-embedded-cryptol-type-brace-face)
("\\(.*\\)\\(|}\\)" (saw-script--add-multiline "|}" 'saw-script-embedded-cryptol-type-face) nil
(1 'saw-script-embedded-cryptol-type-face)
(2 'saw-script-embedded-cryptol-type-brace-face)))
(,saw-script--operator-regexp . 'saw-script-operator-face)
(";" . 'saw-script-operator-face)
("," . 'saw-script-operator-face)
("\\[" . 'saw-script-operator-face)
("\\]" . 'saw-script-operator-face)
)
nil nil nil
(font-lock-extend-after-change-region-function . saw-script--extend-after-change-region-function))
"Highlighting instructions for SAWScript.")
;;; Running SAWScript
(defun saw-script--compilation-buffer-name-function (_mode)
"Compute a name for the SAW compilation buffer."
"*SAW*")
(defun saw-script-run-file (filename)
"Run FILENAME in saw."
(interactive "fFile to run in saw: ")
(let* ((dir (file-name-directory filename))
(file (file-name-nondirectory filename))
(command (concat saw-script-command " --output-locations " file))
;; Special variables that configure compilation mode
(compilation-buffer-name-function
'saw-script--compilation-buffer-name-function)
(default-directory dir) )
(let ((compilation-finish-functions (list (lambda (_x) (message "Done!")))))
(compile command))))
(defun saw-script-run-buffer (buffer)
"Run the file from BUFFER in saw."
(interactive "bBuffer: ")
(let ((file (buffer-file-name buffer)))
(if file
(progn (when (buffer-modified-p buffer)
(when (yes-or-no-p "Buffer modified. Save first? ")
(save-buffer buffer)))
(saw-script-run-file file))
(error "Buffer %s has no file" buffer))))
(defun saw-script-run-current-buffer ()
"Run the current buffer in saw."
(interactive)
(saw-script-run-buffer (current-buffer)))
;;; Default keybindings
(defvar saw-script-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") 'saw-script-run-current-buffer)
(define-key map (kbd "C-c C-l") 'saw-script-run-current-buffer)
(define-key map (kbd "<mouse-3>") 'prop-menu-show-menu)
(define-key map (kbd "C-c C-SPC") 'prop-menu-by-completing-read)
map)
"Keymap for SAWScript mode.")
;;; Output tracking
(defvar saw-script-output '() "The current output from SAWScript.")
(make-variable-buffer-local 'saw-script-output)
(defun saw-script-output-overlay-p (overlay)
"Determine whether OVERLAY is describing SAWScript output."
(and (overlayp overlay)
(overlay-get overlay 'saw-script-output)))
(defun saw-script-clear-output ()
"Clear the SAWScript output for the current buffer."
(interactive)
(save-excursion
(save-restriction
(widen)
(dolist (o (overlays-in (point-min) (point-max)))
(when (saw-script-output-overlay-p o)
(delete-overlay o))))))
(defun saw-script-record-output (start-line start-column end-line end-column output)
"Save SAWScript OUTPUT in the region delimited by START-LINE, START-COLUMN, END-LINE, END-COLUMN."
(save-excursion
(save-restriction
(widen)
(let ((start (progn (goto-char (point-min))
(forward-line (1- start-line))
(forward-char (1- start-column))
(point)))
(end (progn (goto-char (point-min))
(forward-line (1- end-line))
(forward-char (1- end-column))
(point))))
(let ((overlay (make-overlay start end)))
(overlay-put overlay 'saw-script-output output)
(overlay-put overlay 'face 'underline))))))
(defun saw-script--context-menu-items (plist)
"Compute context menu items for PLIST."
(let ((output (plist-get plist 'saw-script-output)))
(if output
(list (list "Show Output"
(let ((loc (point)))
(lambda ()
(interactive)
(saw-script-view-output loc)))))
(list))))
(defun saw-script-output-buffer-name (file)
"Find the name for output from FILE."
(format "*SAW Output[%s]*" file))
(defun saw-script--view-overlay-output (overlay)
"View the SAWScript output in OVERLAY."
(let ((output (overlay-get overlay 'saw-script-output))
(buffer (get-buffer-create (saw-script-output-buffer-name (buffer-file-name)))))
(with-current-buffer buffer
(read-only-mode 1)
(let ((inhibit-read-only t))
(widen)
(erase-buffer)
(insert output)
(goto-char (point-min)))
(pop-to-buffer buffer))))
(defun saw-script-view-output (pos)
"View the SAWScript output at POS, if any."
(interactive "d")
(let ((tag (cl-gensym)))
(let ((o (catch tag
(progn (dolist (o (overlays-at pos))
(when (saw-script-output-overlay-p o)
(throw tag o)))
(error "No output at position %s" pos)))))
(saw-script--view-overlay-output o))))
;; Compilation mode highlighting
(add-to-list 'compilation-error-regexp-alist-alist
'(saw-script "$\\([^:]+\\):\\([0-9]+\\):\\(0-9\\)-\\([0-9]+\\):\\(0-9\\): "
1 (2 . 4) (3 . 5) nil 0))
(add-to-list 'compilation-error-regexp-alist-alist
'(cryptol-warning
"\\[warning\\] at \\([^:]+\\):\\([0-9]+\\):\\([0-9]+\\)--\\([0-9]+\\):\\([0-9+]\\)"
1 (2 . 4) (3 . 5) 1))
(add-to-list 'compilation-error-regexp-alist-alist
'(cryptol-error
"\\[error\\] at \\([^:]+\\):\\([0-9]+\\):\\([0-9]+\\)--\\([0-9]+\\):\\([0-9+]\\)"
1 (2 . 4) (3 . 5) 1))
(add-to-list 'compilation-error-regexp-alist 'saw-script)
(add-to-list 'compilation-error-regexp-alist 'cryptol-warning)
(add-to-list 'compilation-error-regexp-alist 'cryptol-error)
;;; The mode itself
;;;###autoload
(define-derived-mode saw-script-mode prog-mode "SAWScript"
"A major mode for editing SAWScript files."
(setq font-lock-defaults saw-script-font-lock-defaults)
(setq font-lock-multiline t)
;; Comment syntax
(setq-local comment-start "// ")
(setq-local comment-end "")
;; Right click
(set (make-local-variable 'prop-menu-item-functions) '(saw-script--context-menu-items)))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.saw\\'" . saw-script-mode))
(provide 'saw-script)
;;; saw-script.el ends here