debug.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package parser
  2. import "fmt"
  3. import "strings"
  4. import "unicode/utf8"
  5. import "./syntax"
  6. func Strlen (s string) int {
  7. return utf8.RuneCountInString(s)
  8. }
  9. func GetANSIColor (n int) int {
  10. return 31 + n % 6
  11. }
  12. func Repeat (n int, f func(int)) {
  13. for i := 0; i < n; i++ {
  14. f(i)
  15. }
  16. }
  17. func Fill (buf *strings.Builder, n int, s string, blank string) {
  18. buf.WriteString(s)
  19. Repeat(n-Strlen(s), func (_ int) {
  20. buf.WriteString(blank)
  21. })
  22. }
  23. func PrintTreeNode (ptr int, node *TreeNode) {
  24. var buf strings.Builder
  25. fmt.Fprintf(&buf, "\033[1m\033[%vm", GetANSIColor(ptr))
  26. fmt.Fprintf(&buf, "(%v)", ptr)
  27. fmt.Fprintf(&buf, "\033[0m")
  28. buf.WriteRune(' ')
  29. fmt.Fprintf(&buf, "\033[1m")
  30. buf.WriteString(syntax.Id2Name[node.Part.Id])
  31. fmt.Fprintf(&buf, "\033[0m")
  32. buf.WriteRune(' ')
  33. buf.WriteRune('[')
  34. for i := 0; i < node.Length; i++ {
  35. var child_ptr = node.Children[i]
  36. fmt.Fprintf(&buf, "\033[1m\033[%vm", GetANSIColor(child_ptr))
  37. fmt.Fprintf(&buf, "%v", child_ptr)
  38. fmt.Fprintf(&buf, "\033[0m")
  39. if i != node.Length-1 {
  40. buf.WriteString(", ")
  41. }
  42. }
  43. buf.WriteRune(']')
  44. fmt.Fprintf(
  45. &buf, " <\033[1m\033[%vm%v\033[0m> ",
  46. GetANSIColor(node.Parent), node.Parent,
  47. )
  48. fmt.Fprintf(
  49. &buf, "status=%v, tried=%v, index=%v, pos=%+v, amount=%v\n",
  50. node.Status, node.Tried, node.Index, node.Pos, node.Amount,
  51. )
  52. fmt.Print(buf.String())
  53. }
  54. func PrintBareTree (tree BareTree) {
  55. for i := 0; i < len(tree); i++ {
  56. PrintTreeNode(i, &tree[i])
  57. }
  58. }
  59. func PrintTreeRecursively (
  60. buf *strings.Builder,
  61. tree Tree, ptr int, depth int, is_last []bool,
  62. ) {
  63. const INC = 2
  64. const SPACE = " "
  65. var node = &tree.Nodes[ptr]
  66. Repeat(depth+1, func (i int) {
  67. if depth > 0 && i < depth {
  68. if is_last[i] {
  69. Fill(buf, INC, "", SPACE)
  70. } else {
  71. Fill(buf, INC, "│", SPACE)
  72. }
  73. } else {
  74. if is_last[depth] {
  75. Fill(buf, INC, "└", "─")
  76. } else {
  77. Fill(buf, INC, "├", "─")
  78. }
  79. }
  80. })
  81. if node.Length > 0 {
  82. buf.WriteString("┬─")
  83. } else {
  84. buf.WriteString("──")
  85. }
  86. fmt.Fprintf(buf, "\033[1m\033[%vm", GetANSIColor(depth))
  87. fmt.Fprintf(buf, "[%v]", syntax.Id2Name[node.Part.Id])
  88. fmt.Fprintf(buf, "\033[0m")
  89. fmt.Fprintf(buf, "\033[%vm", GetANSIColor(depth))
  90. buf.WriteRune(' ')
  91. switch node.Part.Partype {
  92. case syntax.MatchToken:
  93. var token = tree.Tokens[node.Pos]
  94. fmt.Fprintf(buf, "'%v'", string(token.Content))
  95. buf.WriteRune(' ')
  96. var point = tree.Info[tree.Tokens[node.Pos].Pos]
  97. fmt.Fprintf(buf, "at <%v,%v>", point.Row, point.Col)
  98. fmt.Fprintf(buf, "\033[0m")
  99. buf.WriteRune('\n')
  100. case syntax.MatchKeyword:
  101. fmt.Fprintf(buf, "\033[0m")
  102. buf.WriteRune('\n')
  103. case syntax.Recursive:
  104. if node.Length == 0 {
  105. buf.WriteString("(empty)")
  106. }
  107. fmt.Fprintf(buf, "\033[0m")
  108. buf.WriteRune('\n')
  109. for i := 0; i < node.Length; i++ {
  110. var child = node.Children[i]
  111. is_last = append(is_last, i == node.Length-1)
  112. PrintTreeRecursively(buf, tree, child, depth+1, is_last)
  113. is_last = is_last[0: len(is_last)-1]
  114. }
  115. }
  116. }
  117. func PrintTree (tree Tree) {
  118. var buf strings.Builder
  119. var is_last = make([]bool, 0, 1000)
  120. is_last = append(is_last, true)
  121. PrintTreeRecursively(&buf, tree, 0, 0, is_last)
  122. fmt.Println(buf.String())
  123. }