Completed
Push — master ( 56b74c...17974d )
by Ilias
02:06
created

Parser::parseTokenStream()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 23
ccs 17
cts 17
cp 1
rs 8.7972
cc 4
eloc 14
nc 8
nop 2
crap 4
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
        while (! $operatorStack->isEmpty()) {
37 7
            $operandStack->push(
38
                $tokenMap
39 7
                    ->buildOperand($operatorStack->pop())
40 7
                    ->consumeTokens($operandStack)
41 7
            );
42 7
        }
43
44 14
        return $operandStack->isEmpty()
45 14
            ? null
46 14
            : $operandStack->top();
47
    }
48
}
49