Skip to content

Commit ad2b9ab

Browse files
committed
add global node
1 parent 2143b98 commit ad2b9ab

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

docs/AST.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
- [Eval](#eval)
4444
- [Exit](#exit)
4545
- [Clone](#clone)
46+
- [Global](#global)
4647
- [Include](#include)
4748
- [Assign](#assign)
4849
- [RetIf](#retif)
@@ -511,6 +512,16 @@ Defines a classic function
511512
- `nullable` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
512513
- `body` **([Block](#block) | null)**
513514

515+
# Global
516+
517+
**Extends Statement**
518+
519+
Imports a variable from the global scope
520+
521+
**Properties**
522+
523+
- `items` **[Array](#array)<[Variable](#variable)>**
524+
514525
# Goto
515526

516527
**Extends Statement**

src/ast.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var Position = require('./ast/position');
4949
* - [Eval](#eval)
5050
* - [Exit](#exit)
5151
* - [Clone](#clone)
52+
* - [Global](#global)
5253
* - [Include](#include)
5354
* - [Assign](#assign)
5455
* - [RetIf](#retif)
@@ -199,6 +200,7 @@ AST.prototype.prepare = function(kind, parser) {
199200
require('./ast/for'),
200201
require('./ast/foreach'),
201202
require('./ast/function'),
203+
require('./ast/global'),
202204
require('./ast/goto'),
203205
require('./ast/identifier'),
204206
require('./ast/if'),

src/ast/global.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 = 'global';
9+
10+
/**
11+
* Imports a variable from the global scope
12+
* @constructor Global
13+
* @extends {Statement}
14+
* @property {Variable[]} items
15+
*/
16+
var Global = Statement.extends(function Global(items, location) {
17+
Statement.apply(this, [KIND, location]);
18+
this.items = items;
19+
});
20+
21+
module.exports = Global;

src/parser/statement.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,10 @@ module.exports = {
213213
return result();
214214

215215
case this.tok.T_GLOBAL:
216+
var result = this.node('global');
216217
var items = this.next().read_list(this.read_simple_variable, ',');
217218
this.expectEndOfStatement();
218-
return ['global', items];
219+
return result(items);
219220

220221
case this.tok.T_STATIC:
221222
var current = [this.token, this.lexer.getState()];

test/statementTests.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ describe('Test statements', function() {
1616
ast.children[2].kind.should.be.exactly('goto');
1717
ast.children[2].label.should.be.exactly('start');
1818
});
19+
it('test global', function() {
20+
var ast = parser.parseEval([
21+
'function foo() {',
22+
' global $a, $b;',
23+
'}'
24+
].join('\n'), {
25+
parser: { debug: false }
26+
});
27+
var expr = ast.children[0].body.children[0];
28+
expr.kind.should.be.exactly('global');
29+
expr.items[0].kind.should.be.exactly('variable');
30+
expr.items[0].name.should.be.exactly('a');
31+
expr.items[1].kind.should.be.exactly('variable');
32+
expr.items[1].name.should.be.exactly('b');
33+
});
34+
1935
it('test try', function() {
2036
var ast = parser.parseEval([
2137
'try {',

0 commit comments

Comments
 (0)