Completed
Push — master ( cc2765...340ff8 )
by Richard
08:19
created

SymbolTable::add_symbol()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 *
5
 * Copyright (c) 2016 Richard Klees <[email protected]>
6
 * 
7
 * This software is licensed under The MIT License. You should have received
8
 * a copy of the license along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Definition;
12
13
/**
14
 * The symbol table knows all symbols we could construct.
15
 */
16
class SymbolTable {
17
    /**
18
     * @var Symbol[]
19
     */
20
    protected $symbols = array();
21
22
    /**
23
     * Generator over the symbols the SymbolTable knows.
24
     *
25
     * @return Generator
26
     */
27 37
    public function symbols() {
28 37
        foreach($this->symbols as $symbol) {
29 36
            yield $symbol;
30 36
        }
31 2
    }
32
33
    /**
34
     * Add a symbol to the table.
35
     *
36
     * @param   string  $regexp
37
     * @param   int     $binding_power
38
     * @throws  \InvalidArgumentException if %$regexp% is not a regexp
39
     * @throws  \LogicException if there already is a symbol with that $regexp.
40
     * @return  Symbol
41
     */
42 39
    public function add_symbol($regexp, $binding_power = 0) {
43 39
        if (array_key_exists($regexp, $this->symbols)) {
44 1
            throw new \LogicException("Symbol for regexp $regexp already exists.");
45
        }
46 39
        $s = new Symbol($regexp, $binding_power);
47 39
        $this->symbols[$regexp] = $s;
48 39
        return $s;
49
    }
50
}
51
52