Skip to content

Commit 6d253c1

Browse files
committed
glayzzle#71 implement static closures
1 parent 9685e3e commit 6d253c1

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/parser/expr.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,20 @@ module.exports = {
332332
return result(expr);
333333

334334
case this.tok.T_FUNCTION:
335-
// @fixme later - removed static lambda function declarations (colides with static keyword usage)
336335
return this.read_function(true);
337336

337+
case this.tok.T_STATIC:
338+
var backup = [this.token, this.lexer.getState()];
339+
if (this.next().token === this.tok.T_FUNCTION) {
340+
// handles static function
341+
return this.read_function(true, [0, 1, 0]);
342+
} else {
343+
// rollback
344+
this.lexer.tokens.push(backup);
345+
this.next();
346+
}
347+
348+
338349
}
339350

340351
// SCALAR | VARIABLE

src/parser/function.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ module.exports = {
3333
*/
3434
,read_function: function(closure, flag) {
3535
var result = this.read_function_declaration(
36-
closure ? 1 : (flag ? 2 : 0)
36+
closure ? 1 : (flag ? 2 : 0),
37+
flag && flag[1] === 1
3738
);
3839
if (flag && flag[2] == 1) {
3940
// abstract function :
@@ -48,7 +49,7 @@ module.exports = {
4849
result.loc.end = result.body.loc.end;
4950
}
5051
}
51-
if (flag) {
52+
if (!closure && flag) {
5253
result.parseFlags(flag);
5354
}
5455
}
@@ -60,14 +61,15 @@ module.exports = {
6061
* function_declaration ::= T_FUNCTION '&'? T_STRING '(' parameter_list ')'
6162
* ```
6263
*/
63-
,read_function_declaration: function(type) {
64+
,read_function_declaration: function(type, isStatic) {
6465
var nodeName = 'function';
6566
if (type === 1) {
6667
nodeName = 'closure';
6768
} else if (type === 2) {
6869
nodeName = 'method';
6970
}
7071
var result = this.node(nodeName);
72+
7173
if (this.expect(this.tok.T_FUNCTION)) {
7274
this.next();
7375
}
@@ -96,7 +98,7 @@ module.exports = {
9698
}
9799
if (type === 1) {
98100
// closure
99-
return result(params, isRef, use, returnType, nullable);
101+
return result(params, isRef, use, returnType, nullable, isStatic);
100102
}
101103
return result(name, params, isRef, returnType, nullable);
102104
}

src/parser/statement.js

+3
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ module.exports = {
244244
this.expect(';') && this.nextWithComments();
245245
return expr;
246246
}
247+
if (this.token === this.tok.T_FUNCTION) {
248+
return this.read_function(true, [0, 1, 0]);
249+
}
247250
var items = this.read_variable_declarations();
248251
this.expectEndOfStatement();
249252
return result(items);

0 commit comments

Comments
 (0)