Skip to content

Commit 4063d9a

Browse files
committed
Merge pull request #12 from php-school/fix-new-line-printing
Fix newlines being interpolated
2 parents 606b04f + a51fd8c commit 4063d9a

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

src/Lexer.php

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public function getNextToken(&$value = null, &$startAttributes = null, &$endAttr
2323
if ($tokenId == Tokens::T_EXIT) {
2424
$startAttributes['isDie'] = strtolower($value) === 'die';
2525
}
26+
27+
if ($tokenId == Tokens::T_CONSTANT_ENCAPSED_STRING) {
28+
$endAttributes['originalValue'] = $value;
29+
}
2630

2731
return $tokenId;
2832
}

src/SyntaxHighlightPrinter.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ public function pStmt_Echo(Stmt\Echo_ $node)
8181
*/
8282
public function pScalar_String(Scalar\String_ $node)
8383
{
84-
$string = '\'' . $this->pNoIndent(addcslashes($node->value, '\'\\')) . '\'';
84+
if ($node->hasAttribute('originalValue')) {
85+
$string = $node->getAttribute('originalValue');
86+
} else {
87+
$string = '\'' . $this->pNoIndent(addcslashes($node->value, '\'\\')) . '\'';
88+
}
8589
return $this->color($string, SyntaxHighlighterConfig::TYPE_STRING);
8690
}
8791

test/LexerTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function provideTestLex()
140140
array(
141141
array(
142142
Tokens::T_CONSTANT_ENCAPSED_STRING, '"foo' . "\n" . 'bar"',
143-
array('startLine' => 1), array('endLine' => 2)
143+
array('startLine' => 1), array('endLine' => 2, 'originalValue' => "\"foo\nbar\"")
144144
),
145145
)
146146
),
@@ -151,15 +151,15 @@ public function provideTestLex()
151151
array(
152152
array(
153153
Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
154-
array('startFilePos' => 6), array('endFilePos' => 8)
154+
array('startFilePos' => 6), array('endFilePos' => 8, 'originalValue' => '"a"')
155155
),
156156
array(
157157
ord(';'), ';',
158158
array('startFilePos' => 9), array('endFilePos' => 9)
159159
),
160160
array(
161161
Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
162-
array('startFilePos' => 18), array('endFilePos' => 20)
162+
array('startFilePos' => 18), array('endFilePos' => 20, 'originalValue' => '"b"')
163163
),
164164
array(
165165
ord(';'), ';',
@@ -174,15 +174,15 @@ public function provideTestLex()
174174
array(
175175
array(
176176
Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
177-
array('startTokenPos' => 1), array('endTokenPos' => 1)
177+
array('startTokenPos' => 1), array('endTokenPos' => 1, 'originalValue' => '"a"')
178178
),
179179
array(
180180
ord(';'), ';',
181181
array('startTokenPos' => 2), array('endTokenPos' => 2)
182182
),
183183
array(
184184
Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
185-
array('startTokenPos' => 5), array('endTokenPos' => 5)
185+
array('startTokenPos' => 5), array('endTokenPos' => 5, 'originalValue' => '"b"')
186186
),
187187
array(
188188
ord(';'), ';',

test/SyntaxHighlighterTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testSyntaxHighlighter()
4343
{
4444
$code = '<?php echo "hello world!";';
4545
$highlighter = $this->getHighlighter();
46-
$expected = "[36m<?php[0m\n\n[33mecho[0m [32m'hello world!'[0m;";
46+
$expected = "[36m<?php[0m\n\n[33mecho[0m [32m\"hello world!\"[0m;";
4747
$this->assertEquals($expected, $highlighter->highlight($code));
4848
}
4949

test/code/literals.test

+8-10
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,11 @@ INF;
120120
'a';
121121
'a
122122
b';
123-
'a';
124-
'a
125-
b';
123+
"a";
124+
"a\nb";
126125
'a\'b';
127-
'a\'b';
128-
'a\\b';
126+
"a'b";
127+
"a\b";
129128
'a\\nb$a
130129
{$b}';
131130
// strings (normalized to double quoted)
@@ -146,10 +145,9 @@ b';
146145
// make sure indentation doesn't mess anything up
147146
function foo()
148147
{
149-
'a
150-
b';
151-
'a
152-
b';
148+
"a\nb";
153149
'a
154150
b';
155-
}
151+
'a
152+
b';
153+
}

0 commit comments

Comments
 (0)