Skip to content

Commit 36e64a8

Browse files
committed
add static keyword node
1 parent 53318c4 commit 36e64a8

File tree

7 files changed

+119
-12
lines changed

7 files changed

+119
-12
lines changed

docs/AST.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
- [Exit](#exit)
4545
- [Clone](#clone)
4646
- [Global](#global)
47+
- [Static](#static)
4748
- [Include](#include)
4849
- [Assign](#assign)
4950
- [RetIf](#retif)
@@ -885,6 +886,16 @@ Avoids to show/log warnings & notices from the inner expression
885886

886887
Any statement.
887888

889+
# Static
890+
891+
**Extends Statement**
892+
893+
Declares a static variable into the current scope
894+
895+
**Properties**
896+
897+
- `items` **([Array](#array)<[Variable](#variable)> | [Array](#array)<[Assign](#assign)>)**
898+
888899
# StaticLookup
889900

890901
**Extends Lookup**

docs/parser.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,24 @@ Sample code :
688688

689689
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<Identifier>**
690690

691+
# read_variable_declarations
692+
693+
Reads a list of variables declarations
694+
695+
```ebnf
696+
variable_declaration ::= T_VARIABLE ('=' expr)?*
697+
variable_declarations ::= variable_declaration (',' variable_declaration)*
698+
```
699+
700+
Sample code :
701+
702+
```php
703+
<?php class foo extends bar, baz { }
704+
```
705+
706+
Returns **([Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;Variable> | [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;Assign>)** Returns an array composed by a list of variables, or
707+
assign values
708+
691709
# read_variable
692710

693711
Reads a variable

src/ast.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var Position = require('./ast/position');
5050
* - [Exit](#exit)
5151
* - [Clone](#clone)
5252
* - [Global](#global)
53+
* - [Static](#static)
5354
* - [Include](#include)
5455
* - [Assign](#assign)
5556
* - [RetIf](#retif)
@@ -229,6 +230,7 @@ AST.prototype.prepare = function(kind, parser) {
229230
require('./ast/return'),
230231
require('./ast/shell'),
231232
require('./ast/silent'),
233+
require('./ast/static'),
232234
require('./ast/staticlookup'),
233235
require('./ast/string'),
234236
require('./ast/switch'),

src/ast/static.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 = 'static';
9+
10+
/**
11+
* Declares a static variable into the current scope
12+
* @constructor Static
13+
* @extends {Statement}
14+
* @property {Variable[]|Assign[]} items
15+
*/
16+
var Static = Statement.extends(function Static(items, location) {
17+
Statement.apply(this, [KIND, location]);
18+
this.items = items;
19+
});
20+
21+
module.exports = Static;

src/parser/statement.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -228,19 +228,9 @@ module.exports = {
228228
this.expect(';') && this.nextWithComments();
229229
return expr;
230230
}
231-
var items = this.read_list(function() {
232-
var value = null, name = null;
233-
if (this.expect(this.tok.T_VARIABLE)) {
234-
name = this.text();
235-
this.next();
236-
}
237-
if (this.token === '=') {
238-
value = this.next().read_expr();
239-
}
240-
return [name, value];
241-
}, ',');
231+
var items = this.read_variable_declarations();
242232
this.expectEndOfStatement();
243-
return result('declare', items);
233+
return result(items);
244234

245235
case this.tok.T_ECHO:
246236
var result = this.node('echo');

src/parser/utils.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,40 @@ module.exports = {
8181
);
8282
}
8383

84+
/**
85+
* Reads a list of variables declarations
86+
*
87+
* ```ebnf
88+
* variable_declaration ::= T_VARIABLE ('=' expr)?*
89+
* variable_declarations ::= variable_declaration (',' variable_declaration)*
90+
* ```
91+
*
92+
* Sample code :
93+
* ```php
94+
* <?php class foo extends bar, baz { }
95+
* ```
96+
* @return {Variable[]|Assign[]} Returns an array composed by a list of variables, or
97+
* assign values
98+
*/
99+
,read_variable_declarations: function() {
100+
return this.read_list(function() {
101+
var node = this.node('assign'),
102+
value = null,
103+
variable = this.node('variable');
104+
// plain variable name
105+
if (this.expect(this.tok.T_VARIABLE)) {
106+
var name = this.text().substring(1);
107+
this.next();
108+
variable = variable(name, false);
109+
} else {
110+
variable = variable('#ERR', false);
111+
}
112+
if (this.token === '=') {
113+
return node(variable, this.next().read_expr());
114+
} else {
115+
return variable;
116+
}
117+
}, ',');
118+
}
119+
84120
};

test/statementTests.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,35 @@ describe('Test statements', function() {
3232
expr.items[1].name.should.be.exactly('b');
3333
});
3434

35+
it('test static', function() {
36+
var ast = parser.parseEval([
37+
'function foo() {',
38+
' static $a, $b = 5;',
39+
'}',
40+
'static $sVar1 = 11;'
41+
].join('\n'), {
42+
parser: { debug: false }
43+
});
44+
45+
// test function multi statements
46+
var expr = ast.children[0].body.children[0];
47+
expr.kind.should.be.exactly('static');
48+
expr.items.length.should.be.exactly(2);
49+
expr.items[0].kind.should.be.exactly('variable');
50+
expr.items[1].kind.should.be.exactly('assign');
51+
52+
// test single statement
53+
ast.children[1].kind.should.be.exactly('static');
54+
ast.children[1].items.length.should.be.exactly(1);
55+
ast.children[1].items[0].kind.should.be.exactly('assign');
56+
ast.children[1].items[0].left.kind.should.be.exactly('variable');
57+
ast.children[1].items[0].left.name.should.be.exactly('sVar1');
58+
ast.children[1].items[0].right.kind.should.be.exactly('number');
59+
ast.children[1].items[0].right.value.should.be.exactly('11');
60+
61+
});
62+
63+
3564
it('test try', function() {
3665
var ast = parser.parseEval([
3766
'try {',

0 commit comments

Comments
 (0)