[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/Parsing/Lexer/ -> StateStack.php (source)

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