Parser.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Yaml;
  11. use Symfony\Component\Yaml\Exception\ParseException;
  12. use Symfony\Component\Yaml\Tag\TaggedValue;
  13. /**
  14. * Parser parses YAML strings to convert them to PHP arrays.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. *
  18. * @final
  19. */
  20. class Parser
  21. {
  22. const TAG_PATTERN = '(?P<tag>![\w!.\/:-]+)';
  23. const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
  24. private $filename;
  25. private $offset = 0;
  26. private $totalNumberOfLines;
  27. private $lines = [];
  28. private $currentLineNb = -1;
  29. private $currentLine = '';
  30. private $refs = [];
  31. private $skippedLineNumbers = [];
  32. private $locallySkippedLineNumbers = [];
  33. private $refsBeingParsed = [];
  34. /**
  35. * Parses a YAML file into a PHP value.
  36. *
  37. * @param string $filename The path to the YAML file to be parsed
  38. * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  39. *
  40. * @return mixed The YAML converted to a PHP value
  41. *
  42. * @throws ParseException If the file could not be read or the YAML is not valid
  43. */
  44. public function parseFile(string $filename, int $flags = 0)
  45. {
  46. if (!is_file($filename)) {
  47. throw new ParseException(sprintf('File "%s" does not exist.', $filename));
  48. }
  49. if (!is_readable($filename)) {
  50. throw new ParseException(sprintf('File "%s" cannot be read.', $filename));
  51. }
  52. $this->filename = $filename;
  53. try {
  54. return $this->parse(file_get_contents($filename), $flags);
  55. } finally {
  56. $this->filename = null;
  57. }
  58. }
  59. /**
  60. * Parses a YAML string to a PHP value.
  61. *
  62. * @param string $value A YAML string
  63. * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  64. *
  65. * @return mixed A PHP value
  66. *
  67. * @throws ParseException If the YAML is not valid
  68. */
  69. public function parse(string $value, int $flags = 0)
  70. {
  71. if (false === preg_match('//u', $value)) {
  72. throw new ParseException('The YAML value does not appear to be valid UTF-8.', -1, null, $this->filename);
  73. }
  74. $this->refs = [];
  75. $mbEncoding = null;
  76. if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
  77. $mbEncoding = mb_internal_encoding();
  78. mb_internal_encoding('UTF-8');
  79. }
  80. try {
  81. $data = $this->doParse($value, $flags);
  82. } finally {
  83. if (null !== $mbEncoding) {
  84. mb_internal_encoding($mbEncoding);
  85. }
  86. $this->lines = [];
  87. $this->currentLine = '';
  88. $this->refs = [];
  89. $this->skippedLineNumbers = [];
  90. $this->locallySkippedLineNumbers = [];
  91. $this->totalNumberOfLines = null;
  92. }
  93. return $data;
  94. }
  95. private function doParse(string $value, int $flags)
  96. {
  97. $this->currentLineNb = -1;
  98. $this->currentLine = '';
  99. $value = $this->cleanup($value);
  100. $this->lines = explode("\n", $value);
  101. $this->locallySkippedLineNumbers = [];
  102. if (null === $this->totalNumberOfLines) {
  103. $this->totalNumberOfLines = \count($this->lines);
  104. }
  105. if (!$this->moveToNextLine()) {
  106. return null;
  107. }
  108. $data = [];
  109. $context = null;
  110. $allowOverwrite = false;
  111. while ($this->isCurrentLineEmpty()) {
  112. if (!$this->moveToNextLine()) {
  113. return null;
  114. }
  115. }
  116. // Resolves the tag and returns if end of the document
  117. if (null !== ($tag = $this->getLineTag($this->currentLine, $flags, false)) && !$this->moveToNextLine()) {
  118. return new TaggedValue($tag, '');
  119. }
  120. do {
  121. if ($this->isCurrentLineEmpty()) {
  122. continue;
  123. }
  124. // tab?
  125. if ("\t" === $this->currentLine[0]) {
  126. throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  127. }
  128. Inline::initialize($flags, $this->getRealCurrentLineNb(), $this->filename);
  129. $isRef = $mergeNode = false;
  130. if ('-' === $this->currentLine[0] && self::preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) {
  131. if ($context && 'mapping' == $context) {
  132. throw new ParseException('You cannot define a sequence item when in a mapping.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  133. }
  134. $context = 'sequence';
  135. if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
  136. $isRef = $matches['ref'];
  137. $this->refsBeingParsed[] = $isRef;
  138. $values['value'] = $matches['value'];
  139. }
  140. if (isset($values['value'][1]) && '?' === $values['value'][0] && ' ' === $values['value'][1]) {
  141. throw new ParseException('Complex mappings are not supported.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  142. }
  143. // array
  144. if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
  145. $data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true) ?? '', $flags);
  146. } elseif (null !== $subTag = $this->getLineTag(ltrim($values['value'], ' '), $flags)) {
  147. $data[] = new TaggedValue(
  148. $subTag,
  149. $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags)
  150. );
  151. } else {
  152. if (
  153. isset($values['leadspaces'])
  154. && (
  155. '!' === $values['value'][0]
  156. || self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->trimTag($values['value']), $matches)
  157. )
  158. ) {
  159. // this is a compact notation element, add to next block and parse
  160. $block = $values['value'];
  161. if ($this->isNextLineIndented()) {
  162. $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + \strlen($values['leadspaces']) + 1);
  163. }
  164. $data[] = $this->parseBlock($this->getRealCurrentLineNb(), $block, $flags);
  165. } else {
  166. $data[] = $this->parseValue($values['value'], $flags, $context);
  167. }
  168. }
  169. if ($isRef) {
  170. $this->refs[$isRef] = end($data);
  171. array_pop($this->refsBeingParsed);
  172. }
  173. } elseif (
  174. self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
  175. && (false === strpos($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"]))
  176. ) {
  177. if ($context && 'sequence' == $context) {
  178. throw new ParseException('You cannot define a mapping item when in a sequence.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
  179. }
  180. $context = 'mapping';
  181. try {
  182. $key = Inline::parseScalar($values['key']);
  183. } catch (ParseException $e) {
  184. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  185. $e->setSnippet($this->currentLine);
  186. throw $e;
  187. }
  188. if (!\is_string($key) && !\is_int($key)) {
  189. throw new ParseException(sprintf('%s keys are not supported. Quote your evaluable mapping keys instead.', is_numeric($key) ? 'Numeric' : 'Non-string'), $this->getRealCurrentLineNb() + 1, $this->currentLine);
  190. }
  191. // Convert float keys to strings, to avoid being converted to integers by PHP
  192. if (\is_float($key)) {
  193. $key = (string) $key;
  194. }
  195. if ('<<' === $key && (!isset($values['value']) || '&' !== $values['value'][0] || !self::preg_match('#^&(?P<ref>[^ ]+)#u', $values['value'], $refMatches))) {
  196. $mergeNode = true;
  197. $allowOverwrite = true;
  198. if (isset($values['value'][0]) && '*' === $values['value'][0]) {
  199. $refName = substr(rtrim($values['value']), 1);
  200. if (!\array_key_exists($refName, $this->refs)) {
  201. if (false !== $pos = array_search($refName, $this->refsBeingParsed, true)) {
  202. throw new ParseException(sprintf('Circular reference [%s, %s] detected for reference "%s".', implode(', ', \array_slice($this->refsBeingParsed, $pos)), $refName, $refName), $this->currentLineNb + 1, $this->currentLine, $this->filename);
  203. }
  204. throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  205. }
  206. $refValue = $this->refs[$refName];
  207. if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $refValue instanceof \stdClass) {
  208. $refValue = (array) $refValue;
  209. }
  210. if (!\is_array($refValue)) {
  211. throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  212. }
  213. $data += $refValue; // array union
  214. } else {
  215. if (isset($values['value']) && '' !== $values['value']) {
  216. $value = $values['value'];
  217. } else {
  218. $value = $this->getNextEmbedBlock();
  219. }
  220. $parsed = $this->parseBlock($this->getRealCurrentLineNb() + 1, $value, $flags);
  221. if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $parsed instanceof \stdClass) {
  222. $parsed = (array) $parsed;
  223. }
  224. if (!\is_array($parsed)) {
  225. throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  226. }
  227. if (isset($parsed[0])) {
  228. // If the value associated with the merge key is a sequence, then this sequence is expected to contain mapping nodes
  229. // and each of these nodes is merged in turn according to its order in the sequence. Keys in mapping nodes earlier
  230. // in the sequence override keys specified in later mapping nodes.
  231. foreach ($parsed as $parsedItem) {
  232. if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $parsedItem instanceof \stdClass) {
  233. $parsedItem = (array) $parsedItem;
  234. }
  235. if (!\is_array($parsedItem)) {
  236. throw new ParseException('Merge items must be arrays.', $this->getRealCurrentLineNb() + 1, $parsedItem, $this->filename);
  237. }
  238. $data += $parsedItem; // array union
  239. }
  240. } else {
  241. // If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the
  242. // current mapping, unless the key already exists in it.
  243. $data += $parsed; // array union
  244. }
  245. }
  246. } elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u', $values['value'], $matches)) {
  247. $isRef = $matches['ref'];
  248. $this->refsBeingParsed[] = $isRef;
  249. $values['value'] = $matches['value'];
  250. }
  251. $subTag = null;
  252. if ($mergeNode) {
  253. // Merge keys
  254. } elseif (!isset($values['value']) || '' === $values['value'] || 0 === strpos($values['value'], '#') || (null !== $subTag = $this->getLineTag($values['value'], $flags)) || '<<' === $key) {
  255. // hash
  256. // if next line is less indented or equal, then it means that the current value is null
  257. if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
  258. // Spec: Keys MUST be unique; first one wins.
  259. // But overwriting is allowed when a merge node is used in current block.
  260. if ($allowOverwrite || !isset($data[$key])) {
  261. if (null !== $subTag) {
  262. $data[$key] = new TaggedValue($subTag, '');
  263. } else {
  264. $data[$key] = null;
  265. }
  266. } else {
  267. throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine);
  268. }
  269. } else {
  270. // remember the parsed line number here in case we need it to provide some contexts in error messages below
  271. $realCurrentLineNbKey = $this->getRealCurrentLineNb();
  272. $value = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(), $flags);
  273. if ('<<' === $key) {
  274. $this->refs[$refMatches['ref']] = $value;
  275. if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $value instanceof \stdClass) {
  276. $value = (array) $value;
  277. }
  278. $data += $value;
  279. } elseif ($allowOverwrite || !isset($data[$key])) {
  280. // Spec: Keys MUST be unique; first one wins.
  281. // But overwriting is allowed when a merge node is used in current block.
  282. if (null !== $subTag) {
  283. $data[$key] = new TaggedValue($subTag, $value);
  284. } else {
  285. $data[$key] = $value;
  286. }
  287. } else {
  288. throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $realCurrentLineNbKey + 1, $this->currentLine);
  289. }
  290. }
  291. } else {
  292. $value = $this->parseValue(rtrim($values['value']), $flags, $context);
  293. // Spec: Keys MUST be unique; first one wins.
  294. // But overwriting is allowed when a merge node is used in current block.
  295. if ($allowOverwrite || !isset($data[$key])) {
  296. $data[$key] = $value;
  297. } else {
  298. throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine);
  299. }
  300. }
  301. if ($isRef) {
  302. $this->refs[$isRef] = $data[$key];
  303. array_pop($this->refsBeingParsed);
  304. }
  305. } elseif ('"' === $this->currentLine[0] || "'" === $this->currentLine[0]) {
  306. if (null !== $context) {
  307. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  308. }
  309. try {
  310. return Inline::parse($this->parseQuotedString($this->currentLine), $flags, $this->refs);
  311. } catch (ParseException $e) {
  312. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  313. $e->setSnippet($this->currentLine);
  314. throw $e;
  315. }
  316. } elseif ('{' === $this->currentLine[0]) {
  317. if (null !== $context) {
  318. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  319. }
  320. try {
  321. $parsedMapping = Inline::parse($this->lexInlineMapping($this->currentLine), $flags, $this->refs);
  322. while ($this->moveToNextLine()) {
  323. if (!$this->isCurrentLineEmpty()) {
  324. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  325. }
  326. }
  327. return $parsedMapping;
  328. } catch (ParseException $e) {
  329. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  330. $e->setSnippet($this->currentLine);
  331. throw $e;
  332. }
  333. } elseif ('[' === $this->currentLine[0]) {
  334. if (null !== $context) {
  335. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  336. }
  337. try {
  338. $parsedSequence = Inline::parse($this->lexInlineSequence($this->currentLine), $flags, $this->refs);
  339. while ($this->moveToNextLine()) {
  340. if (!$this->isCurrentLineEmpty()) {
  341. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  342. }
  343. }
  344. return $parsedSequence;
  345. } catch (ParseException $e) {
  346. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  347. $e->setSnippet($this->currentLine);
  348. throw $e;
  349. }
  350. } else {
  351. // multiple documents are not supported
  352. if ('---' === $this->currentLine) {
  353. throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
  354. }
  355. if ($deprecatedUsage = (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1])) {
  356. throw new ParseException('Complex mappings are not supported.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  357. }
  358. // 1-liner optionally followed by newline(s)
  359. if (\is_string($value) && $this->lines[0] === trim($value)) {
  360. try {
  361. $value = Inline::parse($this->lines[0], $flags, $this->refs);
  362. } catch (ParseException $e) {
  363. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  364. $e->setSnippet($this->currentLine);
  365. throw $e;
  366. }
  367. return $value;
  368. }
  369. // try to parse the value as a multi-line string as a last resort
  370. if (0 === $this->currentLineNb) {
  371. $previousLineWasNewline = false;
  372. $previousLineWasTerminatedWithBackslash = false;
  373. $value = '';
  374. foreach ($this->lines as $line) {
  375. if ('' !== ltrim($line) && '#' === ltrim($line)[0]) {
  376. continue;
  377. }
  378. // If the indentation is not consistent at offset 0, it is to be considered as a ParseError
  379. if (0 === $this->offset && !$deprecatedUsage && isset($line[0]) && ' ' === $line[0]) {
  380. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  381. }
  382. if (false !== strpos($line, ': ')) {
  383. @trigger_error('Support for mapping keys in multi-line blocks is deprecated since Symfony 4.3 and will throw a ParseException in 5.0.', \E_USER_DEPRECATED);
  384. }
  385. if ('' === trim($line)) {
  386. $value .= "\n";
  387. } elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
  388. $value .= ' ';
  389. }
  390. if ('' !== trim($line) && '\\' === substr($line, -1)) {
  391. $value .= ltrim(substr($line, 0, -1));
  392. } elseif ('' !== trim($line)) {
  393. $value .= trim($line);
  394. }
  395. if ('' === trim($line)) {
  396. $previousLineWasNewline = true;
  397. $previousLineWasTerminatedWithBackslash = false;
  398. } elseif ('\\' === substr($line, -1)) {
  399. $previousLineWasNewline = false;
  400. $previousLineWasTerminatedWithBackslash = true;
  401. } else {
  402. $previousLineWasNewline = false;
  403. $previousLineWasTerminatedWithBackslash = false;
  404. }
  405. }
  406. try {
  407. return Inline::parse(trim($value));
  408. } catch (ParseException $e) {
  409. // fall-through to the ParseException thrown below
  410. }
  411. }
  412. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  413. }
  414. } while ($this->moveToNextLine());
  415. if (null !== $tag) {
  416. $data = new TaggedValue($tag, $data);
  417. }
  418. if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && !\is_object($data) && 'mapping' === $context) {
  419. $object = new \stdClass();
  420. foreach ($data as $key => $value) {
  421. $object->$key = $value;
  422. }
  423. $data = $object;
  424. }
  425. return empty($data) ? null : $data;
  426. }
  427. private function parseBlock(int $offset, string $yaml, int $flags)
  428. {
  429. $skippedLineNumbers = $this->skippedLineNumbers;
  430. foreach ($this->locallySkippedLineNumbers as $lineNumber) {
  431. if ($lineNumber < $offset) {
  432. continue;
  433. }
  434. $skippedLineNumbers[] = $lineNumber;
  435. }
  436. $parser = new self();
  437. $parser->offset = $offset;
  438. $parser->totalNumberOfLines = $this->totalNumberOfLines;
  439. $parser->skippedLineNumbers = $skippedLineNumbers;
  440. $parser->refs = &$this->refs;
  441. $parser->refsBeingParsed = $this->refsBeingParsed;
  442. return $parser->doParse($yaml, $flags);
  443. }
  444. /**
  445. * Returns the current line number (takes the offset into account).
  446. *
  447. * @internal
  448. *
  449. * @return int The current line number
  450. */
  451. public function getRealCurrentLineNb(): int
  452. {
  453. $realCurrentLineNumber = $this->currentLineNb + $this->offset;
  454. foreach ($this->skippedLineNumbers as $skippedLineNumber) {
  455. if ($skippedLineNumber > $realCurrentLineNumber) {
  456. break;
  457. }
  458. ++$realCurrentLineNumber;
  459. }
  460. return $realCurrentLineNumber;
  461. }
  462. /**
  463. * Returns the current line indentation.
  464. *
  465. * @return int The current line indentation
  466. */
  467. private function getCurrentLineIndentation(): int
  468. {
  469. return \strlen($this->currentLine) - \strlen(ltrim($this->currentLine, ' '));
  470. }
  471. /**
  472. * Returns the next embed block of YAML.
  473. *
  474. * @param int|null $indentation The indent level at which the block is to be read, or null for default
  475. * @param bool $inSequence True if the enclosing data structure is a sequence
  476. *
  477. * @return string A YAML string
  478. *
  479. * @throws ParseException When indentation problem are detected
  480. */
  481. private function getNextEmbedBlock(int $indentation = null, bool $inSequence = false): string
  482. {
  483. $oldLineIndentation = $this->getCurrentLineIndentation();
  484. if (!$this->moveToNextLine()) {
  485. return '';
  486. }
  487. if (null === $indentation) {
  488. $newIndent = null;
  489. $movements = 0;
  490. do {
  491. $EOF = false;
  492. // empty and comment-like lines do not influence the indentation depth
  493. if ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) {
  494. $EOF = !$this->moveToNextLine();
  495. if (!$EOF) {
  496. ++$movements;
  497. }
  498. } else {
  499. $newIndent = $this->getCurrentLineIndentation();
  500. }
  501. } while (!$EOF && null === $newIndent);
  502. for ($i = 0; $i < $movements; ++$i) {
  503. $this->moveToPreviousLine();
  504. }
  505. $unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem();
  506. if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
  507. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  508. }
  509. } else {
  510. $newIndent = $indentation;
  511. }
  512. $data = [];
  513. if ($this->getCurrentLineIndentation() >= $newIndent) {
  514. $data[] = substr($this->currentLine, $newIndent);
  515. } elseif ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) {
  516. $data[] = $this->currentLine;
  517. } else {
  518. $this->moveToPreviousLine();
  519. return '';
  520. }
  521. if ($inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) {
  522. // the previous line contained a dash but no item content, this line is a sequence item with the same indentation
  523. // and therefore no nested list or mapping
  524. $this->moveToPreviousLine();
  525. return '';
  526. }
  527. $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
  528. $isItComment = $this->isCurrentLineComment();
  529. while ($this->moveToNextLine()) {
  530. if ($isItComment && !$isItUnindentedCollection) {
  531. $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
  532. $isItComment = $this->isCurrentLineComment();
  533. }
  534. $indent = $this->getCurrentLineIndentation();
  535. if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
  536. $this->moveToPreviousLine();
  537. break;
  538. }
  539. if ($this->isCurrentLineBlank()) {
  540. $data[] = substr($this->currentLine, $newIndent);
  541. continue;
  542. }
  543. if ($indent >= $newIndent) {
  544. $data[] = substr($this->currentLine, $newIndent);
  545. } elseif ($this->isCurrentLineComment()) {
  546. $data[] = $this->currentLine;
  547. } elseif (0 == $indent) {
  548. $this->moveToPreviousLine();
  549. break;
  550. } else {
  551. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  552. }
  553. }
  554. return implode("\n", $data);
  555. }
  556. /**
  557. * Moves the parser to the next line.
  558. */
  559. private function moveToNextLine(): bool
  560. {
  561. if ($this->currentLineNb >= \count($this->lines) - 1) {
  562. return false;
  563. }
  564. $this->currentLine = $this->lines[++$this->currentLineNb];
  565. return true;
  566. }
  567. /**
  568. * Moves the parser to the previous line.
  569. */
  570. private function moveToPreviousLine(): bool
  571. {
  572. if ($this->currentLineNb < 1) {
  573. return false;
  574. }
  575. $this->currentLine = $this->lines[--$this->currentLineNb];
  576. return true;
  577. }
  578. /**
  579. * Parses a YAML value.
  580. *
  581. * @param string $value A YAML value
  582. * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  583. * @param string $context The parser context (either sequence or mapping)
  584. *
  585. * @return mixed A PHP value
  586. *
  587. * @throws ParseException When reference does not exist
  588. */
  589. private function parseValue(string $value, int $flags, string $context)
  590. {
  591. if (0 === strpos($value, '*')) {
  592. if (false !== $pos = strpos($value, '#')) {
  593. $value = substr($value, 1, $pos - 2);
  594. } else {
  595. $value = substr($value, 1);
  596. }
  597. if (!\array_key_exists($value, $this->refs)) {
  598. if (false !== $pos = array_search($value, $this->refsBeingParsed, true)) {
  599. throw new ParseException(sprintf('Circular reference [%s, %s] detected for reference "%s".', implode(', ', \array_slice($this->refsBeingParsed, $pos)), $value, $value), $this->currentLineNb + 1, $this->currentLine, $this->filename);
  600. }
  601. throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine, $this->filename);
  602. }
  603. return $this->refs[$value];
  604. }
  605. if (\in_array($value[0], ['!', '|', '>'], true) && self::preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
  606. $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
  607. $data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), abs((int) $modifiers));
  608. if ('' !== $matches['tag'] && '!' !== $matches['tag']) {
  609. if ('!!binary' === $matches['tag']) {
  610. return Inline::evaluateBinaryScalar($data);
  611. }
  612. return new TaggedValue(substr($matches['tag'], 1), $data);
  613. }
  614. return $data;
  615. }
  616. try {
  617. if ('' !== $value && '{' === $value[0]) {
  618. return Inline::parse($this->lexInlineMapping($value), $flags, $this->refs);
  619. } elseif ('' !== $value && '[' === $value[0]) {
  620. return Inline::parse($this->lexInlineSequence($value), $flags, $this->refs);
  621. }
  622. $quotation = '' !== $value && ('"' === $value[0] || "'" === $value[0]) ? $value[0] : null;
  623. // do not take following lines into account when the current line is a quoted single line value
  624. if (null !== $quotation && self::preg_match('/^'.$quotation.'.*'.$quotation.'(\s*#.*)?$/', $value)) {
  625. return Inline::parse($value, $flags, $this->refs);
  626. }
  627. $lines = [];
  628. while ($this->moveToNextLine()) {
  629. // unquoted strings end before the first unindented line
  630. if (null === $quotation && 0 === $this->getCurrentLineIndentation()) {
  631. $this->moveToPreviousLine();
  632. break;
  633. }
  634. $lines[] = trim($this->currentLine);
  635. // quoted string values end with a line that is terminated with the quotation character
  636. $escapedLine = str_replace(['\\\\', '\\"'], '', $this->currentLine);
  637. if ('' !== $escapedLine && substr($escapedLine, -1) === $quotation) {
  638. break;
  639. }
  640. }
  641. for ($i = 0, $linesCount = \count($lines), $previousLineBlank = false; $i < $linesCount; ++$i) {
  642. if ('' === $lines[$i]) {
  643. $value .= "\n";
  644. $previousLineBlank = true;
  645. } elseif ($previousLineBlank) {
  646. $value .= $lines[$i];
  647. $previousLineBlank = false;
  648. } else {
  649. $value .= ' '.$lines[$i];
  650. $previousLineBlank = false;
  651. }
  652. }
  653. Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
  654. $parsedValue = Inline::parse($value, $flags, $this->refs);
  655. if ('mapping' === $context && \is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
  656. throw new ParseException('A colon cannot be used in an unquoted mapping value.', $this->getRealCurrentLineNb() + 1, $value, $this->filename);
  657. }
  658. return $parsedValue;
  659. } catch (ParseException $e) {
  660. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  661. $e->setSnippet($this->currentLine);
  662. throw $e;
  663. }
  664. }
  665. /**
  666. * Parses a block scalar.
  667. *
  668. * @param string $style The style indicator that was used to begin this block scalar (| or >)
  669. * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
  670. * @param int $indentation The indentation indicator that was used to begin this block scalar
  671. */
  672. private function parseBlockScalar(string $style, string $chomping = '', int $indentation = 0): string
  673. {
  674. $notEOF = $this->moveToNextLine();
  675. if (!$notEOF) {
  676. return '';
  677. }
  678. $isCurrentLineBlank = $this->isCurrentLineBlank();
  679. $blockLines = [];
  680. // leading blank lines are consumed before determining indentation
  681. while ($notEOF && $isCurrentLineBlank) {
  682. // newline only if not EOF
  683. if ($notEOF = $this->moveToNextLine()) {
  684. $blockLines[] = '';
  685. $isCurrentLineBlank = $this->isCurrentLineBlank();
  686. }
  687. }
  688. // determine indentation if not specified
  689. if (0 === $indentation) {
  690. $currentLineLength = \strlen($this->currentLine);
  691. for ($i = 0; $i < $currentLineLength && ' ' === $this->currentLine[$i]; ++$i) {
  692. ++$indentation;
  693. }
  694. }
  695. if ($indentation > 0) {
  696. $pattern = sprintf('/^ {%d}(.*)$/', $indentation);
  697. while (
  698. $notEOF && (
  699. $isCurrentLineBlank ||
  700. self::preg_match($pattern, $this->currentLine, $matches)
  701. )
  702. ) {
  703. if ($isCurrentLineBlank && \strlen($this->currentLine) > $indentation) {
  704. $blockLines[] = substr($this->currentLine, $indentation);
  705. } elseif ($isCurrentLineBlank) {
  706. $blockLines[] = '';
  707. } else {
  708. $blockLines[] = $matches[1];
  709. }
  710. // newline only if not EOF
  711. if ($notEOF = $this->moveToNextLine()) {
  712. $isCurrentLineBlank = $this->isCurrentLineBlank();
  713. }
  714. }
  715. } elseif ($notEOF) {
  716. $blockLines[] = '';
  717. }
  718. if ($notEOF) {
  719. $blockLines[] = '';
  720. $this->moveToPreviousLine();
  721. } elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) {
  722. $blockLines[] = '';
  723. }
  724. // folded style
  725. if ('>' === $style) {
  726. $text = '';
  727. $previousLineIndented = false;
  728. $previousLineBlank = false;
  729. for ($i = 0, $blockLinesCount = \count($blockLines); $i < $blockLinesCount; ++$i) {
  730. if ('' === $blockLines[$i]) {
  731. $text .= "\n";
  732. $previousLineIndented = false;
  733. $previousLineBlank = true;
  734. } elseif (' ' === $blockLines[$i][0]) {
  735. $text .= "\n".$blockLines[$i];
  736. $previousLineIndented = true;
  737. $previousLineBlank = false;
  738. } elseif ($previousLineIndented) {
  739. $text .= "\n".$blockLines[$i];
  740. $previousLineIndented = false;
  741. $previousLineBlank = false;
  742. } elseif ($previousLineBlank || 0 === $i) {
  743. $text .= $blockLines[$i];
  744. $previousLineIndented = false;
  745. $previousLineBlank = false;
  746. } else {
  747. $text .= ' '.$blockLines[$i];
  748. $previousLineIndented = false;
  749. $previousLineBlank = false;
  750. }
  751. }
  752. } else {
  753. $text = implode("\n", $blockLines);
  754. }
  755. // deal with trailing newlines
  756. if ('' === $chomping) {
  757. $text = preg_replace('/\n+$/', "\n", $text);
  758. } elseif ('-' === $chomping) {
  759. $text = preg_replace('/\n+$/', '', $text);
  760. }
  761. return $text;
  762. }
  763. /**
  764. * Returns true if the next line is indented.
  765. *
  766. * @return bool Returns true if the next line is indented, false otherwise
  767. */
  768. private function isNextLineIndented(): bool
  769. {
  770. $currentIndentation = $this->getCurrentLineIndentation();
  771. $movements = 0;
  772. do {
  773. $EOF = !$this->moveToNextLine();
  774. if (!$EOF) {
  775. ++$movements;
  776. }
  777. } while (!$EOF && ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()));
  778. if ($EOF) {
  779. return false;
  780. }
  781. $ret = $this->getCurrentLineIndentation() > $currentIndentation;
  782. for ($i = 0; $i < $movements; ++$i) {
  783. $this->moveToPreviousLine();
  784. }
  785. return $ret;
  786. }
  787. /**
  788. * Returns true if the current line is blank or if it is a comment line.
  789. *
  790. * @return bool Returns true if the current line is empty or if it is a comment line, false otherwise
  791. */
  792. private function isCurrentLineEmpty(): bool
  793. {
  794. return $this->isCurrentLineBlank() || $this->isCurrentLineComment();
  795. }
  796. /**
  797. * Returns true if the current line is blank.
  798. *
  799. * @return bool Returns true if the current line is blank, false otherwise
  800. */
  801. private function isCurrentLineBlank(): bool
  802. {
  803. return '' == trim($this->currentLine, ' ');
  804. }
  805. /**
  806. * Returns true if the current line is a comment line.
  807. *
  808. * @return bool Returns true if the current line is a comment line, false otherwise
  809. */
  810. private function isCurrentLineComment(): bool
  811. {
  812. //checking explicitly the first char of the trim is faster than loops or strpos
  813. $ltrimmedLine = ltrim($this->currentLine, ' ');
  814. return '' !== $ltrimmedLine && '#' === $ltrimmedLine[0];
  815. }
  816. private function isCurrentLineLastLineInDocument(): bool
  817. {
  818. return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1);
  819. }
  820. /**
  821. * Cleanups a YAML string to be parsed.
  822. *
  823. * @param string $value The input YAML string
  824. *
  825. * @return string A cleaned up YAML string
  826. */
  827. private function cleanup(string $value): string
  828. {
  829. $value = str_replace(["\r\n", "\r"], "\n", $value);
  830. // strip YAML header
  831. $count = 0;
  832. $value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#u', '', $value, -1, $count);
  833. $this->offset += $count;
  834. // remove leading comments
  835. $trimmedValue = preg_replace('#^(\#.*?\n)+#s', '', $value, -1, $count);
  836. if (1 === $count) {
  837. // items have been removed, update the offset
  838. $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
  839. $value = $trimmedValue;
  840. }
  841. // remove start of the document marker (---)
  842. $trimmedValue = preg_replace('#^\-\-\-.*?\n#s', '', $value, -1, $count);
  843. if (1 === $count) {
  844. // items have been removed, update the offset
  845. $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
  846. $value = $trimmedValue;
  847. // remove end of the document marker (...)
  848. $value = preg_replace('#\.\.\.\s*$#', '', $value);
  849. }
  850. return $value;
  851. }
  852. /**
  853. * Returns true if the next line starts unindented collection.
  854. *
  855. * @return bool Returns true if the next line starts unindented collection, false otherwise
  856. */
  857. private function isNextLineUnIndentedCollection(): bool
  858. {
  859. $currentIndentation = $this->getCurrentLineIndentation();
  860. $movements = 0;
  861. do {
  862. $EOF = !$this->moveToNextLine();
  863. if (!$EOF) {
  864. ++$movements;
  865. }
  866. } while (!$EOF && ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()));
  867. if ($EOF) {
  868. return false;
  869. }
  870. $ret = $this->getCurrentLineIndentation() === $currentIndentation && $this->isStringUnIndentedCollectionItem();
  871. for ($i = 0; $i < $movements; ++$i) {
  872. $this->moveToPreviousLine();
  873. }
  874. return $ret;
  875. }
  876. /**
  877. * Returns true if the string is un-indented collection item.
  878. *
  879. * @return bool Returns true if the string is un-indented collection item, false otherwise
  880. */
  881. private function isStringUnIndentedCollectionItem(): bool
  882. {
  883. return '-' === rtrim($this->currentLine) || 0 === strpos($this->currentLine, '- ');
  884. }
  885. /**
  886. * A local wrapper for "preg_match" which will throw a ParseException if there
  887. * is an internal error in the PCRE engine.
  888. *
  889. * This avoids us needing to check for "false" every time PCRE is used
  890. * in the YAML engine
  891. *
  892. * @throws ParseException on a PCRE internal error
  893. *
  894. * @see preg_last_error()
  895. *
  896. * @internal
  897. */
  898. public static function preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int
  899. {
  900. if (false === $ret = preg_match($pattern, $subject, $matches, $flags, $offset)) {
  901. switch (preg_last_error()) {
  902. case \PREG_INTERNAL_ERROR:
  903. $error = 'Internal PCRE error.';
  904. break;
  905. case \PREG_BACKTRACK_LIMIT_ERROR:
  906. $error = 'pcre.backtrack_limit reached.';
  907. break;
  908. case \PREG_RECURSION_LIMIT_ERROR:
  909. $error = 'pcre.recursion_limit reached.';
  910. break;
  911. case \PREG_BAD_UTF8_ERROR:
  912. $error = 'Malformed UTF-8 data.';
  913. break;
  914. case \PREG_BAD_UTF8_OFFSET_ERROR:
  915. $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
  916. break;
  917. default:
  918. $error = 'Error.';
  919. }
  920. throw new ParseException($error);
  921. }
  922. return $ret;
  923. }
  924. /**
  925. * Trim the tag on top of the value.
  926. *
  927. * Prevent values such as "!foo {quz: bar}" to be considered as
  928. * a mapping block.
  929. */
  930. private function trimTag(string $value): string
  931. {
  932. if ('!' === $value[0]) {
  933. return ltrim(substr($value, 1, strcspn($value, " \r\n", 1)), ' ');
  934. }
  935. return $value;
  936. }
  937. private function getLineTag(string $value, int $flags, bool $nextLineCheck = true): ?string
  938. {
  939. if ('' === $value || '!' !== $value[0] || 1 !== self::preg_match('/^'.self::TAG_PATTERN.' *( +#.*)?$/', $value, $matches)) {
  940. return null;
  941. }
  942. if ($nextLineCheck && !$this->isNextLineIndented()) {
  943. return null;
  944. }
  945. $tag = substr($matches['tag'], 1);
  946. // Built-in tags
  947. if ($tag && '!' === $tag[0]) {
  948. throw new ParseException(sprintf('The built-in tag "!%s" is not implemented.', $tag), $this->getRealCurrentLineNb() + 1, $value, $this->filename);
  949. }
  950. if (Yaml::PARSE_CUSTOM_TAGS & $flags) {
  951. return $tag;
  952. }
  953. throw new ParseException(sprintf('Tags support is not enabled. You must use the flag "Yaml::PARSE_CUSTOM_TAGS" to use "%s".', $matches['tag']), $this->getRealCurrentLineNb() + 1, $value, $this->filename);
  954. }
  955. private function parseQuotedString(string $yaml): ?string
  956. {
  957. if ('' === $yaml || ('"' !== $yaml[0] && "'" !== $yaml[0])) {
  958. throw new \InvalidArgumentException(sprintf('"%s" is not a quoted string.', $yaml));
  959. }
  960. $lines = [$yaml];
  961. while ($this->moveToNextLine()) {
  962. $lines[] = $this->currentLine;
  963. if (!$this->isCurrentLineEmpty() && $yaml[0] === $this->currentLine[-1]) {
  964. break;
  965. }
  966. }
  967. $value = '';
  968. for ($i = 0, $linesCount = \count($lines), $previousLineWasNewline = false, $previousLineWasTerminatedWithBackslash = false; $i < $linesCount; ++$i) {
  969. if ('' === trim($lines[$i])) {
  970. $value .= "\n";
  971. } elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
  972. $value .= ' ';
  973. }
  974. if ('' !== trim($lines[$i]) && '\\' === substr($lines[$i], -1)) {
  975. $value .= ltrim(substr($lines[$i], 0, -1));
  976. } elseif ('' !== trim($lines[$i])) {
  977. $value .= trim($lines[$i]);
  978. }
  979. if ('' === trim($lines[$i])) {
  980. $previousLineWasNewline = true;
  981. $previousLineWasTerminatedWithBackslash = false;
  982. } elseif ('\\' === substr($lines[$i], -1)) {
  983. $previousLineWasNewline = false;
  984. $previousLineWasTerminatedWithBackslash = true;
  985. } else {
  986. $previousLineWasNewline = false;
  987. $previousLineWasTerminatedWithBackslash = false;
  988. }
  989. }
  990. return $value;
  991. for ($i = 1; isset($yaml[$i]) && $quotation !== $yaml[$i]; ++$i) {
  992. }
  993. // quoted single line string
  994. if (isset($yaml[$i]) && $quotation === $yaml[$i]) {
  995. return $yaml;
  996. }
  997. $lines = [$yaml];
  998. while ($this->moveToNextLine()) {
  999. for ($i = 1; isset($this->currentLine[$i]) && $quotation !== $this->currentLine[$i]; ++$i) {
  1000. }
  1001. $lines[] = trim($this->currentLine);
  1002. if (isset($this->currentLine[$i]) && $quotation === $this->currentLine[$i]) {
  1003. break;
  1004. }
  1005. }
  1006. }
  1007. private function lexInlineMapping(string $yaml): string
  1008. {
  1009. if ('' === $yaml || '{' !== $yaml[0]) {
  1010. throw new \InvalidArgumentException(sprintf('"%s" is not a sequence.', $yaml));
  1011. }
  1012. for ($i = 1; isset($yaml[$i]) && '}' !== $yaml[$i]; ++$i) {
  1013. }
  1014. if (isset($yaml[$i]) && '}' === $yaml[$i]) {
  1015. return $yaml;
  1016. }
  1017. $lines = [$yaml];
  1018. while ($this->moveToNextLine()) {
  1019. $lines[] = $this->currentLine;
  1020. }
  1021. return implode("\n", $lines);
  1022. }
  1023. private function lexInlineSequence(string $yaml): string
  1024. {
  1025. if ('' === $yaml || '[' !== $yaml[0]) {
  1026. throw new \InvalidArgumentException(sprintf('"%s" is not a sequence.', $yaml));
  1027. }
  1028. for ($i = 1; isset($yaml[$i]) && ']' !== $yaml[$i]; ++$i) {
  1029. }
  1030. if (isset($yaml[$i]) && ']' === $yaml[$i]) {
  1031. return $yaml;
  1032. }
  1033. $value = $yaml;
  1034. while ($this->moveToNextLine()) {
  1035. for ($i = 1; isset($this->currentLine[$i]) && ']' !== $this->currentLine[$i]; ++$i) {
  1036. }
  1037. $trimmedValue = trim($this->currentLine);
  1038. if ('' !== $trimmedValue && '#' === $trimmedValue[0]) {
  1039. continue;
  1040. }
  1041. $value .= $trimmedValue;
  1042. if (isset($this->currentLine[$i]) && ']' === $this->currentLine[$i]) {
  1043. break;
  1044. }
  1045. }
  1046. return $value;
  1047. }
  1048. }