Parser.php 42 KB

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