Skip to content

Commit 88871e0

Browse files
committed
add silent node
1 parent aab775b commit 88871e0

File tree

5 files changed

+74
-32
lines changed

5 files changed

+74
-32
lines changed

docs/AST.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- [Foreach](#foreach)
4949
- [Switch](#switch)
5050
- [Goto](#goto)
51+
- [Silent](#silent)
5152
- [Try](#try)
5253
- [Catch](#catch)
5354
- [Call](#call)
@@ -790,6 +791,16 @@ A continue statement
790791

791792
Defines inline html output (treated as echo output)
792793

794+
# Silent
795+
796+
**Extends Statement**
797+
798+
Avoids to show/log warnings & notices from the inner expression
799+
800+
**Properties**
801+
802+
- `expr` **[Expression](#expression)**
803+
793804
# Statement
794805

795806
**Extends Node**

src/ast.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var Position = require('./ast/position');
5454
* - [Foreach](#foreach)
5555
* - [Switch](#switch)
5656
* - [Goto](#goto)
57+
* - [Silent](#silent)
5758
* - [Try](#try)
5859
* - [Catch](#catch)
5960
* - [Call](#call)
@@ -212,6 +213,7 @@ AST.prototype.prepare = function(kind, parser) {
212213
require('./ast/retif'),
213214
require('./ast/return'),
214215
require('./ast/shell'),
216+
require('./ast/silent'),
215217
require('./ast/string'),
216218
require('./ast/switch'),
217219
require('./ast/trait'),

src/ast/silent.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 = 'silent';
10+
11+
/**
12+
* Avoids to show/log warnings & notices from the inner expression
13+
* @constructor Silent
14+
* @extends {Statement}
15+
* @property {Expression} expr
16+
*/
17+
var Silent = Statement.extends(function Silent(expr, location) {
18+
Statement.apply(this, [KIND, location]);
19+
this.expr = expr;
20+
});
21+
22+
module.exports = Silent;

src/parser/expr.js

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,65 +12,65 @@ module.exports = {
1212
var expr = this.read_expr_item();
1313
// binary operations
1414
if (this.token === '|')
15-
return result('bin', '|', expr, this.next().read_expr());
15+
return result('bin', '|', expr, this.next().read_expr());
1616
if (this.token === '&')
17-
return result('bin', '&', expr, this.next().read_expr());
17+
return result('bin', '&', expr, this.next().read_expr());
1818
if (this.token === '^')
19-
return result('bin', '^', expr, this.next().read_expr());
19+
return result('bin', '^', expr, this.next().read_expr());
2020
if (this.token === '.')
21-
return result('bin', '.', expr, this.next().read_expr());
21+
return result('bin', '.', expr, this.next().read_expr());
2222
if (this.token === '+')
23-
return result('bin', '+', expr, this.next().read_expr());
23+
return result('bin', '+', expr, this.next().read_expr());
2424
if (this.token === '-')
25-
return result('bin', '-', expr, this.next().read_expr());
25+
return result('bin', '-', expr, this.next().read_expr());
2626
if (this.token === '*')
27-
return result('bin', '*', expr, this.next().read_expr());
27+
return result('bin', '*', expr, this.next().read_expr());
2828
if (this.token === '/')
29-
return result('bin', '/', expr, this.next().read_expr());
29+
return result('bin', '/', expr, this.next().read_expr());
3030
if (this.token === '%')
31-
return result('bin', '%', expr, this.next().read_expr());
31+
return result('bin', '%', expr, this.next().read_expr());
3232
if (this.token === this.tok.T_POW)
33-
return result('bin', '**', expr, this.next().read_expr());
33+
return result('bin', '**', expr, this.next().read_expr());
3434
if (this.token === this.tok.T_SL)
35-
return result('bin', '<<', expr, this.next().read_expr());
35+
return result('bin', '<<', expr, this.next().read_expr());
3636
if (this.token === this.tok.T_SR)
37-
return result('bin', '>>', expr, this.next().read_expr());
37+
return result('bin', '>>', expr, this.next().read_expr());
3838
// boolean operations
3939
if (this.token === this.tok.T_BOOLEAN_OR)
40-
return result('bool', '|', expr, this.next().read_expr());
40+
return result('bool', '|', expr, this.next().read_expr());
4141
if (this.token === this.tok.T_LOGICAL_OR)
42-
return result('bool', '|', expr, this.next().read_expr());
42+
return result('bool', '|', expr, this.next().read_expr());
4343
if (this.token === this.tok.T_BOOLEAN_AND)
44-
return result('bool', '&', expr, this.next().read_expr());
44+
return result('bool', '&', expr, this.next().read_expr());
4545
if (this.token === this.tok.T_LOGICAL_AND)
46-
return result('bool', '&', expr, this.next().read_expr());
46+
return result('bool', '&', expr, this.next().read_expr());
4747
if (this.token === this.tok.T_LOGICAL_XOR)
48-
return result('bool', '^', expr, this.next().read_expr());
48+
return result('bool', '^', expr, this.next().read_expr());
4949
if (this.token === this.tok.T_IS_IDENTICAL)
50-
return result('bool', '=', expr, this.next().read_expr());
50+
return result('bool', '=', expr, this.next().read_expr());
5151
if (this.token === this.tok.T_IS_NOT_IDENTICAL)
52-
return result('bool', '!=', expr, this.next().read_expr());
52+
return result('bool', '!=', expr, this.next().read_expr());
5353
if (this.token === this.tok.T_IS_EQUAL)
54-
return result('bool', '~', expr, this.next().read_expr());
54+
return result('bool', '~', expr, this.next().read_expr());
5555
if (this.token === this.tok.T_IS_NOT_EQUAL)
56-
return result('bool', '!~', expr, this.next().read_expr());
56+
return result('bool', '!~', expr, this.next().read_expr());
5757
if (this.token === '<')
58-
return result('bool', '<', expr, this.next().read_expr());
58+
return result('bool', '<', expr, this.next().read_expr());
5959
if (this.token === '>')
60-
return result('bool', '!~', expr, this.next().read_expr());
60+
return result('bool', '!~', expr, this.next().read_expr());
6161
if (this.token === this.tok.T_IS_SMALLER_OR_EQUAL)
62-
return result('bool', '<=', expr, this.next().read_expr());
62+
return result('bool', '<=', expr, this.next().read_expr());
6363
if (this.token === this.tok.T_IS_GREATER_OR_EQUAL)
64-
return result('bool', '=>', expr, this.next().read_expr());
64+
return result('bool', '=>', expr, this.next().read_expr());
6565
if (this.token === this.tok.T_SPACESHIP)
66-
return result('bool', '<=>', expr, this.next().read_expr());
66+
return result('bool', '<=>', expr, this.next().read_expr());
6767
if (this.token === this.tok.T_INSTANCEOF)
68-
return result('bool', '?', expr, this.next().read_expr());
68+
return result('bool', '?', expr, this.next().read_expr());
6969

7070
// extra operations :
7171
// $username = $_GET['user'] ?? 'nobody';
7272
if (this.token === this.tok.T_COALESCE)
73-
return result('coalesce', expr, this.next().read_expr());
73+
return result('coalesce', expr, this.next().read_expr());
7474

7575
// extra operations :
7676
// $username = $_GET['user'] ? true : false;
@@ -94,11 +94,10 @@ module.exports = {
9494
*/
9595
,read_expr_item: function() {
9696

97-
switch(this.token) {
98-
99-
case '@':
100-
return ['silent', this.next().read_expr()];
97+
if (this.token === '@')
98+
return this.node('silent')(this.next().read_expr());
10199

100+
switch(this.token) {
102101
case '-':
103102
var result = this.node();
104103
this.next();

test/exprTests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ describe('Test expressions', function() {
6565
// @todo
6666
});
6767

68+
it('test silent', function() {
69+
var ast = parser.parseEval([
70+
'@trigger_error();'
71+
].join('\n'));
72+
ast.children[0].kind.should.be.exactly('silent');
73+
ast.children[0].expr.kind.should.be.exactly('call');
74+
});
75+
6876
it('test unary', function() {
6977
var ast = parser.parseEval([
7078
'+$var;',

0 commit comments

Comments
 (0)