skipper.go 878 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package ini
  2. // skipper is used to skip certain blocks of an ini file.
  3. // Currently skipper is used to skip nested blocks of ini
  4. // files. See example below
  5. //
  6. // [ foo ]
  7. // nested = ; this section will be skipped
  8. // a=b
  9. // c=d
  10. // bar=baz ; this will be included
  11. type skipper struct {
  12. shouldSkip bool
  13. TokenSet bool
  14. prevTok Token
  15. }
  16. func newSkipper() skipper {
  17. return skipper{
  18. prevTok: emptyToken,
  19. }
  20. }
  21. func (s *skipper) ShouldSkip(tok Token) bool {
  22. // should skip state will be modified only if previous token was new line (NL);
  23. // and the current token is not WhiteSpace (WS).
  24. if s.shouldSkip &&
  25. s.prevTok.Type() == TokenNL &&
  26. tok.Type() != TokenWS {
  27. s.Continue()
  28. return false
  29. }
  30. s.prevTok = tok
  31. return s.shouldSkip
  32. }
  33. func (s *skipper) Skip() {
  34. s.shouldSkip = true
  35. }
  36. func (s *skipper) Continue() {
  37. s.shouldSkip = false
  38. s.prevTok = emptyToken
  39. }