Flexible doc strings: Fix some issues, add more tests

This commit is contained in:
Nikita Popov 2018-09-22 10:09:00 +02:00
parent 0ed9065b4c
commit eebaa94647
3 changed files with 212 additions and 23 deletions

View File

@ -802,20 +802,25 @@ abstract class ParserAbstract implements Parser
);
}
foreach ($contents as $i => $s) {
if ($s instanceof Node\Scalar\EncapsedStringPart) {
$s->value = $this->stripIndentation(
$s->value, $indentLen, $indentChar,
$i === 0, $i === \count($contents) - 1, $s->getAttributes()
$newContents = [];
foreach ($contents as $i => $part) {
if ($part instanceof Node\Scalar\EncapsedStringPart) {
$isLast = $i === \count($contents) - 1;
$part->value = $this->stripIndentation(
$part->value, $indentLen, $indentChar,
$i === 0, $isLast, $part->getAttributes()
);
$s->value = String_::parseEscapeSequences($s->value, null, $parseUnicodeEscape);
$part->value = String_::parseEscapeSequences($part->value, null, $parseUnicodeEscape);
if ($isLast) {
$part->value = preg_replace('~(\r\n|\n|\r)\z~', '', $part->value);
}
if ('' === $part->value) {
continue;
}
}
$newContents[] = $part;
}
$s->value = preg_replace('~(\r\n|\n|\r)\z~', '', $s->value);
if ('' === $s->value) {
array_pop($contents);
}
return new Encapsed($contents, $attributes);
return new Encapsed($newContents, $attributes);
}
}

View File

@ -123,7 +123,7 @@ EOC;
* @dataProvider provideTestExtraAttributes
*/
public function testExtraAttributes($code, $expectedAttributes) {
$parser = $this->getParser(new Lexer);
$parser = $this->getParser(new Lexer\Emulative);
$stmts = $parser->parse("<?php $code;");
$node = $stmts[0] instanceof Stmt\Expression ? $stmts[0]->expr : $stmts[0];
$attributes = $node->getAttributes();
@ -152,17 +152,20 @@ EOC;
['"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
['b"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
['B"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
["<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']],
["<<<STR\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']],
["<<<\"STR\"\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']],
["b<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']],
["B<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']],
["<<< \t 'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']],
["<<<'\xff'\n\xff\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => "\xff"]],
["<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']],
["b<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']],
["B<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']],
["<<< \t \"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']],
["<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
["<<<STR\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
["<<<\"STR\"\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
["b<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
["B<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
["<<< \t 'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
["<<<'\xff'\n\xff\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => "\xff", 'docIndentation' => '']],
["<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
["b<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
["B<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
["<<< \t \"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
["<<<STR\n STR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => ' ']],
["<<<STR\n\tSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => "\t"]],
["<<<'STR'\n Foo\n STR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => ' ']],
["die", ['kind' => Expr\Exit_::KIND_DIE]],
["die('done')", ['kind' => Expr\Exit_::KIND_DIE]],
["exit", ['kind' => Expr\Exit_::KIND_EXIT]],

View File

@ -62,6 +62,52 @@ END;
d\r\n
e\n
END;
<<<BAR
$one-
BAR;
<<<BAR
$two -
BAR;
<<<BAR
$three -
BAR;
<<<BAR
$four-$four
BAR;
<<<BAR
$five-$five-
BAR;
<<<BAR
$six-$six-$six
BAR;
<<<BAR
$seven
-
BAR;
<<<BAR
$eight
-
BAR;
<<<BAR
$nine
BAR;
<<<BAR
-
BAR;
<<<BAR
-
BAR;
-----
array(
0: Stmt_Expression(
@ -175,4 +221,139 @@ array(
)
)
)
8: Stmt_Expression(
expr: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: one
)
1: Scalar_EncapsedStringPart(
value: -
)
)
)
)
9: Stmt_Expression(
expr: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: two
)
1: Scalar_EncapsedStringPart(
value: -
)
)
)
)
10: Stmt_Expression(
expr: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: three
)
1: Scalar_EncapsedStringPart(
value: -
)
)
)
)
11: Stmt_Expression(
expr: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: four
)
1: Scalar_EncapsedStringPart(
value: -
)
2: Expr_Variable(
name: four
)
)
)
)
12: Stmt_Expression(
expr: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: five
)
1: Scalar_EncapsedStringPart(
value: -
)
2: Expr_Variable(
name: five
)
3: Scalar_EncapsedStringPart(
value: -
)
)
)
)
13: Stmt_Expression(
expr: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: six
)
1: Scalar_EncapsedStringPart(
value: -
)
2: Expr_Variable(
name: six
)
3: Scalar_EncapsedStringPart(
value: -
)
4: Expr_Variable(
name: six
)
)
)
)
14: Stmt_Expression(
expr: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: seven
)
1: Scalar_EncapsedStringPart(
value:
-
)
)
)
)
15: Stmt_Expression(
expr: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: eight
)
1: Scalar_EncapsedStringPart(
value:
-
)
)
)
)
16: Stmt_Expression(
expr: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: nine
)
)
)
)
17: Stmt_Expression(
expr: Scalar_String(
value: -
)
)
18: Stmt_Expression(
expr: Scalar_String(
value: -
)
)
)