Skip to content

Commit d520b9f

Browse files
committed
add goto, label, post nodes
1 parent 794d765 commit d520b9f

File tree

11 files changed

+168
-5
lines changed

11 files changed

+168
-5
lines changed

docs/AST.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ Returns **[Function](#function)**
3939
- [TraitPrecedence](#traitprecedence)
4040
- [Entry](#entry)
4141
- [Case](#case)
42+
- [Label](#label)
4243
- [Doc](#doc)
4344
- [Error](#error)
4445
- [Expression](#expression)
4546
- [Array](#array)
4647
- [Variable](#variable)
4748
- [ConstRef](#constref)
49+
- [Operation](#operation)
50+
- [Post](#post)
4851
- [Literal](#literal)
4952
- [Boolean](#boolean)
5053
- [String](#string)
@@ -65,6 +68,7 @@ Returns **[Function](#function)**
6568
- [For](#for)
6669
- [Foreach](#foreach)
6770
- [Switch](#switch)
71+
- [Goto](#goto)
6872
- [Block](#block)
6973
- [Program](#program)
7074
- [Namespace](#namespace)
@@ -381,6 +385,16 @@ Defines a classic function
381385
- `byref` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
382386
- `body` **([Block](#block) | null)**
383387

388+
# Goto
389+
390+
**Extends Statement**
391+
392+
Defines goto statement
393+
394+
**Properties**
395+
396+
- `label` **[String](#string)**
397+
384398
# Identifier
385399

386400
**Extends Node**
@@ -440,6 +454,16 @@ An interface definition
440454

441455
Defines an isset call
442456

457+
# Label
458+
459+
**Extends Node**
460+
461+
A label statement (referenced by goto)
462+
463+
**Properties**
464+
465+
- `name` **[String](#string)**
466+
443467
# List
444468

445469
**Extends Sys**
@@ -532,6 +556,12 @@ Returns **[Function](#function)**
532556

533557
Defines a numeric value
534558

559+
# Operation
560+
561+
**Extends Expression**
562+
563+
Defines binary operations
564+
535565
# Parameter
536566

537567
**Extends Declaration**
@@ -561,6 +591,17 @@ Each Position object consists of a line number (1-indexed) and a column number (
561591
- `column` **[Number](#number)**
562592
- `offset` **[Number](#number)**
563593

594+
# Post
595+
596+
**Extends Operation**
597+
598+
Defines a post operation `$i++` or `$i--`
599+
600+
**Properties**
601+
602+
- `type` **[String](#string)**
603+
- `what` **[Variable](#variable)**
604+
564605
# Print
565606

566607
**Extends Sys**

src/ast.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ var Position = require('./ast/position');
1919
* - [TraitPrecedence](#traitprecedence)
2020
* - [Entry](#entry)
2121
* - [Case](#case)
22+
* - [Label](#label)
2223
* - [Doc](#doc)
2324
* - [Error](#error)
2425
* - [Expression](#expression)
2526
* - [Array](#array)
2627
* - [Variable](#variable)
2728
* - [ConstRef](#constref)
29+
* - [Operation](#operation)
30+
* - [Post](#post)
2831
* - [Literal](#literal)
2932
* - [Boolean](#boolean)
3033
* - [String](#string)
@@ -45,6 +48,7 @@ var Position = require('./ast/position');
4548
* - [For](#for)
4649
* - [Foreach](#foreach)
4750
* - [Switch](#switch)
51+
* - [Goto](#goto)
4852
* - [Block](#block)
4953
* - [Program](#program)
5054
* - [Namespace](#namespace)
@@ -161,19 +165,22 @@ AST.prototype.prepare = function(kind, parser) {
161165
require('./ast/for'),
162166
require('./ast/foreach'),
163167
require('./ast/function'),
168+
require('./ast/goto'),
164169
require('./ast/identifier'),
165170
require('./ast/if'),
166171
require('./ast/include'),
167172
require('./ast/inline'),
168173
require('./ast/interface'),
169174
require('./ast/isset'),
175+
require('./ast/label'),
170176
require('./ast/list'),
171177
require('./ast/literal'),
172178
require('./ast/magic'),
173179
require('./ast/method'),
174180
require('./ast/namespace'),
175181
require('./ast/number'),
176182
require('./ast/parameter'),
183+
require('./ast/post'),
177184
require('./ast/print'),
178185
require('./ast/program'),
179186
require('./ast/property'),

src/ast/goto.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 = 'goto';
10+
11+
/**
12+
* Defines goto statement
13+
* @constructor Goto
14+
* @extends {Statement}
15+
* @property {String} label
16+
* @see {Label}
17+
*/
18+
var Goto = Statement.extends(function Goto(label, location) {
19+
Statement.apply(this, [KIND, location]);
20+
this.label = label;
21+
});
22+
23+
module.exports = Goto;

src/ast/label.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 Node = require('./node');
8+
var KIND = 'label';
9+
10+
/**
11+
* A label statement (referenced by goto)
12+
* @constructor Label
13+
* @extends {Node}
14+
* @property {String} name
15+
*/
16+
var Label = Node.extends(function Label(name, location) {
17+
Node.apply(this, [KIND, location]);
18+
this.name = name;
19+
});
20+
21+
module.exports = Label;

src/ast/operation.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 Expr = require('./expression');
9+
var KIND = 'operation';
10+
11+
/**
12+
* Defines binary operations
13+
* @constructor Operation
14+
* @extends {Expression}
15+
*/
16+
var Operation = Expr.extends(function Operation(kind, location) {
17+
Expr.apply(this, [kind || KIND, location]);
18+
});
19+
20+
module.exports = Operation;

src/ast/post.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 Operation = require('./operation');
9+
var KIND = 'post';
10+
11+
/**
12+
* Defines a post operation `$i++` or `$i--`
13+
* @constructor Post
14+
* @extends {Operation}
15+
* @property {String} type
16+
* @property {Variable} what
17+
*/
18+
var Post = Operation.extends(function Post(type, what, location) {
19+
Operation.apply(this, [KIND, location]);
20+
this.type = type;
21+
this.what = what;
22+
});
23+
24+
module.exports = Post;

src/parser/expr.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,13 @@ module.exports = {
354354
case this.tok.T_SR_EQUAL:
355355
return ['set', expr, ['bin', '>>', expr, this.next().read_expr()]];
356356
case this.tok.T_INC:
357+
var result = this.node('post');
357358
this.next();
358-
return ['post', '+', expr];
359+
return result('+', expr);
359360
case this.tok.T_DEC:
361+
var result = this.node('post');
360362
this.next();
361-
return ['post', '-', expr];
363+
return result('+', expr);
362364
}
363365
} else if (this.is('SCALAR')) {
364366
expr = this.read_scalar();

test/astTests.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ describe('Test AST structure', function() {
7979
it('should be variable', function() {
8080
// @todo
8181
});
82-
it('should be assign', function() {
83-
// @todo
84-
});
8582
it('test literals', function() {
8683
// @todo string / numbers / booleans
8784
});

test/exprTests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var should = require("should");
2+
var parser = require('../src/index');
3+
4+
describe('Test expressions', function() {
5+
it('test assignments', function() {
6+
// @todo
7+
});
8+
});

test/loopTests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe('Test loops statements (for, while)', function() {
5454
'echo $i;',
5555
'for(;;):',
5656
'echo $ok;',
57+
'continue;;',
5758
'endfor;'
5859
].join('\n'), {
5960
parser: { debug: false }

test/statementTests.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var should = require("should");
2+
var parser = require('../src/index');
3+
4+
describe('Test statements', function() {
5+
it('test goto label', function() {
6+
var ast = parser.parseEval([
7+
'start:',
8+
' $i++;',
9+
'goto start;'
10+
].join('\n'), {
11+
parser: { debug: false }
12+
});
13+
ast.children[0].kind.should.be.exactly('label');
14+
ast.children[0].name.should.be.exactly('start');
15+
ast.children[1].kind.should.be.exactly('post');
16+
ast.children[2].kind.should.be.exactly('goto');
17+
ast.children[2].label.should.be.exactly('start');
18+
});
19+
});

0 commit comments

Comments
 (0)