debug_print.odin 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package encoding_xml
  2. /*
  3. An XML 1.0 / 1.1 parser
  4. Copyright 2021-2022 Jeroen van Rijn <[email protected]>.
  5. Made available under Odin's BSD-3 license.
  6. A from-scratch XML implementation, loosely modeled on the [spec](https://www.w3.org/TR/2006/REC-xml11-20060816).
  7. List of contributors:
  8. Jeroen van Rijn: Initial implementation.
  9. */
  10. import "core:io"
  11. import "core:fmt"
  12. /*
  13. Just for debug purposes.
  14. */
  15. print :: proc(writer: io.Writer, doc: ^Document) -> (written: int, err: io.Error) {
  16. if doc == nil { return }
  17. written += fmt.wprintf(writer, "[XML Prolog]\n")
  18. for attr in doc.prologue {
  19. written += fmt.wprintf(writer, "\t%v: %v\n", attr.key, attr.val)
  20. }
  21. written += fmt.wprintf(writer, "[Encoding] %v\n", doc.encoding)
  22. if len(doc.doctype.ident) > 0 {
  23. written += fmt.wprintf(writer, "[DOCTYPE] %v\n", doc.doctype.ident)
  24. if len(doc.doctype.rest) > 0 {
  25. fmt.wprintf(writer, "\t%v\n", doc.doctype.rest)
  26. }
  27. }
  28. for comment in doc.comments {
  29. written += fmt.wprintf(writer, "[Pre-root comment] %v\n", comment)
  30. }
  31. if len(doc.elements) > 0 {
  32. fmt.wprintln(writer, " --- ")
  33. print_element(writer, doc, 0)
  34. fmt.wprintln(writer, " --- ")
  35. }
  36. return written, .None
  37. }
  38. print_element :: proc(writer: io.Writer, doc: ^Document, element_id: Element_ID, indent := 0) -> (written: int, err: io.Error) {
  39. tab :: proc(writer: io.Writer, indent: int) {
  40. for _ in 0..=indent {
  41. fmt.wprintf(writer, "\t")
  42. }
  43. }
  44. tab(writer, indent)
  45. element := doc.elements[element_id]
  46. if element.kind == .Element {
  47. fmt.wprintf(writer, "<%v>\n", element.ident)
  48. for value in element.value {
  49. switch v in value {
  50. case string:
  51. tab(writer, indent + 1)
  52. fmt.wprintf(writer, "[Value] %v\n", v)
  53. case Element_ID:
  54. print_element(writer, doc, v, indent + 1)
  55. }
  56. }
  57. for attr in element.attribs {
  58. tab(writer, indent + 1)
  59. fmt.wprintf(writer, "[Attr] %v: %v\n", attr.key, attr.val)
  60. }
  61. } else if element.kind == .Comment {
  62. fmt.wprintf(writer, "[COMMENT] %v\n", element.value)
  63. }
  64. return written, .None
  65. }