20
20
*/
21
21
class Parser
22
22
{
23
- const FOLDED_SCALAR_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)? ' ;
23
+ const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)? ' ;
24
+ // BC - wrongly named
25
+ const FOLDED_SCALAR_PATTERN = self ::BLOCK_SCALAR_HEADER_PATTERN ;
24
26
25
27
private $ offset = 0 ;
26
28
private $ lines = array ();
@@ -337,8 +339,8 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
337
339
338
340
$ isItUnindentedCollection = $ this ->isStringUnIndentedCollectionItem ($ this ->currentLine );
339
341
340
- // Comments must not be removed inside a string block (ie. after a line ending with "|")
341
- $ removeCommentsPattern = '~ ' .self ::FOLDED_SCALAR_PATTERN .'$~ ' ;
342
+ // Comments must not be removed inside a block scalar
343
+ $ removeCommentsPattern = '~ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$~ ' ;
342
344
$ removeComments = !preg_match ($ removeCommentsPattern , $ this ->currentLine );
343
345
344
346
while ($ this ->moveToNextLine ()) {
@@ -427,10 +429,10 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
427
429
return $ this ->refs [$ value ];
428
430
}
429
431
430
- if (preg_match ('/^ ' .self ::FOLDED_SCALAR_PATTERN .'$/ ' , $ value , $ matches )) {
432
+ if (preg_match ('/^ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$/ ' , $ value , $ matches )) {
431
433
$ modifiers = isset ($ matches ['modifiers ' ]) ? $ matches ['modifiers ' ] : '' ;
432
434
433
- return $ this ->parseFoldedScalar ($ matches ['separator ' ], preg_replace ('#\d+# ' , '' , $ modifiers ), (int ) abs ($ modifiers ));
435
+ return $ this ->parseBlockScalar ($ matches ['separator ' ], preg_replace ('#\d+# ' , '' , $ modifiers ), (int ) abs ($ modifiers ));
434
436
}
435
437
436
438
try {
@@ -444,15 +446,15 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
444
446
}
445
447
446
448
/**
447
- * Parses a folded scalar.
449
+ * Parses a block scalar.
448
450
*
449
- * @param string $separator The separator that was used to begin this folded scalar (| or >)
450
- * @param string $indicator The indicator that was used to begin this folded scalar (+ or -)
451
- * @param int $indentation The indentation that was used to begin this folded scalar
451
+ * @param string $style The style indicator that was used to begin this block scalar (| or >)
452
+ * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
453
+ * @param int $indentation The indentation indicator that was used to begin this block scalar
452
454
*
453
455
* @return string The text value
454
456
*/
455
- private function parseFoldedScalar ( $ separator , $ indicator = '' , $ indentation = 0 )
457
+ private function parseBlockScalar ( $ style , $ chomping = '' , $ indentation = 0 )
456
458
{
457
459
$ notEOF = $ this ->moveToNextLine ();
458
460
if (!$ notEOF ) {
@@ -507,17 +509,23 @@ private function parseFoldedScalar($separator, $indicator = '', $indentation = 0
507
509
$ this ->moveToPreviousLine ();
508
510
}
509
511
510
- // replace all non-trailing single newlines with spaces in folded blocks
511
- if ('> ' === $ separator ) {
512
+ // folded style
513
+ if ('> ' === $ style ) {
514
+ // folded lines
515
+ // replace all non-leading/non-trailing single newlines with spaces
512
516
preg_match ('/(\n*)$/ ' , $ text , $ matches );
513
- $ text = preg_replace ('/(?<!\n)\n(?!\n)/ ' , ' ' , rtrim ($ text , "\n" ));
517
+ $ text = preg_replace ('/(?<!\n|^ )\n(?!\n)/ ' , ' ' , rtrim ($ text , "\n" ));
514
518
$ text .= $ matches [1 ];
519
+
520
+ // empty separation lines
521
+ // remove one newline from each group of non-leading/non-trailing newlines
522
+ $ text = preg_replace ('/[^\n]\n+\K\n(?=[^\n])/ ' , '' , $ text );
515
523
}
516
524
517
- // deal with trailing newlines as indicated
518
- if ('' === $ indicator ) {
525
+ // deal with trailing newlines
526
+ if ('' === $ chomping ) {
519
527
$ text = preg_replace ('/\n+$/ ' , "\n" , $ text );
520
- } elseif ('- ' === $ indicator ) {
528
+ } elseif ('- ' === $ chomping ) {
521
529
$ text = preg_replace ('/\n+$/ ' , '' , $ text );
522
530
}
523
531
0 commit comments