Skip to content

Commit 4febbcf

Browse files
committed
fix declare implementation + improve coverage
1 parent 5a8c158 commit 4febbcf

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

src/parser/statement.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ module.exports = function(api, tokens, EOF) {
9191
this.expectEndOfStatement();
9292
return ['const', result];
9393
}
94+
/**
95+
* Reads a list of constants declaration
96+
* <ebnf>
97+
* const_list ::= T_CONST T_STRING '=' expr (',' T_STRING '=' expr)*
98+
* </ebnf>
99+
*/
100+
,read_declare_list: function() {
101+
return this.read_list(function() {
102+
this.expect(tokens.T_STRING);
103+
var name = this.text();
104+
this.next().expect('=').next();
105+
return [name, this.read_expr()];
106+
}, ',');
107+
}
94108
/**
95109
* reads a simple inner statement
96110
* <ebnf>
@@ -202,11 +216,12 @@ module.exports = function(api, tokens, EOF) {
202216
return ['sys', 'unset', items];
203217

204218
case tokens.T_DECLARE:
219+
var result = this.node('declare');
205220
this.next().expect('(').next();
206-
var options = this.read_list(this.read_const_list, ',');
221+
var options = this.read_declare_list();
207222
this.expect(')').next();
208223
var body = this.read_statement();
209-
return ['declare', options, body]
224+
return result(options, body);
210225
break;
211226

212227
case tokens.T_TRY:
@@ -225,8 +240,9 @@ module.exports = function(api, tokens, EOF) {
225240
case tokens.T_STRING:
226241
var label = this.text();
227242
if (this.next().token === ':') {
243+
var result = this.node('label');
228244
this.next();
229-
return ['label', label];
245+
return result(label);
230246
} else {
231247
// default fallback expr
232248
this.lexer.unput(label.length + this.text().length);
@@ -236,9 +252,10 @@ module.exports = function(api, tokens, EOF) {
236252
}
237253

238254
case tokens.T_GOTO:
255+
var result = this.node('goto');
239256
var label = this.next().expect(tokens.T_STRING).text();
240257
this.next().expectEndOfStatement();
241-
return ['goto', label];
258+
return result(label);
242259

243260
default: // default fallback expr
244261
var expr = this.read_expr();

test/parser/statement.parser

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,38 @@ while(true) {
1818
$i++;
1919
}
2020
--PASS--
21-
/* @todo
2221
switch(true) {
2322
case 1: break;
2423
case 2: break;
2524
default: break;
26-
} */
25+
}
2726
--PASS--
28-
/* try {
27+
try {
2928
$i++;
29+
} catch(CustomException $e) {
30+
$i--;
3031
} catch(Exception $e) {
3132
$i--;
3233
} finally {
3334
unset($i);
34-
} */
35+
}
36+
--PASS--
37+
namespace a {
38+
const A = 1, B = 2;
39+
if (true) {
40+
interface foo {
41+
}
42+
class bar {
43+
}
44+
trait boo {
45+
}
46+
function baz() {
47+
}
48+
}
49+
}
50+
--PASS--
51+
declare(ticks=1) {
52+
haha:
53+
throw new Exception('foo');
54+
goto haha;
55+
}

0 commit comments

Comments
 (0)