-
Notifications
You must be signed in to change notification settings - Fork 7
/
z3-mode.el
173 lines (132 loc) · 5.88 KB
/
z3-mode.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
;;; z3-mode.el --- A z3/SMTLIBv2 interactive development environment -*-lexical-binding: t-*-
;; Version: 0.0.1
;; Author: Zephyr Pellerin <zephyr.pellerin@gmail.com>
;; Homepage: https://github.com/zv/z3-mode
;; Keywords: z3 yices mathsat smt beaver
;; Package-Requires: ((flycheck "0.23") (emacs "24"))
;; This file is not part of GNU Emacs.
;; 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, 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 this file. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; An interactive development environment for SMT-LIB files and Z3. Z3 and
;; Contessa as well as various SMTLIB supporting theorem provers & solvers are
;; supported. Structured statements can be inserted and ran with C-c
;;; Code:
(require 'flycheck)
(defgroup z3 nil
"Z3/SMT script Mode"
:group 'languages
:prefix "z3-")
(defcustom z3-solver-cmd (executable-find "z3")
"The command used when you run the solver.
The following solvers are currently supported
Z3"
:type 'file
:group 'z3)
(defcustom z3-input-format "smt2"
"The input format."
:group 'z3
:options '(("SMTLIBv1" "smt")
("SMTLIBv2" "smt2")
("Datalog" "dl")
("DIMACS" "dimacs")))
(defcustom z3-builtins
'((z3/smtlib2 sh-append smtlib2
"check-sat-using"
"declare-var"
"declare-rel"
"rule"
"query"
"set-predicate-representation"
;; Z3Opt
"maximize"
"minimize"
"assert-soft"
"assert-weighted"
;; iZ3
"compute-interpolant"
))
"List of solver specific builtins and keywords.
Note that on some systems and builds, not all are available.")
(defvar z3-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") 'z3-execute-region) map)
"Keymap for z3-mode.")
;; font-lock support
;; Matches alternative base numeric primitives such as `#xF0FA' & `#b010'
(defconst z3-altbase-literal-regexp "\\(#x[0-9a-fA-F]+\\|#b[01]+\\)")
; Matches the simplest symbol regexp format
(defconst z3-symbol-regexp "[a-zA-Z~!@$%^&*_+=<>.?/-][0-9a-zA-Z~!@$%^&*_+=<>.?/-]*")
;; Matches an alternative quote symbol regexp format
(defconst z3-quoted-symbol-regexp "|[]!-[^-{}~ \t\r\n]*|")
;; Matches lisp-symbol style keywords, i.e `:keyword'
(defconst z3-keyword-symbol-regexp ":[0-9a-zA-Z~!@$%^&*_+=<>.?/-]+")
;; Z3 commands as keywords
(defvar z3-keywords '("apply" "assert" "assert-soft" "check-sat" "check-sat-using"
"compute-interpolant" "declare-const" "declare-datatypes" "declare-fun"
"declare-map" "declare-rel" "declare-sort" "declare-tactic"
"define-sort" "display" "echo" "eval" "exit" "fixedpoint-pop"
"fixedpoint-push" "get-assertions" "get-assignment" "get-info"
"get-interpolant" "get-model" "get-option" "get-proof" "get-unsat-core"
"get-user-tactics" "get-value" "help" "help-tactic" "labels" "maximize"
"minimize" "pop" "push" "query" "reset" "rule" "set-info" "set-logic"
"set-option" "simplify"))
;; Define our font-lock
(defvar z3-keywords-regexp (regexp-opt z3-keywords 'words))
(defvar z3-font-lock-defaults
`(((,(regexp-opt z3-keywords 'words) . font-lock-keyword-face)
(,z3-keyword-symbol-regexp . font-lock-builtin-face)
(,z3-altbase-literal-regexp . font-lock-constant-face)
;; We *can* highlight symbols, but it compromises the clarity
;; (,z3-symbol-regexp . font-lock-function-name-face)
)))
;; mode-command and utility functions
;; Define the mode
;;;###autoload
(define-derived-mode z3-mode lisp-mode "Z3/SMT2"
"Major mode for editing Z3 files"
;; code for syntax highlighting
(setq font-lock-defaults z3-font-lock-defaults))
;; Setup Syntax Checking
;; Command to run SMT solver on the whole buffer
(defun z3-execute-region ()
"Pass optional header and region to a prover for noninteractive execution.
The working directory is that of the buffer, and only environment variables
are already set which is why you can mark a header within the script."
(interactive)
(shell-command-on-region (if (region-active-p) (region-beginning) (point-min))
(if (region-active-p) (region-end) (point-max))
(concat z3-solver-cmd " -in")))
;;; syntax and checker
;; Setup the flycheck specific customize group
(defcustom flycheck-z3-smt2-lint-executable nil
(format "The executable of the z3-smt2-lint syntax checker.
Either a string containing the name or the path of the
executable, or nil to use the default executable from the syntax
checker declaration.
The default executable is %S." z3-solver-cmd)
:type '(choice (const :tag "Default executable" nil)
(string :tag "Name or path"))
:group 'flycheck-executables
:risky t)
;; Configure the command checker
(flycheck-define-command-checker 'z3-smt2-lint
"A syntax and style checker for SMTLIBv2 with Z3"
:command `(,z3-solver-cmd "-v:1" "-smt2" source)
:error-patterns
;; Error pattern of the form `(error "line X column Y: message)`
'((error "error \"line " line " column " column ": " (message) "\")"))
:modes 'z3-mode)
(setq auto-mode-alist
(append
'(("\\.smt[2]?$" . z3-mode)) auto-mode-alist))
(provide 'z3-mode)
;;; z3-mode.el ends here