Skip to content

Commit 53318c4

Browse files
committed
implement the throw node
1 parent eeb00b8 commit 53318c4

File tree

5 files changed

+66
-11
lines changed

5 files changed

+66
-11
lines changed

docs/AST.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
- [Silent](#silent)
5858
- [Try](#try)
5959
- [Catch](#catch)
60+
- [Throw](#throw)
6061
- [Call](#call)
6162
- [Closure](#closure)
6263
- [New](#new)
@@ -922,6 +923,16 @@ Defines system based call
922923

923924
- `arguments` **[Array](#array)<[Node](#node)>**
924925

926+
# Throw
927+
928+
**Extends Statement**
929+
930+
Defines a throw statement
931+
932+
**Properties**
933+
934+
- `what` **[Expression](#expression)**
935+
925936
# Trait
926937

927938
**Extends Declaration**

docs/lexer.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ Sets the current lexer state
5656

5757
- `state`
5858

59+
# T_COMMENT
60+
61+
Reads a single line comment
62+
5963
# T_DOC_COMMENT
6064

6165
Behaviour : <https://github.com/php/php-src/blob/master/Zend/zend_language_scanner.l#L1927>

src/ast.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ var Position = require('./ast/position');
6363
* - [Silent](#silent)
6464
* - [Try](#try)
6565
* - [Catch](#catch)
66+
* - [Throw](#throw)
6667
* - [Call](#call)
6768
* - [Closure](#closure)
6869
* - [New](#new)
@@ -231,6 +232,7 @@ AST.prototype.prepare = function(kind, parser) {
231232
require('./ast/staticlookup'),
232233
require('./ast/string'),
233234
require('./ast/switch'),
235+
require('./ast/throw'),
234236
require('./ast/trait'),
235237
require('./ast/traitalias'),
236238
require('./ast/traitprecedence'),

src/ast/throw.js

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

test/statementTests.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,38 @@ describe('Test statements', function() {
3838
' foo();',
3939
'} catch(FooError|BarError $err) {',
4040
' var_dump($err);',
41+
' throw $err;',
4142
'} finally {',
4243
' clean();',
4344
'}'
4445
].join('\n'), {
4546
parser: { debug: false }
4647
});
47-
ast.children[0].kind.should.be.exactly('try');
48-
ast.children[0].body.kind.should.be.exactly('block');
49-
ast.children[0].body.children[0].kind.should.be.exactly('call');
50-
ast.children[0].body.children[0].what.name.should.be.exactly('foo');
51-
ast.children[0].catches[0].kind.should.be.exactly('catch');
52-
ast.children[0].catches[0].what.length.should.be.exactly(2);
53-
ast.children[0].catches[0].what[0].name.should.be.exactly('FooError');
54-
ast.children[0].catches[0].what[1].name.should.be.exactly('BarError');
55-
ast.children[0].catches[0].variable.kind.should.be.exactly('variable');
56-
ast.children[0].catches[0].variable.name.should.be.exactly('err');
57-
ast.children[0].always.kind.should.be.exactly('block');
48+
49+
var expr = ast.children[0];
50+
expr.kind.should.be.exactly('try');
51+
expr.body.kind.should.be.exactly('block');
52+
expr.body.children[0].kind.should.be.exactly('call');
53+
expr.body.children[0].what.name.should.be.exactly('foo');
54+
expr.catches.length.should.be.exactly(1);
55+
56+
// test catch
57+
var catchExpr = expr.catches[0];
58+
catchExpr.kind.should.be.exactly('catch');
59+
catchExpr.what.length.should.be.exactly(2);
60+
catchExpr.what[0].name.should.be.exactly('FooError');
61+
catchExpr.what[1].name.should.be.exactly('BarError');
62+
catchExpr.variable.kind.should.be.exactly('variable');
63+
catchExpr.variable.name.should.be.exactly('err');
64+
65+
// test the throw statement
66+
catchExpr.body.kind.should.be.exactly('block');
67+
catchExpr.body.children.length.should.be.exactly(2);
68+
catchExpr.body.children[1].kind.should.be.exactly('throw');
69+
catchExpr.body.children[1].what.kind.should.be.exactly('variable');
70+
catchExpr.body.children[1].what.name.should.be.exactly('err');
71+
72+
// always block
73+
expr.always.kind.should.be.exactly('block');
5874
});
5975
});

0 commit comments

Comments
 (0)