xref: /dokuwiki/inc/Parsing/Lexer/StateStack.php (revision 1c00c02121477c81b95fe750b94acdf109cba20a)
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
11namespace dokuwiki\Parsing\Lexer;
12
13/**
14 * States for a stack machine.
15 */
16class 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     * The full state stack, from the oldest (bottom) state to the current
40     * (top) one.
41     *
42     * @return string[]
43     */
44    public function getStack()
45    {
46        return $this->stack;
47    }
48
49    /**
50     * Adds a state to the stack and sets it to be the current state.
51     *
52     * @param string $state        New state.
53     */
54    public function enter($state)
55    {
56        $this->stack[] = $state;
57    }
58
59    /**
60     * Leaves the current state and reverts
61     * to the previous one.
62     * @return boolean    false if we attempt to drop off the bottom of the list.
63     */
64    public function leave()
65    {
66        if (count($this->stack) == 1) {
67            return false;
68        }
69        array_pop($this->stack);
70        return true;
71    }
72}
73