parse.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package jade
  5. import (
  6. "fmt"
  7. "net/http"
  8. "runtime"
  9. )
  10. // Tree is the representation of a single parsed template.
  11. type tree struct {
  12. Name string // name of the template represented by the tree.
  13. Root *listNode // top-level root of the tree.
  14. text string // text parsed to create the template (or its parent)
  15. // Parsing only; cleared after parse.
  16. lex *lexer
  17. token [3]item // three-token lookahead for parser.
  18. peekCount int
  19. mixin map[string]*mixinNode
  20. block map[string]*listNode
  21. fs http.FileSystem // embedded file system
  22. }
  23. // Copy returns a copy of the Tree. Any parsing state is discarded.
  24. func (t *tree) Copy() *tree {
  25. if t == nil {
  26. return nil
  27. }
  28. return &tree{
  29. Name: t.Name,
  30. Root: t.Root.CopyList(),
  31. text: t.text,
  32. }
  33. }
  34. // next returns the next token.
  35. func (t *tree) next() item {
  36. if t.peekCount > 0 {
  37. t.peekCount--
  38. } else {
  39. t.token[0] = t.lex.nextItem()
  40. }
  41. return t.token[t.peekCount]
  42. }
  43. // backup backs the input stream up one token.
  44. func (t *tree) backup() {
  45. t.peekCount++
  46. }
  47. // backup2 backs the input stream up two tokens.
  48. // The zeroth token is already there.
  49. func (t *tree) backup2(t1 item) {
  50. t.token[1] = t1
  51. t.peekCount = 2
  52. }
  53. // backup3 backs the input stream up three tokens
  54. // The zeroth token is already there.
  55. func (t *tree) backup3(t2, t1 item) { // Reverse order: we're pushing back.
  56. t.token[1] = t1
  57. t.token[2] = t2
  58. t.peekCount = 3
  59. }
  60. // peek returns but does not consume the next token.
  61. func (t *tree) peek() item {
  62. if t.peekCount > 0 {
  63. return t.token[t.peekCount-1]
  64. }
  65. t.peekCount = 1
  66. t.token[0] = t.lex.nextItem()
  67. return t.token[0]
  68. }
  69. // nextNonSpace returns the next non-space token.
  70. func (t *tree) nextNonSpace() (token item) {
  71. for {
  72. token = t.next()
  73. if token.typ != itemIdent && token.typ != itemEndL && token.typ != itemEmptyLine {
  74. break
  75. }
  76. }
  77. // fmt.Println("\t\tnextNonSpace", token.val)
  78. return token
  79. }
  80. // peekNonSpace returns but does not consume the next non-space token.
  81. func (t *tree) peekNonSpace() (token item) {
  82. for {
  83. token = t.next()
  84. if token.typ != itemIdent && token.typ != itemEndL && token.typ != itemEmptyLine {
  85. break
  86. }
  87. }
  88. t.backup()
  89. return token
  90. }
  91. // errorf formats the error and terminates processing.
  92. func (t *tree) errorf(format string, args ...interface{}) {
  93. t.Root = nil
  94. format = fmt.Sprintf("template:%d: %s", t.token[0].line, format)
  95. panic(fmt.Errorf(format, args...))
  96. }
  97. //
  98. //
  99. //
  100. // recover is the handler that turns panics into returns from the top level of Parse.
  101. func (t *tree) recover(errp *error) {
  102. e := recover()
  103. if e != nil {
  104. if _, ok := e.(runtime.Error); ok {
  105. panic(e)
  106. }
  107. if t != nil {
  108. t.lex.drain()
  109. t.lex = nil
  110. }
  111. *errp = e.(error)
  112. }
  113. }
  114. func (t *tree) Parse(text []byte) (tree *tree, err error) {
  115. defer t.recover(&err)
  116. t.lex = lex(t.Name, text)
  117. t.text = string(text)
  118. t.topParse()
  119. t.lex = nil
  120. return t, nil
  121. }
  122. // New allocates a new parse tree with the given name.
  123. func New(name string) *tree {
  124. return &tree{
  125. Name: name,
  126. mixin: map[string]*mixinNode{},
  127. block: map[string]*listNode{},
  128. }
  129. }