Parser.php 40 KB

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