-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathneocaml-ocamllex.el
More file actions
261 lines (211 loc) · 8.81 KB
/
Copy pathneocaml-ocamllex.el
File metadata and controls
261 lines (211 loc) · 8.81 KB
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
;;; neocaml-ocamllex.el --- Major mode for OCamllex files -*- lexical-binding: t; -*-
;; Copyright © 2025-2026 Bozhidar Batsov
;;
;; Author: Bozhidar Batsov <bozhidar@batsov.dev>
;; Maintainer: Bozhidar Batsov <bozhidar@batsov.dev>
;; URL: http://github.com/bbatsov/neocaml
;; Keywords: languages ocaml
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Tree-sitter based major mode for editing OCamllex (.mll) lexer
;; definition files. Provides font-lock for the lexer DSL (rules,
;; regexps, keywords) and, when the OCaml tree-sitter grammar is
;; installed, full syntax highlighting for embedded OCaml code inside
;; { } blocks via language injection.
;;
;; For the tree-sitter grammar this mode is based on,
;; see https://github.com/314eter/tree-sitter-ocamllex.
;;; License:
;; 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Code:
(require 'treesit)
(declare-function neocaml-mode--font-lock-settings "neocaml")
(defgroup neocaml-ocamllex nil
"Major mode for editing OCamllex files with tree-sitter."
:prefix "neocaml-ocamllex-"
:group 'languages
:link '(url-link :tag "GitHub" "https://github.com/bbatsov/neocaml"))
(defcustom neocaml-ocamllex-indent-offset 2
"Number of spaces for each indentation step in `neocaml-ocamllex-mode'."
:type 'natnum
:safe 'natnump
:group 'neocaml-ocamllex
:package-version '(neocaml . "0.7.0"))
;;; Grammar installation
(defconst neocaml-ocamllex-grammar-recipes
'((ocamllex "https://github.com/314eter/tree-sitter-ocamllex"
"v0.24.0"
"src"))
"Tree-sitter grammar recipe for OCamllex files.
Each entry is a list of (LANGUAGE URL REV SOURCE-DIR).
Suitable for use as the value of `treesit-language-source-alist'.")
(defun neocaml-ocamllex-install-grammar (&optional force)
"Install the OCamllex tree-sitter grammar if not already available.
With prefix argument FORCE, reinstall even if already installed."
(interactive "P")
(when (or force (not (treesit-language-available-p 'ocamllex nil)))
(message "Installing OCamllex tree-sitter grammar...")
(let ((treesit-language-source-alist neocaml-ocamllex-grammar-recipes))
(treesit-install-language-grammar 'ocamllex))))
;;; Font-lock
(defvar neocaml-ocamllex--font-lock-settings
(treesit-font-lock-rules
:language 'ocamllex
:feature 'comment
'((comment) @font-lock-comment-face)
:language 'ocamllex
:feature 'keyword
'(["and" "as" "let" "parse" "refill" "rule" "shortest"]
@font-lock-keyword-face)
:language 'ocamllex
:feature 'definition
'((lexer_entry name: (lexer_entry_name) @font-lock-function-name-face)
(named_regexp name: (regexp_name) @font-lock-variable-name-face))
:language 'ocamllex
:feature 'string
'((string) @font-lock-string-face
(character) @font-lock-string-face)
:language 'ocamllex
:feature 'escape-sequence
:override t
'((escape_sequence) @font-lock-escape-face)
:language 'ocamllex
:feature 'constant
'((any) @font-lock-constant-face
(eof) @font-lock-constant-face)
:language 'ocamllex
:feature 'variable
'((regexp_name) @font-lock-variable-use-face)
:language 'ocamllex
:feature 'operator
'((regexp_repetition ["*" "+" "?"] @font-lock-operator-face)
(regexp_difference "#" @font-lock-operator-face))
:language 'ocamllex
:feature 'bracket
'(["(" ")" "[" "]" "{" "}"] @font-lock-bracket-face)
:language 'ocamllex
:feature 'delimiter
'(["=" "|" "-"] @font-lock-delimiter-face))
"Font-lock settings for `neocaml-ocamllex-mode'.")
(defun neocaml-ocamllex--injection-available-p ()
"Non-nil if OCaml language injection is available.
Requires Emacs 30+ (for `treesit-range-rules' with `:embed') and
the OCaml tree-sitter grammar."
(and (>= emacs-major-version 30)
(treesit-language-available-p 'ocaml)))
(defun neocaml-ocamllex--font-lock-settings ()
"Return font-lock settings for `neocaml-ocamllex-mode'.
When OCaml injection is available, includes font-lock rules for
embedded OCaml code inside { } blocks."
(append
neocaml-ocamllex--font-lock-settings
(when (neocaml-ocamllex--injection-available-p)
(require 'neocaml)
(neocaml-mode--font-lock-settings 'ocaml))))
(defun neocaml-ocamllex--range-settings ()
"Return range settings for embedded OCaml code injection.
Returns nil if injection is not available."
(when (neocaml-ocamllex--injection-available-p)
(treesit-range-rules
:embed 'ocaml
:host 'ocamllex
:local t
'((ocaml) @capture))))
;;; Indentation
(defvar neocaml-ocamllex--indent-rules
`((ocamllex
((parent-is "lexer_definition") column-0 0)
((node-is "}") parent-bol 0)
;; Don't reindent inside OCaml code blocks
((parent-is "ocaml") no-indent 0)
;; Lexer cases (| regexp { action })
((node-is "lexer_case") parent-bol neocaml-ocamllex-indent-offset)
;; Inside a lexer entry
((parent-is "lexer_entry") parent-bol neocaml-ocamllex-indent-offset)
;; Inside character sets
((parent-is "character_set") parent-bol 0)
;; Catch-all
(no-node parent-bol neocaml-ocamllex-indent-offset)))
"Indentation rules for `neocaml-ocamllex-mode'.")
;;; Imenu
(defvar neocaml-ocamllex--imenu-settings
'(("Rule" "\\`lexer_entry\\'" nil nil)
("Regexp" "\\`named_regexp\\'" nil nil))
"Imenu settings for `neocaml-ocamllex-mode'.
See `treesit-simple-imenu-settings' for the format.")
;;; Navigation
(defun neocaml-ocamllex--defun-name (node)
"Return a name for NODE suitable for imenu and which-func."
(pcase (treesit-node-type node)
("lexer_entry"
(let ((name (treesit-node-child-by-field-name node "name")))
(when name (treesit-node-text name t))))
("named_regexp"
(let ((name (treesit-node-child-by-field-name node "name")))
(when name (treesit-node-text name t))))))
;;; Mode definition
;;;###autoload
(define-derived-mode neocaml-ocamllex-mode prog-mode "OCamllex"
"Major mode for editing OCamllex lexer definition files.
When the OCaml tree-sitter grammar is installed, embedded OCaml
code inside { } blocks gets full syntax highlighting via language
injection.
\\{neocaml-ocamllex-mode-map}"
(when (< (treesit-library-abi-version) 14)
(error "The OCamllex grammar requires tree-sitter ABI version 14+, but \
your Emacs was built against ABI version %d; rebuild Emacs with \
tree-sitter >= 0.24" (treesit-library-abi-version)))
(unless (treesit-ready-p 'ocamllex)
(when (y-or-n-p "OCamllex tree-sitter grammar is not installed. Install it now?")
(neocaml-ocamllex-install-grammar))
(unless (treesit-ready-p 'ocamllex)
(error "Cannot activate neocaml-ocamllex-mode without the OCamllex grammar")))
(treesit-parser-create 'ocamllex)
;; Comments (OCaml-style)
(setq-local comment-start "(* ")
(setq-local comment-end " *)")
(setq-local comment-start-skip "(\\*+ *")
;; Language injection for embedded OCaml code
(let ((range-settings (neocaml-ocamllex--range-settings)))
(when range-settings
(setq-local treesit-range-settings range-settings)))
;; Font-lock
(setq-local treesit-font-lock-settings (neocaml-ocamllex--font-lock-settings))
(setq-local treesit-font-lock-feature-list
'((comment definition)
(keyword string type)
(constant escape-sequence attribute builtin number)
(variable operator bracket delimiter property label function)))
;; Indentation
(setq-local treesit-simple-indent-rules neocaml-ocamllex--indent-rules)
(setq-local indent-tabs-mode nil)
;; Imenu
(setq-local treesit-simple-imenu-settings neocaml-ocamllex--imenu-settings)
;; Navigation
(setq-local treesit-defun-type-regexp
"\\`\\(?:lexer_entry\\|named_regexp\\)\\'")
(setq-local treesit-defun-name-function #'neocaml-ocamllex--defun-name)
(setq-local add-log-current-defun-function #'treesit-add-log-current-defun)
;; Final newline
(setq-local require-final-newline mode-require-final-newline)
;; Make URLs and bug references in comments clickable
(goto-address-prog-mode)
(bug-reference-prog-mode)
(treesit-major-mode-setup))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.mll\\'" . neocaml-ocamllex-mode))
(provide 'neocaml-ocamllex)
;;; neocaml-ocamllex.el ends here