forked from death/cl-tree-sitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhigh-level.lisp
199 lines (175 loc) · 7.69 KB
/
high-level.lisp
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
;;;; +----------------------------------------------------------------+
;;;; | cl-tree-sitter |
;;;; +----------------------------------------------------------------+
(defpackage #:cl-tree-sitter/high-level
(:use #:cl #:cl-tree-sitter/low-level)
(:import-from
#:cffi
#:null-pointer
#:null-pointer-p
#:foreign-string-to-lisp
#:foreign-free
#:define-foreign-library
#:use-foreign-library
#:foreign-funcall)
(:import-from
#:babel
#:string-size-in-octets)
(:export
#:register-language
#:list-all-languages
#:tree-sitter-error
#:tree-sitter-error-language
#:tree-sitter-error-string
#:tree-sitter-error-string-start
#:tree-sitter-error-string-end
#:cant-create-parser
#:cant-set-language
#:cant-parse-string
#:null-node-pointer
#:null-tree-cursor-pointer
#:parse-string
#:node-type
#:node-range
#:node-children))
(in-package #:cl-tree-sitter/high-level)
;; Language registry
(defvar *language-registry*
(make-hash-table))
(defmacro register-language (name lib &key fn-name)
(check-type name symbol)
(check-type fn-name (or null string))
(let ((fn-name (or fn-name (substitute #\_ #\- (pathname-name lib))))
(cffi-name (make-symbol (symbol-name name))))
`(progn
(define-foreign-library ,cffi-name
(t (:default ,lib)))
(use-foreign-library ,cffi-name)
(setf (gethash ',name *language-registry*)
(lambda ()
(foreign-funcall ,fn-name ts-language)))
',name)))
(defun language-module (name)
(funcall
(or (gethash name *language-registry*)
(error "Can't find tree-sitter language module with name ~S." name))))
(defun list-all-languages ()
(loop for name being each hash-key of *language-registry*
collect name))
;; Node representation
(defstruct (node (:type list))
type
range
children)
(defun make-lisp-name (string)
(intern (string-upcase (substitute #\- #\_ string))
(load-time-value (find-package "KEYWORD"))))
;; Error conditions
(define-condition tree-sitter-error (error)
())
(define-condition cant-create-parser (tree-sitter-error)
())
(define-condition cant-set-language (tree-sitter-error)
((language :initarg :language :reader tree-sitter-error-language)))
(define-condition cant-parse-string (tree-sitter-error)
((string :initarg :string :reader tree-sitter-error-string)
(string-start :initarg :string-start :reader tree-sitter-error-string-start)
(string-end :initarg :string-end :reader tree-sitter-error-string-end)
(language :initarg :language :reader tree-sitter-error-language)))
(define-condition null-node-pointer (tree-sitter-error)
())
(define-condition null-tree-cursor-pointer (tree-sitter-error)
())
;; Util
(defmacro with-ts-node-pointer ((var node) &body forms)
`(let ((,var ,node))
(when (null-pointer-p ,var)
(error 'null-node-pointer))
(unwind-protect
(progn ,@forms)
(cffi-sys:foreign-free ,var))))
(defmacro with-tree-cursor-pointer ((var tree) &body forms &aux (node (gensym)))
`(with-ts-node-pointer (,node (ts-tree-root-node-pointer ,tree))
(let ((,var (ts-tree-cursor-new-pointer ,node)))
(when (null-pointer-p ,var)
(error 'null-tree-cursor-pointer))
(unwind-protect
(progn ,@forms)
(ts-tree-cursor-delete ,var)))))
;; Parser
(defun parse-string (language string &key (start 0) end produce-cst (name-generator #'make-lisp-name))
"Parse a STRING that represents LANGUAGE code using tree-sitter. START is
where to start parsing STRING. END is where to stop parsing STRING.
When PRODUCE-CST is set, the full concrete syntax tree will be produced as
opposed to the abstract syntax tree. See 'Named vs Anonymous Nodes':
http://tree-sitter.github.io/tree-sitter/using-parsers#named-vs-anonymous-nodes
NAME-GENERATOR is a function which converts a string from tree-sitter into a
desired name for use in lisp."
(let ((parser (ts-parser-new)))
(when (null-pointer-p parser)
(error 'cant-create-parser))
(unwind-protect (parse-string-with-language language string parser
:start start
:end end
:produce-cst produce-cst
:name-generator name-generator)
(ts-parser-delete parser))))
(defun parse-string-with-language (language string parser
&key (start 0) end produce-cst name-generator)
(unless (ts-parser-set-language parser (language-module language))
(error 'cant-set-language :language language))
(let* ((string-start start)
(string-end (or end (length string)))
(string-length (string-size-in-octets string :start string-start :end string-end))
(string-to-pass (if (plusp string-start)
(subseq string string-start string-end)
string))
(tree (ts-parser-parse-string parser (null-pointer) string-to-pass string-length)))
(when (null-pointer-p tree)
(error 'cant-parse-string
:string string
:string-start start
:string-end end
:language language))
(unwind-protect (convert-foreign-tree-to-list tree :produce-cst produce-cst
:name-generator name-generator)
(ts-tree-delete tree))))
(defun convert-foreign-tree-to-list (tree &key produce-cst name-generator
&aux did-visit-children parse-stack)
(with-tree-cursor-pointer (cursor tree)
;; Closely follows tree-sitter-cli parse
;; implementation with a modification to
;; allow for production of the full CST.
(loop
(with-ts-node-pointer (node (ts-tree-cursor-current-node-pointer cursor))
(let ((is-named (or produce-cst (ts-node-is-named-pointer node))))
(cond (did-visit-children
(when (and is-named (second parse-stack))
(let ((item (pop parse-stack)))
(setf (node-children item)
(nreverse (node-children item)))
(push item (node-children (first parse-stack)))))
(cond ((ts-tree-cursor-goto-next-sibling cursor)
(setf did-visit-children nil))
((ts-tree-cursor-goto-parent cursor)
(setf did-visit-children t))
(t
(let ((root (first parse-stack)))
(setf (node-children root)
(nreverse (node-children root)))
(return root)))))
(t
(when is-named
(let ((start-point (ts-node-start-point-pointer node))
(end-point (ts-node-end-point-pointer node))
(type (funcall name-generator (ts-node-type-pointer node)))
(field-name-ptr (ts-tree-cursor-current-field-name cursor)))
(unless (null-pointer-p field-name-ptr)
(let ((field-name (foreign-string-to-lisp field-name-ptr)))
(setf type (list (funcall name-generator field-name) type))))
(push (make-node :type type
:range (list (list (second start-point) (fourth start-point))
(list (second end-point) (fourth end-point))))
parse-stack)))
(setf did-visit-children
(not (ts-tree-cursor-goto-first-child cursor))))))))))