1 | <?php |
||
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() { |
|
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) { |
|
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) { |
|
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) { |
|
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) { |
|
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) { |
|
114 | } |
||
115 | |||
116 |