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 ();
@@ -332,8 +334,8 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
332
334
333
335
$ isItUnindentedCollection = $ this ->isStringUnIndentedCollectionItem ($ this ->currentLine );
334
336
335
- // Comments must not be removed inside a string block (ie. after a line ending with "|")
336
- $ removeCommentsPattern = '~ ' .self ::FOLDED_SCALAR_PATTERN .'$~ ' ;
337
+ // Comments must not be removed inside a block scalar
338
+ $ removeCommentsPattern = '~ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$~ ' ;
337
339
$ removeComments = !preg_match ($ removeCommentsPattern , $ this ->currentLine );
338
340
339
341
while ($ this ->moveToNextLine ()) {
@@ -422,10 +424,10 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
422
424
return $ this ->refs [$ value ];
423
425
}
424
426
425
- if (preg_match ('/^ ' .self ::FOLDED_SCALAR_PATTERN .'$/ ' , $ value , $ matches )) {
427
+ if (preg_match ('/^ ' .self ::BLOCK_SCALAR_HEADER_PATTERN .'$/ ' , $ value , $ matches )) {
426
428
$ modifiers = isset ($ matches ['modifiers ' ]) ? $ matches ['modifiers ' ] : '' ;
427
429
428
- return $ this ->parseFoldedScalar ($ matches ['separator ' ], preg_replace ('#\d+# ' , '' , $ modifiers ), (int ) abs ($ modifiers ));
430
+ return $ this ->parseBlockScalar ($ matches ['separator ' ], preg_replace ('#\d+# ' , '' , $ modifiers ), (int ) abs ($ modifiers ));
429
431
}
430
432
431
433
try {
@@ -439,15 +441,15 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
439
441
}
440
442
441
443
/**
442
- * Parses a folded scalar.
444
+ * Parses a block scalar.
443
445
*
444
- * @param string $separator The separator that was used to begin this folded scalar (| or >)
445
- * @param string $indicator The indicator that was used to begin this folded scalar (+ or -)
446
- * @param int $indentation The indentation that was used to begin this folded scalar
446
+ * @param string $style The style indicator that was used to begin this block scalar (| or >)
447
+ * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
448
+ * @param int $indentation The indentation indicator that was used to begin this block scalar
447
449
*
448
450
* @return string The text value
449
451
*/
450
- private function parseFoldedScalar ( $ separator , $ indicator = '' , $ indentation = 0 )
452
+ private function parseBlockScalar ( $ style , $ chomping = '' , $ indentation = 0 )
451
453
{
452
454
$ notEOF = $ this ->moveToNextLine ();
453
455
if (!$ notEOF ) {
@@ -502,17 +504,23 @@ private function parseFoldedScalar($separator, $indicator = '', $indentation = 0
502
504
$ this ->moveToPreviousLine ();
503
505
}
504
506
505
- // replace all non-trailing single newlines with spaces in folded blocks
506
- if ('> ' === $ separator ) {
507
+ // folded style
508
+ if ('> ' === $ style ) {
509
+ // folded lines
510
+ // replace all non-leading/non-trailing single newlines with spaces
507
511
preg_match ('/(\n*)$/ ' , $ text , $ matches );
508
- $ text = preg_replace ('/(?<!\n)\n(?!\n)/ ' , ' ' , rtrim ($ text , "\n" ));
512
+ $ text = preg_replace ('/(?<!\n|^ )\n(?!\n)/ ' , ' ' , rtrim ($ text , "\n" ));
509
513
$ text .= $ matches [1 ];
514
+
515
+ // empty separation lines
516
+ // remove one newline from each group of non-leading/non-trailing newlines
517
+ $ text = preg_replace ('/[^\n]\n+\K\n(?=[^\n])/ ' , '' , $ text );
510
518
}
511
519
512
- // deal with trailing newlines as indicated
513
- if ('' === $ indicator ) {
520
+ // deal with trailing newlines
521
+ if ('' === $ chomping ) {
514
522
$ text = preg_replace ('/\n+$/ ' , "\n" , $ text );
515
- } elseif ('- ' === $ indicator ) {
523
+ } elseif ('- ' === $ chomping ) {
516
524
$ text = preg_replace ('/\n+$/ ' , '' , $ text );
517
525
}
518
526
0 commit comments