Parser.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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 (!preg_match('//u', $value)) {
  75. throw new ParseException('The YAML value does not appear to be valid UTF-8.');
  76. }
  77. $this->currentLineNb = -1;
  78. $this->currentLine = '';
  79. $value = $this->cleanup($value);
  80. $this->lines = explode("\n", $value);
  81. if (null === $this->totalNumberOfLines) {
  82. $this->totalNumberOfLines = count($this->lines);
  83. }
  84. if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
  85. $mbEncoding = mb_internal_encoding();
  86. mb_internal_encoding('UTF-8');
  87. }
  88. $data = array();
  89. $context = null;
  90. $allowOverwrite = false;
  91. while ($this->moveToNextLine()) {
  92. if ($this->isCurrentLineEmpty()) {
  93. continue;
  94. }
  95. // tab?
  96. if ("\t" === $this->currentLine[0]) {
  97. throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  98. }
  99. $isRef = $mergeNode = false;
  100. if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
  101. if ($context && 'mapping' == $context) {
  102. throw new ParseException('You cannot define a sequence item when in a mapping', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  103. }
  104. $context = 'sequence';
  105. if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
  106. $isRef = $matches['ref'];
  107. $values['value'] = $matches['value'];
  108. }
  109. // array
  110. if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
  111. $data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags);
  112. } else {
  113. if (isset($values['leadspaces'])
  114. && preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $values['value'], $matches)
  115. ) {
  116. // this is a compact notation element, add to next block and parse
  117. $block = $values['value'];
  118. if ($this->isNextLineIndented()) {
  119. $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + strlen($values['leadspaces']) + 1);
  120. }
  121. $data[] = $this->parseBlock($this->getRealCurrentLineNb(), $block, $flags);
  122. } else {
  123. $data[] = $this->parseValue($values['value'], $flags, $context);
  124. }
  125. }
  126. if ($isRef) {
  127. $this->refs[$isRef] = end($data);
  128. }
  129. } elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values) && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))) {
  130. if ($context && 'sequence' == $context) {
  131. throw new ParseException('You cannot define a mapping item when in a sequence', $this->currentLineNb + 1, $this->currentLine);
  132. }
  133. $context = 'mapping';
  134. // force correct settings
  135. Inline::parse(null, $flags, $this->refs);
  136. try {
  137. Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
  138. $key = Inline::parseScalar($values['key']);
  139. } catch (ParseException $e) {
  140. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  141. $e->setSnippet($this->currentLine);
  142. throw $e;
  143. }
  144. // Convert float keys to strings, to avoid being converted to integers by PHP
  145. if (is_float($key)) {
  146. $key = (string) $key;
  147. }
  148. if ('<<' === $key) {
  149. $mergeNode = true;
  150. $allowOverwrite = true;
  151. if (isset($values['value']) && 0 === strpos($values['value'], '*')) {
  152. $refName = substr($values['value'], 1);
  153. if (!array_key_exists($refName, $this->refs)) {
  154. throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this->getRealCurrentLineNb() + 1, $this->currentLine);
  155. }
  156. $refValue = $this->refs[$refName];
  157. if (!is_array($refValue)) {
  158. throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  159. }
  160. $data += $refValue; // array union
  161. } else {
  162. if (isset($values['value']) && $values['value'] !== '') {
  163. $value = $values['value'];
  164. } else {
  165. $value = $this->getNextEmbedBlock();
  166. }
  167. $parsed = $this->parseBlock($this->getRealCurrentLineNb() + 1, $value, $flags);
  168. if (!is_array($parsed)) {
  169. throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  170. }
  171. if (isset($parsed[0])) {
  172. // If the value associated with the merge key is a sequence, then this sequence is expected to contain mapping nodes
  173. // and each of these nodes is merged in turn according to its order in the sequence. Keys in mapping nodes earlier
  174. // in the sequence override keys specified in later mapping nodes.
  175. foreach ($parsed as $parsedItem) {
  176. if (!is_array($parsedItem)) {
  177. throw new ParseException('Merge items must be arrays.', $this->getRealCurrentLineNb() + 1, $parsedItem);
  178. }
  179. $data += $parsedItem; // array union
  180. }
  181. } else {
  182. // If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the
  183. // current mapping, unless the key already exists in it.
  184. $data += $parsed; // array union
  185. }
  186. }
  187. } elseif (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
  188. $isRef = $matches['ref'];
  189. $values['value'] = $matches['value'];
  190. }
  191. if ($mergeNode) {
  192. // Merge keys
  193. } elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
  194. // hash
  195. // if next line is less indented or equal, then it means that the current value is null
  196. if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
  197. // Spec: Keys MUST be unique; first one wins.
  198. // But overwriting is allowed when a merge node is used in current block.
  199. if ($allowOverwrite || !isset($data[$key])) {
  200. $data[$key] = null;
  201. } else {
  202. @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);
  203. }
  204. } else {
  205. // remember the parsed line number here in case we need it to provide some contexts in error messages below
  206. $realCurrentLineNbKey = $this->getRealCurrentLineNb();
  207. $value = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(), $flags);
  208. // Spec: Keys MUST be unique; first one wins.
  209. // But overwriting is allowed when a merge node is used in current block.
  210. if ($allowOverwrite || !isset($data[$key])) {
  211. $data[$key] = $value;
  212. } else {
  213. @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);
  214. }
  215. }
  216. } else {
  217. $value = $this->parseValue($values['value'], $flags, $context);
  218. // Spec: Keys MUST be unique; first one wins.
  219. // But overwriting is allowed when a merge node is used in current block.
  220. if ($allowOverwrite || !isset($data[$key])) {
  221. $data[$key] = $value;
  222. } else {
  223. @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);
  224. }
  225. }
  226. if ($isRef) {
  227. $this->refs[$isRef] = $data[$key];
  228. }
  229. } else {
  230. // multiple documents are not supported
  231. if ('---' === $this->currentLine) {
  232. throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine);
  233. }
  234. // 1-liner optionally followed by newline(s)
  235. if (is_string($value) && $this->lines[0] === trim($value)) {
  236. try {
  237. Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
  238. $value = Inline::parse($this->lines[0], $flags, $this->refs);
  239. } catch (ParseException $e) {
  240. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  241. $e->setSnippet($this->currentLine);
  242. throw $e;
  243. }
  244. if (isset($mbEncoding)) {
  245. mb_internal_encoding($mbEncoding);
  246. }
  247. return $value;
  248. }
  249. switch (preg_last_error()) {
  250. case PREG_INTERNAL_ERROR:
  251. $error = 'Internal PCRE error.';
  252. break;
  253. case PREG_BACKTRACK_LIMIT_ERROR:
  254. $error = 'pcre.backtrack_limit reached.';
  255. break;
  256. case PREG_RECURSION_LIMIT_ERROR:
  257. $error = 'pcre.recursion_limit reached.';
  258. break;
  259. case PREG_BAD_UTF8_ERROR:
  260. $error = 'Malformed UTF-8 data.';
  261. break;
  262. case PREG_BAD_UTF8_OFFSET_ERROR:
  263. $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
  264. break;
  265. default:
  266. $error = 'Unable to parse.';
  267. }
  268. throw new ParseException($error, $this->getRealCurrentLineNb() + 1, $this->currentLine);
  269. }
  270. }
  271. if (isset($mbEncoding)) {
  272. mb_internal_encoding($mbEncoding);
  273. }
  274. if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && !is_object($data) && 'mapping' === $context) {
  275. $object = new \stdClass();
  276. foreach ($data as $key => $value) {
  277. $object->$key = $value;
  278. }
  279. $data = $object;
  280. }
  281. return empty($data) ? null : $data;
  282. }
  283. private function parseBlock($offset, $yaml, $flags)
  284. {
  285. $skippedLineNumbers = $this->skippedLineNumbers;
  286. foreach ($this->locallySkippedLineNumbers as $lineNumber) {
  287. if ($lineNumber < $offset) {
  288. continue;
  289. }
  290. $skippedLineNumbers[] = $lineNumber;
  291. }
  292. $parser = new self($offset, $this->totalNumberOfLines, $skippedLineNumbers);
  293. $parser->refs = &$this->refs;
  294. return $parser->parse($yaml, $flags);
  295. }
  296. /**
  297. * Returns the current line number (takes the offset into account).
  298. *
  299. * @return int The current line number
  300. */
  301. private function getRealCurrentLineNb()
  302. {
  303. $realCurrentLineNumber = $this->currentLineNb + $this->offset;
  304. foreach ($this->skippedLineNumbers as $skippedLineNumber) {
  305. if ($skippedLineNumber > $realCurrentLineNumber) {
  306. break;
  307. }
  308. ++$realCurrentLineNumber;
  309. }
  310. return $realCurrentLineNumber;
  311. }
  312. /**
  313. * Returns the current line indentation.
  314. *
  315. * @return int The current line indentation
  316. */
  317. private function getCurrentLineIndentation()
  318. {
  319. return strlen($this->currentLine) - strlen(ltrim($this->currentLine, ' '));
  320. }
  321. /**
  322. * Returns the next embed block of YAML.
  323. *
  324. * @param int $indentation The indent level at which the block is to be read, or null for default
  325. * @param bool $inSequence True if the enclosing data structure is a sequence
  326. *
  327. * @return string A YAML string
  328. *
  329. * @throws ParseException When indentation problem are detected
  330. */
  331. private function getNextEmbedBlock($indentation = null, $inSequence = false)
  332. {
  333. $oldLineIndentation = $this->getCurrentLineIndentation();
  334. $blockScalarIndentations = array();
  335. if ($this->isBlockScalarHeader()) {
  336. $blockScalarIndentations[] = $this->getCurrentLineIndentation();
  337. }
  338. if (!$this->moveToNextLine()) {
  339. return;
  340. }
  341. if (null === $indentation) {
  342. $newIndent = $this->getCurrentLineIndentation();
  343. $unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem();
  344. if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
  345. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  346. }
  347. } else {
  348. $newIndent = $indentation;
  349. }
  350. $data = array();
  351. if ($this->getCurrentLineIndentation() >= $newIndent) {
  352. $data[] = substr($this->currentLine, $newIndent);
  353. } else {
  354. $this->moveToPreviousLine();
  355. return;
  356. }
  357. if ($inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) {
  358. // the previous line contained a dash but no item content, this line is a sequence item with the same indentation
  359. // and therefore no nested list or mapping
  360. $this->moveToPreviousLine();
  361. return;
  362. }
  363. $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
  364. if (empty($blockScalarIndentations) && $this->isBlockScalarHeader()) {
  365. $blockScalarIndentations[] = $this->getCurrentLineIndentation();
  366. }
  367. $previousLineIndentation = $this->getCurrentLineIndentation();
  368. while ($this->moveToNextLine()) {
  369. $indent = $this->getCurrentLineIndentation();
  370. // terminate all block scalars that are more indented than the current line
  371. if (!empty($blockScalarIndentations) && $indent < $previousLineIndentation && trim($this->currentLine) !== '') {
  372. foreach ($blockScalarIndentations as $key => $blockScalarIndentation) {
  373. if ($blockScalarIndentation >= $this->getCurrentLineIndentation()) {
  374. unset($blockScalarIndentations[$key]);
  375. }
  376. }
  377. }
  378. if (empty($blockScalarIndentations) && !$this->isCurrentLineComment() && $this->isBlockScalarHeader()) {
  379. $blockScalarIndentations[] = $this->getCurrentLineIndentation();
  380. }
  381. $previousLineIndentation = $indent;
  382. if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
  383. $this->moveToPreviousLine();
  384. break;
  385. }
  386. if ($this->isCurrentLineBlank()) {
  387. $data[] = substr($this->currentLine, $newIndent);
  388. continue;
  389. }
  390. // we ignore "comment" lines only when we are not inside a scalar block
  391. if (empty($blockScalarIndentations) && $this->isCurrentLineComment()) {
  392. // remember ignored comment lines (they are used later in nested
  393. // parser calls to determine real line numbers)
  394. //
  395. // CAUTION: beware to not populate the global property here as it
  396. // will otherwise influence the getRealCurrentLineNb() call here
  397. // for consecutive comment lines and subsequent embedded blocks
  398. $this->locallySkippedLineNumbers[] = $this->getRealCurrentLineNb();
  399. continue;
  400. }
  401. if ($indent >= $newIndent) {
  402. $data[] = substr($this->currentLine, $newIndent);
  403. } elseif (0 == $indent) {
  404. $this->moveToPreviousLine();
  405. break;
  406. } else {
  407. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  408. }
  409. }
  410. return implode("\n", $data);
  411. }
  412. /**
  413. * Moves the parser to the next line.
  414. *
  415. * @return bool
  416. */
  417. private function moveToNextLine()
  418. {
  419. if ($this->currentLineNb >= count($this->lines) - 1) {
  420. return false;
  421. }
  422. $this->currentLine = $this->lines[++$this->currentLineNb];
  423. return true;
  424. }
  425. /**
  426. * Moves the parser to the previous line.
  427. *
  428. * @return bool
  429. */
  430. private function moveToPreviousLine()
  431. {
  432. if ($this->currentLineNb < 1) {
  433. return false;
  434. }
  435. $this->currentLine = $this->lines[--$this->currentLineNb];
  436. return true;
  437. }
  438. /**
  439. * Parses a YAML value.
  440. *
  441. * @param string $value A YAML value
  442. * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  443. * @param string $context The parser context (either sequence or mapping)
  444. *
  445. * @return mixed A PHP value
  446. *
  447. * @throws ParseException When reference does not exist
  448. */
  449. private function parseValue($value, $flags, $context)
  450. {
  451. if (0 === strpos($value, '*')) {
  452. if (false !== $pos = strpos($value, '#')) {
  453. $value = substr($value, 1, $pos - 2);
  454. } else {
  455. $value = substr($value, 1);
  456. }
  457. if (!array_key_exists($value, $this->refs)) {
  458. throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine);
  459. }
  460. return $this->refs[$value];
  461. }
  462. if (preg_match('/^'.self::TAG_PATTERN.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
  463. $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
  464. $data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
  465. if (isset($matches['tag']) && '!!binary' === $matches['tag']) {
  466. return Inline::evaluateBinaryScalar($data);
  467. }
  468. return $data;
  469. }
  470. try {
  471. $quotation = '' !== $value && ('"' === $value[0] || "'" === $value[0]) ? $value[0] : null;
  472. // do not take following lines into account when the current line is a quoted single line value
  473. if (null !== $quotation && preg_match('/^'.$quotation.'.*'.$quotation.'(\s*#.*)?$/', $value)) {
  474. return Inline::parse($value, $flags, $this->refs);
  475. }
  476. while ($this->moveToNextLine()) {
  477. // unquoted strings end before the first unindented line
  478. if (null === $quotation && $this->getCurrentLineIndentation() === 0) {
  479. $this->moveToPreviousLine();
  480. break;
  481. }
  482. $value .= ' '.trim($this->currentLine);
  483. // quoted string values end with a line that is terminated with the quotation character
  484. if ('' !== $this->currentLine && substr($this->currentLine, -1) === $quotation) {
  485. break;
  486. }
  487. }
  488. Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
  489. $parsedValue = Inline::parse($value, $flags, $this->refs);
  490. if ('mapping' === $context && is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
  491. throw new ParseException('A colon cannot be used in an unquoted mapping value.');
  492. }
  493. return $parsedValue;
  494. } catch (ParseException $e) {
  495. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  496. $e->setSnippet($this->currentLine);
  497. throw $e;
  498. }
  499. }
  500. /**
  501. * Parses a block scalar.
  502. *
  503. * @param string $style The style indicator that was used to begin this block scalar (| or >)
  504. * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
  505. * @param int $indentation The indentation indicator that was used to begin this block scalar
  506. *
  507. * @return string The text value
  508. */
  509. private function parseBlockScalar($style, $chomping = '', $indentation = 0)
  510. {
  511. $notEOF = $this->moveToNextLine();
  512. if (!$notEOF) {
  513. return '';
  514. }
  515. $isCurrentLineBlank = $this->isCurrentLineBlank();
  516. $blockLines = array();
  517. // leading blank lines are consumed before determining indentation
  518. while ($notEOF && $isCurrentLineBlank) {
  519. // newline only if not EOF
  520. if ($notEOF = $this->moveToNextLine()) {
  521. $blockLines[] = '';
  522. $isCurrentLineBlank = $this->isCurrentLineBlank();
  523. }
  524. }
  525. // determine indentation if not specified
  526. if (0 === $indentation) {
  527. if (preg_match('/^ +/', $this->currentLine, $matches)) {
  528. $indentation = strlen($matches[0]);
  529. }
  530. }
  531. if ($indentation > 0) {
  532. $pattern = sprintf('/^ {%d}(.*)$/', $indentation);
  533. while (
  534. $notEOF && (
  535. $isCurrentLineBlank ||
  536. preg_match($pattern, $this->currentLine, $matches)
  537. )
  538. ) {
  539. if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) {
  540. $blockLines[] = substr($this->currentLine, $indentation);
  541. } elseif ($isCurrentLineBlank) {
  542. $blockLines[] = '';
  543. } else {
  544. $blockLines[] = $matches[1];
  545. }
  546. // newline only if not EOF
  547. if ($notEOF = $this->moveToNextLine()) {
  548. $isCurrentLineBlank = $this->isCurrentLineBlank();
  549. }
  550. }
  551. } elseif ($notEOF) {
  552. $blockLines[] = '';
  553. }
  554. if ($notEOF) {
  555. $blockLines[] = '';
  556. $this->moveToPreviousLine();
  557. } elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) {
  558. $blockLines[] = '';
  559. }
  560. // folded style
  561. if ('>' === $style) {
  562. $text = '';
  563. $previousLineIndented = false;
  564. $previousLineBlank = false;
  565. for ($i = 0, $blockLinesCount = count($blockLines); $i < $blockLinesCount; ++$i) {
  566. if ('' === $blockLines[$i]) {
  567. $text .= "\n";
  568. $previousLineIndented = false;
  569. $previousLineBlank = true;
  570. } elseif (' ' === $blockLines[$i][0]) {
  571. $text .= "\n".$blockLines[$i];
  572. $previousLineIndented = true;
  573. $previousLineBlank = false;
  574. } elseif ($previousLineIndented) {
  575. $text .= "\n".$blockLines[$i];
  576. $previousLineIndented = false;
  577. $previousLineBlank = false;
  578. } elseif ($previousLineBlank || 0 === $i) {
  579. $text .= $blockLines[$i];
  580. $previousLineIndented = false;
  581. $previousLineBlank = false;
  582. } else {
  583. $text .= ' '.$blockLines[$i];
  584. $previousLineIndented = false;
  585. $previousLineBlank = false;
  586. }
  587. }
  588. } else {
  589. $text = implode("\n", $blockLines);
  590. }
  591. // deal with trailing newlines
  592. if ('' === $chomping) {
  593. $text = preg_replace('/\n+$/', "\n", $text);
  594. } elseif ('-' === $chomping) {
  595. $text = preg_replace('/\n+$/', '', $text);
  596. }
  597. return $text;
  598. }
  599. /**
  600. * Returns true if the next line is indented.
  601. *
  602. * @return bool Returns true if the next line is indented, false otherwise
  603. */
  604. private function isNextLineIndented()
  605. {
  606. $currentIndentation = $this->getCurrentLineIndentation();
  607. $EOF = !$this->moveToNextLine();
  608. while (!$EOF && $this->isCurrentLineEmpty()) {
  609. $EOF = !$this->moveToNextLine();
  610. }
  611. if ($EOF) {
  612. return false;
  613. }
  614. $ret = false;
  615. if ($this->getCurrentLineIndentation() > $currentIndentation) {
  616. $ret = true;
  617. }
  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 = false;
  702. if (
  703. $this->getCurrentLineIndentation() == $currentIndentation
  704. &&
  705. $this->isStringUnIndentedCollectionItem()
  706. ) {
  707. $ret = true;
  708. }
  709. $this->moveToPreviousLine();
  710. return $ret;
  711. }
  712. /**
  713. * Returns true if the string is un-indented collection item.
  714. *
  715. * @return bool Returns true if the string is un-indented collection item, false otherwise
  716. */
  717. private function isStringUnIndentedCollectionItem()
  718. {
  719. return '-' === rtrim($this->currentLine) || 0 === strpos($this->currentLine, '- ');
  720. }
  721. /**
  722. * Tests whether or not the current line is the header of a block scalar.
  723. *
  724. * @return bool
  725. */
  726. private function isBlockScalarHeader()
  727. {
  728. return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
  729. }
  730. }