-
Notifications
You must be signed in to change notification settings - Fork 0
/
rl-parser.el
60 lines (48 loc) · 2.01 KB
/
rl-parser.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
;;; This file is used to generate elisp code from raylib.h
(require 'treesit)
(require 'cl-lib)
(require 'seq)
(cl-assert (treesit-available-p))
(cl-assert (treesit-language-available-p 'c))
(defvar rl--header-path "./raylib/src/raylib.h")
(defvar rl--parser-buffer (find-file-noselect rl--header-path))
(defvar rl--parser (treesit-parser-create 'c rl--parser-buffer))
(defun rl--find-enum (name)
(let ((result (treesit-query-capture rl--parser
`((type_definition
type: (enum_specifier
body: (enumerator_list) @enum)
declarator: ((type_identifier) @name
(:match ,name @name)))))))
(map-elt result 'enum)))
(defun rl--collect-enum-values (enum)
"Collect enum values"
(let (keys)
(dolist (node (treesit-node-children enum))
(when (equal (treesit-node-type node) "enumerator")
(push (cons (treesit-node-get node '((child 0 nil) (text t)))
(treesit-node-get node '((child 2 nil) (text t))))
keys)))
(nreverse keys)))
(defun rl--generate-enum-values (name)
(insert "\n")
(insert (format ";; enum %s\n" name))
(pcase-dolist (`(,key . ,value) (rl--collect-enum-values (rl--find-enum name)))
(prin1 `(defconst
,(intern (format "rl-%s" (string-replace "_" "-" (downcase key))))
,(string-to-number value))
(current-buffer))
(insert "\n")))
(defun rl--generate ()
(let ((filename "rl-const.el"))
(with-current-buffer (get-buffer-create filename)
(erase-buffer)
(emacs-lisp-mode)
(insert ";; DO NOT EDIT!\n")
(insert ";; This file is autogenerated by rl-parser.el\n")
(rl--generate-enum-values "KeyboardKey")
(rl--generate-enum-values "MouseButton")
(insert "(provide 'rl-const)")
(write-file filename))))
(rl--generate)
(treesit-parser-delete rl--parser)