Debian Codegraph @

share/emacs/acsl.el

raw
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ; ;
3 ; This file is part of Frama-C. ;
4 ; ;
5 ; Copyright (C) 2008-2011 ;
6 ; Pierre Roux ;
7 ; ;
8 ; Copyright (C) 2009-2022 ;
9 ; CEA LIST ;
10 ; ;
11 ; you can redistribute it and/or modify it under the terms of the GNU ;
12 ; Lesser General Public License as published by the Free Software ;
13 ; Foundation, version 2.1. ;
14 ; ;
15 ; It is distributed in the hope that it will be useful, ;
16 ; but WITHOUT ANY WARRANTY; without even the implied warranty of ;
17 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;
18 ; GNU Lesser General Public License for more details. ;
19 ; ;
20 ; See the GNU Lesser General Public License version 2.1 ;
21 ; for more details (enclosed in the file licenses/LGPLv2.1). ;
22 ; ;
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24
25 ;; How to install:
26 ;; copy the following in your .emacs file
27 ;;
28 ;; (setq load-path (cons "/directory/in/which/you/put/the/file/acsl.el" load-path))
29 ;; (autoload 'acsl-mode "acsl" "Major mode for editing ACSL code" t)
30 ;; ;; uncomment this if you want to automatically load ACSL mode with
31 ;; ;; each C file
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33
34 (setq auto-mode-alist
35 (append
36 '(("\\.[chi]" . acsl-mode))
37 auto-mode-alist))
38
39 ;; you can then load the mode in emacs with M-x acsl-mode
40
41 ;; TODO:
42 ;; - font-lock for ghost code
43
44 ;; code
45
46 (defun acsl-keymap-init ()
47 "Init keymap"
48 (define-key acsl-mode-map "\C-c\C-j" 'acsl-jessie-gui))
49
50 ;; font-lock
51
52 (defconst acsl-keywords
53 '("assert"
54 "assigns"
55 "assumes"
56 "allocates"
57 "axiom"
58 "axiomatic"
59 "behavior"
60 "behaviors"
61 "breaks"
62 "case"
63 "char"
64 "complete"
65 "continues"
66 "decreases"
67 "disjoint"
68 "double"
69 "else"
70 "ensures"
71 "enum"
72 "exits"
73 "float"
74 "for"
75 "frees"
76 "if"
77 "inductive"
78 "int"
79 "integer"
80 "invariant"
81 "global"
82 "label"
83 "lemma"
84 "logic"
85 "long"
86 "loop"
87 "pragma"
88 "predicate"
89 "reads"
90 "real"
91 "requires"
92 "returns"
93 "short"
94 "signed"
95 "sizeof"
96 "slice"
97 "impact"
98 "struct"
99 "terminates"
100 "type"
101 "union"
102 "unsigned"
103 "variant"
104 "void"
105 )
106 "List of ACSL keywords to highlight.")
107
108 (defun acsl-in-acsl-annot ()
109 "If we are in a C comment beginning with @."
110 (and (nth 4 (syntax-ppss))
111 (eq (char-after (+ (nth 8 (syntax-ppss)) 2)) ?@)))
112
113 (defvar acsl-font-lock-keywords
114 (let ((pre-match-form (lambda ()
115 (goto-char (match-beginning 0))
116 (match-end 0)))
117 (find-annot (lambda (limit)
118 ;; skip comments
119 (if (and (looking-at "//") (acsl-in-acsl-annot))
120 (re-search-forward "\n" limit 'e))
121 (while (and (not (acsl-in-acsl-annot)) (< (point) limit))
122 (re-search-forward "/[*/]" limit 'e))
123 (if (>= (point) limit)
124 nil
125 (let ((b (save-excursion
126 (re-search-backward "/[*/]" (- (point) 2) t)
127 (point))))
128 (re-search-forward "[*/]/\\|\n" limit 'e)
129 (re-search-backward "//" (- (point) 2) t) ; don't recolor comments
130 (set-match-data (list b (point) (nth 2 (match-data t))))
131 t)))))
132 (list
133 `(,find-annot
134 (0 font-lock-type-face t)
135 (,(concat (regexp-opt acsl-keywords 'words) "\\|<?=?=>?\\|&&\\|||\\|!=?\\|\\^\\^") (,pre-match-form) nil
136 (0 font-lock-keyword-face t))
137 ("\\(\\?\\)[^:]*\\(:\\)" (,pre-match-form) nil
138 (1 font-lock-keyword-face t)
139 (2 font-lock-keyword-face t))
140 ("\\(axiom\\|behavior\\|case\\|inductive\\|predicate\\|l\\(ogic\\|emma\\)\\)\\>[ \t\n@]*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" (,pre-match-form) nil
141 (3 font-lock-function-name-face t))
142 ("\\\\\\(at\\|e\\(mpty\\|xists\\)\\|f\\(alse\\|orall\\)\\|old\\|result\\|true\\|valid\\(_range\\|_index\\)?\\)" (,pre-match-form) nil
143 (0 font-lock-constant-face t)))))
144 "Default highlighting for ACSL mode")
145
146 (defun acsl-font-lock-init ()
147 "Initialize font-lock for ACSL."
148 (add-hook 'c-mode-hook
149 (lambda ()
150 (font-lock-add-keywords nil acsl-font-lock-keywords))))
151
152 ;; custom variables
153
154 (require 'custom)
155
156 (defcustom acsl-jessie-gui-prog-name "frama-c -jessie"
157 "Frama-C/Jessie executable name."
158 :group 'acsl
159 :type 'string)
160
161 (defcustom acsl-jessie-int-model "exact"
162 "Jessie int model."
163 :group 'acsl
164 :type '(choice (const :tag "Exact" "exact")
165 (const :tag "Bounded" "bounded")
166 (const :tag "Modulo" "modulo")))
167
168 (defun acsl-jessie-gui ()
169 "Generate VCs and show them in a GUI"
170 (interactive)
171 (compile (concat acsl-jessie-gui-prog-name
172 " -jessie-int-model "
173 acsl-jessie-int-model
174 " "
175 (buffer-file-name))))
176
177 ;; menu
178
179 (require 'easymenu)
180
181 (defun acsl-menu-init ()
182 (easy-menu-define
183 acsl-menu (list acsl-mode-map)
184 "ACSL Mode Menu."
185 '("ACSL"
186 ["Customize ACSL mode" (customize-group 'acsl) t]
187 "---"
188 ["Jessie GUI" acsl-jessie-gui t]
189 ))
190 (easy-menu-add acsl-menu))
191
192 ;; indent
193
194 (defun acsl-indent-command (&optional arg)
195 "Indent ACSL code (quite basic yet)."
196 (interactive "*")
197 (c-indent-line)
198 (when (and (acsl-in-acsl-annot)
199 (< (nth 8 (syntax-ppss)) (line-beginning-position))) ; not the first line of an annot (which don't need to be indented)
200 (save-excursion
201 (back-to-indentation)
202 (if (not (eq (char-after) ?@))
203 (insert "@")
204 (goto-char (+ (point) 1)))
205 (if (not (looking-at "*/")) ; to avoid indenting last lines of annotation of the form "@*/" (thanks Yannick)
206 (let ((current (save-excursion
207 (skip-chars-forward " \t@")))
208 (expected (save-excursion
209 (let ((cc (current-column)))
210 (forward-line -1)
211 (move-to-column cc))
212 (skip-chars-forward " \t@"))))
213 (if (save-excursion
214 (skip-chars-backward " \t\n@")
215 (memq (char-before) '(?: ?=)))
216 (setq expected (+ expected 2)))
217 (if (save-excursion
218 (skip-chars-forward " \t@")
219 (looking-at "\\<\\(axiom\\|behavior\\|predicate\\|l\\(ogic\\|emma\\)\\|inductive\\)\\>"))
220 (setq expected (save-excursion
221 (goto-char (+ (nth 8 (syntax-ppss)) 3))
222 (skip-chars-forward " \t@"))))
223 (if (< current expected)
224 (insert-char ? (- expected current)))
225 (if (> current expected)
226 (kill-forward-chars (- current expected))))))
227 (if (eq (char-after) ?@)
228 (skip-chars-forward " \t@"))))
229
230 (defun acsl-indent-init ()
231 (setq indent-line-function 'acsl-indent-command)
232 ;; maybe not the best solution for C code but still works
233 (setq indent-region-function nil))
234
235 ;; main function for the mode
236
237 (define-derived-mode acsl-mode c-mode
238 "ACSL"
239 "Major mode for C annoted with ACSL."
240 (acsl-font-lock-init)
241 (acsl-keymap-init)
242 (acsl-indent-init)
243 (acsl-menu-init))
244
245 (provide 'acsl-mode)