-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipl-mode.el
188 lines (180 loc) · 4.65 KB
/
ipl-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
;;; ipl-mode.el --- Emacs mode for Imandra Protocol Language
;;
;; Copyright (c) 2023 Imandra, Inc.
;;
;; Author: Matt Bray <[email protected]>
;; Author: Nicola Mometto <[email protected]>
;; URL: https://github.com/imandra-ai/ipl-mode
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defconst ipl-builtins-extra
'("None"
"Some"
"add"
"abs"
"delete"
"get"
"getDefault"
"insert"
"intOfString"
"mapAdd"
"remove"
"strLen"
"subset"
"toFloat"
"toInt"
"truncate"))
(defconst ipl-builtins
'("false"
"true"
"||"
"&&"
"!"
"="
">"
"<"
";"))
(defconst ipl-keywords
'(
"TimeStampPrecisions"
"VerificationPacks"
"action"
"alias"
"anonymous"
"assignFrom"
"assignable"
"break"
"case"
"dataset"
"datatype"
"declare"
"default"
"description"
"else"
"enum"
"events"
"extend"
"for"
"forall"
"function"
"if"
"ign"
"ignore"
"imandramarkets"
"import"
"in"
"interLibraryCheck"
"internal"
"invalid"
"invalidfield"
"let"
"library"
"libraryMarker"
"message"
"messageFlows"
"micro"
"milli"
"missingfield"
"name"
"opt"
"optional"
"outbound"
"overloadFunction"
"overrideFieldTypes"
"precision"
"present"
"receive"
"record"
"reject"
"repeating"
"repeatingGroup"
"req"
"require"
"return"
"scenario"
"send"
"service"
"template"
"testfile"
"then"
"unique"
"using"
"valid"
"validate"
"when"
"with"))
(defun ipl--block-indentation ()
(let ((curline (line-number-at-pos)))
(save-excursion
(condition-case nil
(progn
(backward-up-list)
(unless (= curline (line-number-at-pos))
(current-indentation)))
(scan-error nil)))))
(defun ipl--previous-indentation ()
(save-excursion
(forward-line -1)
(let (finish)
(while (not finish)
(cond ((bobp) (setq finish t))
(t
(let ((line (buffer-substring-no-properties
(line-beginning-position) (line-end-position))))
(if (not (string-match-p "\\`\\s-*\\'" line))
(setq finish t)
(forward-line -1))))))
(current-indentation))))
(defun ipl-indent-line ()
(interactive)
(let* ((curpoint (point))
(pos (- (point-max) curpoint)))
(back-to-indentation)
(let ((block-indentation (ipl--block-indentation)))
(delete-region (line-beginning-position) (point))
(if block-indentation
(if (looking-at "[]}]")
(indent-to block-indentation)
(indent-to (+ block-indentation standard-indent)))
(indent-to (ipl--previous-indentation)))
(when (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos))))))
(define-derived-mode ipl-mode
text-mode "IPL"
"Major mode for Imandra Protocol Language."
(progn
(setq comment-start "//")
(setq comment-start-skip "//\\s *")
(setq ipl-highlights
`(("//.*" . font-lock-comment-face)
("/\\*.*\\*/" . font-lock-comment-face)
("@+[A-Za-z.]+:?" . font-lock-preprocessor-face)
("\\(@description:\\)\\(.+\\)" . ((1 font-lock-preprocessor-face)
(2 font-lock-string-face)))
("function\\s-+\\([a-zA-Z0-9]+\\)" . ((1 font-lock-function-name-face)))
("\\(:\\)\\s-*\\([A-Za-z0-9.[:blank:]]+\\)"
. ((1 font-lock-builtin-face)
;; (2 font-lock-type-face)
;; conflicts with case syntax :/
;; we need to use a function to do this properly,
;; as emacs doesn't support look-behind
))
("\\(:[*?]\\)\\s-*\\([A-Za-z0-9.[:blank:]]+\\)"
. ((1 font-lock-builtin-face)
(2 font-lock-type-face)
))
("\"[0-9]+\"\\s-*\\(:\\) *\\([A-Za-z0-9.[:blank:]]+\\)"
. ((1 font-lock-builtin-face)
(2 font-lock-type-face)
))
("\"[^\"]*\"" . font-lock-string-face)
("'[^']*'" . font-lock-string-face)
("[^A-Za-z0-9.]\\([A-Z][A-Za-z0-9.]+\\)\\." . ((1 font-lock-constant-face)))
(,(regexp-opt ipl-keywords 'words) . font-lock-keyword-face)
(,(regexp-opt ipl-builtins ) . font-lock-builtin-face)))
(setq font-lock-defaults '(ipl-highlights))
(set (make-local-variable 'standard-indent) 4)
(set (make-local-variable 'indent-line-function) #'ipl-indent-line)))
(provide 'ipl-mode)