print.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package ast
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // printVisitor implements the Visitor interface to print a AST.
  7. type printVisitor struct {
  8. buf string
  9. depth int
  10. original bool
  11. inBlock bool
  12. }
  13. func newPrintVisitor() *printVisitor {
  14. return &printVisitor{}
  15. }
  16. // Print returns a string representation of given AST, that can be used for debugging purpose.
  17. func Print(node Node) string {
  18. visitor := newPrintVisitor()
  19. node.Accept(visitor)
  20. return visitor.output()
  21. }
  22. func (v *printVisitor) output() string {
  23. return v.buf
  24. }
  25. func (v *printVisitor) indent() {
  26. for i := 0; i < v.depth; {
  27. v.buf += " "
  28. i++
  29. }
  30. }
  31. func (v *printVisitor) str(val string) {
  32. v.buf += val
  33. }
  34. func (v *printVisitor) nl() {
  35. v.str("\n")
  36. }
  37. func (v *printVisitor) line(val string) {
  38. v.indent()
  39. v.str(val)
  40. v.nl()
  41. }
  42. //
  43. // Visitor interface
  44. //
  45. // Statements
  46. // VisitProgram implements corresponding Visitor interface method
  47. func (v *printVisitor) VisitProgram(node *Program) interface{} {
  48. if len(node.BlockParams) > 0 {
  49. v.line("BLOCK PARAMS: [ " + strings.Join(node.BlockParams, " ") + " ]")
  50. }
  51. for _, n := range node.Body {
  52. n.Accept(v)
  53. }
  54. return nil
  55. }
  56. // VisitMustache implements corresponding Visitor interface method
  57. func (v *printVisitor) VisitMustache(node *MustacheStatement) interface{} {
  58. v.indent()
  59. v.str("{{ ")
  60. node.Expression.Accept(v)
  61. v.str(" }}")
  62. v.nl()
  63. return nil
  64. }
  65. // VisitBlock implements corresponding Visitor interface method
  66. func (v *printVisitor) VisitBlock(node *BlockStatement) interface{} {
  67. v.inBlock = true
  68. v.line("BLOCK:")
  69. v.depth++
  70. node.Expression.Accept(v)
  71. if node.Program != nil {
  72. v.line("PROGRAM:")
  73. v.depth++
  74. node.Program.Accept(v)
  75. v.depth--
  76. }
  77. if node.Inverse != nil {
  78. // if node.Program != nil {
  79. // v.depth++
  80. // }
  81. v.line("{{^}}")
  82. v.depth++
  83. node.Inverse.Accept(v)
  84. v.depth--
  85. // if node.Program != nil {
  86. // v.depth--
  87. // }
  88. }
  89. v.inBlock = false
  90. return nil
  91. }
  92. // VisitPartial implements corresponding Visitor interface method
  93. func (v *printVisitor) VisitPartial(node *PartialStatement) interface{} {
  94. v.indent()
  95. v.str("{{> PARTIAL:")
  96. v.original = true
  97. node.Name.Accept(v)
  98. v.original = false
  99. if len(node.Params) > 0 {
  100. v.str(" ")
  101. node.Params[0].Accept(v)
  102. }
  103. // hash
  104. if node.Hash != nil {
  105. v.str(" ")
  106. node.Hash.Accept(v)
  107. }
  108. v.str(" }}")
  109. v.nl()
  110. return nil
  111. }
  112. // VisitContent implements corresponding Visitor interface method
  113. func (v *printVisitor) VisitContent(node *ContentStatement) interface{} {
  114. v.line("CONTENT[ '" + node.Value + "' ]")
  115. return nil
  116. }
  117. // VisitComment implements corresponding Visitor interface method
  118. func (v *printVisitor) VisitComment(node *CommentStatement) interface{} {
  119. v.line("{{! '" + node.Value + "' }}")
  120. return nil
  121. }
  122. // Expressions
  123. // VisitExpression implements corresponding Visitor interface method
  124. func (v *printVisitor) VisitExpression(node *Expression) interface{} {
  125. if v.inBlock {
  126. v.indent()
  127. }
  128. // path
  129. node.Path.Accept(v)
  130. // params
  131. v.str(" [")
  132. for i, n := range node.Params {
  133. if i > 0 {
  134. v.str(", ")
  135. }
  136. n.Accept(v)
  137. }
  138. v.str("]")
  139. // hash
  140. if node.Hash != nil {
  141. v.str(" ")
  142. node.Hash.Accept(v)
  143. }
  144. if v.inBlock {
  145. v.nl()
  146. }
  147. return nil
  148. }
  149. // VisitSubExpression implements corresponding Visitor interface method
  150. func (v *printVisitor) VisitSubExpression(node *SubExpression) interface{} {
  151. node.Expression.Accept(v)
  152. return nil
  153. }
  154. // VisitPath implements corresponding Visitor interface method
  155. func (v *printVisitor) VisitPath(node *PathExpression) interface{} {
  156. if v.original {
  157. v.str(node.Original)
  158. } else {
  159. path := strings.Join(node.Parts, "/")
  160. result := ""
  161. if node.Data {
  162. result += "@"
  163. }
  164. v.str(result + "PATH:" + path)
  165. }
  166. return nil
  167. }
  168. // Literals
  169. // VisitString implements corresponding Visitor interface method
  170. func (v *printVisitor) VisitString(node *StringLiteral) interface{} {
  171. if v.original {
  172. v.str(node.Value)
  173. } else {
  174. v.str("\"" + node.Value + "\"")
  175. }
  176. return nil
  177. }
  178. // VisitBoolean implements corresponding Visitor interface method
  179. func (v *printVisitor) VisitBoolean(node *BooleanLiteral) interface{} {
  180. if v.original {
  181. v.str(node.Original)
  182. } else {
  183. v.str(fmt.Sprintf("BOOLEAN{%s}", node.Canonical()))
  184. }
  185. return nil
  186. }
  187. // VisitNumber implements corresponding Visitor interface method
  188. func (v *printVisitor) VisitNumber(node *NumberLiteral) interface{} {
  189. if v.original {
  190. v.str(node.Original)
  191. } else {
  192. v.str(fmt.Sprintf("NUMBER{%s}", node.Canonical()))
  193. }
  194. return nil
  195. }
  196. // Miscellaneous
  197. // VisitHash implements corresponding Visitor interface method
  198. func (v *printVisitor) VisitHash(node *Hash) interface{} {
  199. v.str("HASH{")
  200. for i, p := range node.Pairs {
  201. if i > 0 {
  202. v.str(", ")
  203. }
  204. p.Accept(v)
  205. }
  206. v.str("}")
  207. return nil
  208. }
  209. // VisitHashPair implements corresponding Visitor interface method
  210. func (v *printVisitor) VisitHashPair(node *HashPair) interface{} {
  211. v.str(node.Key + "=")
  212. node.Val.Accept(v)
  213. return nil
  214. }