Parser.php 41 KB

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