Parser.php 45 KB

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