Skip to content

Commit 0bdb379

Browse files
committed
add new node
1 parent 5747a93 commit 0bdb379

File tree

5 files changed

+94
-46
lines changed

5 files changed

+94
-46
lines changed

docs/AST.md

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,44 @@
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+
543
## Class hierarchy
644

745
- [Location](#location)
@@ -53,6 +91,7 @@
5391
- [Try](#try)
5492
- [Catch](#catch)
5593
- [Call](#call)
94+
- [New](#new)
5695
- [UseGroup](#usegroup)
5796
- [UseItem](#useitem)
5897
- [Block](#block)
@@ -107,44 +146,6 @@ Prepares an AST node
107146

108147
Returns **[Function](#function)**
109148

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

150151
**Extends Expression**
@@ -658,6 +659,17 @@ The main program node
658659
- `name` **[Identifier](#identifier)**
659660
- `withBrackets` **[Boolean](#boolean)**
660661

662+
# New
663+
664+
**Extends Statement**
665+
666+
Creates a new instance of the specified class
667+
668+
**Properties**
669+
670+
- `what` **([Identifier](#identifier) \| [Variable](#variable) \| [Class](#class))**
671+
- `arguments` **[Array](#array)<Arguments>**
672+
661673
# Node
662674

663675
A generic AST node

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+
* - [New](#new)
6263
* - [UseGroup](#usegroup)
6364
* - [UseItem](#useitem)
6465
* - [Block](#block)
@@ -205,6 +206,7 @@ AST.prototype.prepare = function(kind, parser) {
205206
require('./ast/magic'),
206207
require('./ast/method'),
207208
require('./ast/namespace'),
209+
require('./ast/new'),
208210
require('./ast/number'),
209211
require('./ast/parameter'),
210212
require('./ast/post'),

src/ast/new.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
8+
var Statement = require('./statement');
9+
var KIND = 'new';
10+
11+
/**
12+
* Creates a new instance of the specified class
13+
* @constructor New
14+
* @extends {Statement}
15+
* @property {Identifier|Variable|Class} what
16+
* @property {Arguments[]} arguments
17+
*/
18+
var New = Statement.extends(function New(what, args, location) {
19+
Statement.apply(this, [KIND, location]);
20+
this.what = what;
21+
this.arguments = args;
22+
});
23+
24+
module.exports = New;

src/parser/expr.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ module.exports = {
433433
,read_new_expr: function() {
434434
var result = this.node('new');
435435
if (this.token === this.tok.T_CLASS) {
436+
var what = this.node('class');
436437
// Annonymous class declaration
437438
var propExtends = null, propImplements = null, body = null;
438439
if (this.next().token == this.tok.T_EXTENDS) {
@@ -445,10 +446,13 @@ module.exports = {
445446
body = this.next().read_class_body();
446447
}
447448
return result(
448-
false // class name => false : means it's an annonymous class
449-
,propExtends
450-
,propImplements
451-
,body
449+
what(
450+
null
451+
,propExtends
452+
,propImplements
453+
,body
454+
,[0, 0, 0]
455+
), []
452456
);
453457
} else {
454458
// Already existing class
@@ -471,8 +475,6 @@ module.exports = {
471475
var result = this.read_namespace_name();
472476
if (this.token === this.tok.T_DOUBLE_COLON) {
473477
result = this.read_static_getter(result);
474-
} else {
475-
result = ['ns', result];
476478
}
477479
return result;
478480
} else if (this.is('VARIABLE')) {

test/exprTests.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,19 @@ describe('Test expressions', function() {
185185
it('test new', function() {
186186
var ast = parser.parseEval([
187187
'$a = new foo();',
188-
'$a = new $foo();'
188+
'$a = new $foo();',
189+
'$a = new class extends foo implements bar { };',
189190
].join('\n'), {
190191
ast: { debug: false }
191192
});
192-
console.log(ast);
193+
ast.children[0].right.kind.should.be.exactly('new');
194+
ast.children[0].right.what.kind.should.be.exactly('identifier');
195+
196+
ast.children[1].right.kind.should.be.exactly('new');
197+
ast.children[1].right.what.kind.should.be.exactly('variable');
198+
199+
ast.children[2].right.kind.should.be.exactly('new');
200+
ast.children[2].right.what.kind.should.be.exactly('class');
193201
});
194202

195203
});

0 commit comments

Comments
 (0)