1
|
|
|
<?php |
2
|
|
|
define("T_G_IDENT", 253); |
3
|
|
|
|
4
|
|
|
class mod_grant { |
5
|
|
|
function get_token() { |
6
|
|
|
$this->token = $this->token_ahead; |
|
|
|
|
7
|
|
|
$this->token_value = $this->token_ahead_value; |
|
|
|
|
8
|
|
|
$this->input = preg_replace('/^([[:space:]]|,)*/', '', $this->input); |
|
|
|
|
9
|
|
|
//echo "scanner: input(".$this->input.")<br>\n"; |
10
|
|
|
switch (true) { |
|
|
|
|
11
|
|
View Code Duplication |
case preg_match('/^([a-zA-Z0-9_][a-zA-Z0-9._:]*)/', $this->input, $regs): |
12
|
|
|
$this->token_ahead = T_G_IDENT; |
13
|
|
|
$this->token_ahead_value = $regs[1]; |
|
|
|
|
14
|
|
|
$this->input = substr($this->input, strlen($regs[1])); |
|
|
|
|
15
|
|
|
break; |
16
|
|
View Code Duplication |
case preg_match('/^([]+->=(){}[])/', $this->input, $regs): |
17
|
|
|
$this->token_ahead = $regs[1]; |
|
|
|
|
18
|
|
|
$this->token_ahead_value = $regs[1]; |
|
|
|
|
19
|
|
|
$this->input = substr($this->input, 1); |
20
|
|
|
break; |
21
|
|
|
default: |
22
|
|
|
$this->token_ahead = 0; |
23
|
|
|
$this->token_ahead_value = ''; |
|
|
|
|
24
|
|
|
} |
25
|
|
|
//echo "scanner:: (".$this->token_ahead.") (".$this->token_ahead_value.")<br>\n"; |
26
|
|
|
return $this->token_ahead; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
function parse_stmt(&$grants) { |
30
|
|
|
$g_array = Array(); |
31
|
|
|
switch ($this->token_ahead) { |
32
|
|
|
case '-': |
33
|
|
|
$g_op = '-'; |
34
|
|
|
$this->get_token(); |
35
|
|
|
break; |
36
|
|
|
case '+': |
37
|
|
|
$this->get_token(); |
38
|
|
|
default: |
39
|
|
|
$g_op = '+'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
switch ($this->token_ahead) { |
43
|
|
|
case '>': |
44
|
|
|
$g_val = ARGRANTCHILDREN; |
45
|
|
|
$this->get_token(); |
46
|
|
|
break; |
47
|
|
|
case '=': |
48
|
|
|
$g_val = ARGRANTLOCAL; |
49
|
|
|
$this->get_token(); |
50
|
|
|
break; |
51
|
|
|
default: |
52
|
|
|
$g_val = ARGRANTGLOBAL; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($this->token_ahead === '[') { |
56
|
|
|
$this->get_token(); |
57
|
|
|
while ($this->token_ahead === T_G_IDENT) { |
58
|
|
|
$this->get_token(); |
59
|
|
|
$g_array[$this->token_value] = $g_val; |
60
|
|
|
} |
61
|
|
|
if (!$this->token_ahead === ']') { |
62
|
|
|
$this->error = 'expected ]'; |
|
|
|
|
63
|
|
|
return 0; |
64
|
|
|
} |
65
|
|
|
$this->get_token(); |
66
|
|
|
} else |
67
|
|
|
if ($this->token_ahead === T_G_IDENT) { |
68
|
|
|
$this->get_token(); |
69
|
|
|
$g_array[$this->token_value] = $g_val; |
70
|
|
|
} else { |
71
|
|
|
return 0; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$m_array = Array(); |
75
|
|
|
|
76
|
|
|
/* parse modifiers */ |
77
|
|
|
if ($this->token_ahead === '(') { |
78
|
|
|
$this->get_token(); |
79
|
|
|
do { |
80
|
|
View Code Duplication |
switch ($this->token_ahead) { |
81
|
|
|
case '>': |
82
|
|
|
$m_val = ARGRANTCHILDREN; |
83
|
|
|
$this->get_token(); |
84
|
|
|
break; |
85
|
|
|
case '=': |
86
|
|
|
$m_val = ARGRANTLOCAL; |
87
|
|
|
$this->get_token(); |
88
|
|
|
break; |
89
|
|
|
default: |
90
|
|
|
$m_val = $g_val; |
91
|
|
|
} |
92
|
|
|
if ($this->token_ahead != T_G_IDENT) { |
93
|
|
|
$this->error = 'expected modifier near: '.$this->input; |
94
|
|
|
return 0; |
95
|
|
|
} |
96
|
|
|
$this->get_token(); |
97
|
|
|
$m_array[$this->token_value] = $m_val; |
98
|
|
|
} while ($this->token_ahead && $this->token_ahead != ')'); |
99
|
|
|
|
100
|
|
|
if ($this->token_ahead != ')') { |
101
|
|
|
$this->error = 'modifier list is not closed'; |
102
|
|
|
return 0; |
103
|
|
|
} |
104
|
|
|
$this->get_token(); |
105
|
|
|
|
106
|
|
|
foreach($g_array as $grant => $g_val) { |
107
|
|
|
$g_array[$grant] = $m_array; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
foreach($g_array as $grant => $g_val) { |
112
|
|
|
if ($g_op === '-') { |
113
|
|
|
unset($grants[$grant]); |
114
|
|
|
} else { |
115
|
|
|
$grants[$grant] = $g_val; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
return 1; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
function compile($input, &$grants) { |
122
|
|
|
$this->input = $input; |
123
|
|
|
$this->get_token(); |
124
|
|
|
while (!$this->error && $this->parse_stmt($grants)); |
125
|
|
|
ksort($grants); |
126
|
|
|
foreach( $grants as $key => $value ) { |
127
|
|
|
if( is_array($value) ) { |
128
|
|
|
ksort($grants[$key]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: