aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/Parsing/Lexer/StateStack.php
blob: a2d72e4879c9a2eb5189a758024255d9986f8d97 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php

/**
 * Lexer adapted from Simple Test: http://sourceforge.net/projects/simpletest/
 * For an intro to the Lexer see:
 * https://web.archive.org/web/20120125041816/http://www.phppatterns.com/docs/develop/simple_test_lexer_notes
 *
 * @author Marcus Baker http://www.lastcraft.com
 */

namespace dokuwiki\Parsing\Lexer;

/**
 * States for a stack machine.
 */
class StateStack
{
    protected $stack;

    /**
     * Constructor. Starts in named state.
     * @param string $start        Starting state name.
     */
    public function __construct($start)
    {
        $this->stack = [$start];
    }

    /**
     * Accessor for current state.
     * @return string       State.
     */
    public function getCurrent()
    {
        return $this->stack[count($this->stack) - 1];
    }

    /**
     * Adds a state to the stack and sets it to be the current state.
     *
     * @param string $state        New state.
     */
    public function enter($state)
    {
        $this->stack[] = $state;
    }

    /**
     * Leaves the current state and reverts
     * to the previous one.
     * @return boolean    false if we attempt to drop off the bottom of the list.
     */
    public function leave()
    {
        if (count($this->stack) == 1) {
            return false;
        }
        array_pop($this->stack);
        return true;
    }
}