Parser.php 48 KB

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