-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
zetz-mode.el
464 lines (389 loc) · 15.9 KB
/
zetz-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
;;; zetz-mode.el --- A major mode for the ZetZ programming language -*- lexical-binding: t; -*-
;; Copyright (c) 2020 Damon Kwok
;; Authors: Damon Kwok <damon-kwok@outlook.com>
;; Version: 0.0.1
;; URL: https://github.com/damon-kwok/zetz-mode
;; Keywords: languages programming
;; Package-Requires: ((emacs "25.1") (dash "2.17.0") (hydra "0.15.0"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Description:
;;
;; This is a major mode for the ZetZ programming language
;;
;; For more details, see the project page at
;; https://github.com/damon-kwok/zetz-mode
;;
;; Installation:
;;
;; The simple way is to use package.el:
;;
;; M-x package-install zetz-mode
;;
;; Or, copy zetz-mode.el to some location in your Emacs load
;; path. Then add "(require 'zetz-mode)" to your Emacs initialization
;; (.emacs, init.el, or something).
;;
;; Example config:
;;
;; (require 'zetz-mode)
;;; Code:
(require 'cl-lib)
(require 'subr-x)
(require 'js)
(require 'dash)
(require 'xref)
(require 'hydra)
(require 'imenu)
(require 'easymenu)
(defvar zetz-mode-hook nil)
(defconst zetz-mode-syntax-table
(let ((table (make-syntax-table)))
;; fontify " using zetz-keywords
;; Operators
(dolist (i '(?+ ?- ?* ?/ ?% ?& ?| ?= ?! ?< ?>))
(modify-syntax-entry i "." table))
;; / is punctuation, but // is a comment starter
(modify-syntax-entry ?/ ". 124" table)
;; /* */ comments, which can be nested
(modify-syntax-entry ?* ". 23bn" table)
;; \n is a comment ender
(modify-syntax-entry ?\n ">" table)
;; string
(modify-syntax-entry ?\' "\"" table)
(modify-syntax-entry ?\" "\"" table)
;; Don't treat underscores as whitespace
(modify-syntax-entry ?_ "w" table) table))
(defvar zetz-mode-map
(let ((map (make-keymap)))
(define-key map "\C-j" 'newline-and-indent) map)
"Keymap for ZetZ major mode.")
(defconst zetz-keywords
'("if" "else" "switch" "case" "while" "for" "do" ;
"default" "sizeof")
"ZetZ language keywords.")
(defconst zetz-preprocessor-keywords
'("using" ;
"export" "pub" ;
"inline" "packed")
"ZetZ declaration keywords.")
(defconst zetz-declaration-keywords
'("let" ;
"enum" "struct" "theory" ;
"symbol" ;
"fn" "macro" "closure")
"ZetZ declaration keywords.")
(defconst zetz-careful-keywords
'("new" ;
"goto" "continue" "break" "return" ;
"mut" "mutable" "unsafe" ;
"is" "as" "needs" ;
"test" ;
"assert" "assert2" "assert3" "assert4" ;
"static_attest" "static_assert" "nullterm" ;
"where" "model" ;
"const" "static" "atomic" "thread_local")
"ZetZ language careful keywords.")
(defconst zetz-builtin-keywords
'("u8" "i8" "u16" "i16" "u32" "i32" "u64" "i64" ;
"isize" "usize" "int" "uint" "f32" "f64" "bool" ;
"void" "char" "byte")
"ZetZ language keywords.")
(defconst zetz-constants '("false" "true" "self")
"Common constants.")
(defconst zetz-operator-functions '("len" "sizeof" "safe" )
"ZetZ language operators functions.")
;;; create the regex string for each class of keywords
(defconst zetz-keywords-regexp (regexp-opt zetz-keywords 'words)
"Regular expression for matching keywords.")
(defconst zetz-declaration-keywords-regexp
(regexp-opt zetz-declaration-keywords 'words)
"Regular expression for matching declaration keywords.")
(defconst zetz-preprocessor-keywords-regexp
(regexp-opt zetz-preprocessor-keywords 'words)
"Regular expression for matching preprocessor keywords.")
(defconst zetz-careful-keywords-regexp (regexp-opt zetz-careful-keywords 'words)
"Regular expression for matching careful keywords.")
(defconst zetz-builtin-keywords-regexp (regexp-opt zetz-builtin-keywords 'words)
"Regular expression for matching builtin type.")
(defconst zetz-constant-regexp (regexp-opt zetz-constants 'words)
"Regular expression for matching constants.")
(defconst zetz-operator-functions-regexp
(regexp-opt zetz-operator-functions 'words)
"Regular expression for matching operator functions.")
(defconst zetz-font-lock-keywords
`(
;; builtin
(,zetz-builtin-keywords-regexp . font-lock-builtin-face)
;; careful
(,zetz-careful-keywords-regexp . font-lock-warning-face)
;; declaration
(,zetz-declaration-keywords-regexp . font-lock-keyword-face)
;; preprocessor
(,zetz-preprocessor-keywords-regexp . font-lock-preprocessor-face)
;; keywords
(,zetz-keywords-regexp . font-lock-keyword-face) ;;font-lock-keyword-face
;; delimiter: modifier
("\\(->\\|=>\\|\\.>\\|:>\\|::\\||\\)" 1 'font-lock-keyword-face)
;; delimiter: . , ; separate
("\\($?[.,;]+\\)" 1 'font-lock-comment-delimiter-face)
;; delimiter: operator symbols
("\\($?[+-/*//%~=<>]+\\)$?,?" 1 'font-lock-negation-char-face)
("\\($?[?^!&]+\\)" 1 'font-lock-warning-face)
;; delimiter: = : separate
("[^+-/*//%~^!=<>]\\([=:]\\)[^+-/*//%~^!=<>]" 1
'font-lock-comment-delimiter-face)
;; delimiter: brackets
("\\(\\[\\|\\]\\|[(){}]\\)" 1 'font-lock-comment-delimiter-face)
;; operator methods
(,zetz-operator-functions-regexp . font-lock-builtin-face)
;; C
;; ("#\\(?:include\\|if\\|def\\|undef\\|else\\|elif\\|endif\\)" . 'font-lock-builtin-face)
("#\\([A-Za-z ]+\\)" . 'font-lock-builtin-face)
;; method definitions
;; ("\\(?:fn\\)\s+\\($?[a-z_][A-Za-z0-9_]*\\)" 1 'font-lock-function-name-face)
;; type
;; ("\\(?:struct\\|trait\\|type\\)\s+\\($?_?[A-Z][A-Za-z0-9_]*\\)" 1 'font-lock-type-face)
("\\([A-Z][A-Za-z0-9_]*\\)" 1 'font-lock-type-face)
("\\([A-Za-z0-9_]*_t\\)" 1 'font-lock-type-face)
("\\([A-Za-z0-9_]*\\)::" 1 'font-lock-type-face)
;; constants references
(,zetz-constant-regexp . font-lock-constant-face)
;; @
("@[A-Za-z_]*[A-Z-a-z0-9_]*" . 'font-lock-builtin-face)
;; method references
("\\([a-z_]$?[a-z0-9_]?+\\)$?[ \t]?(+" 1 'font-lock-function-name-face)
;; parameter
;; ("\\(?:(\\|,\\)\\([a-z_][a-z0-9_']*\\)\\([^ \t\r\n,:)]*\\)" 1
;; 'font-lock-variable-name-face)
;; ("\\(?:(\\|,\\)[ \t]+\\([a-z_][a-z0-9_']*\\)\\([^ \t\r\n,:)]*\\)" 1
;; 'font-lock-variable-name-face)
;; tuple references
("[.]$?[ \t]?\\($?_[1-9]$?[0-9]?*\\)" 1 'font-lock-variable-name-face)
;; character literals
("\\('[\\].'\\)" 1 'font-lock-constant-face)
;; numeric literals
("[ \t/+-/*//=><([,;]\\([0-9]+[0-9a-zA-Z_]*\\)+" 1
'font-lock-constant-face)
;; variable references
("\\([a-z_]+[a-z0-9_']*\\)+" 1 'font-lock-variable-name-face))
"An alist mapping regexes to font-lock faces.")
(defun zetz-project-root-p (path)
"Return t if directory `PATH' is the root of the ZetZ project."
(let* ((files '("zz.toml" "make.bat" "Makefile" ;
"Dockerfile" ".editorconfig" ".gitignore" ;
".git" ".svn" ".hg" ".bzr"))
(foundp nil))
(while (and (> (length files) 0)
(not foundp))
(let* ((filename (car files))
(filepath (concat (file-name-as-directory path) filename)))
(setq files (cdr files))
(setq foundp (file-exists-p filepath)))) ;
foundp))
(defun zetz-project-root
(&optional
path)
"Return the root of the ZetZ project.
Optional argument PATH: project path."
(let* ((bufdir (if buffer-file-name ;
(file-name-directory buffer-file-name) default-directory))
(curdir (if path (file-name-as-directory path) bufdir))
(parent (file-name-directory (directory-file-name curdir))))
(if (or (not parent)
(string= parent curdir)
(string= parent (file-name-as-directory (getenv "HOME")))
(string= parent "/")
(zetz-project-root-p curdir)) ;
curdir ;
(zetz-project-root parent))))
(defun zetz-project-name ()
"Return ZetZ project name."
(file-name-base (directory-file-name (zetz-project-root))))
(defun zetz-project-file-exists-p (filename)
"Return t if file `FILENAME' exists."
(file-exists-p (concat (zetz-project-root) filename)))
(defun zetz-run-command (command &optional path)
"Return `COMMAND' in the root of the ZetZ project.
Optional argument PATH: project path."
(let ((oldir default-directory))
(setq default-directory (if path path (zetz-project-root path)))
(compile command)
(setq default-directory oldir)))
(defun zetz-project-build ()
"Build project with veronac."
(interactive)
(if (zetz-project-file-exists-p "Makefile")
(zetz-run-command "make")
(zetz-run-command "veronac .")))
(defun zetz-project-open ()
"Open `zz.toml' file."
(interactive)
(if (zetz-project-file-exists-p "zz.toml")
(find-file (concat (zetz-project-root) "zz.toml"))))
(defun zetz-buffer-dirname ()
"Return current buffer directory file name."
(directory-file-name (if buffer-file-name (file-name-directory
buffer-file-name)
default-directory)))
(defun zetz-project-run ()
"Run project."
(interactive)
(let* ((bin1 (concat (zetz-project-root) "bin/" (zetz-project-name)))
(bin2 (concat (zetz-project-root) "/" (zetz-project-name)))
(bin3 (concat (zetz-buffer-dirname) "/" (zetz-project-name))))
(cond ((file-exists-p bin1)
(zetz-run-command bin1))
((file-exists-p bin2)
(zetz-run-command bin2))
((file-exists-p bin2)
(zetz-run-command bin3)))))
(easy-menu-define zetz-mode-menu zetz-mode-map ;
"Menu for ZetZ mode." ;
'("ZetZ" ;
["Build" zetz-project-build t] ;
["Run" zetz-project-run t] ;
"---" ;
("Community" ;
["Home" (zetz-run-command "xdg-open https://github.com/zetzit/zz") t]
["ZetZ Tweets" ;;
(zetz-run-command "xdg-open https://twitter.com/zetztweets") t])))
(defun zetz-banner-default ()
"ZetZ banner."
"
_
| |
_______| |_ ____
|_ / _ \\ __|_ /
/ / __/ |_ / /
/___\\___|\\__/___|
")
(defhydra zetz-hydra-menu
(:color blue
:hint none)
"
%s(zetz-banner-default)
Project | _b_: Build _r_: Run
Community | _1_: Home _2_: News
_q_: Quit" ;
("b" zetz-project-build "Build")
("r" zetz-project-run "Run")
("1" (zetz-run-command "xdg-open https://github.com/zetzit/zz") "Home")
("2" (zetz-run-command "xdg-open https://twitter.com/zetztweets") "News")
("q" nil "Quit"))
(defun zetz-menu ()
"Open ZetZ hydra menu."
(interactive)
(zetz-hydra-menu/body))
(defun zetz-build-tags ()
"Build tags for current project."
(interactive)
(let ((tags-buffer (get-buffer "TAGS"))
(tags-buffer2 (get-buffer (format "TAGS<%s>" (zetz-project-name)))))
(if tags-buffer (kill-buffer tags-buffer))
(if tags-buffer2 (kill-buffer tags-buffer2)))
(let* ((zetz-path (string-trim (shell-command-to-string "which zz")))
(zetz-executable ;
(string-trim (shell-command-to-string (concat "readlink -f "
zetz-path))))
(packages-path ;
(expand-file-name (concat (file-name-directory zetz-executable)
"../modules")))
(ctags-params ;
(concat "ctags --languages=-zetz --langdef=zetz --langmap=zetz:.zz "
"--regex-zetz=/[ \\t]*fn[ \\t]+([a-zA-Z0-9_]+)/\\1/f,fn/ "
"--regex-zetz=/[ \\t]*macro[ \\t]+([a-zA-Z0-9_]+)/\\1/m,macro/ "
"--regex-zetz=/[ \\t]*theory[ \\t]+([a-zA-Z0-9_]+)/\\1/h,theory/ "
"--regex-zetz=/[ \\t]*closure[ \\t]+([a-zA-Z0-9_]+)/\\1/l,closure/ "
"--regex-zetz=/[ \\t]*symbol[ \\t]+([a-zA-Z0-9_]+)/\\1/y,symbol/ "
"--regex-zetz=/[ \\t]*struct[ \\t]+([a-zA-Z0-9_]+)/\\1/s,sturct/ "
"--regex-zetz=/[ \\t]*test[ \\t]+([a-zA-Z0-9_]+)/\\1/t,test/ " ;
"--regex-zetz=/[ \\t]*enum[ \\t]+([a-zA-Z0-9_]+)/\\1/e,enum/ "
"--regex-zetz=/[ \\t]*const[ \\t]+([a-zA-Z0-9_]+)[ \\t]+([a-zA-Z0-9_]+)[ \\t]*=/\\2/n,const/ "
"-e -R . " packages-path)))
(when (file-exists-p packages-path)
(let ((oldir default-directory))
(setq default-directory (zetz-project-root))
(message "ctags:%s" (shell-command-to-string ctags-params))
(zetz-load-tags)
(setq default-directory oldir)))))
(defun zetz-load-tags
(&optional
build)
"Visit tags table.
Optional argument BUILD If the tags file does not exist, execute the build."
(interactive)
(let* ((tags-file (concat (zetz-project-root) "TAGS")))
(if (file-exists-p tags-file)
(progn (visit-tags-table (concat (zetz-project-root) "TAGS")))
(if build (zetz-build-tags)))))
(defun zetz-after-save-hook ()
"After save hook."
(when (eq major-mode 'zetz-mode)
(shell-command (concat "zz fmt " (buffer-file-name)))
(revert-buffer
:ignore-auto
:noconfirm)
(if (not (executable-find "ctags"))
(message "Could not locate executable '%s'" "ctags")
(zetz-build-tags))))
;;;###autoload
(define-derived-mode zetz-mode prog-mode
"ZetZ"
"Major mode for editing ZetZ files."
:syntax-table zetz-mode-syntax-table
;; (setq-local bidi-paragraph-direction 'left-to-right)
(setq-local require-final-newline mode-require-final-newline)
(setq-local parse-sexp-ignore-comments t)
(setq-local comment-start "/*")
(setq-local comment-end "*/")
(setq-local comment-start-skip "\\(//+\\|/\\*+\\)\\s *")
;;
(setq-local indent-tabs-mode nil)
(setq-local tab-width 4)
(setq-local buffer-file-coding-system 'utf-8-unix)
;;
(setq-local electric-indent-chars (append "{}():;," electric-indent-chars))
(setq-local indent-line-function #'js-indent-line)
(setq-local js-indent-level tab-width)
;;
;; (setq-local font-lock-defaults ;
;; '(zetz-font-lock-keywords ;
;; nil nil nil nil ;
;; (font-lock-syntactic-face-function . zetz-mode-syntactic-face-function)))
(setq-local font-lock-defaults '(zetz-font-lock-keywords))
(font-lock-ensure)
;;
;; (setq-local syntax-propertize-function zetz-syntax-propertize-function)
;;
(setq-local imenu-generic-expression ;;
'(("TODO" ".*TODO:[ \t]*\\(.*\\)$" 1)
("fn" "[ \t]*fn[ \t]+\\([a-zA-Z0-9_]+\\)" 1)
("macro" "[ \t]*macro[ \t]+\\([a-zA-Z0-9_]+\\)" 1)
("theory" "[ \t]*theory[ \t]+\\([a-zA-Z0-9_]+\\)" 1)
("closure" "[ \t]*closure[ \t]+\\([a-zA-Z0-9_]+\\)" 1)
("symbol" "[ \t]*symbol[ \t]+\\([a-zA-Z0-9_]+\\)" 1)
("struct" "[ \t]*struct[ \t]+\\([a-zA-Z0-9_]+\\)" 1)
("test" "[ \t]*test[ \t]+\\([a-zA-Z0-9_]+\\)" 1)
("enum" "[ \t]*enum[ \t]+\\([a-zA-Z0-9_]+\\)" 1)
("const" "[ \t]*const[ \t]+\\(.+\\)[ \t]*=" 1)
("export" "[ \t]*export[ \t]+\\(.*\\)(" 1)
("pub" "[ \t]*pub[ \t]+\\(.*\\)[({]" 1)))
(imenu-add-to-menubar "Index")
;;
(add-hook 'after-save-hook #'zetz-after-save-hook nil t)
(zetz-load-tags))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.zz\\'" . zetz-mode))
;;
(provide 'zetz-mode)
;;; zetz-mode.el ends here