Blame


1 6c5ab754 2021-11-07 op ;;; a68-mode.el --- Major mode for editing Algol 68 code -*- lexical-binding: t; -*-
2 364d8905 2021-11-07 op
3 364d8905 2021-11-07 op ;; Copyright (C) 2011 Jose E. Marchesi
4 364d8905 2021-11-07 op ;; Copyright (C) 2021 Omar Polo <op@omarpolo.com>
5 364d8905 2021-11-07 op
6 364d8905 2021-11-07 op ;; Author: Jose E. Marchesi
7 364d8905 2021-11-07 op ;; Omar Polo <op@omarpolo.com>
8 364d8905 2021-11-07 op ;; Maintainer: Omar Polo
9 364d8905 2021-11-07 op ;; URL: https://git.omarpolo.com/a68-mode
10 364d8905 2021-11-07 op ;; Keywords: languages
11 364d8905 2021-11-07 op ;; Version: 0
12 364d8905 2021-11-07 op ;; Package-Requires: ((emacs "24.3"))
13 364d8905 2021-11-07 op
14 364d8905 2021-11-07 op ;; This file is NOT part of GNU Emacs.
15 364d8905 2021-11-07 op
16 364d8905 2021-11-07 op ;; This program is free software; you can redistribute it and/or modify
17 364d8905 2021-11-07 op ;; it under the terms of the GNU General Public License as published by
18 364d8905 2021-11-07 op ;; the Free Software Foundation; either version 3, or (at your option)
19 364d8905 2021-11-07 op ;; any later version.
20 364d8905 2021-11-07 op
21 364d8905 2021-11-07 op ;; This program is distributed in the hope that it will be useful,
22 364d8905 2021-11-07 op ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 364d8905 2021-11-07 op ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 364d8905 2021-11-07 op ;; GNU General Public License for more details.
25 364d8905 2021-11-07 op
26 364d8905 2021-11-07 op ;; You should have received a copy of the GNU General Public License
27 364d8905 2021-11-07 op ;; along with this program; see the file COPYING. If not, write to the
28 364d8905 2021-11-07 op ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 364d8905 2021-11-07 op ;; Boston, MA 02110-1301, USA.
30 364d8905 2021-11-07 op
31 364d8905 2021-11-07 op ;;; Commentary:
32 364d8905 2021-11-07 op
33 364d8905 2021-11-07 op ;; A major mode for editing Algol 68 code.
34 364d8905 2021-11-07 op ;;
35 bb4b3a62 2021-11-07 op ;; This is an improved and modernized version of the a68-mode written
36 bb4b3a62 2021-11-07 op ;; by Jose E. Marchesi. The original code was taken from
37 bb4b3a62 2021-11-07 op ;;
38 bb4b3a62 2021-11-07 op ;; https://github.com/lachrymology/me/blob/master/.emacs.d/extras/algol-mode.el
39 bb4b3a62 2021-11-07 op ;;
40 364d8905 2021-11-07 op ;; TODO: support quote and dot stropping.
41 364d8905 2021-11-07 op
42 364d8905 2021-11-07 op ;;; Code:
43 364d8905 2021-11-07 op
44 364d8905 2021-11-07 op (require 'font-lock)
45 61b0b383 2021-11-08 op (require 'smie)
46 61b0b383 2021-11-08 op (require 'syntax)
47 364d8905 2021-11-07 op
48 364d8905 2021-11-07 op (eval-when-compile
49 364d8905 2021-11-07 op (require 'rx))
50 364d8905 2021-11-07 op
51 caaad6ea 2021-11-07 op (defgroup a68 nil
52 caaad6ea 2021-11-07 op "Major mode for editing Algol68 code."
53 caaad6ea 2021-11-07 op :prefix "a68-"
54 caaad6ea 2021-11-07 op :group 'languages)
55 364d8905 2021-11-07 op
56 caaad6ea 2021-11-07 op (defcustom a68-indent-level 3
57 caaad6ea 2021-11-07 op "Indentation step for Algol 68."
58 caaad6ea 2021-11-07 op :type 'integer
59 caaad6ea 2021-11-07 op :safe #'integerp)
60 caaad6ea 2021-11-07 op
61 18be0e13 2021-11-07 op (defcustom a68-comment-style "#"
62 29b924fd 2021-11-07 op "Default comment style used by e.g. `comment-dwim'."
63 18be0e13 2021-11-07 op :type '(choice (const "#")
64 18be0e13 2021-11-07 op (const "CO")
65 18be0e13 2021-11-07 op (const "COMMENT"))
66 29b924fd 2021-11-07 op :safe #'consp)
67 29b924fd 2021-11-07 op
68 364d8905 2021-11-07 op (defvar a68-mode-hook '()
69 364d8905 2021-11-07 op "Hook run when entering Algol68 mode.")
70 364d8905 2021-11-07 op
71 364d8905 2021-11-07 op (defvar a68-mode-map
72 8cd9fb9c 2021-11-07 op (let ((map (make-sparse-keymap)))
73 364d8905 2021-11-07 op (define-key map (kbd "C-j") #'newline-and-indent)
74 61b0b383 2021-11-08 op ;; (define-key map (kbd "RET") #'a68-electric-terminate-line)
75 364d8905 2021-11-07 op map)
76 364d8905 2021-11-07 op "Keymap for Algol 68 major mode.")
77 364d8905 2021-11-07 op
78 364d8905 2021-11-07 op (defconst a68-font-lock-keywords
79 364d8905 2021-11-07 op (list
80 364d8905 2021-11-07 op (cons (rx word-start
81 364d8905 2021-11-07 op (or "DECS" "PROGRAM" "CONTEXT" "USE" "FINISH" "KEEP"
82 364d8905 2021-11-07 op "ALIEN"
83 364d8905 2021-11-07 op "MODE" "OP" "PRIO" "PROC"
84 364d8905 2021-11-07 op "OF" "AT" "IS" "ISNT" "EMPTY" "SKIP"
85 364d8905 2021-11-07 op "PR" "PRAGMAT"
86 364d8905 2021-11-07 op "CASE" "IN" "OUSE" "OUT" "ESAC"
87 364d8905 2021-11-07 op "FOR" "FORALL" "FROM" "TO" "BY" "WHILE" "DO" "OD"
88 364d8905 2021-11-07 op "IF" "THEN" "ELIF" "THEN" "ELSE" "FI"
89 364d8905 2021-11-07 op "PAR" "BEGIN" "END" "GOTO" "EXIT"
90 364d8905 2021-11-07 op "LWB" "UPB" "NOT" "ABS" "BIN" "REPR" "LENG"
91 364d8905 2021-11-07 op "SHORTEN" "ODD" "SIGN" "ROUND" "ENTIER" "AND" "OR"
92 364d8905 2021-11-07 op "DIV" "OVER" "MOD" "ELEM" "SHL" "SHR" "OVERAB" "DIVAB" "MODAB"
93 364d8905 2021-11-07 op "REF")
94 364d8905 2021-11-07 op word-end)
95 364d8905 2021-11-07 op 'font-lock-keyword-face)
96 364d8905 2021-11-07 op (cons (rx word-start
97 364d8905 2021-11-07 op (or "TRUE" "FALSE")
98 364d8905 2021-11-07 op word-end)
99 364d8905 2021-11-07 op 'font-lock-constant-face)
100 364d8905 2021-11-07 op ;; only valid for bold stropping
101 364d8905 2021-11-07 op (cons (concat "\\<[A-Z]+\\>") 'font-lock-type-face)
102 364d8905 2021-11-07 op (cons "\\('\\w*'\\)"
103 364d8905 2021-11-07 op 'font-lock-variable-name-face))
104 364d8905 2021-11-07 op "Highlighting expressions for Algol 68 mode.")
105 364d8905 2021-11-07 op
106 61b0b383 2021-11-08 op (defvar a68--keywords-regexp
107 0effda17 2021-11-08 op (regexp-opt '("+" "*" ";" ">" "<" ":=" "=" ",")))
108 364d8905 2021-11-07 op
109 61b0b383 2021-11-08 op (defvar a68--smie-grammar
110 61b0b383 2021-11-08 op (smie-prec2->grammar
111 61b0b383 2021-11-08 op (smie-bnf->prec2 '((id)
112 61b0b383 2021-11-08 op (inst ("BEGIN" inst "END")
113 61b0b383 2021-11-08 op ("(" inst ")")
114 61b0b383 2021-11-08 op ("IF" insts "THEN" insts "ELSE" insts "FI")
115 61b0b383 2021-11-08 op (id "=" exp)
116 61b0b383 2021-11-08 op (id ":=" exp)
117 61b0b383 2021-11-08 op (exp))
118 61b0b383 2021-11-08 op (insts (insts ";" insts)
119 61b0b383 2021-11-08 op (inst))
120 61b0b383 2021-11-08 op (exp ("-" exp)
121 61b0b383 2021-11-08 op (exp "+" exp)
122 61b0b383 2021-11-08 op (exp "*" exp)
123 61b0b383 2021-11-08 op (exp "/" exp))
124 61b0b383 2021-11-08 op (exps (exps "," exps)
125 61b0b383 2021-11-08 op (inst)
126 61b0b383 2021-11-08 op (exp)))
127 61b0b383 2021-11-08 op '((assoc ";"))
128 61b0b383 2021-11-08 op '((assoc ","))
129 61b0b383 2021-11-08 op '((assoc "+") (assoc "-") (assoc "*") (assoc "/")))))
130 364d8905 2021-11-07 op
131 61b0b383 2021-11-08 op (defun a68--smie-rules (kind token)
132 61b0b383 2021-11-08 op (pcase (cons kind token)
133 61b0b383 2021-11-08 op (`(:elem . basic) a68-indent-level)
134 61b0b383 2021-11-08 op ;; (`(,_ . ",") (smie-rule-separator kind))
135 61b0b383 2021-11-08 op (`(,_ . ",") (smie-rule-separator kind))
136 61b0b383 2021-11-08 op (`(,_ . ";") (when (smie-rule-parent-p)
137 61b0b383 2021-11-08 op (smie-rule-parent)))
138 61b0b383 2021-11-08 op (`(:after . ":=") a68-indent-level)
139 61b0b383 2021-11-08 op (`(:after . "=") a68-indent-level)
140 61b0b383 2021-11-08 op (`(:before . ,(or `"BEGIN" '"(")) (when (smie-rule-hanging-p)
141 61b0b383 2021-11-08 op (smie-rule-parent)))
142 61b0b383 2021-11-08 op (`(:before . "IF")
143 61b0b383 2021-11-08 op (and (not (smie-rule-bolp))
144 61b0b383 2021-11-08 op (smie-rule-prev-p "ELSE")
145 61b0b383 2021-11-08 op (smie-rule-parent)))))
146 61b0b383 2021-11-08 op
147 61b0b383 2021-11-08 op (defun a68--smie-forward-token ()
148 61b0b383 2021-11-08 op (forward-comment (point-max))
149 61b0b383 2021-11-08 op (cond
150 61b0b383 2021-11-08 op ((looking-at a68--keywords-regexp)
151 61b0b383 2021-11-08 op (goto-char (match-end 0))
152 61b0b383 2021-11-08 op (match-string-no-properties 0))
153 61b0b383 2021-11-08 op (t (buffer-substring-no-properties (point)
154 61b0b383 2021-11-08 op (progn (skip-syntax-forward "w_")
155 61b0b383 2021-11-08 op (point))))))
156 61b0b383 2021-11-08 op
157 61b0b383 2021-11-08 op (defun a68--smie-backward-token ()
158 61b0b383 2021-11-08 op (forward-comment (- (point)))
159 61b0b383 2021-11-08 op (cond
160 61b0b383 2021-11-08 op ((looking-back a68--keywords-regexp (- (point) 2) t)
161 61b0b383 2021-11-08 op (goto-char (match-beginning 0))
162 61b0b383 2021-11-08 op (match-string-no-properties 0))
163 61b0b383 2021-11-08 op (t (buffer-substring-no-properties (point)
164 61b0b383 2021-11-08 op (progn (skip-syntax-backward "w_")
165 61b0b383 2021-11-08 op (point))))))
166 61b0b383 2021-11-08 op
167 364d8905 2021-11-07 op (defvar a68-mode-syntax-table
168 364d8905 2021-11-07 op (let ((st (make-syntax-table)))
169 364d8905 2021-11-07 op (modify-syntax-entry ?# "<" st)
170 364d8905 2021-11-07 op (modify-syntax-entry ?# ">" st)
171 364d8905 2021-11-07 op (modify-syntax-entry ?\\ "." st)
172 0effda17 2021-11-08 op (modify-syntax-entry ?, "." st)
173 364d8905 2021-11-07 op ;; define parentheses to match
174 364d8905 2021-11-07 op (modify-syntax-entry ?\( "()" st)
175 364d8905 2021-11-07 op (modify-syntax-entry ?\) ")(" st)
176 364d8905 2021-11-07 op st))
177 364d8905 2021-11-07 op
178 e9c672cf 2021-11-07 op (defvar a68-mode-abbrev-table nil
179 e9c672cf 2021-11-07 op "Abbreviation table used in `a68-mode' buffers.")
180 e9c672cf 2021-11-07 op
181 e9c672cf 2021-11-07 op (define-abbrev-table 'a68-mode-abbrev-table
182 e9c672cf 2021-11-07 op '())
183 e9c672cf 2021-11-07 op
184 364d8905 2021-11-07 op ;;;###autoload
185 364d8905 2021-11-07 op (define-derived-mode a68-mode prog-mode "Algol68"
186 364d8905 2021-11-07 op "Major mode for editing Alogl68 files."
187 e9c672cf 2021-11-07 op :abbrev-table a68-mode-abbrev-table
188 364d8905 2021-11-07 op (setq-local font-lock-defaults '(a68-font-lock-keywords))
189 61b0b383 2021-11-08 op (smie-setup a68--smie-grammar #'a68--smie-rules
190 61b0b383 2021-11-08 op :forward-token #'a68--smie-forward-token
191 61b0b383 2021-11-08 op :backward-token #'a68--smie-backward-token)
192 18be0e13 2021-11-07 op (setq-local comment-start a68-comment-style)
193 18be0e13 2021-11-07 op (setq-local comment-end a68-comment-style)
194 4fcb92ee 2021-11-07 op (setq-local syntax-propertize-function
195 4fcb92ee 2021-11-07 op (syntax-propertize-rules ((rx (group bow "COMMENT" eow)
196 4fcb92ee 2021-11-07 op (group (*? anychar))
197 4fcb92ee 2021-11-07 op (group bow "COMMENT" eow))
198 4fcb92ee 2021-11-07 op (1 "<")
199 4fcb92ee 2021-11-07 op (3 ">"))
200 4fcb92ee 2021-11-07 op ((rx (group bow "CO" eow)
201 4fcb92ee 2021-11-07 op (group (*? anychar))
202 4fcb92ee 2021-11-07 op (group bow "CO" eow))
203 4fcb92ee 2021-11-07 op (1 "<")
204 4fcb92ee 2021-11-07 op (3 ">"))
205 4fcb92ee 2021-11-07 op ((rx (group "#")
206 4fcb92ee 2021-11-07 op (group (*? anychar))
207 4fcb92ee 2021-11-07 op (group "#"))
208 4fcb92ee 2021-11-07 op (1 "<")
209 4fcb92ee 2021-11-07 op (3 ">")))))
210 364d8905 2021-11-07 op
211 364d8905 2021-11-07 op ;;;###autoload
212 364d8905 2021-11-07 op (add-to-list 'auto-mode-alist '("\\.a68\\'" . a68-mode))
213 364d8905 2021-11-07 op
214 48415058 2021-11-07 op (provide 'a68-mode)
215 48415058 2021-11-07 op ;;; a68-mode.el ends here