Merge "StringUtils: Add a utility for checking if a string is a valid regex"
[lhc/web/wiklou.git] / includes / parser / PPDStack.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Parser
20 */
21
22 /**
23 * Stack class to help Preprocessor::preprocessToObj()
24 * @ingroup Parser
25 */
26 class PPDStack {
27 /** @var PPDStackElement[] */
28 public $stack;
29 public $rootAccum;
30 /** @var string|array */
31 public $accum;
32
33 /**
34 * @var PPDStackElement|false
35 */
36 public $top;
37 public $out;
38 public $elementClass = PPDStackElement::class;
39
40 public static $false = false;
41
42 public function __construct() {
43 $this->stack = [];
44 $this->top = false;
45 $this->rootAccum = '';
46 $this->accum =& $this->rootAccum;
47 }
48
49 /**
50 * @return int
51 */
52 public function count() {
53 return count( $this->stack );
54 }
55
56 public function &getAccum() {
57 return $this->accum;
58 }
59
60 /**
61 * @return bool|PPDPart
62 */
63 public function getCurrentPart() {
64 if ( $this->top === false ) {
65 return false;
66 } else {
67 return $this->top->getCurrentPart();
68 }
69 }
70
71 public function push( $data ) {
72 if ( $data instanceof $this->elementClass ) {
73 $this->stack[] = $data;
74 } else {
75 $class = $this->elementClass;
76 $this->stack[] = new $class( $data );
77 }
78 $this->top = $this->stack[count( $this->stack ) - 1];
79 $this->accum =& $this->top->getAccum();
80 }
81
82 public function pop() {
83 if ( $this->stack === [] ) {
84 throw new MWException( __METHOD__ . ': no elements remaining' );
85 }
86 $temp = array_pop( $this->stack );
87
88 if ( count( $this->stack ) ) {
89 $this->top = $this->stack[count( $this->stack ) - 1];
90 $this->accum =& $this->top->getAccum();
91 } else {
92 $this->top = self::$false;
93 $this->accum =& $this->rootAccum;
94 }
95 return $temp;
96 }
97
98 public function addPart( $s = '' ) {
99 $this->top->addPart( $s );
100 $this->accum =& $this->top->getAccum();
101 }
102
103 /**
104 * @return array
105 */
106 public function getFlags() {
107 if ( $this->stack === [] ) {
108 return [
109 'findEquals' => false,
110 'findPipe' => false,
111 'inHeading' => false,
112 ];
113 } else {
114 return $this->top->getFlags();
115 }
116 }
117 }