parser.go 3.1 KB

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