-
Notifications
You must be signed in to change notification settings - Fork 1
/
splunk-mode.el
395 lines (334 loc) · 15.2 KB
/
splunk-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
;;; splunk-mode.el --- Major Mode for editing Splunk SPL source code -*- lexical-binding: t -*-
;; Copyright (C) 2022–2024 Jake Ireland
;; Author: Jake Ireland <[email protected]>
;; URL: https://github.com/jakewilliami/splunk-mode/
;; Version: 1.1
;; Keywords: languages splunk mode query
;; Package-Requires: ((emacs "27.1"))
;;; Usage:
;;
;; Put the following code in your .emacs, site-load.el, or other relevant file
;; (add-to-list 'load-path "path-to-splunk-mode")
;; (require 'splunk-mode)
;;; Licence:
;;
;; This file is not part of GNU Emacs.
;;
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
;; files (the "Software"), to deal in the Software without
;; restriction, including without limitation the rights to use, copy,
;; modify, merge, publish, distribute, sublicense, and/or sell copies
;; of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;; Commentary:
;;
;; Major Mode for editing Splunk Search Processing Language (SPL) source code.
;;
;; This package provides syntax highlighting and indentation support for
;; SPL source code.
;;
;; Syntax resources:
;; - https://github.com/splunk/vscode-extension-splunk/
;;
;; The following resources are from
;; https://docs.splunk.com/Documentation/Splunk/9.1.1/: (or latest)
;; - SPL: Search/Aboutthesearchlanguage
;; - SPL Syntax: SearchReference/UnderstandingSPLsyntax
;;
;; Specific resources are referenced where relevant within the code.
;;; Notes:
;;
;; TODO:
;; - only highlight transforming and eval functions after parentheses added
;; - I need to go through and actually test things at some point, but I should probably prioritise releasing this and doing small touch-ups later
;; - Electric indent mode (see below)
;;
;; NOTE: Feature possibilities:
;; - Operator highlighting
;; - Comparison or assignment highlighting
;; - Different brackets colours when nested
;; - Make keyword highlighting more similar to official Splunk
;; highlighting (i.e., most things are function highlights.)
;; - Linting
;; - Autocomplete
;;; Code
;;; Main
;;; Code:
(eval-when-compile
(require 'rx)
(require 'regexp-opt))
(defvar splunk-mode-hook nil)
(defgroup splunk ()
"Major mode for Splunk SPL code."
:link '(url-link "https://docs.splunk.com/")
:version "1.1"
:group 'languages
:prefix "splunk-")
;;; Faces
;;
;; Custom font faces for Splunk syntax.
;;
;; After some trial and error, and taking inspiration from different places, here are
;; some possible colour schemes:*
;;
;; ----------------------------------------------------------------------------
;; | font-lock-*-face | Apt | Falcon | VS Code |
;; |-------------------|------------------|------------------|------------------|
;; | warning | — | — | — |
;; | function-name | Trans/Eval/Macro | Builtin | Constants |
;; | [n] function-call | — | — | — |
;; | variable-name | Keyword | — | Keyword |
;; | [n] variable-use | — | — | — |
;; | keyword | — | Trans/Eval/Macro | — |
;; | type | — | Constants | Trans/Eval/Macro |
;; | constant | Constants | Keyword | Builtin |
;; | builtin | Builtin | — | — |
;; | preprocessor | — | — | — |
;; | [n] property-name | — | — | — |
;; | [n] number | — | — | — |
;; | [n] escape | — | — | — |
;; ----------------------------------------------------------------------------
;;
;; Note: font-lock faces prefixed with [n] have been considered too new to use here,
;; as they do not yet have enough widespread support.
;;
;; Ref:
;; - https://www.gnu.org/software/emacs/manual/html_node/elisp/Faces-for-Font-Lock.html
(defface splunk-comment-face
'((t :inherit font-lock-comment-face))
"Face for alternative comment syntax in Splunk."
:group 'splunk)
(defface splunk-builtin-functions-face
'((t :inherit font-lock-builtin-face))
;; '((t :inherit font-lock-function-name-face))
;; '((t :inherit font-lock-constant-face))
"Face for builtin functions such as `rename', `table', and `stat' in Splunk."
:group 'splunk)
(defface splunk-eval-functions-face
'((t :inherit font-lock-function-name-face))
;; '((t :inherit font-lock-keyword-face))
;; '((t :inherit font-lock-type-face))
"Face for eval functions such as `abs' and `mvindex' in Splunk."
:group 'splunk)
(defface splunk-transforming-functions-face
'((t :inherit font-lock-function-name-face))
;; '((t :inherit font-lock-keyword-face))
;; '((t :inherit font-lock-type-face))
"Face for transforming functions such as `count' and `values' in Splunk."
:group 'splunk)
(defface splunk-language-constants-face
'((t :inherit font-lock-constant-face))
;; '((t :inherit font-lock-type-face))
;; '((t :inherit font-lock-function-name-face))
"Face for language constants such as `as' and `by' in Splunk."
:group 'splunk)
(defface splunk-macros-face
'((t :inherit font-lock-function-name-face))
;; '((t :inherit font-lock-keyword-face))
;; '((t :inherit font-lock-type-face))
"Face for macros in Splunk."
:group 'splunk)
(defface splunk-keyword-face
'((t :inherit font-lock-variable-name-face))
;; '((t :inherit font-lock-constant-face))
;; '((t :inherit font-lock-variable-name-face))
"Face for keywords (e.g. `sourcetype=*') in Splunk."
:group 'splunk)
(defface splunk-digits-face
;; '((t :inherit font-lock-number-face)) ;; Added too recently
'((t :inherit font-lock-type-face))
"Face for digits in Splunk."
:group 'splunk)
(defface splunk-escape-chars-face
;; '((t :inherit font-lock-escape-face)) ;; Added too recently
'((t :inherit font-lock-constant-face))
"Face for escape characters in Splunk."
:group 'splunk)
(defface splunk-operators-face
'((t :inherit font-lock-builtin-face
:weight bold))
"Face for operators in Splunk."
:group 'splunk)
;;; Syntax
;; Update syntax table; refs:
;; - https://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-Table-Functions.html
;; - https://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-Flags.html
;; - https://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-Descriptors.html
;; - https://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-Class-Table.html
(defconst splunk-mode-syntax-table
(with-syntax-table (copy-syntax-table)
;; C/C++ style comments
(modify-syntax-entry ?/ ". 124b")
(modify-syntax-entry ?* ". 23")
(modify-syntax-entry ?\n "> b")
;; The pipe character needs to be counted as a symbol-constituent
;; character, so that symbols are broken up by pipes; refs:
(modify-syntax-entry ?| " ")
;; Chars are the same as strings
(modify-syntax-entry ?' "\"")
;; Syntax table
(syntax-table))
"Syntax table for `splunk-mode'.")
(eval-and-compile
(defconst splunk-builtin-functions
'("abstract" "accum" "addcoltotals" "addinfo" "addtotals"
"analyzefields" "anomalies" "anomalousvalue" "append"
"appendcols" "appendpipe" "arules" "associate" "audit"
"autoregress" "bucket" "bucketdir" "chart" "cluster"
"collect" "concurrency" "contingency" "convert" "correlate"
"crawl" "datamodel" "dbinspect" "dbxquery" "dbxlookup"
"dedup" "delete" "delta" "diff" "dispatch" "erex" "eval"
"eventcount" "eventstats" "extract" "fieldformat" "fields"
"fieldsummary" "file" "filldown" "fillnull" "findtypes"
"folderize" "foreach" "format" "from" "gauge" "gentimes"
"geostats" "head" "highlight" "history" "input" "index"
"inputcsv" "inputlookup" "iplocation" "join" "kmeans" "kvform"
"loadjob" "localize" "localop" "lookup" "makecontinuous"
"makemv" "makeresults" "map" "metadata" "metasearch"
"multikv" "multisearch" "mvcombine" "mvexpand" "nomv"
"outlier" "outputcsv" "outputlookup" "outputtext" "overlap"
"pivot" "predict" "rangemap" "rare" "regex" "relevancy"
"reltime" "rename" "replace" "rest" "return" "reverse"
"rex" "rtorder" "run" "savedsearch" "script" "scrub"
"search" "searchtxn" "selfjoin" "sendemail" "set" "setfields"
"sichart" "sirare" "sistats" "sitimechart" "sitop" "sort"
"spath" "stats" "strcat" "streamstats" "table" "tags"
"tail" "timechart" "top" "transaction" "transpose" "trendline"
"tscollect" "tstats" "typeahead" "typelearner" "typer" "uniq"
"untable" "where" "x11" "xmlkv" "xmlunescape" "xpath"
"xyseries"))
(defconst splunk-eval-functions
'("abs" "acos" "acosh" "asin" "asinh" "atan" "atan2" "atanh"
"case" "cidrmatch" "ceiling" "coalesce" "commands" "cos"
"cosh" "exact" "exp" "floor" "hypot" "if" "in" "isbool"
"isint" "isnotnull" "isnull" "isnum" "isstr" "len" "like"
"ln" "log" "lower" "ltrim" "match" "max" "md5" "min" "mvappend"
"mvcount" "mvdedup" "mvfilter" "mvfind" "mvindex" "mvjoin"
"mvrange" "mvsort" "mvzip" "now" "null" "nullif" "pi" "pow"
"printf" "random" "relative_time" "replace" "round" "rtrim"
"searchmatch" "sha1" "sha256" "sha512" "sigfig" "sin" "sinh"
"spath" "split" "sqrt" "strftime" "strptime" "substr" "tan"
"tanh" "time" "tonumber" "tostring" "trim" "typeof" "upper"
"urldecode" "validate"))
(defconst splunk-transforming-functions
'("avg" "count" "distinct_count" "estdc" "estdc_error" "eval"
"max" "mean" "median" "min" "mode" "percentile" "range"
"stdev" "stdevp" "sum" "sumsq" "var" "varp" "first" "last"
"list" "values" "earliest" "earliest_time" "latest"
"latest_time" "per_day" "per_hour" "per_minute" "per_second"
"rate"))
;; TODO: improve "case-insensitive" workaround
(defconst splunk-language-constants-lower
'("as" "by" "or" "and" "over" "where" "output" "outputnew" "not"
"true" "false"))
(defconst splunk-language-constants
(append splunk-language-constants-lower (mapcar 'upcase splunk-language-constants-lower))))
;; A Splunk word can contain underscores. To use in the place of `word'
;;
;; Reference on extending rx:
;; - https://www.gnu.org/software/emacs/manual/html_node/elisp/Extending-Rx.html
;; - https://emacs.stackexchange.com/q/79050
;; - https://emacsdocs.org/docs/elisp/Rx-Notation#macro-rx-define-name-arglist-rx-form
(rx-define splunk-word
(or word "_"))
;; "(?<=\\`)[\\w]+(?=\\(|\\`)"
(defconst splunk-macro-names-regexp
(rx "`" (group (one-or-more splunk-word)) (or "(" "`")))
;; "\\b(\\d+)\\b"
(defconst splunk-digits-regexp
(rx word-boundary (group (one-or-more digit)) word-boundary))
;; "(\\\\\\\\|\\\\\\||\\\\\\*|\\\\\\=)"
(defconst splunk-escape-chars-regexp
(rx (group (or "\\\\" "\\*" "\\|" "\\=" "(" ")" "[" "]"))))
;; "(\\|,)"
(defconst splunk-operators-regexp
;; (rx (group (or "\\" ","))))
(rx unmatchable))
;; E.g., `sourcetype=access_*'
(defconst splunk-keyword-regexp
(rx (and (group (one-or-more splunk-word))
(optional (one-or-more space)) "=" (optional (one-or-more space))
(or (one-or-more digit)
(and (optional "\"") (one-or-more (or "*" splunk-word)) (optional "\""))))))
;; Only highlight eval functions in eval blocks
;; E.g., `eval isLocalAdmin=if(...)'; ref:
;; - https://docs.splunk.com/Documentation/Splunk/9.2.0/SearchReference/CommonEvalFunctions
(defconst splunk-eval-regexp
(rx (and
(group (and
(or "eval" "where" "fieldformat")
(or (one-or-more space) eol)
(zero-or-more (not (any "|[")))))
(group (regexp (regexp-opt splunk-eval-functions 'words))))))
;; Alternative comment syntax; refs:
;; - https://docs.splunk.com/Documentation/Splunk/9.1.1/Search/Comments
;; - https://docs.splunk.com/Documentation/SCS/current/Search/Comments
;; - https://docs.splunk.com/Documentation/Splunk/8.0.10/Search/Addcommentstosearches
(defconst splunk-alt-comment-regexp
(rx (or
;; Triple backtick style
(and (repeat 3 "`") (zero-or-more not-newline) (repeat 3 "`"))
;; Comment macro
(and "`comment(\"" (zero-or-more not-newline) "\")`"))))
;; Relevant refs
;; - Font faces: https://www.gnu.org/software/emacs/manual/html_node/elisp/Faces-for-Font-Lock.html
;; - Regex: https://www.gnu.org/software/emacs/manual/html_node/elisp/Rx-Constructs.html
;;
;; Note the double apostrophe before providing custom type faces:
;; - https://emacs.stackexchange.com/a/3587
(defconst splunk-font-lock-keywords
(list
;; Syntax defined by keyword lists
(cons (regexp-opt splunk-builtin-functions 'symbols) ''splunk-builtin-functions-face)
(cons (regexp-opt splunk-transforming-functions 'symbols) ''splunk-transforming-functions-face)
(cons (regexp-opt splunk-language-constants 'symbols) ''splunk-language-constants-face)
;; Eval functions
;; (cons splunk-eval-regexp ''splunk-eval-functions-face)
(list splunk-eval-regexp 2 ''splunk-eval-functions-face)
;; Alternative comment styles
;;
;; Note the syntax-level override:
;; - https://emacs.stackexchange.com/a/79049
;; - https://stackoverflow.com/a/24107675
;; - https://emacs.stackexchange.com/a/61891
(list splunk-alt-comment-regexp 0 ''splunk-comment-face t)
;; Syntax defined by regex
;;
;; Note the extraction of specific groups from the regex:
;; - https://emacs.stackexchange.com/a/79044
(list splunk-macro-names-regexp 1 ''splunk-macros-face)
(cons splunk-digits-regexp ''splunk-digits-face)
(cons splunk-escape-chars-regexp ''splunk-escape-chars-face)
(cons splunk-operators-regexp ''splunk-operators-face)
(list splunk-keyword-regexp 1 ''splunk-keyword-face)))
;;; Mode
;;;###autoload
(define-derived-mode splunk-mode prog-mode "Splunk"
"Major Mode for editing Splunk SPL source code.
\\{splunk-mode-map}"
:syntax-table splunk-mode-syntax-table
(setq-local font-lock-defaults '(splunk-font-lock-keywords))
(setq-local comment-start "//")
(setq-local indent-line-function 'splunk-indent-line))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.spl\\'" . splunk-mode))
(add-to-list 'auto-mode-alist '("\\.splunk\\'" . splunk-mode))
(provide 'splunk-mode)
;; Local Variables:
;; coding: utf-8
;; checkdoc-major-mode: t
;; End:
;;; splunk-mode.el ends here