-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfuse-ux-parser.el
233 lines (193 loc) · 6.22 KB
/
fuse-ux-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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
(require 's)
(require 'cl-lib)
(require 'dash)
(cl-defstruct (attrib
(:constructor new-attrib (name value)))
name value)
(cl-defstruct (element
(:constructor new-element (name attribs content)))
name attribs content)
(cl-defstruct (start-tag
(:constructor new-start-tag (name attribs type)))
name attribs type)
(defun new-self-closing-tag (name attribs)
(new-start-tag name attribs 'self-closing-tag))
(defun self-closing-tag-p (start-tag)
(equal (aref start-tag 3) 'self-closing-tag))
(defun new-opening-tag (name attribs)
(new-start-tag name attribs 'opening-tag))
(defun opening-tag-p (opening-tag)
(equal (aref opening-tag 3) 'opening-tag))
(defmacro def-parser (name args &rest body)
`(defun ,name ,args
(let ((last-pos ux-pos)
(to-be-returned (progn ,@body)))
(if to-be-returned
to-be-returned
(progn
(setq ux-pos last-pos)
to-be-returned)))))
(defvar ux-buffer "")
(defvar ux-pos 0)
(defvar ux-identifier-chars)
(defvar ux-identifier-chars-list)
(defvar ux-attribute-identifier-chars)
(setq ux-identifier-chars "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_")
(setq ux-identifier-chars-list (mapcar (lambda (x) x) "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"))
(setq ux-attribute-identifier-chars (cons ?. (cons ?: ux-identifier-chars-list)))
(defun fuse-aref (buf pos)
(if (<= (length buf) pos)
'nil
(aref buf pos)))
(defun fuse-peek (&optional c)
(fuse-aref ux-buffer (if c (+ ux-pos c) ux-pos)))
(defun fuse-consume (&optional c)
(let ((ret (fuse-peek)))
(setf ux-pos (+ ux-pos (if c c 1)))
ret))
(def-parser fuse-parse-char (c &optional offset)
(if (eq c (fuse-aref ux-buffer (+ ux-pos (if offset offset 0))))
(fuse-consume)
'nil))
(def-parser fuse-parse-while (predicate)
(let ((beginning-pos ux-pos))
(while (funcall predicate))
(substring ux-buffer beginning-pos ux-pos)))
(def-parser fuse-parse-any-char (char-list)
(if (member (fuse-peek) char-list)
(fuse-consume)
'nil))
(def-parser fuse-parse-char-except (exception-list &optional offset)
(if (not (member (fuse-peek) exception-list))
(fuse-consume)
'nil))
(def-parser fuse-parse-string (s)
(let ((offset 0))
(while (and (< offset (length s))
(fuse-parse-char (aref s offset) 0))
(setq offset (1+ offset)))
(if (equal offset (length s))
s
'nil)))
(def-parser fuse-parse-whitespace ()
(fuse-parse-any-char '(?\s ?\t ?\n)))
(def-parser fuse-parse-many-whitespace ()
(while (fuse-parse-whitespace))
't)
(def-parser fuse-parse-identifier ()
(let ((beginning-pos ux-pos))
(while (fuse-parse-any-char ux-identifier-chars-list))
(if (> ux-pos beginning-pos)
(substring ux-buffer beginning-pos ux-pos)
'nil)))
(def-parser fuse-parse-attribute-identifier ()
(let ((beginning-pos ux-pos))
(while (fuse-parse-any-char ux-attribute-identifier-chars))
(if (> ux-pos beginning-pos)
(substring ux-buffer beginning-pos ux-pos)
'nil)))
(def-parser fuse-parse-string-literal ()
(if (fuse-parse-string "\"")
(let ((ret (fuse-parse-while (lambda () (fuse-parse-char-except '(?\"))))))
(if (fuse-parse-string "\"")
ret
'nil))
'nil))
(def-parser fuse-parse-attribute ()
(let ((identifier (fuse-parse-attribute-identifier)))
(if (and identifier
(fuse-parse-string "="))
(let ((value (fuse-parse-string-literal)))
(if value
(new-attrib identifier value)
'nil))
'nil)))
(def-parser fuse-parse-0-or-more (parser)
(let ((r)
(call-res 'nil))
(while (setq call-res (funcall parser))
(setq r (cons call-res r)))
(if r
(reverse r)
'empty)))
(def-parser fuse-parse-or (p1 p2)
(unless (apply p1)
(apply p2)))
(def-parser fuse-parse-start-tag ()
(let (element-name
attributes)
(let (r)
(if (and (fuse-parse-string "<")
(setq element-name (fuse-parse-identifier))
(fuse-parse-many-whitespace)
(progn
(setq attributes (fuse-parse-0-or-more
(lambda ()
(fuse-parse-many-whitespace)
(fuse-parse-attribute))))
't)
(setq r (cond
((fuse-parse-string "/>") (new-self-closing-tag element-name attributes))
((fuse-parse-string ">") (new-opening-tag element-name attributes))
('t 'nil))))
r
'nil))))
(def-parser fuse-parse-end-tag ()
(let ((ret)))
(if (and (fuse-parse-string "</")
(setq ret (fuse-parse-identifier))
(fuse-parse-string ">"))
ret
'nil))
(def-parser fuse-parse-element ()
(fuse-parse-many-whitespace)
(let ((start-tag (fuse-parse-start-tag)))
(cond
((equal start-tag 'nil) 'nil)
((self-closing-tag-p start-tag)
(princ "self closing tag") (princ (start-tag-name start-tag))
(new-element (start-tag-name start-tag) (start-tag-attribs start-tag) 'nil))
((opening-tag-p start-tag)
(fuse-parse-many-whitespace)
(let ((content (fuse-parse-0-or-more 'fuse-parse-element)))
(fuse-parse-many-whitespace)
(if (equal (start-tag-name start-tag) (fuse-parse-end-tag))
(progn
(princ "normal tag") (princ (start-tag-name start-tag))
(new-element (start-tag-name start-tag)
(start-tag-attribs start-tag)
content))
'nil))))))
(defun testittest ()
(setq ux-pos 0)
(fuse-parse-element))
(defun fuse--get-buffer-contents ()
(buffer-substring-no-properties (point-min) (point-max)))
(defun fuse-attrib-to-string (a &optional depth)
(if a
(let ((ret (make-string depth ?\t)))
(-each a (lambda (attr)
(princ attr)
(setq ret (concat ret "AName: " (attrib-name attr) " AVal: " (attrib-value attr) ", "))))
ret)
""))
(defun fuse-element-to-string (e &optional depth)
(let ((str ""))
(unless depth (setq depth 0))
(unless str (setq str ""))
(setq str (concat str (make-string depth ?\t) "EName: " (element-name e) " \n"))
(when (not (equal (element-attribs e) 'empty))
(setq str (concat str (fuse-attrib-to-string (element-attribs e) depth) "\n")))
(let ((elem-cont (element-content e)))
(when (and elem-cont
(not (equal elem-cont 'empty)))
(-each elem-cont
(lambda (elem)
(setq str (concat str (fuse-element-to-string elem (1+ depth))))))))
str))
(defun fuse-parse-current-ux-buffer ()
(interactive)
(setq ux-pos 0)
(setq ux-buffer (fuse--get-buffer-contents))
(let ((ret (fuse-parse-element)))
(message (fuse-element-to-string ret))))