Completed
Push — master ( e15c58...b150a8 )
by Changwan
07:08
created

SyntaxAnalyzer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 64
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setTokens() 0 4 1
A addSyntax() 0 5 1
B analyze() 0 29 3
1
<?php
2
namespace Wandu\Compiler;
3
4
use Closure;
5
use Wandu\Compiler\Exception\UnknownTokenException;
6
use Wandu\Compiler\Exception\UnknownTokenFromLexException;
7
8
/**
9
 * Use LALR Parsing Table.
10
 */
11
class SyntaxAnalyzer
12
{
13
    /** @var \Wandu\Compiler\LexicalAnalyzer */
14
    protected $lexer;
15
16
    protected $tokens = [];
17
18
    protected $syntaxes;
19
20
    /**
21
     * @param \Wandu\Compiler\LexicalAnalyzer $lexer
22
     */
23
    public function __construct(LexicalAnalyzer $lexer)
24
    {
25
        $this->lexer = $lexer;
26
    }
27
28
    public function setTokens(array $tokens) // Nonterminal
29
    {
30
        $this->tokens = array_unique_union($this->tokens, $tokens);
31
    }
32
33
    /**
34
     * @param $nonTermName
35
     * @param array $tokens
36
     * @param \Closure $handler
37
     * @return self
38
     */
39
    public function addSyntax($nonTermName, array $tokens, Closure $handler)
0 ignored issues
show
Unused Code introduced by
The parameter $handler is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        $this->syntaxes[] = [$nonTermName, $tokens, ];//$handler];
42
        return $this;
43
    }
44
45
    public function analyze($context)
46
    {
47
        // get lex tokens
48
        $predefinedTokens = array_flip($this->tokens);
49
        $lexTokens = array_map(function ($lexToken) use ($predefinedTokens) {
0 ignored issues
show
Unused Code introduced by
$lexTokens is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
            if ($lexToken instanceof Token) {
51
                return $lexToken;
52
            }
53
            if (isset($predefinedTokens[$lexToken])) {
54
                return new Token($lexToken);
55
            }
56
            throw new UnknownTokenFromLexException($lexToken);
57
        }, $this->lexer->analyze($context));
58
59
        // get FIRST
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
60
//        print_r($this->syntaxes);
61
62
        // get FOLLOW
63
64
        // get LOOKAHEAD
65
66
        // with null
67
68
        //
69
70
        $parsingTables = []; // [NO_STATES][NO_SYMBOLS + 1]
0 ignored issues
show
Unused Code introduced by
$parsingTables is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
//        print_r($lexTokens);
72
        return '';
73
    }
74
}
75