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