Skip to content

Commit b0044cf

Browse files
committed
Fix #305. Empty Heredoc string causes syntax error.
1 parent dca7abe commit b0044cf

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

exts/jphp-zend-ext/src/main/tests/strings/StringsTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,9 @@ public void testPhpCompat() {
6969
check("ext/strings/001.phpt");
7070
check("ext/strings/002.phpt");
7171
}
72+
73+
@Test
74+
public void testBug305() {
75+
check("ext/strings/bug305.phpt", true);
76+
}
7277
}

jphp-core/src/org/develnext/jphp/core/tokenizer/Tokenizer.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ protected ValueExprToken readString(StringExprToken.Quote quote, int startPositi
277277
List<StringExprToken.Segment> segments = new ArrayList<StringExprToken.Segment>();
278278

279279

280+
boolean firstIter = true;
280281
for(; i < codeLength; i++){
281282
char ch = code.charAt(i);
282283

@@ -288,15 +289,16 @@ protected ValueExprToken readString(StringExprToken.Quote quote, int startPositi
288289
break;
289290
}
290291

291-
if (checkNewLine(ch)) {
292+
if (checkNewLine(ch) || firstIter) {
292293
pos = 0;
293294
if (endString != null){
294295
int end = i + 1 + endString.length();
295-
if (end < codeLength){
296-
if (code.substring(i + 1, end).equals(endString)) {
296+
if (end < codeLength) {
297+
String substring = firstIter ? code.substring(i, end - 1) : code.substring(i + 1, end);
298+
if (substring.equals(endString)) {
297299
if ((code.charAt(end) == ';' && TokenizeGrammarUtils.isNewline(code.charAt(end + 1))) || TokenizeGrammarUtils.isNewline(code.charAt(end))) {
298-
currentPosition = i + endString.length();
299-
relativePosition = endString.length();
300+
currentPosition = firstIter ? i + endString.length() - 1 : i + endString.length();
301+
relativePosition = firstIter ? endString.length() - 1 : endString.length();
300302
ch_quote = StringExprToken.Quote.DOC;
301303
break;
302304
}
@@ -305,6 +307,8 @@ protected ValueExprToken readString(StringExprToken.Quote quote, int startPositi
305307
}
306308
}
307309

310+
firstIter = false;
311+
308312
if (!isMagic){
309313
switch (ch) {
310314
case '\\':
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Empty Heredoc string causes syntax error
3+
--FILE--
4+
<?php
5+
$heredoc_empty_string = <<<EOD
6+
EOD;
7+
var_dump($heredoc_empty_string);
8+
--EXPECTF--
9+
string(0) ""

0 commit comments

Comments
 (0)