raz / editor / emacs / raz-mode.el

268 lines · 10.3 KiB · raw · history · last change 1a0e8dde

1;;; raz-mode.el --- a major mode for raz -*- lexical-binding: t -*-
2
3;; Colours raz's reserved words, basic types, built-in procedures,
4;; numbers, characters, strings, raw strings over lines and nested
5;; comments, and indents by
6;; `raz-indent-offset': a block's statements one level in from the
7;; word that opens it; end, until, else and elsif at that word's
8;; level; match arms at the level of match; procedures, sections
9;; (const, type, var) and module bodies at the left margin. The text
10;; of a comment or of a raw string, and a line that goes on with the
11;; expression of the line before, keep the indent they have.
12;;
13;; (add-to-list 'load-path "/path/to/raz/editor/emacs")
14;; (require 'raz-mode)
15
16;;; Code:
17
18(defgroup raz nil "The raz programming language." :group 'languages)
19
20(defcustom raz-indent-offset 4
21 "Columns that a block's statements are indented by."
22 :type 'integer :group 'raz)
23
24(defconst raz-keywords
25 '("and" "begin" "break" "by" "code" "const" "continue" "defer" "div" "do" "else"
26 "elsif" "end" "extern" "for" "forward" "if" "import" "in" "link"
27 "match" "mod" "module" "not" "of" "or" "packed" "proc" "record"
28 "repeat" "return" "set" "shl" "shr" "then" "to" "try" "type" "until"
29 "var" "variant" "VOLATILE" "while" "xor"))
30
31(defconst raz-types
32 '("I8" "I16" "I32" "I64" "INT" "UINT" "U8" "U16" "U32" "U64" "BOOL" "F32" "F64" "ERROR"))
33
34(defconst raz-builtins
35 '("SIZE" "OFFSET" "ADR" "LEN" "PTR" "BYTES" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD"
36 "ARGC" "ARGV" "INC" "DEC" "COPY" "FILL" "ROL" "ROR" "MIN" "MAX" "ABS"
37 "CLZ" "CTZ" "POPCOUNT"))
38
39(defvar raz-mode-syntax-table
40 (let ((st (make-syntax-table)))
41 ;; (* ... *), nested
42 (modify-syntax-entry ?\( "()1n" st)
43 (modify-syntax-entry ?\) ")(4n" st)
44 (modify-syntax-entry ?* ". 23n" st)
45 (modify-syntax-entry ?\" "\"" st)
46 (modify-syntax-entry ?\' "\"" st)
47 (modify-syntax-entry ?\\ "\\" st)
48 (modify-syntax-entry ?` "." st)
49 (modify-syntax-entry ?_ "_" st)
50 (dolist (c '(?+ ?- ?/ ?= ?# ?< ?> ?: ?\; ?, ?. ?^ ?| ?& ?~))
51 (modify-syntax-entry c "." st))
52 st)
53 "Syntax table for `raz-mode'.")
54
55(defvar raz-font-lock-keywords
56 `((,(regexp-opt raz-keywords 'symbols) . font-lock-keyword-face)
57 (,(regexp-opt raz-types 'symbols) . font-lock-type-face)
58 (,(regexp-opt raz-builtins 'symbols) . font-lock-builtin-face)
59 (,(regexp-opt '("true" "false" "nil") 'symbols) . font-lock-constant-face)
60 ;; 123, 0FFH, 1.5, 2.0E-3
61 ("\\_<[0-9][0-9A-F]*H\\_>\\|\\_<[0-9]+\\(?:\\.[0-9]+\\(?:E[-+]?[0-9]+\\)?\\)?\\_>"
62 . font-lock-constant-face))
63 "What `raz-mode' colours, besides strings and comments.")
64
65;;; Raw strings
66
67(defun raz--raw-string ()
68 "Make the backquote just matched open a raw string, if it isn't in a
69comment or string: it and the closing one string fences, and the
70backslashes between them nothing special, since a raw string has no
71escapes."
72 (let ((open (match-beginning 0)))
73 (unless (nth 8 (save-excursion (syntax-ppss open)))
74 (put-text-property open (1+ open) 'syntax-table (string-to-syntax "|"))
75 (when (search-forward "`" nil t)
76 (put-text-property (1- (point)) (point) 'syntax-table (string-to-syntax "|"))
77 (save-excursion
78 (let ((close (1- (point))))
79 (goto-char open)
80 (while (search-forward "\\" close t)
81 (put-text-property (1- (point)) (point) 'syntax-table (string-to-syntax ".")))))))))
82
83(defconst raz--syntax-propertize
84 (syntax-propertize-rules ("`" (0 (ignore (raz--raw-string))))))
85
86;;; Indenting
87
88(defconst raz--decl
89 "^[ \t]*\\(?:module\\|import\\|link\\|extern\\|proc\\|const\\|type\\|var\\|begin\\|code\\)\\_>")
90(defconst raz--leading "^[ \t]*\\(?:or\\|and\\)\\_>")
91(defconst raz--block-word
92 "\\_<\\(if\\|while\\|for\\|match\\|record\\|variant\\|begin\\|repeat\\|code\\|end\\|until\\)\\_>")
93
94(defun raz--line (n)
95 "The text of line N."
96 (save-excursion
97 (goto-char (point-min))
98 (forward-line (1- n))
99 (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
100
101(defun raz--indent-of (n)
102 (save-excursion (goto-char (point-min)) (forward-line (1- n)) (current-indentation)))
103
104(defun raz--in-comment (n)
105 "Whether line N starts inside a comment that began before it."
106 (save-excursion
107 (goto-char (point-min))
108 (forward-line (1- n))
109 (back-to-indentation)
110 (and (nth 4 (syntax-ppss))
111 (not (looking-at "(\\*")))))
112
113(defun raz--in-raw (n)
114 "Whether line N starts inside a raw string, which only a raw string
115can, since it alone runs over lines."
116 (save-excursion
117 (goto-char (point-min))
118 (forward-line (1- n))
119 (eq (nth 3 (syntax-ppss)) t)))
120
121(defun raz--prev (n)
122 "The last line before N that isn't blank, or 0."
123 (let ((l (1- n)))
124 (while (and (> l 0) (string-match-p "\\`[ \t]*\\'" (raz--line l)))
125 (setq l (1- l)))
126 l))
127
128(defun raz--code (n)
129 "Line N's code, without strings, characters and comments."
130 (let ((line (raz--line n)))
131 (when (raz--in-comment n)
132 (setq line (if (string-match "\\*)" line) (substring line (match-end 0)) "")))
133 ;; and inside a raw string, after the string
134 (when (raz--in-raw n)
135 (setq line (if (string-match "`" line) (concat "0" (substring line (match-end 0))) "")))
136 ;; one pattern, so that whichever comes first is taken
137 (setq line (replace-regexp-in-string
138 "\"\\(?:[^\"\\\\]\\|\\\\.\\)*\"\\|'\\(?:[^'\\\\]\\|\\\\.\\)*'\\|`[^`]*`\\|(\\*.*?\\*)"
139 (lambda (m) (if (string-prefix-p "(" m) "" "0")) line t t))
140 (setq line (replace-regexp-in-string "(\\*.*\\'" "" line t t))
141 (setq line (replace-regexp-in-string "`.*\\'" "0" line t t))
142 (string-trim line)))
143
144(defun raz--count (re s)
145 (let ((n 0) (i 0))
146 (while (string-match re s i)
147 (setq n (1+ n) i (match-end 0)))
148 n))
149
150(defun raz--continues (n)
151 "Whether the expression on line N goes on to the next line."
152 (let ((code (raz--code n)))
153 (or (> (raz--count "[([{]" code) (raz--count "[])}]" code))
154 (string-match-p "\\(?:,\\|:=\\|[-+*/&|=#<>]\\|\\_<and\\|\\_<or\\)\\'" code))))
155
156(defun raz--start (n)
157 "The first line of the statement that ends on line N."
158 (let ((l n) (done nil))
159 (while (not done)
160 (let ((p (raz--prev l)))
161 (if (or (= p 0)
162 (and (not (raz--continues p))
163 (not (string-match-p raz--leading (raz--line l)))
164 (not (raz--in-raw l))))
165 (setq done t)
166 (setq l p))))
167 l))
168
169(defun raz--opens (code)
170 "How many blocks CODE opens, less those it closes."
171 (let ((n 0) (i 0) (case-fold-search nil))
172 (while (string-match raz--block-word code i)
173 (setq n (if (member (match-string 1 code) '("end" "until")) (1- n) (1+ n))
174 i (match-end 0)))
175 n))
176
177(defun raz--opener (n)
178 "The indent of the word that opens the block line N is in, or nil."
179 (save-excursion
180 (goto-char (point-min))
181 (forward-line (1- n))
182 (let ((depth 0) (found nil) (case-fold-search nil))
183 (while (and (not found) (re-search-backward raz--block-word nil t))
184 (unless (nth 8 (syntax-ppss))
185 (if (member (match-string 1) '("end" "until"))
186 (setq depth (1+ depth))
187 (if (= depth 0)
188 (setq found (current-indentation))
189 (setq depth (1- depth))))))
190 found)))
191
192(defun raz--comment-start (n)
193 "Where the comment that line N, holding only comment, began."
194 (let ((l n))
195 (while (and (> l 1) (not (string-match-p "(\\*" (raz--line l))))
196 (setq l (1- l)))
197 l))
198
199(defun raz-calculate-indent ()
200 "The column line at point should start at, or nil to leave it."
201 (let* ((case-fold-search nil)
202 (n (line-number-at-pos))
203 (cur (raz--line n))
204 (prev (raz--prev n)))
205 (catch 'done
206 (when (or (raz--in-comment n) (raz--in-raw n)) (throw 'done nil))
207 (when (and (string-match-p raz--decl cur) (not (string-match-p "^[ \t]*proc[ \t]*(" cur)))
208 (throw 'done 0))
209 ;; a comment before a declaration goes with it, at the margin
210 (when (string-match-p "^[ \t]*(\\*" cur)
211 (let ((l n) (last (line-number-at-pos (point-max))))
212 (while (and (< l last) (not (string-match-p "\\*)" (raz--line l))))
213 (setq l (1+ l)))
214 (setq l (1+ l))
215 (while (and (<= l last) (string-match-p "\\`[ \t]*\\'" (raz--line l)))
216 (setq l (1+ l)))
217 (when (and (<= l last) (string-match-p raz--decl (raz--line l)))
218 (throw 'done 0))))
219 (when (string-match-p "^[ \t]*\\(?:end\\|until\\)\\_>" cur)
220 (throw 'done (or (raz--opener n) 0)))
221 (when (= prev 0) (throw 'done 0))
222 (when (string-match-p "^[ \t]*\\(?:\\(?:else\\|elsif\\)\\_>\\||\\)" cur)
223 (throw 'done (or (raz--opener n) (raz--indent-of prev))))
224 (when (or (raz--continues prev) (string-match-p raz--leading cur))
225 (throw 'done nil))
226 (when (string= (raz--code prev) "")
227 (throw 'done (raz--indent-of (raz--comment-start prev))))
228 (let* ((start (raz--start prev))
229 (ind (raz--indent-of start))
230 (code (mapconcat #'raz--code (number-sequence start prev) " ")))
231 (cond
232 ((or (string-match-p "\\`\\(?:const\\|type\\|var\\)\\'" code)
233 (and (string-match-p "\\`else\\_>" code) (>= (raz--opens code) 0))
234 (string-match-p "\\`|.*:\\'" code)
235 (string-match-p "\\`elsif\\_>.*\\_<then\\'" code))
236 (+ ind raz-indent-offset))
237 ((or (string-match-p "\\`|" code) (string-match-p "\\_<match\\_>.*\\_<of\\'" code))
238 ind)
239 ((> (raz--opens code) 0) (+ ind raz-indent-offset))
240 (t ind))))))
241
242(defun raz-indent-line ()
243 "Indent the current line as raz's blocks say."
244 (interactive)
245 (let ((col (save-excursion (raz-calculate-indent)))
246 (offset (- (current-column) (current-indentation))))
247 (when col
248 (indent-line-to col)
249 (when (> offset 0) (move-to-column (+ col offset))))))
250
251;;;###autoload
252(define-derived-mode raz-mode prog-mode "raz"
253 "Major mode for editing raz."
254 :syntax-table raz-mode-syntax-table
255 (setq-local font-lock-defaults '(raz-font-lock-keywords nil nil))
256 (setq-local syntax-propertize-function raz--syntax-propertize)
257 (setq-local comment-start "(* ")
258 (setq-local comment-end " *)")
259 (setq-local comment-start-skip "(\\*+[ \t]*")
260 (setq-local indent-tabs-mode nil)
261 (setq-local indent-line-function #'raz-indent-line))
262
263;;;###autoload
264(add-to-list 'auto-mode-alist '("\\.raz\\'" . raz-mode))
265
266(provide 'raz-mode)
267
268;;; raz-mode.el ends here