Blob


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
11 ;; Version: 0
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)
19 ;; any later version.
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.
31 ;;; Commentary:
33 ;; A major mode for editing Algol 68 code.
34 ;;
35 ;; This is an improved and modernized version of the a68-mode written
36 ;; by Jose E. Marchesi. The original code was taken from
37 ;;
38 ;; https://github.com/lachrymology/me/blob/master/.emacs.d/extras/algol-mode.el
39 ;;
40 ;; TODO: support quote and dot stropping.
42 ;;; Code:
44 (require 'font-lock)
45 (require 'smie)
46 (require 'syntax)
48 (eval-when-compile
49 (require 'rx))
51 (defgroup a68 nil
52 "Major mode for editing Algol68 code."
53 :prefix "a68-"
54 :group 'languages)
56 (defcustom a68-indent-level 3
57 "Indentation step for Algol 68."
58 :type 'integer
59 :safe #'integerp)
61 (defcustom a68-comment-style "#"
62 "Default comment style used by e.g. `comment-dwim'."
63 :type '(choice (const "#")
64 (const "CO")
65 (const "COMMENT"))
66 :safe #'consp)
68 (defvar a68-mode-hook '()
69 "Hook run when entering Algol68 mode.")
71 (defvar a68-mode-map
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)
75 map)
76 "Keymap for Algol 68 major mode.")
78 (defconst a68-font-lock-keywords
79 (list
80 (cons (rx word-start
81 (or "DECS" "PROGRAM" "CONTEXT" "USE" "FINISH" "KEEP"
82 "ALIEN"
83 "MODE" "OP" "PRIO" "PROC"
84 "OF" "AT" "IS" "ISNT" "EMPTY" "SKIP"
85 "PR" "PRAGMAT"
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"
93 "REF")
94 word-end)
95 'font-lock-keyword-face)
96 (cons (rx word-start
97 (or "TRUE" "FALSE")
98 word-end)
99 'font-lock-constant-face)
100 ;; only valid for bold stropping
101 (cons (concat "\\<[A-Z]+\\>") 'font-lock-type-face)
102 (cons "\\('\\w*'\\)"
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
110 (smie-prec2->grammar
111 (smie-bnf->prec2 '((id)
112 (inst ("BEGIN" inst "END")
113 ("(" inst ")")
114 ("IF" insts "THEN" insts "ELSE" insts "FI")
115 (id "=" exp)
116 (id ":=" exp)
117 (exp))
118 (insts (insts ";" insts)
119 (inst))
120 (exp ("-" exp)
121 (exp "+" exp)
122 (exp "*" exp)
123 (exp "/" exp))
124 (exps (exps "," exps)
125 (inst)
126 (exp)))
127 '((assoc ";"))
128 '((assoc ","))
129 '((assoc "+") (assoc "-") (assoc "*") (assoc "/")))))
131 (defun a68--smie-rules (kind token)
132 (pcase (cons kind token)
133 (`(:elem . basic) a68-indent-level)
134 ;; (`(,_ . ",") (smie-rule-separator kind))
135 (`(,_ . ",") (smie-rule-separator kind))
136 (`(,_ . ";") (when (smie-rule-parent-p)
137 (smie-rule-parent)))
138 (`(:after . ":=") a68-indent-level)
139 (`(:after . "=") a68-indent-level)
140 (`(:before . ,(or `"BEGIN" '"(")) (when (smie-rule-hanging-p)
141 (smie-rule-parent)))
142 (`(:before . "IF")
143 (and (not (smie-rule-bolp))
144 (smie-rule-prev-p "ELSE")
145 (smie-rule-parent)))))
147 (defun a68--smie-forward-token ()
148 (forward-comment (point-max))
149 (cond
150 ((looking-at a68--keywords-regexp)
151 (goto-char (match-end 0))
152 (match-string-no-properties 0))
153 (t (buffer-substring-no-properties (point)
154 (progn (skip-syntax-forward "w_")
155 (point))))))
157 (defun a68--smie-backward-token ()
158 (forward-comment (- (point)))
159 (cond
160 ((looking-back a68--keywords-regexp (- (point) 2) t)
161 (goto-char (match-beginning 0))
162 (match-string-no-properties 0))
163 (t (buffer-substring-no-properties (point)
164 (progn (skip-syntax-backward "w_")
165 (point))))))
167 (defvar a68-mode-syntax-table
168 (let ((st (make-syntax-table)))
169 (modify-syntax-entry ?# "<" st)
170 (modify-syntax-entry ?# ">" st)
171 (modify-syntax-entry ?\\ "." st)
172 (modify-syntax-entry ?, "." st)
173 ;; define parentheses to match
174 (modify-syntax-entry ?\( "()" st)
175 (modify-syntax-entry ?\) ")(" st)
176 st))
178 (defvar a68-mode-abbrev-table nil
179 "Abbreviation table used in `a68-mode' buffers.")
181 (define-abbrev-table 'a68-mode-abbrev-table
182 '())
184 ;;;###autoload
185 (define-derived-mode a68-mode prog-mode "Algol68"
186 "Major mode for editing Alogl68 files."
187 :abbrev-table a68-mode-abbrev-table
188 (setq-local font-lock-defaults '(a68-font-lock-keywords))
189 (smie-setup a68--smie-grammar #'a68--smie-rules
190 :forward-token #'a68--smie-forward-token
191 :backward-token #'a68--smie-backward-token)
192 (setq-local comment-start a68-comment-style)
193 (setq-local comment-end a68-comment-style)
194 (setq-local syntax-propertize-function
195 (syntax-propertize-rules ((rx (group bow "COMMENT" eow)
196 (group (*? anychar))
197 (group bow "COMMENT" eow))
198 (1 "<")
199 (3 ">"))
200 ((rx (group bow "CO" eow)
201 (group (*? anychar))
202 (group bow "CO" eow))
203 (1 "<")
204 (3 ">"))
205 ((rx (group "#")
206 (group (*? anychar))
207 (group "#"))
208 (1 "<")
209 (3 ">")))))
211 ;;;###autoload
212 (add-to-list 'auto-mode-alist '("\\.a68\\'" . a68-mode))
214 (provide 'a68-mode)
215 ;;; a68-mode.el ends here