1: <?php
2:
3: namespace BN\Compiler\Parser;
4:
5: use BN\Compiler\Token\TokenType;
6:
7: class StackCleaning
8: {
9: private $accumulator;
10:
11: public function __construct(QueueAccumulator $accumulator)
12: {
13: $this->accumulator = $accumulator;
14: }
15:
16: public function pushOperatorsFromStack()
17: {
18: while (!$this->accumulator->isStackEmpty()) {
19: if ($this->accumulator->isOperatorAtTopOfStack()) {
20: $this->accumulator->pushTokenFromStackToQueue();
21: } elseif ($this->isBracketAtTopOfStack()) {
22: $bracket = $this->accumulator->valueAtStackPeek();
23: $this->accumulator->stopParsing('mismatchedBrackets', $bracket);
24: return;
25: }
26: }
27: }
28:
29: private function isBracketAtTopOfStack()
30: {
31: return $this->accumulator->isTypeAtStackPeek(TokenType::BRACKET_OPENING)
32: || $this->accumulator->isTypeAtStackPeek(TokenType::BRACKET_CLOSING);
33: }
34: }
35: