-
Notifications
You must be signed in to change notification settings - Fork 2
/
phps-mode-ast.el
201 lines (175 loc) · 6.09 KB
/
phps-mode-ast.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
;;; phps-mode-ast.el --- Abstract Syntax Tree functions -*- lexical-binding: t -*-
;; Copyright (C) 2018-2024 Free Software Foundation, Inc.
;;; Commentary:
;;; Code:
(require 'phps-mode-macros)
(require 'phps-mode-parser-sdt)
;;; Variables:
(defvar-local
phps-mode-ast--parse-trail
nil
"Parse trail for current buffer.")
(defvar-local
phps-mode-ast--tree
nil
"Tree for current buffer.")
;; Functions:
(defun phps-mode-ast--generate ()
"Generate AST for current buffer."
(setq
phps-mode-parser-sdt-bookkeeping
(make-hash-table :test 'equal))
(setq
phps-mode-parser-sdt-symbol-table-index
0)
(setq
phps-mode-parser-sdt-symbol-imenu--table
(make-hash-table :test 'equal))
(setq
phps-mode-parser-sdt-symbol-imenu--namespace
nil)
(setq
phps-mode-parser-sdt-symbol-imenu--stack
nil)
(setq
phps-mode-parser-sdt-symbol-table
(make-hash-table :test 'equal))
(setq
phps-mode-parser-sdt-symbol-table-by-uri
(make-hash-table :test 'equal))
(setq
phps-mode-parser-sdt--bookkeeping-namespace
nil)
(setq
phps-mode-parser-sdt--bookkeeping-symbol-stack
nil)
(setq
phps-mode-parser-sdt--bookkeeping-anonymous-function-count
0)
(setq
phps-mode-parser-sdt--bookkeeping-arrow-function-count
0)
(let* ((result (phps-mode-parser--parse t))
(parse-trail (nth 0 result))
(translation (nth 1 result)))
(setq
phps-mode-ast--parse-trail
parse-trail)
(phps-mode-debug-message
(message "\nTranslation:\n%S\n\n" translation))
;; Convert imenu index from hash-table to list structure here
(let ((imenu-index))
(maphash
(lambda (k v)
(if (hash-table-p v)
(let ((v-list)
(v-index))
(maphash
(lambda (k2 v2)
(if (hash-table-p v2)
(let ((v2-list)
(v2-index))
(maphash
(lambda (k3 v3)
(if (hash-table-p v3)
(let ((v3-list)
(v3-index))
(maphash
(lambda (k4 v4)
(if (symbolp k4)
(setq v3-index v4)
(push `(,k4 . ,v4) v3-list)))
v3)
;; Sort level 4
(setq
v3-list
(sort
v3-list
(lambda (a b)
(cond
((and
(listp (cdr a))
(listp (cdr b)))
(< (cdr (car (cdr a))) (cdr (car (cdr b)))))
((listp (cdr a))
(< (cdr (car (cdr a))) (cdr b)))
((listp (cdr b))
(< (cdr a) (cdr (car (cdr b)))))
(t
(< (cdr a) (cdr b)))))))
(push `("declaration" . ,v3-index) v3-list)
(push (append (list k3) v3-list) v2-list))
(if (symbolp k3)
(setq v2-index v3)
(push `(,k3 . ,v3) v2-list))))
v2)
;; Sort level 3
(setq
v2-list
(sort
v2-list
(lambda (a b)
(cond
((and
(listp (cdr a))
(listp (cdr b)))
(< (cdr (car (cdr a))) (cdr (car (cdr b)))))
((listp (cdr a))
(< (cdr (car (cdr a))) (cdr b)))
((listp (cdr b))
(< (cdr a) (cdr (car (cdr b)))))
(t
(< (cdr a) (cdr b)))))))
(push `("declaration" . ,v2-index) v2-list)
(push (append (list k2) v2-list) v-list))
(if (symbolp k2)
(setq v-index v2)
(push `(,k2 . ,v2) v-list))))
v)
;; Sort level 2
(setq
v-list
(sort
v-list
(lambda (a b)
(cond
((and
(listp (cdr a))
(listp (cdr b)))
(< (cdr (car (cdr a))) (cdr (car (cdr b)))))
((listp (cdr a))
(< (cdr (car (cdr a))) (cdr b)))
((listp (cdr b))
(< (cdr a) (cdr (car (cdr b)))))
(t
(< (cdr a) (cdr b)))))))
(push `("declaration" . ,v-index) v-list)
(push (append (list k) v-list) imenu-index))
(push `(,k . ,v) imenu-index)))
phps-mode-parser-sdt-symbol-imenu--table)
;; Sort level 1
(setq
imenu-index
(sort
imenu-index
(lambda (a b)
(cond
((and
(listp (cdr a))
(listp (cdr b)))
(< (cdr (car (cdr a))) (cdr (car (cdr b)))))
((listp (cdr a))
(< (cdr (car (cdr a))) (cdr b)))
((listp (cdr b))
(< (cdr a) (cdr (car (cdr b)))))
(t
(< (cdr a) (cdr b)))))))
(setq
phps-mode-parser-sdt-symbol-imenu
imenu-index))
(setq
phps-mode-ast--tree
translation)
phps-mode-ast--tree))
(provide 'phps-mode-ast)
;;; phps-mode-ast.el ends here