Parser.php 32 KB

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