Parser.php 43 KB

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