|
| 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