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