1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* For licensing information, please see the LICENSE file accompanied with this file. |
4
|
|
|
* |
5
|
|
|
* @author Gerard van Helden <[email protected]> |
6
|
|
|
* @copyright 2012 Gerard van Helden <http://melp.nl> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Zicht\Tool\Script\Parser; |
10
|
|
|
|
11
|
|
|
use Zicht\Tool\Script\Token; |
12
|
|
|
use Zicht\Tool\Script\Node; |
13
|
|
|
use Zicht\Tool\Script\Node\Expr\Op; |
14
|
|
|
use Zicht\Tool\Script\AbstractParser; |
15
|
|
|
use Zicht\Tool\Script\TokenStream; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Expression parser |
19
|
|
|
*/ |
20
|
|
|
class Expression extends AbstractParser |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Unary prefix operators |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
public static $PREFIX_UNARY = array('!', '-', '~'); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Binary infix operators |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
public static $INFIX_BINARY = array( |
35
|
|
|
'==', '!=', '<=', '>=', '<', '>', '&&', '||', '=~', |
36
|
|
|
'+', '*', '/', '-' |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Does a recursive descent parsing of the token stream and returns the resulting node. |
42
|
|
|
* |
43
|
|
|
* @param \Zicht\Tool\Script\TokenStream $stream |
44
|
|
|
* @return \Zicht\Tool\Script\Node\Node |
45
|
|
|
*/ |
46
|
12 |
|
public function parse(TokenStream $stream) |
47
|
|
|
{ |
48
|
12 |
|
if ($stream->match(Token::OPERATOR, array('!', '-'))) { |
|
|
|
|
49
|
|
|
$value = $stream->current()->value; |
50
|
|
|
$stream->next(); |
51
|
|
|
$ret = new Op\Unary($value, $this->parse($stream)); |
|
|
|
|
52
|
12 |
|
} elseif ($stream->match(Token::KEYWORD, array('true', 'false'))) { |
|
|
|
|
53
|
|
|
$ret = new Node\Expr\Literal($stream->current()->value === 'true'); |
54
|
|
|
$stream->next(); |
55
|
12 |
|
} elseif ($stream->match(Token::KEYWORD, 'null')) { |
56
|
|
|
$ret = new Node\Expr\Literal(null); |
57
|
|
|
$stream->next(); |
58
|
12 |
|
} elseif ($stream->match(Token::IDENTIFIER)) { |
59
|
9 |
|
$name = $stream->current()->value; |
60
|
9 |
|
$stream->next(); |
61
|
|
|
|
62
|
9 |
|
$ret = new Node\Expr\Variable($name); |
63
|
12 |
|
} elseif ($stream->match(Token::STRING)) { |
64
|
2 |
|
$ret = new Node\Expr\Str($stream->current()->value); |
65
|
2 |
|
$stream->next(); |
66
|
5 |
|
} elseif ($stream->match(Token::NUMBER)) { |
67
|
4 |
|
$value = $stream->current()->value; |
68
|
4 |
|
if (strpos('.', $value) !== false) { |
69
|
|
|
$ret = new Node\Expr\Number((float)$value); |
70
|
|
|
} else { |
71
|
4 |
|
$ret = new Node\Expr\Number((int)$value); |
72
|
|
|
} |
73
|
4 |
|
$stream->next(); |
74
|
4 |
|
} elseif ($stream->match(Token::OPERATOR, '(')) { |
75
|
|
|
$stream->next(); |
76
|
|
|
$ret = new Node\Expr\Parens($this->parse($stream)); |
|
|
|
|
77
|
|
|
$stream->expect(Token::OPERATOR, ')'); |
78
|
|
|
} elseif ($stream->match(Token::OPERATOR, '[')) { |
79
|
|
|
$stream->next(); |
80
|
|
|
$ret = new Node\Expr\ListNode(); |
81
|
|
|
if (!$stream->match(Token::OPERATOR, ']')) { |
82
|
|
|
$ret->append($this->parse($stream)); |
|
|
|
|
83
|
|
|
while ($stream->match(',')) { |
84
|
|
|
$stream->next(); |
85
|
|
|
$ret->append($this->parse($stream)); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
$stream->expect(Token::OPERATOR, ']'); |
89
|
|
|
} else { |
90
|
|
|
$this->err($stream); |
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
|
94
|
12 |
|
if ($stream->valid()) { |
95
|
11 |
|
while ($stream->valid() && $stream->match(Token::OPERATOR, array('(', '.', '['))) { |
|
|
|
|
96
|
6 |
|
$type = $stream->current(); |
97
|
|
|
|
98
|
6 |
|
$stream->next(); |
99
|
6 |
|
if ($type->value === '(') { |
100
|
3 |
|
$ret = new Node\Expr\Call($ret); |
101
|
3 |
|
if (!$stream->match(Token::OPERATOR, ')')) { |
102
|
|
|
do { |
103
|
2 |
|
$arg = $this->parse($stream); |
104
|
2 |
|
$ret->append($arg); |
|
|
|
|
105
|
|
|
|
106
|
2 |
|
if ($stream->match(',')) { |
107
|
1 |
|
$stream->next(); |
108
|
1 |
|
} else { |
109
|
2 |
|
break; |
110
|
|
|
} |
111
|
1 |
|
} while (true); |
112
|
2 |
|
} |
113
|
3 |
|
$stream->expect(Token::OPERATOR, ')'); |
114
|
3 |
|
} else { |
115
|
3 |
|
$ret = new Node\Expr\Subscript($ret); |
116
|
|
|
|
117
|
3 |
|
if ($type->value === '.') { |
118
|
1 |
|
$token = $stream->expect(Token::IDENTIFIER); |
119
|
1 |
|
$ret->append(new Node\Expr\Str($token->value)); |
120
|
1 |
|
} else { |
121
|
2 |
|
$ret->append($this->parse($stream)); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
switch ($type->value) { |
125
|
3 |
|
case '[': |
126
|
2 |
|
$stream->expect(Token::OPERATOR, ']'); |
127
|
2 |
|
break; |
128
|
3 |
|
} |
129
|
|
|
} |
130
|
6 |
|
} |
131
|
11 |
|
} |
132
|
|
|
|
133
|
12 |
|
if ($stream->valid()) { |
134
|
9 |
|
if ($stream->match(Token::OPERATOR, self::$INFIX_BINARY) || $stream->match(Token::KEYWORD, 'in')) { |
|
|
|
|
135
|
|
|
$value = $stream->current()->value; |
136
|
|
|
$stream->next(); |
137
|
|
|
$ret = new Op\Binary($value, $ret, $this->parse($stream)); |
|
|
|
|
138
|
9 |
|
} elseif ($stream->match(Token::OPERATOR, '?')) { |
139
|
3 |
|
$stream->next(); |
140
|
3 |
View Code Duplication |
if ($stream->match(Token::OPERATOR, ':')) { |
|
|
|
|
141
|
1 |
|
$then = null; |
142
|
1 |
|
} else { |
143
|
2 |
|
$then = $this->parse($stream); |
144
|
|
|
} |
145
|
|
|
|
146
|
3 |
View Code Duplication |
if ($stream->valid() && $stream->match(Token::OPERATOR, ':')) { |
|
|
|
|
147
|
2 |
|
$stream->next(); |
148
|
2 |
|
$else = $this->parse($stream); |
149
|
2 |
|
} else { |
150
|
1 |
|
$else = null; |
151
|
|
|
} |
152
|
3 |
|
$ret = new Op\Ternary('?', $ret, $then, $else); |
|
|
|
|
153
|
3 |
|
} |
154
|
9 |
|
} |
155
|
|
|
|
156
|
|
|
// little syntactic sugar for function calls without parentheses: |
157
|
12 |
|
$allowInlineCallTokens = array(Token::IDENTIFIER, Token::STRING, Token::NUMBER); |
158
|
12 |
|
if ($stream->valid() && ($stream->match($allowInlineCallTokens))) { |
|
|
|
|
159
|
1 |
|
$ret = new Node\Expr\Call($ret); |
160
|
1 |
|
$ret->append($this->parse($stream)); |
|
|
|
|
161
|
1 |
|
} |
162
|
|
|
|
163
|
12 |
|
return $ret; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: