aboutsummaryrefslogtreecommitdiff
path: root/core/encoding/xml/debug_print.odin
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2021-11-30 23:01:22 +0100
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2021-12-05 02:52:22 +0100
commitb5c828fe4ee3f0942b2eda1dc5753e4ad6d38ea9 (patch)
treeffbd45adb60e3de951dc2948801d5a57b21dd2c9 /core/encoding/xml/debug_print.odin
parent6ce5608003e630bc0de1c591fd4cbea3fe59e1d3 (diff)
[xml] Initial implementation of `core:encoding/xml`.
A from-scratch XML implementation, loosely modeled on the [spec](https://www.w3.org/TR/2006/REC-xml11-20060816). Features: - Supports enough of the XML 1.0/1.1 spec to handle the 99.9% of XML documents in common current usage. - Simple to understand and use. Small. Caveats: - We do NOT support HTML in this package, as that may or may not be valid XML. If it works, great. If it doesn't, that's not considered a bug. - We do NOT support UTF-16. If you have a UTF-16 XML file, please convert it to UTF-8 first. Also, our condolences. - <[!ELEMENT and <[!ATTLIST are not supported, and will be either ignored or return an error depending on the parser options. TODO: - Optional CDATA unboxing. - Optional `&gt;`, `&#32;`, `&#x20;` and other escape substitution in tag bodies. - Test suite MAYBE: - XML writer? - Serialize/deserialize Odin types?
Diffstat (limited to 'core/encoding/xml/debug_print.odin')
-rw-r--r--core/encoding/xml/debug_print.odin73
1 files changed, 73 insertions, 0 deletions
diff --git a/core/encoding/xml/debug_print.odin b/core/encoding/xml/debug_print.odin
new file mode 100644
index 000000000..0b7ffa822
--- /dev/null
+++ b/core/encoding/xml/debug_print.odin
@@ -0,0 +1,73 @@
+package xml
+/*
+ An XML 1.0 / 1.1 parser
+
+ Copyright 2021 Jeroen van Rijn <nom@duclavier.com>.
+ Made available under Odin's BSD-3 license.
+
+ A from-scratch XML implementation, loosely modeled on the [spec](https://www.w3.org/TR/2006/REC-xml11-20060816).
+
+ List of contributors:
+ Jeroen van Rijn: Initial implementation.
+*/
+import "core:fmt"
+
+/*
+ Just for debug purposes.
+*/
+print :: proc(doc: ^Document) {
+ assert(doc != nil)
+
+ using fmt
+ println("[XML Prolog]")
+
+ for attr in doc.prolog {
+ printf("\t%v: %v\n", attr.key, attr.val)
+ }
+
+ printf("[Encoding] %v\n", doc.encoding)
+ printf("[DOCTYPE] %v\n", doc.doctype.ident)
+
+ if len(doc.doctype.rest) > 0 {
+ printf("\t%v\n", doc.doctype.rest)
+ }
+
+ if doc.root != nil {
+ println(" --- ")
+ print_element(0, doc.root)
+ println(" --- ")
+ }
+}
+
+print_element :: proc(indent: int, element: ^Element) {
+ if element == nil { return }
+ using fmt
+
+ tab :: proc(indent: int) {
+ tabs := "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
+
+ i := max(0, min(indent, len(tabs)))
+ printf("%v", tabs[:i])
+ }
+
+ tab(indent)
+
+ if element.kind == .Element {
+ printf("<%v>\n", element.ident)
+ if len(element.value) > 0 {
+ tab(indent + 1)
+ printf("[Value] %v\n", element.value)
+ }
+
+ for attr in element.attribs {
+ tab(indent + 1)
+ printf("[Attr] %v: %v\n", attr.key, attr.val)
+ }
+
+ for child in element.children {
+ print_element(indent + 1, child)
+ }
+ } else if element.kind == .Comment {
+ printf("[COMMENT] %v\n", element.value)
+ }
+} \ No newline at end of file