Parser::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
4
namespace CondParse;
5
6
7
use CondParse\Exception\ParserException;
8
9
class Parser implements ParserInterface
10
{
11
    /** @var TokenParser */
12
    private $tokenParser;
13
14 14
    public function __construct(TokenParser $tokenParser = null)
15
    {
16 14
        $this->tokenParser = $tokenParser ?: new TokenParser;
17 14
    }
18
19
    /**
20
     * @param \Traversable $tokenStream
21
     * @param TokenMap $tokenMap
22
     * @return OperandInterface
23
     * @throws ParserException
24
     */
25 14
    public function parseTokenStream(\Traversable $tokenStream, TokenMap $tokenMap)
26
    {
27 14
        $operandStack = new OperandStack();
28 14
        $operatorStack = new \SplStack();
29
30 14
        foreach ($tokenStream as $lexerToken) {
31 13
            $this->tokenParser->parseToken(
32 13
                new TokenParserParameter($lexerToken, $operandStack, $operatorStack, $tokenMap)
33 13
            );
34 14
        }
35
36 14
        $this->unfoldStack($tokenMap, $operatorStack, $operandStack);
37
38 14
        return $operandStack->top();
39
    }
40
41
    /**
42
     * @param TokenMap $tokenMap
43
     * @param \SplStack $operatorStack
44
     * @param OperandStack $operandStack
45
     */
46 14
    protected function unfoldStack(TokenMap $tokenMap, $operatorStack, $operandStack)
47
    {
48 14
        while (!$operatorStack->isEmpty()) {
49 8
            $operandStack->push(
50
                $tokenMap
51 8
                    ->buildOperand($operatorStack->pop())
52 8
                    ->consumeTokens($operandStack)
53 8
            );
54 8
        }
55 14
    }
56
}
57