1 ;;; a68-mode.el --- Major mode for editing Algol 68 code -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011 Jose E. Marchesi
4 ;; Copyright (C) 2021 Omar Polo <op@omarpolo.com>
6 ;; Author: Jose E. Marchesi
7 ;; Omar Polo <op@omarpolo.com>
8 ;; Maintainer: Omar Polo
9 ;; URL: https://git.omarpolo.com/a68-mode
10 ;; Keywords: languages
12 ;; Package-Requires: ((emacs "24.3"))
14 ;; This file is NOT part of GNU Emacs.
16 ;; This program is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 3, or (at your option)
21 ;; This program is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with this program; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 ;; Boston, MA 02110-1301, USA.
33 ;; A major mode for editing Algol 68 code.
35 ;; This is an improved and modernized version of the a68-mode written
36 ;; by Jose E. Marchesi. The original code was taken from
38 ;; https://github.com/lachrymology/me/blob/master/.emacs.d/extras/algol-mode.el
40 ;; TODO: support quote and dot stropping.
52 "Major mode for editing Algol68 code."
56 (defcustom a68-indent-level 3
57 "Indentation step for Algol 68."
61 (defcustom a68-comment-style "#"
62 "Default comment style used by e.g. `comment-dwim'."
63 :type '(choice (const "#")
68 (defvar a68-mode-hook '()
69 "Hook run when entering Algol68 mode.")
72 (let ((map (make-sparse-keymap)))
73 (define-key map (kbd "C-j") #'newline-and-indent)
74 ;; (define-key map (kbd "RET") #'a68-electric-terminate-line)
76 "Keymap for Algol 68 major mode.")
78 (defconst a68-font-lock-keywords
81 (or "DECS" "PROGRAM" "CONTEXT" "USE" "FINISH" "KEEP"
83 "MODE" "OP" "PRIO" "PROC"
84 "OF" "AT" "IS" "ISNT" "EMPTY" "SKIP"
86 "CASE" "IN" "OUSE" "OUT" "ESAC"
87 "FOR" "FORALL" "FROM" "TO" "BY" "WHILE" "DO" "OD"
88 "IF" "THEN" "ELIF" "THEN" "ELSE" "FI"
89 "PAR" "BEGIN" "END" "GOTO" "EXIT"
90 "LWB" "UPB" "NOT" "ABS" "BIN" "REPR" "LENG"
91 "SHORTEN" "ODD" "SIGN" "ROUND" "ENTIER" "AND" "OR"
92 "DIV" "OVER" "MOD" "ELEM" "SHL" "SHR" "OVERAB" "DIVAB" "MODAB"
95 'font-lock-keyword-face)
99 'font-lock-constant-face)
100 ;; only valid for bold stropping
101 (cons (concat "\\<[A-Z]+\\>") 'font-lock-type-face)
103 'font-lock-variable-name-face))
104 "Highlighting expressions for Algol 68 mode.")
106 (defvar a68--keywords-regexp
107 (regexp-opt '("+" "*" ";" ">" "<" ":=" "=" "," ":")))
109 (defvar a68--smie-grammar
111 (smie-bnf->prec2 '((id)
121 ("BEGIN" exprs "END")
123 ;; why all the repetitions? copied from
125 ("IF" exprs "THEN" exprs "FI")
126 ("IF" exprs "THEN" exprs "ELSE" exprs "FI")
127 ("IF" exprs "THEN" exprs
128 "ELIF" exprs "THEN" exprs "FI")
129 ("IF" exprs "THEN" exprs
130 "ELIF" exprs "THEN" exprs
132 ("IF" exprs "THEN" exprs
133 "ELIF" exprs "THEN" exprs
134 "ELIF" exprs "THEN" exprs
136 ("CASE" exprs "IN" exprs "ESAC")
137 ("CASE" exprs "IN" exprs "OUT" exprs "ESAC"))
138 (exprs (exprs "," exprs)
142 (loop ("FOR" exprs "FROM" exprs "TO" exprs
143 "BY" exprs "DO" exprs "OD")
144 ("FOR" exprs "FROM" exprs "TO" exprs
146 ("FOR" exprs "BY" exprs "TO" exprs
148 ;; ("TO" exprs "DO" exprs "OD")
149 ("WHILE" exprs "DO" exprs "OD")))
150 '((assoc ";" "," ":"))
151 '((assoc "=" "/=" ":=" ":=:" ":/=:"
154 (defun a68--smie-rules (kind token)
155 (pcase (cons kind token)
156 (`(:elem . basic) a68-indent-level)
157 ;; (`(,_ . ",") (smie-rule-separator kind))
158 (`(,_ . ",") (smie-rule-separator kind))
159 (`(,_ . ";") (when (smie-rule-parent-p)
161 (`(:after . ":=") a68-indent-level)
162 (`(:after . "=") a68-indent-level)
163 (`(:before . ,(or `"BEGIN" '"(")) (when (smie-rule-hanging-p)
166 (and (not (smie-rule-bolp))
167 (smie-rule-prev-p "ELSE")
168 (smie-rule-parent)))))
170 (defun a68--smie-forward-token ()
171 (forward-comment (point-max))
173 ((looking-at a68--keywords-regexp)
174 (goto-char (match-end 0))
175 (match-string-no-properties 0))
176 (t (buffer-substring-no-properties (point)
177 (progn (skip-syntax-forward "w_")
180 (defun a68--smie-backward-token ()
181 (forward-comment (- (point)))
183 ((looking-back a68--keywords-regexp (- (point) 2) t)
184 (goto-char (match-beginning 0))
185 (match-string-no-properties 0))
186 (t (buffer-substring-no-properties (point)
187 (progn (skip-syntax-backward "w_")
190 (defvar a68-mode-syntax-table
191 (let ((st (make-syntax-table)))
192 (modify-syntax-entry ?# "<" st)
193 (modify-syntax-entry ?# ">" st)
194 (modify-syntax-entry ?\\ "." st)
195 (modify-syntax-entry ?, "." st)
196 (modify-syntax-entry ?: "." st)
197 ;; define parentheses to match
198 (modify-syntax-entry ?\( "()" st)
199 (modify-syntax-entry ?\) ")(" st)
202 (defvar a68-mode-abbrev-table nil
203 "Abbreviation table used in `a68-mode' buffers.")
205 (define-abbrev-table 'a68-mode-abbrev-table
209 (define-derived-mode a68-mode prog-mode "Algol68"
210 "Major mode for editing Alogl68 files."
211 :abbrev-table a68-mode-abbrev-table
212 (setq-local font-lock-defaults '(a68-font-lock-keywords))
213 (smie-setup a68--smie-grammar #'a68--smie-rules
214 :forward-token #'a68--smie-forward-token
215 :backward-token #'a68--smie-backward-token)
216 (setq-local comment-start a68-comment-style)
217 (setq-local comment-end a68-comment-style)
218 (setq-local syntax-propertize-function
219 (syntax-propertize-rules
220 ((rx (group bow "COMMENT" eow)
222 (group bow "COMMENT" eow))
225 ((rx (group bow "CO" eow)
227 (group bow "CO" eow))
230 ;; ((rx (group "#\\[^!\\]")
231 ;; (group (*? anychar))
238 (add-to-list 'auto-mode-alist '("\\.a68\\'" . a68-mode))
241 ;;; a68-mode.el ends here