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)
112 (ids (id "-anchor-" id))
113 (fields (fields "," fields)
115 (args ("(" fargs ")"))
116 (fargs (fargs "," fargs)
123 (type-decl ("MODE" type-decl*))
124 (type-decl* (type-decl* "," type-decl*)
125 (id "=" type-decl**))
126 (type-decl** ("STRUCT" args)
128 ("PROC" args "-archor-" ids))
129 (op-decl (op-decl "," op-decl)
130 ("OP" ids "=" args ids ":" exp))
131 (proc-decl (proc-decl "," proc-decl)
132 ("OP" ids "=" args ids ":" exp)
133 ("PROC" ids "=" ids ":" exp))
134 ;; TODO: this don't cover all the loop
136 (loop ("FOR" exp "FROM" exp "TO" exp "BY" exp
138 ("FOR" exp "FROM" exp "TO" exp
140 ("FOR" exp "BY" exp "TO" exp
142 ("-to-" "TO" exp "DO" exp "OD")
143 ("WHILE" exp "DO" exp "OD"))
144 (insts (insts ";" insts)
146 ("IF" exp "THEN" insts "FI")
147 ("IF" exp "THEN" insts "ELSE" insts "FI")
148 ("IF" exp "THEN" insts
149 "ELSIF" exp "THEN" insts "ELSE" insts "FI")
150 ("IF" exp "THEN" insts
151 "ELSIF" exp "THEN" insts
152 "ELSIF" exp "THEN" insts "ELSE" insts "FI")
153 ;; TODO OUSE for both case and conformity case
154 ("CASE" exp "IN" fargs "ESAC")
155 ("CASE" exp "IN" conformity-cases "ESAC")
156 ("CASE" exp "IN" fargs "OUT" exp "ESAC")
165 '((assoc "=" "/" ":=" ":=:" ":/=:"
168 (defun a68--smie-rules (kind token)
169 (pcase (cons kind token)
170 (`(:elem . basic) a68-indent-level)
171 ;; (`(,_ . ",") (smie-rule-separator kind))
172 (`(,_ . ",") (smie-rule-separator kind))
173 (`(,_ . ";") (when (smie-rule-parent-p)
175 (`(:after . ":=") a68-indent-level)
176 (`(:after . "=") a68-indent-level)
177 (`(:before . ,(or `"BEGIN" '"(")) (when (smie-rule-hanging-p)
180 (and (not (smie-rule-bolp))
181 (smie-rule-prev-p "ELSE")
182 (smie-rule-parent)))))
184 (defun a68--smie-forward-token ()
185 (forward-comment (point-max))
187 ((looking-at a68--keywords-regexp)
188 (goto-char (match-end 0))
189 (match-string-no-properties 0))
190 (t (buffer-substring-no-properties (point)
191 (progn (skip-syntax-forward "w_")
194 (defun a68--smie-backward-token ()
195 (forward-comment (- (point)))
197 ((looking-back a68--keywords-regexp (- (point) 2) t)
198 (goto-char (match-beginning 0))
199 (match-string-no-properties 0))
200 (t (buffer-substring-no-properties (point)
201 (progn (skip-syntax-backward "w_")
204 (defvar a68-mode-syntax-table
205 (let ((st (make-syntax-table)))
206 (modify-syntax-entry ?# "<" st)
207 (modify-syntax-entry ?# ">" st)
208 (modify-syntax-entry ?\\ "." st)
209 (modify-syntax-entry ?, "." st)
210 (modify-syntax-entry ?: "." st)
211 ;; define parentheses to match
212 (modify-syntax-entry ?\( "()" st)
213 (modify-syntax-entry ?\) ")(" st)
216 (defvar a68-mode-abbrev-table nil
217 "Abbreviation table used in `a68-mode' buffers.")
219 (define-abbrev-table 'a68-mode-abbrev-table
223 (define-derived-mode a68-mode prog-mode "Algol68"
224 "Major mode for editing Alogl68 files."
225 :abbrev-table a68-mode-abbrev-table
226 (setq-local font-lock-defaults '(a68-font-lock-keywords))
227 (smie-setup a68--smie-grammar #'a68--smie-rules
228 :forward-token #'a68--smie-forward-token
229 :backward-token #'a68--smie-backward-token)
230 (setq-local comment-start a68-comment-style)
231 (setq-local comment-end a68-comment-style)
232 (setq-local syntax-propertize-function
233 (syntax-propertize-rules
234 ((rx (group bow "COMMENT" eow)
236 (group bow "COMMENT" eow))
239 ((rx (group bow "CO" eow)
241 (group bow "CO" eow))
244 ;; a comment is # ... #, but I don't want the
245 ;; (eventual) shebang #! to be considered the start of
247 ((rx (group "#" (not "!"))
254 (add-to-list 'auto-mode-alist '("\\.a68\\'" . a68-mode))
257 ;;; a68-mode.el ends here