Completed
Push — master ( bd87f9...9971b9 )
by Richard
06:03
created

SymbolTable::symbol()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
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
    // HELPERS that make defining symbols a little more concise.
52
53
    /**
54
     * Add a symbol to the symbol table.
55
     *
56
     * @param   string  $regexp
57
     * @param   int     $binding_power
58
     * @throws  \InvalidArgumentException if %$regexp% is not a regexp
59
     * @throws  \LogicException if there already is a symbol with that $regexp.
60
     * @return  Symbol
61
     */
62 37
    public function symbol($regexp, $binding_power = 0) {
63 37
        return $this->add_symbol($regexp, $binding_power);
64
    }
65
66
    /**
67
     * Add an operator to the symbol table.
68
     *
69
     * Convenience, will split the given string and wrap each char in []
70
     * before passing it to symbol.
71
     *
72
     * @param   string  $op
73
     * @param   int     $binding_power
74
     * @throws  \InvalidArgumentException if %$regexp% is not a regexp
75
     * @throws  \LogicException if there already is a symbol with that $regexp.
76
     * @return  Symbol
77
     */
78 37
    public function operator($op, $binding_power = 0) {
79 37
        $regexp = $this->operator_regexp($op);
80 37
        return $this->symbol($regexp, $binding_power);
81
    }
82
83
    /**
84
     * Add a literal to the symbol table, where the matches are
85
     * transformed using the $converter.
86
     *
87
     * @param   string      $regexp
88
     * @param   \Closure    $converter
89
     * @throws  \InvalidArgumentException if %$regexp% is not a regexp
90
     * @throws  \LogicException if there already is a symbol with that $regexp.
91
     * @return  Symbol
92
     */
93 37
    public function literal($regexp, $converter) {
94 37
        return $this->symbol($regexp)
95 37
            ->null_denotation_is($converter);
96
    }
97
98
    /**
99
     * "abc" -> "[a][b][c]" 
100
     *
101
     * Makes handling operators like "*" easier.
102
     *
103
     * @param   string  $op
104
     * @return  string
105
     */
106 37
    public function operator_regexp($op) {
107 37
        assert('is_string($op)');
108 37
        $regexp = array();
109 37
        foreach (str_split($op, 1) as $c) {
110 37
            $regexp[] = "[$c]";
111 37
        }
112 37
        return implode("", $regexp);
113
    }
114
}
115
116