Parser.php 34 KB

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