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
|
;;; sol-mode.el --- Major mode for the Sol relational language -*- lexical-binding: t; -*-
;; Author: Marcos Magueta
;; Keywords: languages, logic, relational
;; Version: 0.2.0
;; Package-Requires: ((emacs "26.1"))
;;; Commentary:
;; A major mode for Sol, a total relational language with Smalltalk-like
;; message syntax. It provides:
;;
;; - Syntax highlighting, one face per token category: directives,
;; logical combinators, reserved labels, generic labels, relation
;; heads, variables, references (#Name), literals, delimiters.
;; - Navigation: imenu over Relations, Rules and Queries;
;; beginning/end-of-defun over top-level forms.
;; - An inferior Sol REPL (`run-sol'); `sol-load-file' (C-c C-l) loads
;; the current file into the session, `sol-send-line' (C-c C-e)
;; sends the statement at point, `sol-run-buffer' (C-c C-k) runs the
;; file in a compilation buffer.
;; - Eglot wiring: `sol lsp' serves diagnostics, so admission errors
;; (totality, schemas, integrity) appear while typing.
;;
;; Installation:
;;
;; (add-to-list 'load-path "/path/to/sol2/editors/emacs")
;; (require 'sol-mode)
;;
;; Files ending in .sol open in `sol-mode' automatically.
;;; Code:
(require 'comint)
(defgroup sol nil
"Major mode for the Sol relational language."
:group 'languages
:prefix "sol-")
(defcustom sol-indent-offset 2
"Number of columns to indent per bracket nesting level."
:type 'integer
:group 'sol)
(defcustom sol-program "sol"
"Path to the sol executable, used by the REPL, the runner and eglot."
:type 'string
:group 'sol)
;;; ----------------------------------------------------------------------
;;; Faces -- one colour per token category.
;;; ----------------------------------------------------------------------
(defface sol-keyword-face
'((((background dark)) (:foreground "#c678dd" :weight bold))
(((background light)) (:foreground "#a626a4" :weight bold)))
"Face for top-level directives: Relation, Rule, Query."
:group 'sol)
(defface sol-combinator-face
'((((background dark)) (:foreground "#56b6c2" :weight bold))
(((background light)) (:foreground "#0184bc" :weight bold)))
"Face for the logical combinators :and, :or and :not."
:group 'sol)
(defface sol-builtin-label-face
'((((background dark)) (:foreground "#e5c07b"))
(((background light)) (:foreground "#b07d00")))
"Face for structural labels: named:, schema:, head:, body:, and kin."
:group 'sol)
(defface sol-label-face
'((((background dark)) (:foreground "#61afef"))
(((background light)) (:foreground "#4078f2")))
"Face for generic attribute labels, e.g. value:, left:, age:."
:group 'sol)
(defface sol-relation-face
'((((background dark)) (:foreground "#98c379" :weight bold))
(((background light)) (:foreground "#50a14f" :weight bold)))
"Face for relation heads -- the first name after `['."
:group 'sol)
(defface sol-variable-face
'((((background dark)) (:foreground "#e06c75"))
(((background light)) (:foreground "#e45649")))
"Face for logical variables -- capitalised identifiers in term position."
:group 'sol)
(defface sol-reference-face
'((((background dark)) (:foreground "#d19a66" :weight bold))
(((background light)) (:foreground "#986801" :weight bold)))
"Face for relation references, #Name."
:group 'sol)
(defface sol-number-face
'((((background dark)) (:foreground "#ff79c6"))
(((background light)) (:foreground "#c41a8d")))
"Face for integer and rational literals."
:group 'sol)
(defface sol-bracket-face
'((((background dark)) (:foreground "#828997"))
(((background light)) (:foreground "#9ca0a4")))
"Face for delimiters and the cons bar |."
:group 'sol)
;;; ----------------------------------------------------------------------
;;; Syntax table.
;;; ----------------------------------------------------------------------
(defvar sol-mode-syntax-table
(let ((table (make-syntax-table)))
;; Line comments start with % and run to end of line.
(modify-syntax-entry ?% "<" table)
(modify-syntax-entry ?\n ">" table)
;; Strings.
(modify-syntax-entry ?\" "\"" table)
(modify-syntax-entry ?\\ "\\" table)
;; The delimiter law: [] propositions, {} records, () grouping,
;; #( ... ) sequences. All pair for navigation.
(modify-syntax-entry ?\[ "(]" table)
(modify-syntax-entry ?\] ")[" table)
(modify-syntax-entry ?\{ "(}" table)
(modify-syntax-entry ?\} "){" table)
(modify-syntax-entry ?\( "()" table)
(modify-syntax-entry ?\) ")(" table)
;; # prefixes references and sequences.
(modify-syntax-entry ?# "'" table)
;; Identifier constituents.
(modify-syntax-entry ?_ "_" table)
;; Punctuation used as token glue.
(modify-syntax-entry ?: "." table)
(modify-syntax-entry ?| "." table)
(modify-syntax-entry ?/ "." table)
table)
"Syntax table for `sol-mode'.")
;;; ----------------------------------------------------------------------
;;; Font lock. Order matters: earlier entries win.
;;; ----------------------------------------------------------------------
(defconst sol-font-lock-keywords
(list
;; 1. Top-level directives.
'("^[[:space:]]*\\(Relation\\|Rule\\|Query\\)\\_>" 1 'sol-keyword-face)
;; 2. Logical combinators.
'("\\(:and\\|:or\\|:not\\)\\_>" 1 'sol-combinator-face)
;; 3. Relation references, #Name.
'("\\(#[A-Za-z][A-Za-z0-9_]*\\)" 1 'sol-reference-face)
;; 4. Structural labels.
'("\\_<\\(named\\|schema\\|name\\|head\\|body\\|expression\\|domain\\|variable\\|quantifier\\):"
1 'sol-builtin-label-face)
;; 5. Relation head: the first name after `['.
'("\\[[[:space:]]*\\([A-Za-z][A-Za-z0-9_]*\\)" 1 'sol-relation-face)
;; 6. Generic attribute labels.
'("\\_<\\([A-Za-z_][A-Za-z0-9_]*\\):" 1 'sol-label-face)
;; 7. Logical variables: capitalised identifiers in term position.
'("\\_<\\([A-Z][A-Za-z0-9_]*\\)\\_>" 1 'sol-variable-face)
;; 8. Numeric literals: integers and rationals.
'("\\_<-?[0-9]+\\(?:/[0-9]+\\)?\\_>" 0 'sol-number-face)
;; 9. Delimiters.
'("[][}{|]" 0 'sol-bracket-face))
"Font-lock highlighting for `sol-mode'.")
(defvar sol-prettify-symbols-alist
'((":and" . ?∧)
(":or" . ?∨)
(":not" . ?¬))
"Symbol prettification for `sol-mode'.")
;;; ----------------------------------------------------------------------
;;; Navigation.
;;; ----------------------------------------------------------------------
(defconst sol-defun-regexp
"^[[:space:]]*\\(?:Relation\\|Rule\\|Query\\)\\_>\\|^[[:space:]]*\\["
"Regexp matching the start of a top-level Sol form: a directive or a fact.")
(defvar sol-imenu-generic-expression
(list
(list "Relation"
"\\_<Relation\\_>[[:space:]]+named:[[:space:]]*\\([A-Za-z][A-Za-z0-9_]*\\)" 1)
(list "Rule"
"\\_<Rule\\_>[[:space:]]+name:[[:space:]]*\"\\([^\"]+\\)\"" 1)
(list "Query"
"\\_<Query\\_>[[:space:]]+expression:[[:space:]]*\\(?:(\\|\\[\\)[[:space:]]*\\([A-Za-z][A-Za-z0-9_]*\\)" 1))
"Imenu configuration for `sol-mode'.")
(defun sol-beginning-of-defun (&optional arg)
"Move backward to the start of a top-level Sol form.
With ARG, repeat that many times. Returns non-nil on success."
(setq arg (or arg 1))
(let ((found nil))
(while (> arg 0)
(setq found (re-search-backward sol-defun-regexp nil t))
(setq arg (1- arg)))
(when found
(goto-char (match-beginning 0))
(back-to-indentation))
found))
(defun sol-end-of-defun ()
"Move to the end of the current top-level Sol form."
(when (looking-at-p sol-defun-regexp)
(forward-char 1))
(if (re-search-forward sol-defun-regexp nil 'move)
(progn
(goto-char (match-beginning 0))
(skip-chars-backward " \t\n"))
(goto-char (point-max))))
;;; ----------------------------------------------------------------------
;;; Indentation: bracket-depth based, conservative.
;;; ----------------------------------------------------------------------
(defun sol-indent-line ()
"Indent the current line by its bracket nesting depth."
(let* ((bol (line-beginning-position))
(depth (car (syntax-ppss bol))))
(save-excursion
(back-to-indentation)
(when (and (> depth 0) (looking-at-p "[])}]"))
(setq depth (1- depth))))
(if (> depth 0)
(indent-line-to (* depth sol-indent-offset))
'noindent)))
;;; ----------------------------------------------------------------------
;;; Inferior Sol: the REPL, through comint.
;;; ----------------------------------------------------------------------
(defvar inferior-sol-buffer "*sol*"
"Buffer name of the inferior Sol process.")
(define-derived-mode inferior-sol-mode comint-mode "Inferior Sol"
"Major mode for the Sol REPL."
(setq comint-prompt-regexp "^sol> ")
(setq comint-prompt-read-only t))
;;;###autoload
(defun run-sol ()
"Start the Sol REPL, or pop to it if it is already running."
(interactive)
(unless (comint-check-proc inferior-sol-buffer)
(with-current-buffer (make-comint "sol" sol-program)
(inferior-sol-mode)))
(pop-to-buffer inferior-sol-buffer))
(defun sol--send (statement)
"Send STATEMENT to the REPL, starting it if needed."
(run-sol)
(with-current-buffer inferior-sol-buffer
(goto-char (point-max))
(insert statement)
(comint-send-input)))
(defun sol-send-line ()
"Send the current line (one statement) to the REPL."
(interactive)
(sol--send (string-trim (or (thing-at-point 'line t) ""))))
(defun sol-load-file ()
"Load the current file into the REPL session with :load.
The load is atomic: a rejected file changes nothing in the session."
(interactive)
(when (buffer-modified-p) (save-buffer))
(sol--send (format ":load %s" buffer-file-name)))
(defun sol-run-buffer ()
"Run the current file with sol, showing answers in a compilation buffer."
(interactive)
(when (buffer-modified-p) (save-buffer))
(compile (format "%s %s" sol-program (shell-quote-argument buffer-file-name))))
;;; ----------------------------------------------------------------------
;;; Eglot: diagnostics from `sol lsp'.
;;; ----------------------------------------------------------------------
(defvar eglot-server-programs)
(with-eval-after-load 'eglot
(add-to-list 'eglot-server-programs
`(sol-mode . (,sol-program "lsp"))))
;;; ----------------------------------------------------------------------
;;; Keymap.
;;; ----------------------------------------------------------------------
(defvar sol-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-z") #'run-sol)
(define-key map (kbd "C-c C-l") #'sol-load-file)
(define-key map (kbd "C-c C-e") #'sol-send-line)
(define-key map (kbd "C-c C-k") #'sol-run-buffer)
(define-key map (kbd "C-c C-j") #'imenu)
map)
"Keymap for `sol-mode'.")
;;; ----------------------------------------------------------------------
;;; Mode definition.
;;; ----------------------------------------------------------------------
;;;###autoload
(define-derived-mode sol-mode prog-mode "Sol"
"Major mode for editing Sol relational programs.
\\{sol-mode-map}"
:syntax-table sol-mode-syntax-table
(setq-local font-lock-defaults '(sol-font-lock-keywords))
;; Comments.
(setq-local comment-start "% ")
(setq-local comment-end "")
(setq-local comment-start-skip "%+[ \t]*")
;; Navigation.
(setq-local beginning-of-defun-function #'sol-beginning-of-defun)
(setq-local end-of-defun-function #'sol-end-of-defun)
(setq-local imenu-generic-expression sol-imenu-generic-expression)
(setq-local imenu-case-fold-search nil)
;; Indentation.
(setq-local indent-line-function #'sol-indent-line)
(setq-local indent-tabs-mode nil)
;; Eye-candy.
(setq-local prettify-symbols-alist sol-prettify-symbols-alist))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.sol\\'" . sol-mode))
(provide 'sol-mode)
;;; sol-mode.el ends here
|