Skip to content

Commit 5754eb6

Browse files
authored
Merge pull request #942 from esben-semmle/emacs-1
Emacs syntax highlighting and file-type detection
2 parents 5502627 + b6f2e60 commit 5754eb6

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

misc/emacs/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# QL syntax highlighting and file-type detection for Emacs
2+
3+
To install, add this directory to the `load-path` and add load the mode in the Emacs init file.
4+
5+
Example:
6+
7+
```elisp
8+
; ~/.emacs, ~/.emacs.el, or ~/.emacs.d/init.el
9+
10+
; ...
11+
12+
(add-to-list 'load-path "~/ql/misc/emacs")
13+
(require 'ql-mode-base)
14+
15+
; ...
16+
```

misc/emacs/ql-mode-base.el

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
;;; ql-mode-base.el --- A major mode for editing QL files
2+
3+
;;; Commentary:
4+
;;
5+
;; A basic major mode for editing QL files, a more advanced major
6+
;; mode is available for internal use at Semmle.
7+
;;
8+
;; Provides syntax highlightning, comment support, and a mode-specific
9+
;; keymap.
10+
11+
;;; Code:
12+
13+
(defconst ql--at-type-regex "\\_<@\\w+\\>")
14+
(defconst ql--predicate-regex "\\(\\_<\\w+\\(\\+\\|\\*\\)?\\_>\\)\\s-*(")
15+
(defconst ql--primitive-type-regex (regexp-opt '("int" "string" "float" "boolean" "date") 'symbols))
16+
(defconst ql--annotation-regex (regexp-opt '("abstract" "cached" "external" "final" "transient" "library" "private" "deprecated" "override" "query" "pragma" "language" "bindingset") 'words))
17+
(defconst ql--annotation-arg-regex (regexp-opt '("inline" "noinline" "nomagic" "noopt" "monotonicAggregates") 'words))
18+
(defconst ql--keywords
19+
'("and" "any" "as" "asc" "avg" "boolean" "by" "class" "concat" "count" "date" "desc" "else" "exists" "extends" "false" "float" "forall" "forex" "from" "if" "implies" "import" "in" "instanceof" "int" "max" "min" "module" "not" "none" "or" "order" "predicate" "rank" "result" "select" "strictconcat" "strictcount" "strictsum" "string" "sum" "super" "then" "this" "true" "where"
20+
)
21+
)
22+
23+
(defconst ql--highlights
24+
`((,ql--predicate-regex 1 'font-lock-function-name-face)
25+
(,ql--primitive-type-regex . 'font-lock-type-face)
26+
(,ql--at-type-regex 0 'font-lock-type-face)
27+
(,ql--annotation-regex . 'font-lock-preprocessor-face)
28+
(,ql--annotation-arg-regex . 'font-lock-keyword-face))
29+
)
30+
31+
(defvar ql-mode-base-map
32+
(let ((map (make-sparse-keymap)))
33+
map)
34+
"Keymap for `ql-mode-base'.")
35+
36+
(defvar ql-mode-base-hook nil "Hook run after entering ql-mode-base.")
37+
38+
(define-generic-mode
39+
ql-mode-base
40+
41+
;; comments
42+
nil
43+
44+
;; keywords
45+
ql--keywords
46+
47+
;; other things to highlight
48+
ql--highlights
49+
50+
;; auto mode alist
51+
'("\\.qll?$")
52+
53+
;; other function to run
54+
(list (lambda ()
55+
(use-local-map ql-mode-base-map)
56+
57+
(modify-syntax-entry ?_ "w" (syntax-table))
58+
;; // Comments (style a)
59+
(modify-syntax-entry ?\/ ". 124" (syntax-table))
60+
(modify-syntax-entry ?\n "> " (syntax-table))
61+
;; /* Comments (style b) */
62+
(modify-syntax-entry ?* ". 23b" (syntax-table))
63+
(modify-syntax-entry ?@ "_" (syntax-table))
64+
65+
(set (make-local-variable 'comment-start) "//")
66+
(set (make-local-variable 'comment-start-skip) "// ")
67+
68+
(run-hooks 'ql-mode-base-hook)
69+
70+
))
71+
"A basic major mode for QL files")
72+
73+
(provide 'ql-mode-base)
74+
;;; ql-mode-base.el ends here

0 commit comments

Comments
 (0)