Skip to content

Commit b8e632d

Browse files
committed
cover fallback mode
1 parent 94145ab commit b8e632d

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/parser/statement.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,18 @@ module.exports = {
166166
case this.tok.T_TRAIT:
167167
return this.read_trait();
168168
case this.tok.T_HALT_COMPILER:
169-
if (this.next().expect('(')) this.next();
170-
if (this.expect(')')) this.next();
171-
if (this.expect(';')) this.next();
172-
this.raiseError('__HALT_COMPILER() can only be used from the outermost scope');
169+
this.raiseError(
170+
'__HALT_COMPILER() can only be used from the outermost scope'
171+
);
172+
// fallback : returns a node but does not stop the parsing
173+
var node = this.node('halt');
174+
this.next().expect('(') && this.next();
175+
this.expect(')') && this.next();
176+
node = node(this.lexer._input.substring(
177+
this.lexer.offset
178+
));
179+
this.expect(';') && this.next();
180+
return node;
173181
default:
174182
return this.read_statement();
175183
}

test/statementTests.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,33 @@ describe('Test statements', function() {
4545
ast.children[0].kind.should.be.exactly('assign');
4646
ast.children[1].kind.should.be.exactly('halt');
4747
ast.children[1].after.should.be.exactly('\n$b = 1;');
48+
49+
// test the inner error
50+
(function() {
51+
var ast = parser.parseEval([
52+
'if (true) {',
53+
' __halt_compiler();',
54+
'}'
55+
].join('\n'));
56+
}).should.throw(/line\s2/);
57+
58+
// test the fallback
59+
ast = parser.parseEval([
60+
'if (true) {',
61+
' __halt_compiler();',
62+
'}',
63+
'$b = 1;'
64+
].join('\n'), {
65+
parser: { suppressErrors: true }
66+
});
67+
68+
ast.children.length.should.be.exactly(2);
69+
ast.errors.length.should.be.exactly(1);
70+
ast.children[0].kind.should.be.exactly('if');
71+
ast.children[0].body.children[0].kind.should.be.exactly('halt');
72+
ast.children[0].body.children[0].after.should.be.exactly('\n}\n$b = 1;');
73+
ast.children[1].kind.should.be.exactly('assign');
74+
4875
});
4976

5077
it('test static', function() {

0 commit comments

Comments
 (0)