summaryrefslogtreecommitdiff
path: root/Util/Emacs/boogiepl.el
blob: 9029a179512d44e778ff2f3e4b901c99556dad95 (plain)
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
;;; boogiepl.el --- BoogiePL major mode

;; Copyright (C) 2009, 2010 Valentin Wüstholz

;; Author: Valentin Wüstholz
;; Keywords: Boogie, BoogiePL, major mode
;; Version: 0.1

;; This file 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 file 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 GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.


;;; Commentary:

;; This is a Emacs mode for BoogiePL. It supports syntax-highlighting,
;; indention and imenu.

;; To install it, you simply have to add the following lines to your .emacs file:

;; (add-to-list 'load-path (expand-file-name "path/to/this/file/"))
;; (require 'boogiepl)
;; (add-to-list 'auto-mode-alist '("\\.bpl$" . boogiepl-mode))


;;; Code:


(require 'imenu)
(require 'easymenu)
(require 'speedbar)


(defvar boogiepl-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map [remap comment-dwim] 'boogiepl-comment-dwim)
    (define-key map [return] 'newline-and-indent)
    (define-key map "\C-c\C-c" 'boogiepl-run-boogie)
    map)
  "Keymap for `boogiepl-mode'.")


(defvar boogiepl-mode-syntax-table
  (let ((st (make-syntax-table)))
    (modify-syntax-entry ?\/  ". 124b" st)
    (modify-syntax-entry ?\*  ". 23n"  st)
    (modify-syntax-entry ?\n  "> b"    st)
    (modify-syntax-entry ?\r  "> b"    st)
    st)
  "Syntax table for `boogiepl-mode'.")


(defun boogiepl-comment-dwim (arg)
  "Comment or uncomment current line or region in a smart way.
For detail, see `comment-dwim'."
  (interactive "*P")
  (require 'newcomment)
  (let ((deactivate-mark nil) (comment-start "// ") (comment-end ""))
    (comment-dwim arg)))


(defvar boogiepl-imenu-generic-expression
  '(
    ("Functions"
     "^[:space:]*function *\\([^(\n\s-]+?\\) *(" 1)
    ("Procedures"
     "^[:space:]*procedure *\\([^(\n\s-]+\\) *(" 1)
    ("Implementations"
     "^[:space:]*implementation *\\([^(\n\s-]+\\) *(" 1)
    ("Types"
     "^[:space:]*type *\\([^;\n\s-]+[^\n]*\\);" 1)
    ("Constants"
     "^[:space:]*const *\\(unique\\)? *\\([^;\n\s-]+\\) *:" 2)
    ("Variables"
     "^[:space:]*var *\\([^;\n\s-]+\\):" 1)
    ("Axioms"
     "^[:space:]*axiom *\\([^;]+\\);" 1)
    )
  )


;;; Speedbar

(if (fboundp 'speedbar-add-supported-extension)
    (speedbar-add-supported-extension '(".bpl")))


;;; Menu

(defun boogiepl-menu ()
  (easy-menu-define
    boogiepl-mode-menu
    (list boogiepl-mode-map)
    "BoogiePL Mode Menu"
    '("BoogiePL"
      ["Run Boogie" boogiepl-run-boogie t]
      ["Run Boogie with /smoke" boogiepl-run-boogie-with-smoke t]
      "-"
      ["Indent the marked region or the whole buffer" boogiepl-indent-marked-region-or-buffer t]
      )
    )
  (easy-menu-add boogiepl-mode-menu)
  )


;;; Commands

(defcustom boogiepl-command
  "boogie /nologo"
  "Command-line command to run Boogie"
  :type 'string
  :group 'boogie
  )

(defun boogiepl-run-boogie-with-smoke ()
  "Run Boogie with /smoke."
  (interactive)
  (boogiepl-run-boogie t))

(defun boogiepl-run-boogie (&optional smokep)
  "Run Boogie."
  (interactive)
  (let* ((filename (file-name-nondirectory buffer-file-name))
         (command (format "%s %s %s"
                          boogiepl-command
                          (if smokep "/smoke" "")
                          filename)))
    (compile command)))


;; Define several class of keywords.
(defvar boogiepl-keywords
  '("type" "const" "function" "returns" "axiom" "var" "procedure" "requires" "modifies" "ensures" "implementation" "goto" "return" "assert" "assume" "havoc" "call" "call forall" "finite" "unique" "free" "invariant" "if" "else" "while" "complete" "break" "where" "extends")
  "BoogiePL keywords")

(defvar boogiepl-types
  '("bool" "int" "ref")
  "BoogiePL types")

(defvar boogiepl-constants
  '("true" "false" "null")
  "BoogiePL constants")

(defvar boogiepl-operators
  '("<==>" "==>" "&&" "||" "==" "!=" "<=" ">=" "<" ">" "!" "forall" "exists" "::" "<:")
  "BoogiePL operators")

(defvar boogiepl-functions
  '("old")
  "BoogiePL functions")

;; Create the regex string for each class of keywords.
(defvar boogiepl-keywords-regexp (regexp-opt boogiepl-keywords 'words))
;; (defvar boogiepl-type-regexp (regexp-opt boogiepl-types 'words))
(defvar boogiepl-type-regexp "\\<\\(b\\(?:ool\\|v[0-9]+\\)\\|int\\|ref\\)\\>")
(defvar boogiepl-constant-regexp (regexp-opt boogiepl-constants 'words))
(defvar boogiepl-operators-regexp (regexp-opt boogiepl-operators))
(defvar boogiepl-functions-regexp (regexp-opt boogiepl-functions 'words))

;; Clear memory.
(setq boogiepl-keywords nil)
(setq boogiepl-types nil)
(setq boogiepl-constants nil)
(setq boogiepl-operators nil)
(setq boogiepl-functions nil)

;; Define a face for each keyword class.
(setq boogiepl-font-lock-keywords
      `(
        (,boogiepl-type-regexp . font-lock-type-face)
        (,boogiepl-constant-regexp . font-lock-constant-face)
        (,boogiepl-operators-regexp . font-lock-builtin-face)
        (,boogiepl-functions-regexp . font-lock-function-name-face)
        (,boogiepl-keywords-regexp . font-lock-keyword-face)
        ))


(defcustom boogiepl-mode-hook nil
  "Mode hook for boogiepl-mode, run after the mode was turned on."
  :type 'hook
  :group 'boogie
  )


;;;###autoload
(define-derived-mode boogiepl-mode fundamental-mode "BoogiePL"
  "A major mode for editing BoogiePL files."
  :group 'boogie
  (set (make-local-variable 'paragraph-separate) (concat "^\\s *$\\|" page-delimiter))
  (set (make-local-variable 'paragraph-start) (concat "^\\s *$\\|" page-delimiter))
  (set (make-local-variable 'paragraph-ignore-fill-prefix) t)
  (set (make-local-variable 'comment-start) "// ")
  (set (make-local-variable 'comment-start-skip) "\\(//+\\|/\\*+\\)\\s *")
  (set (make-local-variable 'comment-end) "")
  (set (make-local-variable 'comment-end-skip) " *[*]+/")
  (set (make-local-variable 'font-lock-multiline) nil)
  (setq font-lock-defaults '((boogiepl-font-lock-keywords)))
  (set (make-local-variable 'font-lock-defaults)
       '(boogiepl-font-lock-keywords))
  (set (make-local-variable 'indent-line-function) 'boogiepl-indent-line)
  (set (make-local-variable 'imenu-generic-expression)
       boogiepl-imenu-generic-expression)
  (set (make-local-variable 'compile-command) (format "%s %s"
                                                      boogiepl-command
                                                      (file-name-nondirectory buffer-file-name)))
  ;; Clear memory.
  (setq boogiepl-keywords-regexp nil)
  (setq boogiepl-types-regexp nil)
  (setq boogiepl-constants-regexp nil)
  (setq boogiepl-operators-regexp nil)
  (setq boogiepl-functions-regexp nil)
  (turn-on-font-lock)
  (jit-lock-fontify-now)
  (boogiepl-menu)
  (imenu-add-menubar-index)
  (imenu-update-menubar)
  (run-mode-hooks 'boogiepl-mode-hook)
  )


;;; Indentation

(defun boogiepl-indent-line ()
  "Indent current line of BoogiePL code."
  (interactive)
  (let* ((savep (point))
         (indent (condition-case nil
                     (save-excursion
                       (forward-line 0)
                       (skip-chars-forward " \t")
                       (if (>= (point) savep) (setq savep nil))
                       (max (boogiepl-calculate-indentation) 0))
                   (error 0))))
    (if savep
        (save-excursion (indent-line-to indent))
      (indent-line-to indent))))

(defun looking-at-comment ()
  (and font-lock-mode
       (or (looking-at "/[*]+ *\\|//+ *")
           (eq (get-text-property (point) 'face) 'font-lock-comment-face)))
  )

(defun boogiepl-calculate-indentation ()
  "Return the column to which the current line should be indented."
  (cond ((looking-at-comment)  ; comments
         (current-indentation))
        ((looking-at "[^\n\s-]+\\s-*:\\s-*$")  ; label
         2)
        ((and font-lock-mode (eq (get-text-property (point) 'face) 'font-lock-builtin-face))  ; operators
         (current-indentation))
        ((looking-at "\\(free +\\)?\\(requires\\|modifies\\|ensures\\)")  ; specification
         2)
        (t
         (let ((cur-indent 0))
           (if (looking-at "}.*")
               (incf cur-indent -4))
           (while (and (not (bobp)) (not (looking-at "\\s-*\\(implementation\\|function\\|procedure\\|const\\|axiom\\|type\\)")))
             (forward-line -1)
             (unless (looking-at-comment)
               (if (looking-at ".*{.*")
                   (incf cur-indent 4))
               (if (looking-at ".*}.*")
                   (incf cur-indent -4))
               )
             )
           cur-indent)
         )
        )
  )

(defun boogiepl-indent-marked-region-or-buffer ()
  "Indent the marked region or the whole buffer."
  (interactive)
  (save-excursion
    (if (not mark-active)
        (mark-whole-buffer))
    (call-interactively 'untabify)
    (call-interactively 'indent-region))
  )


(provide 'boogiepl)

;;; boogiepl.el ends here