-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.odin
More file actions
245 lines (209 loc) · 8.88 KB
/
Copy pathparser.odin
File metadata and controls
245 lines (209 loc) · 8.88 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
package ohtml
import bits "core:container/bit_array"
Element :: struct {
type: string, // This is just the tag name
text: [dynamic] string, // All text from all direct children
attrs: map [string] string, // Attribute map, key = value, attrs[key] == value
parent: ^Element, // The parent element (can be nil, obviously)
children: [dynamic] ^Element, // All children elements...
ordering: bits.Bit_Array, // 0 is for Element, 1 is for Text
}
Context :: struct {
tokens: []Token,
current: int
}
@(private="file")
next :: proc(using ctx: ^Context, o := 0) -> Token {
//current = 0
defer current += 1 + o
if current + o < len(tokens) { return tokens[current + o] }
return { }
}
@(private="file")
peek :: proc(using ctx: ^Context,o := 0) -> Token {
if current + o < len(tokens) { return tokens[current + o] }
return { }
}
parse :: proc(html: string, intermediate_allocator := context.temp_allocator) -> ^Element {
ctx := Context {}
using ctx
raw_tokens := tokenize(html, intermediate_allocator)
lexed_tokens := lex(raw_tokens, intermediate_allocator)
defer free_all(intermediate_allocator)
tokens = lexed_tokens
elem := new(Element)
elem.type = "root"
// Kind of, copied from parse_elem, tbh i cba
for current < len(tokens) {
if peek(&ctx).type == .TEXT {
append(&elem.text, peek(&ctx).text)
bits.set(&elem.ordering, len(elem.ordering.bits), true)
next(&ctx)
} else if peek(&ctx).type == .ELEMENT {
child := parse_elem(&ctx)
if child == nil { continue }
append(&elem.children, child)
child.parent = elem
bits.set(&elem.ordering, len(elem.ordering.bits), false)
} else if peek(&ctx).type == .WHITESPACE {
next(&ctx)
} else { break }
}
return elem
}
// TODO: hashset MAY be faster (since, I can fast hash str -> i32)
// Void tags should not have any children or closing tags
VOID_TAGS : [] string : {
"meta", "link", "base", "frame", "img", "br", "wbr", "embed", "hr", "input", "keygen", "col", "command",
"device", "area", "basefont", "bgsound", "menuitem", "param", "source", "track",
}
// An inline element should not contain a block level element
INLINE_TAGS : [] string : {
"object", "base", "font", "tt", "i", "b", "u", "big", "small", "em", "strong", "dfn", "code", "samp", "kbd",
"var", "cite", "abbr", "time", "acronym", "mark", "ruby", "rt", "rp", "rtc", "a", "img", "br", "wbr", "map", "q",
"sub", "sup", "bdo", "iframe", "embed", "span", "input", "select", "textarea", "label", "optgroup",
"option", "legend", "datalist", "keygen", "output", "progress", "meter", "area", "param", "source", "track",
"summary", "command", "device", "area", "basefont", "bgsound", "menuitem", "param", "source", "track",
"data", "bdi", "s", "strike", "nobr",
"rb", // deprecated but still known / special handling
"text", // in SVG NS
"mi", "mo", "msup", "mn", "mtext",
}
BLOCK_TAGS : [] string : {
"html", "head", "body", "frameset", "script", "noscript", "style", "meta", "link", "title", "frame",
"noframes", "section", "nav", "aside", "hgroup", "header", "footer", "p", "h1", "h2", "h3", "h4", "h5", "h6",
"ul", "ol", "pre", "div", "blockquote", "hr", "address", "figure", "figcaption", "form", "fieldset", "ins",
"del", "dl", "dt", "dd", "li", "table", "caption", "thead", "tfoot", "tbody", "colgroup", "col", "tr", "th",
"td", "video", "audio", "canvas", "details", "menu", "plaintext", "template", "article", "main",
"svg", "math", "center", "template",
"dir", "applet", "marquee", "listing",
}
KEEP_WHITESPACE : [] string : { "pre", "plaintext", "title", "textarea" }
parse_elem :: proc(using ctx: ^Context, pre := false) -> ^Element {
if peek(ctx).type != .ELEMENT { return nil }
elem := new(Element)
elem.type = next(ctx).text
pre := pre || any_of(elem.type, ..KEEP_WHITESPACE)
is_inline := any_of(elem.type, ..INLINE_TAGS)
for current < len(tokens) {
#partial switch peek(ctx).type {
case .A_KEY:
key := to_lower_copy(peek(ctx).text) // TODO, This sucks ass.
if peek(ctx, 1).type == .A_VALUE {
elem.attrs[key] = next(ctx, 1).text
} else {
elem.attrs[key] = "true"
next(ctx)
}
case .TAG_END:
next(ctx)
if any_of(elem.type, ..VOID_TAGS) {
return elem
}
has_closing_tag := is_closed(ctx, elem.type)
inner_for: for current < len(tokens) {
switch {
case peek(ctx).type == .TEXT:
append(&elem.text, peek(ctx).text)
bits.set(&elem.ordering, len(elem.ordering.bits))
next(ctx)
case peek(ctx).type == .ELEMENT:
if any_of(peek(ctx).text, ..BLOCK_TAGS) {
if !has_closing_tag && eq(peek(ctx).text, elem.type) { return elem }
if is_inline { return elem }
}
child := parse_elem(ctx, pre)
if child == nil { continue }
append(&elem.children, child)
child.parent = elem
bits.set(&elem.ordering, len(elem.ordering.bits), false)
case peek(ctx).type == .WHITESPACE:
txt := peek(ctx).text if pre else trim_ws(peek(ctx).text)
append(&elem.text, peek(ctx).text)
bits.set(&elem.ordering, len(elem.ordering.bits), true)
next(ctx)
case peek(ctx).type == .ELEMENT_END && ends_with(peek(ctx).text, elem.type):
return elem
case:
if peek(ctx).type == .ELEMENT_END { next(ctx) }
else { break inner_for }
}
}
case .ELEMENT_END:
if !ends_with(peek(ctx).text, elem.type) && peek(ctx).text != "/" { break }
if peek(ctx).type == .ELEMENT_END { next(ctx) }
if peek(ctx).type == .TAG_END { next(ctx) }
return elem
case: current += 1
} // switch
} // for
return elem
}//}}}
// obviously, there can be <br> anywhere...
is_closed :: proc(using ctx: ^Context, tag: string) -> bool {//{{{
level := 0
for t in tokens[current:] {
if t.type == .ELEMENT && eq(t.text, tag) {
level += 1
} else if t.type == .ELEMENT_END && ends_with(t.text, tag) {
if level <= 0 { return true }
level -= 1
}
}
return false
}//}}}
dbg_fmt_tags :: proc(element: ^Element, level := 0) -> string {//{{{
I := " "
result: [dynamic] u8
fmt_attr :: proc(result: ^[dynamic] u8, value: string) {
append_elems(result, u8(' '), u8('['))
append_elems(result, .. transmute([]u8) value)
append_elems(result, u8(']'), u8(' '))
}
for child in element.children {
for i in 0..<level { append_elems(&result, .. transmute([]u8) I) }
append_elems(&result, .. transmute([]u8) child.type)
if "id" in child.attrs {
fmt_attr(&result, child.attrs["id"])
} else if "name" in child.attrs {
fmt_attr(&result, child.attrs["name"])
} else if "rel" in child.attrs {
fmt_attr(&result, child.attrs["rel"])
} else if "title" in child.attrs {
fmt_attr(&result, child.attrs["title"])
} else {
append_elems(&result, u8(' '), u8('['), u8(']'), u8(' '))
}
append_elems(&result, u8('\n'))
append_elems(&result, .. transmute([]u8) dbg_fmt_tags(child, level + 1))
}
return string(result[:])
}//}}}
// super quick and dirty printer, mostly for debugging {{{
// print_parser :: proc(element: ^Element, level := 0) {
// I := " "
// print :: proc(strs: ..string, end := "\n") {
// for s in strs { pstr(s) }
// }
// print_attrs :: proc(e: ^Element) {
// for k, v in e.attrs { print(k, "=", v, " ") }
// }
// print("<", element.type, " ")
// print_attrs(element)
// print(">")
// child, text: int
// iter := bits.make_iterator(&element.ordering)
// for {
// is_text, index, ok := bits.iterate_by_all(&iter)
// if !ok { break }
// if is_text {
// if element.type != "pre" { pstr(I[:level+1]) }
// pstr(element.text[text])
// text += 1
// } else {
// print_parser(element.children[child], level + 2 if element.type != "pre" else 0)
// child += 1
// }
// }
// print(I[:level], "</", element.type, ">\n")
// }}}}