-
Notifications
You must be signed in to change notification settings - Fork 6
/
mint-mode.el
214 lines (172 loc) · 7.6 KB
/
mint-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
;;; mint-mode.el --- Major mode for the Mint programming language -*- coding: utf-8; lexical-binding: t; -*-
;; Author: Diwank Tomer <singh@diwank.name>
;; Maintainer: jgart <jgart@dismail.de>
;; Summary: Major mode for editing .mint files.
;; Version: 2.0.0
;; Homepage: https://github.com/creatorrr/emacs-mint-mode
;; URL: https://github.com/creatorrr/emacs-mint-mode
;; Created: 18 Nov 2018
;; Keywords: mint languages processes convenience tools files
;; Package-Requires: ((emacs "25.1"))
;;; License:
;; mint-mode.el --- major mode for editing .mint files.
;; Copyright (C) 2018 Diwank Tomer <github.com/creatorrr>
;;
;; 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/>.
;; This file is NOT part of GNU Emacs.
;;; Commentary:
;; Major mode for writing programs in mint lang. Provides:
;; - Syntax highlighting
;;
;; For more info on mint, visit: https://mint-lang.com
;;; Code:
(require 'js)
(require 'subr-x)
;; Utils
(defun mint-get-tokens (filename)
"Get tokens defined from data file FILENAME."
(let* ((script-dir (file-name-directory load-file-name))
(filepath (expand-file-name filename script-dir))
(contents (with-temp-buffer
(insert-file-contents-literally filepath)
(buffer-string)) )
(raw-tokens (split-string contents "\n"))
(trimmed-tokens (mapcar 'string-trim raw-tokens))
(string-not-empty-p (lambda (str)
(not (string-empty-p str)))))
;; Return list minus empty lines
(seq-filter string-not-empty-p trimmed-tokens)))
;; For highlighting language tokens
;; Simple
(defvar mint-lang-blocks
(mint-get-tokens "./tokens/lang/blocks.txt"))
(defvar mint-lang-declarators
(mint-get-tokens "./tokens/lang/declarators.txt"))
(defvar mint-lang-initializers
(mint-get-tokens "./tokens/lang/initializers.txt"))
(defvar mint-lang-keywords
(mint-get-tokens "./tokens/lang/keywords.txt"))
(defvar mint-lang-specifiers
(mint-get-tokens "./tokens/lang/specifiers.txt"))
(defvar mint-lang-literal-types
(mint-get-tokens "./tokens/lang/literal-types.txt"))
;; Compound
(defvar mint-lang-compound-types
(mint-get-tokens "./tokens/lang/compound-types.txt"))
(defvar mint-lang-operators
(mint-get-tokens "./tokens/lang/operators.txt"))
;; For highlighting html tags
(defvar mint-html-tags
(mint-get-tokens "./tokens/html/tags.txt"))
;; For highlighting css tokens
(defvar mint-style-colors
(mint-get-tokens "./tokens/style/colors.txt"))
(defvar mint-style-properties
(mint-get-tokens "./tokens/style/properties.txt"))
(defvar mint-style-units
(mint-get-tokens "./tokens/style/units.txt"))
;; All combined
(defvar mint-all-style-tokens
(append mint-style-units mint-style-properties mint-style-colors))
(defvar mint-all-lang-tokens
(append mint-lang-operators mint-lang-compound-types mint-lang-literal-types
mint-lang-specifiers mint-lang-keywords mint-lang-initializers
mint-lang-declarators mint-lang-blocks))
(defvar mint-all-tokens
(append mint-all-lang-tokens mint-all-style-tokens mint-html-tags))
;; Define regular expressions for syntax highlighting
(defvar mint-font-lock-keywords
;; For simple keywords like `do`, `fun` etc.
(let* ((regexp-blocks (regexp-opt mint-lang-blocks 'words))
(regexp-declarators (regexp-opt mint-lang-declarators 'words))
(regexp-initializers (regexp-opt mint-lang-initializers 'words))
(regexp-keywords (regexp-opt mint-lang-keywords 'words))
(regexp-specifiers (regexp-opt mint-lang-specifiers 'words))
(regexp-literal-types (regexp-opt mint-lang-literal-types 'words))
;; For compound type constructors like `Maybe(Number)`
(regexp-compound-type-constructors
(concat (regexp-opt mint-lang-compound-types)
"[[:space:]]*" "("))
;; For compound type classes like `Maybe.just`
(regexp-compound-type-classes
(concat (regexp-opt mint-lang-compound-types)
"\\."))
;; For operators like `=>`
(regexp-operators
(concat "[[:space:]]+"
(regexp-opt mint-lang-operators)
"[[:space:]]*"))
;; For html tag-open (no style applied)
(regexp-html-tag-open
(concat "<" "[[:space:]]*"
(regexp-opt mint-html-tags)
"[[:space:]]*" ">"))
(regexp-html-tag-open-with-attr
(concat "<" "[[:space:]]*"
(regexp-opt mint-html-tags)
"[[:space:]]+" "[a-zA-Z\\-]+"
"[[:space:]]*" "="))
;; For html tag-open (style applied)
(regexp-html-tag-open-with-style
(concat "<" "[[:space:]]*"
(regexp-opt mint-html-tags)
"[[:space:]]*" "::"))
;; For html tag-close
(regexp-html-tag-close
(concat "<" "/" "[[:space:]]*"
(regexp-opt mint-html-tags)
"[[:space:]]*" ">"))
;; ;; For style colors
(regexp-style-colors (regexp-opt mint-style-colors 'words))
;; For style property names
(regexp-style-properties
(concat (regexp-opt mint-style-properties)
"[[:space:]]*" ":"))
;; For style units
(regexp-style-units
(concat "[[:digit:]]+" "[[:space:]]*"
(regexp-opt mint-style-units)))
;; Other misc categories
(regexp-inline-marker "`"))
;; Set font-lock mode face for each category
`((,regexp-blocks . font-lock-constant-face)
(,regexp-declarators . font-lock-constant-face)
(,regexp-initializers . font-lock-type-face)
(,regexp-keywords . font-lock-warning-face)
(,regexp-specifiers . font-lock-builtin-face)
(,regexp-literal-types . font-lock-variable-name-face)
(,regexp-compound-type-constructors . font-lock-type-face)
(,regexp-compound-type-classes . font-lock-string-face)
(,regexp-operators . font-lock-variable-name-face)
(,regexp-html-tag-open . font-lock-variable-name-face)
(,regexp-html-tag-open-with-attr . font-lock-variable-name-face)
(,regexp-html-tag-open-with-style . font-lock-variable-name-face)
(,regexp-html-tag-close . font-lock-variable-name-face)
(,regexp-style-colors . font-lock-constant-face)
(,regexp-style-properties . font-lock-variable-name-face)
(,regexp-style-units . font-lock-builtin-face)
(,regexp-inline-marker . font-lock-warning-face))))
;;;###autoload
(define-derived-mode mint-mode js-jsx-mode "mint mode"
"Major mode for writing programs in the Mint programming language."
;; For correctly formatting ansi terminal color codes
(add-to-list 'comint-output-filter-functions 'ansi-color-process-output)
(add-hook 'compilation-mode-hook 'ansi-color-for-comint-mode-on)
;; code for syntax highlighting
(setq font-lock-defaults '((mint-font-lock-keywords))))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.mint\\'" . mint-mode))
;; add the mode to the `features' list
(provide 'mint-mode)
;;; mint-mode.el ends here