-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.php
72 lines (65 loc) · 1.49 KB
/
parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Lol;
require_once 'lexer.php';
require_once 'start.php';
require_once 'string.php';
require_once 'unknown.php';
require_once 'end.php';
require_once 'hai.php';
require_once 'btw.php';
require_once 'visible.php';
require_once 'kthxbai.php';
class Parser
{
protected $tree = array();
protected $statement = array();
protected $token = null;
public function __construct(Lexer $lexer)
{
$this->token = new Token\Start();
array_map(array($this, 'parse_token'), $lexer->tokens);
}
protected function parse_token($source)
{
switch($source['type']) {
case 'T_STRING':
$token = new Token\String($source['value']);
break;
case 'T_UNKNOWN':
$token = new Token\Unknown();
break;
case 'T_END':
$token = new Token\End();
break;
case 'T_HAI':
$token = new Token\Hai();
break;
case 'T_BTW':
$token = new Token\Btw();
break;
case 'T_VISIBLE':
$token = new Token\Visible();
break;
case 'T_KTHXBAI':
$token = new Token\Kthxbai();
break;
default:
throw new \Exception(__FUNCTION__ . ": unimplemented keyword $token");
}
if($this->token->expects($source['type'])) {
$this->token = $token;
if ($token instanceof Token\End) {
$this->tree[] = $this->statement;
$this->statement = array();
} else {
$this->statement[] = $token;
}
} else {
throw new \Exception(__FUNCTION__ . ": parser error: '{$source['type']}' unexpected.");
}
}
public function getTree()
{
return $this->tree;
}
}