StateStack.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Lexer adapted from Simple Test: http://sourceforge.net/projects/simpletest/
  4. * For an intro to the Lexer see:
  5. * https://web.archive.org/web/20120125041816/http://www.phppatterns.com/docs/develop/simple_test_lexer_notes
  6. *
  7. * @author Marcus Baker http://www.lastcraft.com
  8. */
  9. namespace dokuwiki\Parsing\Lexer;
  10. /**
  11. * States for a stack machine.
  12. */
  13. class StateStack
  14. {
  15. protected $stack;
  16. /**
  17. * Constructor. Starts in named state.
  18. * @param string $start Starting state name.
  19. */
  20. public function __construct($start)
  21. {
  22. $this->stack = array($start);
  23. }
  24. /**
  25. * Accessor for current state.
  26. * @return string State.
  27. */
  28. public function getCurrent()
  29. {
  30. return $this->stack[count($this->stack) - 1];
  31. }
  32. /**
  33. * Adds a state to the stack and sets it to be the current state.
  34. *
  35. * @param string $state New state.
  36. */
  37. public function enter($state)
  38. {
  39. array_push($this->stack, $state);
  40. }
  41. /**
  42. * Leaves the current state and reverts
  43. * to the previous one.
  44. * @return boolean false if we attempt to drop off the bottom of the list.
  45. */
  46. public function leave()
  47. {
  48. if (count($this->stack) == 1) {
  49. return false;
  50. }
  51. array_pop($this->stack);
  52. return true;
  53. }
  54. }