Skip to content

Commit

Permalink
fix #149
Browse files Browse the repository at this point in the history
  • Loading branch information
ichiriac committed Apr 14, 2018
1 parent 156e2af commit cac41be
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
14 changes: 9 additions & 5 deletions dist/php-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6811,9 +6811,13 @@ module.exports = {
{
value = this.node("string");
var text = this.text();
var isDoubleQuote = text[0] === '"';
var offset = 0;
if (text[0] === "b" || text[0] === "B") {
offset = 1;
}
var isDoubleQuote = text[offset] === '"';
this.next();
value = value(isDoubleQuote, this.resolve_special_chars(text.substring(1, text.length - 1), isDoubleQuote), text);
value = value(isDoubleQuote, this.resolve_special_chars(text.substring(offset + 1, text.length - 1), isDoubleQuote), text);
if (this.token === this.tok.T_DOUBLE_COLON) {
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1151
return this.read_static_getter(value);
Expand Down Expand Up @@ -6856,9 +6860,9 @@ module.exports = {
case 'b"':
case 'B"':
{
node = this.node("cast");
var what = this.next().read_encapsed_string('"');
return node("binary", what);
this.next();
this.lexer.yylloc.prev_offset -= 1;
return this.read_encapsed_string('"');
}

// NUMERIC
Expand Down
4 changes: 2 additions & 2 deletions dist/php-parser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/php-parser.min.js.map

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions src/parser/scalar.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@ module.exports = {
case this.tok.T_CONSTANT_ENCAPSED_STRING: {
value = this.node("string");
const text = this.text();
const isDoubleQuote = text[0] === '"';
let offset = 0;
if (text[0] === "b" || text[0] === "B") {
offset = 1;
}
const isDoubleQuote = text[offset] === '"';
this.next();
value = value(
isDoubleQuote,
this.resolve_special_chars(
text.substring(1, text.length - 1),
text.substring(offset + 1, text.length - 1),
isDoubleQuote
),
text
Expand Down Expand Up @@ -111,9 +115,9 @@ module.exports = {

case 'b"':
case 'B"': {
node = this.node("cast");
const what = this.next().read_encapsed_string('"');
return node("binary", what);
this.next();
this.lexer.yylloc.prev_offset -= 1;
return this.read_encapsed_string('"');
}

// NUMERIC
Expand Down
10 changes: 9 additions & 1 deletion test/stringTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ describe("Test strings", function() {
const text = ast.children[0].right;
text.raw.should.be.exactly('"foo\\nbar"');
});
it("fix #149", function() {
const ast = parser.parseEval('$a = b"foo\\nbar";');
const text = ast.children[0].right;
text.raw.should.be.exactly('b"foo\\nbar"');
text.isDoubleQuote.should.be.exactly(true);
});
it("...", function() {
var ast = parser.parseEval("$a = b'\\t\\ra';");
});
it("...", function() {
it("test binary with double quotes", function() {
var ast = parser.parseEval('echo b"\\colors contains >$colors<\\n";');
const text = ast.children[0].arguments[0];
text.raw.should.be.exactly('b"\\colors contains >$colors<\\n"');
});
it("...", function() {
var ast = parser.parseEval('echo B"\\colors[1] contains >$colors[1]<\\n";');
Expand Down

0 comments on commit cac41be

Please sign in to comment.