parser.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package metla
  2. import (
  3. "bytes"
  4. "fmt"
  5. )
  6. type tokenType uint8
  7. const (
  8. tokenTypeUndefined tokenType = iota
  9. tokenTypeText
  10. tokenTypePrint
  11. tokenTypeComment
  12. tokenTypeExec
  13. tokenTypeLiteral
  14. )
  15. func (s tokenType) closeTag() byte {
  16. switch s {
  17. case tokenTypePrint:
  18. return '}'
  19. case tokenTypeExec:
  20. return '%'
  21. case tokenTypeComment:
  22. return '*'
  23. default:
  24. return 0
  25. }
  26. }
  27. func (s tokenType) String() string {
  28. switch s {
  29. case tokenTypeUndefined:
  30. return "Undefined"
  31. case tokenTypeText:
  32. return "Text"
  33. case tokenTypePrint:
  34. return "Print"
  35. case tokenTypeComment:
  36. return "Comment"
  37. case tokenTypeExec:
  38. return "Exec"
  39. case tokenTypeLiteral:
  40. return "Literal"
  41. default:
  42. return ""
  43. }
  44. }
  45. type tError struct {
  46. alias string
  47. line int
  48. pos int
  49. text string
  50. }
  51. func (s tError) Error() string {
  52. return fmt.Sprintf("%v :: Parse error [%v:%v] : %v", s.alias, s.line, s.pos, s.text)
  53. }
  54. func parseBytes(src []byte, alias string) (res []byte, err error) {
  55. var rb bytes.Buffer
  56. lp, lastPos, lastLine := lineParser{src: src}, 0, 0
  57. flushTokenText := func(fromToken bool) {
  58. if lastPos != lp.Pos() {
  59. offset := -1
  60. if fromToken {
  61. offset = 1
  62. }
  63. rb.Write([]byte("flush('"))
  64. rb.Write(bytes.ReplaceAll(bytes.ReplaceAll(lp.Block(lastPos, offset), []byte("'"), []byte("\\'")), []byte("\n"), []byte("\\n")))
  65. rb.Write([]byte("');"))
  66. lastPos, lastLine = lp.Pos(), lp.Line()
  67. }
  68. }
  69. initToken := func() error {
  70. val, tType := lp.Val(), tokenTypeUndefined
  71. switch val {
  72. case '{':
  73. tType = tokenTypePrint
  74. case '*':
  75. tType = tokenTypeComment
  76. case '%':
  77. tType = tokenTypeExec
  78. default:
  79. return nil
  80. }
  81. flushTokenText(true)
  82. for lp.FindNext(tType.closeTag()) {
  83. if lp.Next() && lp.Val() == '}' {
  84. switch tType {
  85. case tokenTypePrint:
  86. {
  87. rb.Write([]byte("flush("))
  88. rb.Write(lp.Block(lastPos+2, 2))
  89. rb.Write([]byte(");"))
  90. }
  91. case tokenTypeExec:
  92. rb.Write(lp.Block(lastPos+2, 2))
  93. rb.Write([]byte(";"))
  94. }
  95. lp.Next()
  96. lastPos, lastLine = lp.Pos(), lp.Line()
  97. return nil
  98. }
  99. }
  100. return tError{
  101. alias: alias,
  102. line: lastLine,
  103. pos: lastPos,
  104. text: fmt.Sprintf("Not found close token for '%v}'", string(tType.closeTag())),
  105. }
  106. }
  107. for lp.CheckNext() {
  108. if lp.FindNext('{') && lp.Next() {
  109. if err = initToken(); err != nil {
  110. return
  111. }
  112. }
  113. }
  114. flushTokenText(false)
  115. res = rb.Bytes()
  116. return
  117. }
  118. type lineParser struct {
  119. src []byte
  120. pos int
  121. line int
  122. }
  123. func (s *lineParser) CheckNext() bool {
  124. return s.pos < len(s.src)-1
  125. }
  126. func (s *lineParser) Next() bool {
  127. if s.CheckNext() {
  128. s.pos++
  129. if s.src[s.pos] == '\n' {
  130. s.line++
  131. }
  132. return true
  133. }
  134. return false
  135. }
  136. func (s *lineParser) Block(start int, offsetRight int) []byte { return s.src[start : s.pos-offsetRight] }
  137. func (s *lineParser) Line() int { return s.line }
  138. func (s *lineParser) Pos() int { return s.pos }
  139. func (s *lineParser) Val() byte { return s.src[s.pos] }
  140. func (s *lineParser) FindNext(val byte) bool {
  141. for {
  142. if s.Val() == val {
  143. return true
  144. }
  145. if !s.Next() {
  146. return false
  147. }
  148. }
  149. }