why3.el 6.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
;; why3.el - GNU Emacs mode for Why3

(defvar why3-mode-hook nil)

(defvar why3-mode-map nil
  "Keymap for Why3 major mode")

(if why3-mode-map nil
  (setq why3-mode-map (make-keymap))
  ;; (define-key why3-mode-map "\C-c\C-c" 'why3-generate-obligations) **)
  ;; (define-key why3-mode-map "\C-c\C-a" 'why3-find-alternate-file) **)
  ;; (define-key why3-mode-map "\C-c\C-v" 'why3-viewer) **)
  (define-key why3-mode-map [(control return)] 'font-lock-fontify-buffer))

(setq auto-mode-alist
      (append
17
       '(("\\.\\(why\\|mlw\\)\\'" . why3-mode))
18 19 20 21 22
       auto-mode-alist))

;; font-lock

(defun why3-regexp-opt (l)
23
  (regexp-opt l 'words))
24 25 26

(defconst why3-font-lock-keywords-1
  (list
27
   `(,(why3-regexp-opt '("invariant" "variant" "diverges" "requires" "ensures" "pure" "returns" "raises" "reads" "writes" "alias" "assert" "assume" "check")) . font-lock-type-face)
28
   `(,(why3-regexp-opt '("use" "clone" "scope" "import" "export" "coinductive" "inductive" "external" "constant" "function" "predicate" "val" "exception" "axiom" "lemma" "goal" "type" "mutable" "abstract" "private" "any" "match" "let" "rec" "in" "if" "then" "else" "begin" "end" "while" "for" "to" "downto" "do" "done" "loop" "absurd" "ghost" "partial" "raise" "return" "break" "continue" "try" "with" "theory" "uses" "module" "converter" "fun" "at" "old" "true" "false" "forall" "exists" "label" "by" "so" "meta")) . font-lock-keyword-face)
29 30 31 32 33 34 35 36 37 38 39 40
   )
  "Minimal highlighting for Why3 mode")

(defvar why3-font-lock-keywords why3-font-lock-keywords-1
  "Default highlighting for Why3 mode")

(defvar why3-indent 2
  "How many spaces to indent in why3 mode.")
(make-variable-buffer-local 'why3-indent)

;; syntax

Guillaume Melquiond's avatar
Guillaume Melquiond committed
41 42 43 44 45 46 47
(defvar why3-mode-syntax-table
  (let ((st (make-syntax-table)))
    ; identifiers
    (modify-syntax-entry ?' "w" st)
    (modify-syntax-entry ?_ "w" st)
    ; strings
    (modify-syntax-entry ?\" "\"" st)
48
    ; comments
Guillaume Melquiond's avatar
Guillaume Melquiond committed
49 50 51 52 53
    (modify-syntax-entry ?\( "()1n" st)
    (modify-syntax-entry ?\) ")(4n" st)
    (modify-syntax-entry ?* ". 23" st)
    st)
  "Syntax table for why3-mode")
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

;indentation

;http://www.emacswiki.org/emacs/ModeTutorial
(defun why3-indent-line ()
  "Indent current line as why3 logic"
  (interactive)
  (save-excursion
    (beginning-of-line)
    ;(debug)
    (if (bobp)  ; Check for rule 1
        (indent-line-to 0)
      (let ((not-indented t) cur-indent)
        (if (looking-at "^[ \t]*end") ; Check for rule 2
            (progn
              (save-excursion
                (forward-line -1)
                (setq cur-indent (- (current-indentation) why3-indent)))
              (if (< cur-indent 0)
                  (setq cur-indent 0)))
          (progn
            (if (looking-at "^[ \t]*\\(logic\\|type\\|prop\\)") ; check for clone
                (progn
                  (save-excursion
                    (forward-line -1)
                    (if (looking-at "^[ \t]*\\(logic\\|type\\|prop\\).*,[ \t]*$")
                        (progn
                          (setq cur-indent (current-indentation))
                          (setq not-indented nil))
                      (if (looking-at "^[ \t]*clone.*with ")
                          (progn
                            (setq cur-indent (+ (current-indentation) why3-indent))
                            (setq not-indented nil)
                            ))))))
        ;For the definition its very badly done...
          (if (looking-at "^[ \t]*$")
	      ;; (save-excursion
	      ;; 	(forward-line -1)
	      ;; 	(setq cur-indent (current-indentation))
	      ;; 	(setq not-indented nil))
              (progn
                (setq cur-indent 0)
                (setq not-indented nil))
	    (if (not
		 (looking-at "^[ \t]*(\*.*"))
            (if (not
                 (looking-at "^[ \t]*\\(logic\\|type\\|axiom\\|goal\\|lemma\\|inductive\\|use\\|theory\\|clone\\)"))
                (save-excursion
                  (condition-case nil
                      (save-excursion
                        (backward-up-list)
                        (setq cur-indent (+ (current-column) 1))
                        (setq not-indented nil))
                    (error
                     (forward-line -1)
                     (if (looking-at
                          "^[ \t]*\\(logic\\|type\\|axiom\\|goal\\|lemma\\|inductive\\)")
                         (setq cur-indent (+ (current-indentation) why3-indent))
                       (setq cur-indent (current-indentation)))
                     (setq not-indented nil)))))))
          ;For inside theory or namespace
          (save-excursion
            (while not-indented
              (forward-line -1)
              (if (looking-at "^[ \t]*end") ; Check for rule 3
                  (progn
                    (setq cur-indent (current-indentation))
                    (setq not-indented nil))
                                        ; Check for rule 4
                (if (looking-at "^[ \t]*\\(theory\\|namespace\\)")
                    (progn
                      (setq cur-indent (+ (current-indentation) why3-indent))
                      (setq not-indented nil))
                  (if (bobp) ; Check for rule 5
                      (setq not-indented nil)))))))
        (if cur-indent
            (indent-line-to cur-indent)
          (indent-line-to 0)))))))

Guillaume Melquiond's avatar
Guillaume Melquiond committed
133
; compile will propose "why3 ide file" if no Makefile is present
134 135 136 137 138 139

(add-hook 'why3-mode-hook
          (lambda ()
            (unless (file-exists-p "Makefile")
              (set (make-local-variable 'compile-command)
                   (let ((file (file-name-nondirectory buffer-file-name)))
140
                     (format "why3 ide %s" file))))))
141

142 143 144 145 146 147 148 149
(defconst why3--syntax-propertize
  (syntax-propertize-rules
    ; attributes: [@foo]
    ("\\(\\[\\)@[^]]*\\(]\\)" (1 "!]") (2 "!["))
    ; star: (*)
    ("\\((\\)\\*\\()\\)" (1 "()") (2 ")("))
  ))

150 151 152 153 154 155 156 157
;; setting the mode
(defun why3-mode ()
  "Major mode for editing Why3 programs.

\\{why3-mode-map}"
  (interactive)
  (kill-all-local-variables)
  ; hilight
Guillaume Melquiond's avatar
Guillaume Melquiond committed
158 159 160
  (set-syntax-table why3-mode-syntax-table)
  (set (make-local-variable 'font-lock-defaults) '(why3-font-lock-keywords))
  (set (make-local-variable 'font-lock-multiline) t)
161 162 163 164
  ; indentation
  ;(make-local-variable 'indent-line-function)
  ;(setq indent-line-function 'why3-indent-line)
  ; OCaml style comments for comment-region, comment-dwim, etc.
165 166
  (set (make-local-variable 'comment-start) "(*")
  (set (make-local-variable 'comment-end)   "*)")
167
  (setq-local syntax-propertize-function why3--syntax-propertize)
168 169 170 171 172 173 174 175 176
  ; menu
  ; providing the mode
  (setq major-mode 'why3-mode)
  (setq mode-name "Why3")
  (use-local-map why3-mode-map)
  (font-lock-mode 1)
  ; (why3-menu)
  (run-hooks 'why3-mode-hook))

177
(provide 'why3)