1 | <?php |
||
16 | abstract class Parser { |
||
17 | /** |
||
18 | * @var SymbolTable |
||
19 | */ |
||
20 | private $symbol_table; |
||
21 | |||
22 | /** |
||
23 | * @var Tokenizer|null |
||
24 | */ |
||
25 | protected $tokenizer = null; |
||
26 | |||
27 | /** |
||
28 | * @var array|null (Symbol, array $matches) |
||
29 | */ |
||
30 | protected $token = null; |
||
31 | |||
32 | 37 | public function __construct() { |
|
36 | |||
37 | /** |
||
38 | * Parse the string according to this parser. |
||
39 | * |
||
40 | * @return mixed |
||
41 | */ |
||
42 | 37 | public function parse($source) { |
|
53 | |||
54 | /** |
||
55 | * The root for the parse tree. |
||
56 | * |
||
57 | * @return mixed |
||
58 | */ |
||
59 | abstract protected function root(); |
||
60 | |||
61 | // Factory Methods |
||
62 | |||
63 | /** |
||
64 | * Build the Tokenizer. |
||
65 | * |
||
66 | * @return Tokenizer |
||
67 | */ |
||
68 | 37 | public function create_tokenizer($source) { |
|
72 | |||
73 | /** |
||
74 | * Build the SymbolTable |
||
75 | * |
||
76 | * @return SymbolTable |
||
77 | */ |
||
78 | 37 | protected function create_symbol_table() { |
|
81 | |||
82 | /** |
||
83 | * @param SymbolTable |
||
84 | * @return null |
||
85 | */ |
||
86 | abstract protected function add_symbols_to(SymbolTable $table); |
||
87 | |||
88 | // Helpers for defining the grammar. |
||
89 | |||
90 | |||
91 | // Helpers for actual parsing. |
||
92 | |||
93 | /** |
||
94 | * Set the current token to the next token from the tokenizer. |
||
95 | * |
||
96 | * @return null |
||
97 | */ |
||
98 | 35 | protected function fetch_next_token() { |
|
104 | |||
105 | /** |
||
106 | * Get the current symbol. |
||
107 | * |
||
108 | * @return Symbol |
||
109 | */ |
||
110 | 31 | protected function current_symbol() { |
|
113 | |||
114 | /** |
||
115 | * Get the current match. |
||
116 | * |
||
117 | * @return string[] |
||
118 | */ |
||
119 | 35 | protected function current_match() { |
|
122 | |||
123 | /** |
||
124 | * Advance the tokenizer to the next token if current token |
||
125 | * was matched by the given regexp. |
||
126 | * |
||
127 | * @param string $regexp |
||
128 | * @return null |
||
129 | */ |
||
130 | 14 | protected function advance($regexp) { |
|
140 | |||
141 | /** |
||
142 | * Advance the tokenizer to the next token if current token |
||
143 | * was matched by the given operator. |
||
144 | * |
||
145 | * @param string $op |
||
146 | * @return null |
||
147 | */ |
||
148 | 10 | protected function advance_operator($op) { |
|
151 | |||
152 | /** |
||
153 | * Is the end of the file reached? |
||
154 | * |
||
155 | * @return bool |
||
156 | */ |
||
157 | 28 | public function is_end_of_file_reached() { |
|
160 | |||
161 | /** |
||
162 | * Check if the current token was matched by the given regexp. |
||
163 | * |
||
164 | * @param string $regexp |
||
165 | * @return bool |
||
166 | */ |
||
167 | 35 | protected function is_current_token_matched_by($regexp) { |
|
171 | |||
172 | /** |
||
173 | * Check if the current token is the given operator. |
||
174 | * |
||
175 | * @param string $operator |
||
176 | * @return bool |
||
177 | */ |
||
178 | 7 | protected function is_current_token_operator($operator) { |
|
182 | } |
||
183 |