123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- package ast
- import (
- "fmt"
- "strings"
- )
- // printVisitor implements the Visitor interface to print a AST.
- type printVisitor struct {
- buf string
- depth int
- original bool
- inBlock bool
- }
- func newPrintVisitor() *printVisitor {
- return &printVisitor{}
- }
- // Print returns a string representation of given AST, that can be used for debugging purpose.
- func Print(node Node) string {
- visitor := newPrintVisitor()
- node.Accept(visitor)
- return visitor.output()
- }
- func (v *printVisitor) output() string {
- return v.buf
- }
- func (v *printVisitor) indent() {
- for i := 0; i < v.depth; {
- v.buf += " "
- i++
- }
- }
- func (v *printVisitor) str(val string) {
- v.buf += val
- }
- func (v *printVisitor) nl() {
- v.str("\n")
- }
- func (v *printVisitor) line(val string) {
- v.indent()
- v.str(val)
- v.nl()
- }
- //
- // Visitor interface
- //
- // Statements
- // VisitProgram implements corresponding Visitor interface method
- func (v *printVisitor) VisitProgram(node *Program) interface{} {
- if len(node.BlockParams) > 0 {
- v.line("BLOCK PARAMS: [ " + strings.Join(node.BlockParams, " ") + " ]")
- }
- for _, n := range node.Body {
- n.Accept(v)
- }
- return nil
- }
- // VisitMustache implements corresponding Visitor interface method
- func (v *printVisitor) VisitMustache(node *MustacheStatement) interface{} {
- v.indent()
- v.str("{{ ")
- node.Expression.Accept(v)
- v.str(" }}")
- v.nl()
- return nil
- }
- // VisitBlock implements corresponding Visitor interface method
- func (v *printVisitor) VisitBlock(node *BlockStatement) interface{} {
- v.inBlock = true
- v.line("BLOCK:")
- v.depth++
- node.Expression.Accept(v)
- if node.Program != nil {
- v.line("PROGRAM:")
- v.depth++
- node.Program.Accept(v)
- v.depth--
- }
- if node.Inverse != nil {
- // if node.Program != nil {
- // v.depth++
- // }
- v.line("{{^}}")
- v.depth++
- node.Inverse.Accept(v)
- v.depth--
- // if node.Program != nil {
- // v.depth--
- // }
- }
- v.inBlock = false
- return nil
- }
- // VisitPartial implements corresponding Visitor interface method
- func (v *printVisitor) VisitPartial(node *PartialStatement) interface{} {
- v.indent()
- v.str("{{> PARTIAL:")
- v.original = true
- node.Name.Accept(v)
- v.original = false
- if len(node.Params) > 0 {
- v.str(" ")
- node.Params[0].Accept(v)
- }
- // hash
- if node.Hash != nil {
- v.str(" ")
- node.Hash.Accept(v)
- }
- v.str(" }}")
- v.nl()
- return nil
- }
- // VisitContent implements corresponding Visitor interface method
- func (v *printVisitor) VisitContent(node *ContentStatement) interface{} {
- v.line("CONTENT[ '" + node.Value + "' ]")
- return nil
- }
- // VisitComment implements corresponding Visitor interface method
- func (v *printVisitor) VisitComment(node *CommentStatement) interface{} {
- v.line("{{! '" + node.Value + "' }}")
- return nil
- }
- // Expressions
- // VisitExpression implements corresponding Visitor interface method
- func (v *printVisitor) VisitExpression(node *Expression) interface{} {
- if v.inBlock {
- v.indent()
- }
- // path
- node.Path.Accept(v)
- // params
- v.str(" [")
- for i, n := range node.Params {
- if i > 0 {
- v.str(", ")
- }
- n.Accept(v)
- }
- v.str("]")
- // hash
- if node.Hash != nil {
- v.str(" ")
- node.Hash.Accept(v)
- }
- if v.inBlock {
- v.nl()
- }
- return nil
- }
- // VisitSubExpression implements corresponding Visitor interface method
- func (v *printVisitor) VisitSubExpression(node *SubExpression) interface{} {
- node.Expression.Accept(v)
- return nil
- }
- // VisitPath implements corresponding Visitor interface method
- func (v *printVisitor) VisitPath(node *PathExpression) interface{} {
- if v.original {
- v.str(node.Original)
- } else {
- path := strings.Join(node.Parts, "/")
- result := ""
- if node.Data {
- result += "@"
- }
- v.str(result + "PATH:" + path)
- }
- return nil
- }
- // Literals
- // VisitString implements corresponding Visitor interface method
- func (v *printVisitor) VisitString(node *StringLiteral) interface{} {
- if v.original {
- v.str(node.Value)
- } else {
- v.str("\"" + node.Value + "\"")
- }
- return nil
- }
- // VisitBoolean implements corresponding Visitor interface method
- func (v *printVisitor) VisitBoolean(node *BooleanLiteral) interface{} {
- if v.original {
- v.str(node.Original)
- } else {
- v.str(fmt.Sprintf("BOOLEAN{%s}", node.Canonical()))
- }
- return nil
- }
- // VisitNumber implements corresponding Visitor interface method
- func (v *printVisitor) VisitNumber(node *NumberLiteral) interface{} {
- if v.original {
- v.str(node.Original)
- } else {
- v.str(fmt.Sprintf("NUMBER{%s}", node.Canonical()))
- }
- return nil
- }
- // Miscellaneous
- // VisitHash implements corresponding Visitor interface method
- func (v *printVisitor) VisitHash(node *Hash) interface{} {
- v.str("HASH{")
- for i, p := range node.Pairs {
- if i > 0 {
- v.str(", ")
- }
- p.Accept(v)
- }
- v.str("}")
- return nil
- }
- // VisitHashPair implements corresponding Visitor interface method
- func (v *printVisitor) VisitHashPair(node *HashPair) interface{} {
- v.str(node.Key + "=")
- node.Val.Accept(v)
- return nil
- }
|