parse_stack.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package ini
  2. import (
  3. "bytes"
  4. "fmt"
  5. )
  6. // ParseStack is a stack that contains a container, the stack portion,
  7. // and the list which is the list of ASTs that have been successfully
  8. // parsed.
  9. type ParseStack struct {
  10. top int
  11. container []AST
  12. list []AST
  13. index int
  14. }
  15. func newParseStack(sizeContainer, sizeList int) ParseStack {
  16. return ParseStack{
  17. container: make([]AST, sizeContainer),
  18. list: make([]AST, sizeList),
  19. }
  20. }
  21. // Pop will return and truncate the last container element.
  22. func (s *ParseStack) Pop() AST {
  23. s.top--
  24. return s.container[s.top]
  25. }
  26. // Push will add the new AST to the container
  27. func (s *ParseStack) Push(ast AST) {
  28. s.container[s.top] = ast
  29. s.top++
  30. }
  31. // MarkComplete will append the AST to the list of completed statements
  32. func (s *ParseStack) MarkComplete(ast AST) {
  33. s.list[s.index] = ast
  34. s.index++
  35. }
  36. // List will return the completed statements
  37. func (s ParseStack) List() []AST {
  38. return s.list[:s.index]
  39. }
  40. // Len will return the length of the container
  41. func (s *ParseStack) Len() int {
  42. return s.top
  43. }
  44. func (s ParseStack) String() string {
  45. buf := bytes.Buffer{}
  46. for i, node := range s.list {
  47. buf.WriteString(fmt.Sprintf("%d: %v\n", i+1, node))
  48. }
  49. return buf.String()
  50. }