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