Skip to content

Commit d75ddd7

Browse files
committed
add closure node
1 parent ae9fea9 commit d75ddd7

File tree

5 files changed

+99
-43
lines changed

5 files changed

+99
-43
lines changed

docs/AST.md

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,6 @@
22

33
# AST
44

5-
The AST builder class
6-
7-
**Parameters**
8-
9-
- `withPositions`
10-
- `withSource`
11-
12-
**Properties**
13-
14-
- `withPositions` **[Boolean](#boolean)** Should locate any node (by default false)
15-
- `withSource` **[Boolean](#boolean)** Should extract the node original code (by default false)
16-
17-
## position
18-
19-
Create a position node from specified parser
20-
including it's lexer current state
21-
22-
**Parameters**
23-
24-
- `Parser`
25-
- `parser`
26-
27-
Returns **[Position](#position)**
28-
29-
## prepare
30-
31-
Prepares an AST node
32-
33-
**Parameters**
34-
35-
- `kind` **([String](#string) | null)** Defines the node type
36-
(if null, the kind must be passed at the function call)
37-
- `parser` **Parser** The parser instance (use for extracting locations)
38-
39-
Returns **[Function](#function)**
40-
41-
# AST
42-
435
## Class hierarchy
446

457
- [Location](#location)
@@ -91,6 +53,7 @@ Returns **[Function](#function)**
9153
- [Try](#try)
9254
- [Catch](#catch)
9355
- [Call](#call)
56+
- [Closure](#closure)
9457
- [New](#new)
9558
- [UseGroup](#usegroup)
9659
- [UseItem](#useitem)
@@ -146,6 +109,44 @@ Prepares an AST node
146109

147110
Returns **[Function](#function)**
148111

112+
# AST
113+
114+
The AST builder class
115+
116+
**Parameters**
117+
118+
- `withPositions`
119+
- `withSource`
120+
121+
**Properties**
122+
123+
- `withPositions` **[Boolean](#boolean)** Should locate any node (by default false)
124+
- `withSource` **[Boolean](#boolean)** Should extract the node original code (by default false)
125+
126+
## position
127+
128+
Create a position node from specified parser
129+
including it's lexer current state
130+
131+
**Parameters**
132+
133+
- `Parser`
134+
- `parser`
135+
136+
Returns **[Position](#position)**
137+
138+
## prepare
139+
140+
Prepares an AST node
141+
142+
**Parameters**
143+
144+
- `kind` **([String](#string) | null)** Defines the node type
145+
(if null, the kind must be passed at the function call)
146+
- `parser` **Parser** The parser instance (use for extracting locations)
147+
148+
Returns **[Function](#function)**
149+
149150
# Array
150151

151152
**Extends Expression**
@@ -307,6 +308,20 @@ Defines a clone call
307308

308309
- `what` **[Expression](#expression)**
309310

311+
# Closure
312+
313+
**Extends Statement**
314+
315+
Defines a closure
316+
317+
**Properties**
318+
319+
- `arguments` **[Array](#array)<[Parameter](#parameter)>**
320+
- `type` **[Identifier](#identifier)**
321+
- `byref` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
322+
- `nullable` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
323+
- `body` **([Block](#block) | null)**
324+
310325
# Coalesce
311326

312327
**Extends Operation**

src/ast.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ var Position = require('./ast/position');
5959
* - [Try](#try)
6060
* - [Catch](#catch)
6161
* - [Call](#call)
62+
* - [Closure](#closure)
6263
* - [New](#new)
6364
* - [UseGroup](#usegroup)
6465
* - [UseItem](#useitem)
@@ -178,6 +179,7 @@ AST.prototype.prepare = function(kind, parser) {
178179
require('./ast/class'),
179180
require('./ast/classconstant'),
180181
require('./ast/clone'),
182+
require('./ast/closure'),
181183
require('./ast/coalesce'),
182184
require('./ast/constant'),
183185
require('./ast/constref'),

src/ast/closure.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*!
2+
* Copyright (C) 2017 Glayzzle (BSD3 License)
3+
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
4+
* @url http://glayzzle.com
5+
*/
6+
"use strict";
7+
var Statement = require('./statement');
8+
var KIND = 'closure';
9+
10+
/**
11+
* Defines a closure
12+
* @constructor Closure
13+
* @extends {Statement}
14+
* @property {Parameter[]} arguments
15+
* @property {Identifier} type
16+
* @property {boolean} byref
17+
* @property {boolean} nullable
18+
* @property {Block|null} body
19+
*/
20+
var Closure = Statement.extends(function Closure(args, byref, type, nullable, location) {
21+
Statement.apply(this, [KIND, location]);
22+
this.arguments = args;
23+
this.byref = byref;
24+
this.type = type;
25+
this.nullable = nullable;
26+
this.body = null;
27+
});
28+
29+
module.exports = Closure;

test/astTests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ describe('Test AST structure', function() {
1010
});
1111

1212
it('test withSource options', function() {
13-
var ast = parser.parseEval('true;', {
13+
var ast = parser.parseEval('$a = true;', {
1414
ast: {
1515
withSource: true
1616
}
1717
});
1818
// @todo
1919
});
2020
it('test withPositions options', function() {
21-
var ast = parser.parseEval('true;', {
21+
var ast = parser.parseEval('$a = true;', {
2222
ast: {
2323
withPositions: true
2424
}

test/functionTests.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ var should = require("should");
22
var parser = require('../src/index');
33

44
describe('Function tests', function() {
5-
var ast = parser.parseEval(
6-
'function &foo($a = 1, callable $b, ?array &...$params) : ?boolean {}'
7-
);
5+
var ast = parser.parseEval([
6+
'function &foo($a = 1, callable $b, ?array &...$params) : ?boolean {}',
7+
'$a = function &($b) use($c) : array {',
8+
' return true;',
9+
'};'
10+
].join('\n'));
811

912
it('test description', function () {
1013
// Get result from parser
@@ -38,5 +41,12 @@ describe('Function tests', function() {
3841
*/
3942
});
4043

44+
it('test closure', function () {
45+
// @todo
46+
ast.children[1].right.kind.should.be.exactly('closure');
47+
var fn = ast.children[1].right;
48+
fn.byref.should.be.exactly(true);
49+
});
50+
4151

4252
});

0 commit comments

Comments
 (0)