Parser.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien@symfony.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Component\Yaml;
  10. use Symfony\Component\Yaml\Exception\ParseException;
  11. /**
  12. * Parser parses YAML strings to convert them to PHP arrays.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Parser
  17. {
  18. const FOLDED_SCALAR_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
  19. private $offset = 0;
  20. private $lines = array();
  21. private $currentLineNb = -1;
  22. private $currentLine = '';
  23. private $refs = array();
  24. /**
  25. * Constructor
  26. *
  27. * @param integer $offset The offset of YAML document (used for line numbers in error messages)
  28. */
  29. public function __construct($offset = 0)
  30. {
  31. $this->offset = $offset;
  32. }
  33. /**
  34. * Parses a YAML string to a PHP value.
  35. *
  36. * @param string $value A YAML string
  37. * @param Boolean $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
  38. * @param Boolean $objectSupport true if object support is enabled, false otherwise
  39. *
  40. * @return mixed A PHP value
  41. *
  42. * @throws ParseException If the YAML is not valid
  43. */
  44. public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false)
  45. {
  46. $this->currentLineNb = -1;
  47. $this->currentLine = '';
  48. $this->lines = explode("\n", $this->cleanup($value));
  49. if (function_exists('mb_detect_encoding') && false === mb_detect_encoding($value, 'UTF-8', true)) {
  50. throw new ParseException('The YAML value does not appear to be valid UTF-8.');
  51. }
  52. if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
  53. $mbEncoding = mb_internal_encoding();
  54. mb_internal_encoding('UTF-8');
  55. }
  56. $data = array();
  57. $context = null;
  58. while ($this->moveToNextLine()) {
  59. if ($this->isCurrentLineEmpty()) {
  60. continue;
  61. }
  62. // tab?
  63. if ("\t" === $this->currentLine[0]) {
  64. throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  65. }
  66. $isRef = $isInPlace = $isProcessed = false;
  67. if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
  68. if ($context && 'mapping' == $context) {
  69. throw new ParseException('You cannot define a sequence item when in a mapping');
  70. }
  71. $context = 'sequence';
  72. if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
  73. $isRef = $matches['ref'];
  74. $values['value'] = $matches['value'];
  75. }
  76. // array
  77. if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
  78. $c = $this->getRealCurrentLineNb() + 1;
  79. $parser = new Parser($c);
  80. $parser->refs =& $this->refs;
  81. $data[] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
  82. } else {
  83. if (isset($values['leadspaces'])
  84. && ' ' == $values['leadspaces']
  85. && preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $values['value'], $matches)
  86. ) {
  87. // this is a compact notation element, add to next block and parse
  88. $c = $this->getRealCurrentLineNb();
  89. $parser = new Parser($c);
  90. $parser->refs =& $this->refs;
  91. $block = $values['value'];
  92. if ($this->isNextLineIndented()) {
  93. $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + 2);
  94. }
  95. $data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport);
  96. } else {
  97. $data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport);
  98. }
  99. }
  100. } elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values) && false === strpos($values['key'],' #')) {
  101. if ($context && 'sequence' == $context) {
  102. throw new ParseException('You cannot define a mapping item when in a sequence');
  103. }
  104. $context = 'mapping';
  105. // force correct settings
  106. Inline::parse(null, $exceptionOnInvalidType, $objectSupport);
  107. try {
  108. $key = Inline::parseScalar($values['key']);
  109. } catch (ParseException $e) {
  110. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  111. $e->setSnippet($this->currentLine);
  112. throw $e;
  113. }
  114. if ('<<' === $key) {
  115. if (isset($values['value']) && 0 === strpos($values['value'], '*')) {
  116. $isInPlace = substr($values['value'], 1);
  117. if (!array_key_exists($isInPlace, $this->refs)) {
  118. throw new ParseException(sprintf('Reference "%s" does not exist.', $isInPlace), $this->getRealCurrentLineNb() + 1, $this->currentLine);
  119. }
  120. } else {
  121. if (isset($values['value']) && $values['value'] !== '') {
  122. $value = $values['value'];
  123. } else {
  124. $value = $this->getNextEmbedBlock();
  125. }
  126. $c = $this->getRealCurrentLineNb() + 1;
  127. $parser = new Parser($c);
  128. $parser->refs =& $this->refs;
  129. $parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport);
  130. $merged = array();
  131. if (!is_array($parsed)) {
  132. throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  133. } elseif (isset($parsed[0])) {
  134. // Numeric array, merge individual elements
  135. foreach (array_reverse($parsed) as $parsedItem) {
  136. if (!is_array($parsedItem)) {
  137. throw new ParseException('Merge items must be arrays.', $this->getRealCurrentLineNb() + 1, $parsedItem);
  138. }
  139. $merged = array_merge($parsedItem, $merged);
  140. }
  141. } else {
  142. // Associative array, merge
  143. $merged = array_merge($merged, $parsed);
  144. }
  145. $isProcessed = $merged;
  146. }
  147. } elseif (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
  148. $isRef = $matches['ref'];
  149. $values['value'] = $matches['value'];
  150. }
  151. if ($isProcessed) {
  152. // Merge keys
  153. $data = $isProcessed;
  154. // hash
  155. } elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
  156. // if next line is less indented or equal, then it means that the current value is null
  157. if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
  158. $data[$key] = null;
  159. } else {
  160. $c = $this->getRealCurrentLineNb() + 1;
  161. $parser = new Parser($c);
  162. $parser->refs =& $this->refs;
  163. $data[$key] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
  164. }
  165. } else {
  166. if ($isInPlace) {
  167. $data = $this->refs[$isInPlace];
  168. } else {
  169. $data[$key] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport);
  170. }
  171. }
  172. } else {
  173. // 1-liner optionally followed by newline
  174. $lineCount = count($this->lines);
  175. if (1 === $lineCount || (2 === $lineCount && empty($this->lines[1]))) {
  176. try {
  177. $value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport);
  178. } catch (ParseException $e) {
  179. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  180. $e->setSnippet($this->currentLine);
  181. throw $e;
  182. }
  183. if (is_array($value)) {
  184. $first = reset($value);
  185. if (is_string($first) && 0 === strpos($first, '*')) {
  186. $data = array();
  187. foreach ($value as $alias) {
  188. $data[] = $this->refs[substr($alias, 1)];
  189. }
  190. $value = $data;
  191. }
  192. }
  193. if (isset($mbEncoding)) {
  194. mb_internal_encoding($mbEncoding);
  195. }
  196. return $value;
  197. }
  198. switch (preg_last_error()) {
  199. case PREG_INTERNAL_ERROR:
  200. $error = 'Internal PCRE error.';
  201. break;
  202. case PREG_BACKTRACK_LIMIT_ERROR:
  203. $error = 'pcre.backtrack_limit reached.';
  204. break;
  205. case PREG_RECURSION_LIMIT_ERROR:
  206. $error = 'pcre.recursion_limit reached.';
  207. break;
  208. case PREG_BAD_UTF8_ERROR:
  209. $error = 'Malformed UTF-8 data.';
  210. break;
  211. case PREG_BAD_UTF8_OFFSET_ERROR:
  212. $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
  213. break;
  214. default:
  215. $error = 'Unable to parse.';
  216. }
  217. throw new ParseException($error, $this->getRealCurrentLineNb() + 1, $this->currentLine);
  218. }
  219. if ($isRef) {
  220. $this->refs[$isRef] = end($data);
  221. }
  222. }
  223. if (isset($mbEncoding)) {
  224. mb_internal_encoding($mbEncoding);
  225. }
  226. return empty($data) ? null : $data;
  227. }
  228. /**
  229. * Returns the current line number (takes the offset into account).
  230. *
  231. * @return integer The current line number
  232. */
  233. private function getRealCurrentLineNb()
  234. {
  235. return $this->currentLineNb + $this->offset;
  236. }
  237. /**
  238. * Returns the current line indentation.
  239. *
  240. * @return integer The current line indentation
  241. */
  242. private function getCurrentLineIndentation()
  243. {
  244. return strlen($this->currentLine) - strlen(ltrim($this->currentLine, ' '));
  245. }
  246. /**
  247. * Returns the next embed block of YAML.
  248. *
  249. * @param integer $indentation The indent level at which the block is to be read, or null for default
  250. *
  251. * @return string A YAML string
  252. *
  253. * @throws ParseException When indentation problem are detected
  254. */
  255. private function getNextEmbedBlock($indentation = null)
  256. {
  257. $this->moveToNextLine();
  258. if (null === $indentation) {
  259. $newIndent = $this->getCurrentLineIndentation();
  260. $unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem($this->currentLine);
  261. if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
  262. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  263. }
  264. } else {
  265. $newIndent = $indentation;
  266. }
  267. $data = array(substr($this->currentLine, $newIndent));
  268. $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);
  269. // Comments must not be removed inside a string block (ie. after a line ending with "|")
  270. $removeCommentsPattern = '~'.self::FOLDED_SCALAR_PATTERN.'$~';
  271. $removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
  272. while ($this->moveToNextLine()) {
  273. if ($this->getCurrentLineIndentation() === $newIndent) {
  274. $removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
  275. }
  276. if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine)) {
  277. $this->moveToPreviousLine();
  278. break;
  279. }
  280. if ($removeComments && $this->isCurrentLineEmpty() || $this->isCurrentLineBlank()) {
  281. if ($this->isCurrentLineBlank()) {
  282. $data[] = substr($this->currentLine, $newIndent);
  283. }
  284. continue;
  285. }
  286. $indent = $this->getCurrentLineIndentation();
  287. if (preg_match('#^(?P<text> *)$#', $this->currentLine, $match)) {
  288. // empty line
  289. $data[] = $match['text'];
  290. } elseif ($indent >= $newIndent) {
  291. $data[] = substr($this->currentLine, $newIndent);
  292. } elseif (0 == $indent) {
  293. $this->moveToPreviousLine();
  294. break;
  295. } else {
  296. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  297. }
  298. }
  299. return implode("\n", $data);
  300. }
  301. /**
  302. * Moves the parser to the next line.
  303. *
  304. * @return Boolean
  305. */
  306. private function moveToNextLine()
  307. {
  308. if ($this->currentLineNb >= count($this->lines) - 1) {
  309. return false;
  310. }
  311. $this->currentLine = $this->lines[++$this->currentLineNb];
  312. return true;
  313. }
  314. /**
  315. * Moves the parser to the previous line.
  316. */
  317. private function moveToPreviousLine()
  318. {
  319. $this->currentLine = $this->lines[--$this->currentLineNb];
  320. }
  321. /**
  322. * Parses a YAML value.
  323. *
  324. * @param string $value A YAML value
  325. * @param Boolean $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
  326. * @param Boolean $objectSupport True if object support is enabled, false otherwise
  327. *
  328. * @return mixed A PHP value
  329. *
  330. * @throws ParseException When reference does not exist
  331. */
  332. private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
  333. {
  334. if (0 === strpos($value, '*')) {
  335. if (false !== $pos = strpos($value, '#')) {
  336. $value = substr($value, 1, $pos - 2);
  337. } else {
  338. $value = substr($value, 1);
  339. }
  340. if (!array_key_exists($value, $this->refs)) {
  341. throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLine);
  342. }
  343. return $this->refs[$value];
  344. }
  345. if (preg_match('/^'.self::FOLDED_SCALAR_PATTERN.'$/', $value, $matches)) {
  346. $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
  347. return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), intval(abs($modifiers)));
  348. }
  349. try {
  350. return Inline::parse($value, $exceptionOnInvalidType, $objectSupport);
  351. } catch (ParseException $e) {
  352. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  353. $e->setSnippet($this->currentLine);
  354. throw $e;
  355. }
  356. }
  357. /**
  358. * Parses a folded scalar.
  359. *
  360. * @param string $separator The separator that was used to begin this folded scalar (| or >)
  361. * @param string $indicator The indicator that was used to begin this folded scalar (+ or -)
  362. * @param integer $indentation The indentation that was used to begin this folded scalar
  363. *
  364. * @return string The text value
  365. */
  366. private function parseFoldedScalar($separator, $indicator = '', $indentation = 0)
  367. {
  368. $notEOF = $this->moveToNextLine();
  369. if (!$notEOF) {
  370. return '';
  371. }
  372. $isCurrentLineBlank = $this->isCurrentLineBlank();
  373. $text = '';
  374. // leading blank lines are consumed before determining indentation
  375. while ($notEOF && $isCurrentLineBlank) {
  376. // newline only if not EOF
  377. if ($notEOF = $this->moveToNextLine()) {
  378. $text .= "\n";
  379. $isCurrentLineBlank = $this->isCurrentLineBlank();
  380. }
  381. }
  382. // determine indentation if not specified
  383. if (0 === $indentation) {
  384. if (preg_match('/^ +/', $this->currentLine, $matches)) {
  385. $indentation = strlen($matches[0]);
  386. }
  387. }
  388. if ($indentation > 0) {
  389. $pattern = sprintf('/^ {%d}(.*)$/', $indentation);
  390. while (
  391. $notEOF && (
  392. $isCurrentLineBlank ||
  393. preg_match($pattern, $this->currentLine, $matches)
  394. )
  395. ) {
  396. if ($isCurrentLineBlank) {
  397. $text .= substr($this->currentLine, $indentation);
  398. } else {
  399. $text .= $matches[1];
  400. }
  401. // newline only if not EOF
  402. if ($notEOF = $this->moveToNextLine()) {
  403. $text .= "\n";
  404. $isCurrentLineBlank = $this->isCurrentLineBlank();
  405. }
  406. }
  407. } elseif ($notEOF) {
  408. $text .= "\n";
  409. }
  410. if ($notEOF) {
  411. $this->moveToPreviousLine();
  412. }
  413. // replace all non-trailing single newlines with spaces in folded blocks
  414. if ('>' === $separator) {
  415. preg_match('/(\n*)$/', $text, $matches);
  416. $text = preg_replace('/(?<!\n)\n(?!\n)/', ' ', rtrim($text, "\n"));
  417. $text .= $matches[1];
  418. }
  419. // deal with trailing newlines as indicated
  420. if ('' === $indicator) {
  421. $text = preg_replace('/\n+$/s', "\n", $text);
  422. } elseif ('-' === $indicator) {
  423. $text = preg_replace('/\n+$/s', '', $text);
  424. }
  425. return $text;
  426. }
  427. /**
  428. * Returns true if the next line is indented.
  429. *
  430. * @return Boolean Returns true if the next line is indented, false otherwise
  431. */
  432. private function isNextLineIndented()
  433. {
  434. $currentIndentation = $this->getCurrentLineIndentation();
  435. $EOF = !$this->moveToNextLine();
  436. while (!$EOF && $this->isCurrentLineEmpty()) {
  437. $EOF = !$this->moveToNextLine();
  438. }
  439. if ($EOF) {
  440. return false;
  441. }
  442. $ret = false;
  443. if ($this->getCurrentLineIndentation() > $currentIndentation) {
  444. $ret = true;
  445. }
  446. $this->moveToPreviousLine();
  447. return $ret;
  448. }
  449. /**
  450. * Returns true if the current line is blank or if it is a comment line.
  451. *
  452. * @return Boolean Returns true if the current line is empty or if it is a comment line, false otherwise
  453. */
  454. private function isCurrentLineEmpty()
  455. {
  456. return $this->isCurrentLineBlank() || $this->isCurrentLineComment();
  457. }
  458. /**
  459. * Returns true if the current line is blank.
  460. *
  461. * @return Boolean Returns true if the current line is blank, false otherwise
  462. */
  463. private function isCurrentLineBlank()
  464. {
  465. return '' == trim($this->currentLine, ' ');
  466. }
  467. /**
  468. * Returns true if the current line is a comment line.
  469. *
  470. * @return Boolean Returns true if the current line is a comment line, false otherwise
  471. */
  472. private function isCurrentLineComment()
  473. {
  474. //checking explicitly the first char of the trim is faster than loops or strpos
  475. $ltrimmedLine = ltrim($this->currentLine, ' ');
  476. return $ltrimmedLine[0] === '#';
  477. }
  478. /**
  479. * Cleanups a YAML string to be parsed.
  480. *
  481. * @param string $value The input YAML string
  482. *
  483. * @return string A cleaned up YAML string
  484. */
  485. private function cleanup($value)
  486. {
  487. $value = str_replace(array("\r\n", "\r"), "\n", $value);
  488. // strip YAML header
  489. $count = 0;
  490. $value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#su', '', $value, -1, $count);
  491. $this->offset += $count;
  492. // remove leading comments
  493. $trimmedValue = preg_replace('#^(\#.*?\n)+#s', '', $value, -1, $count);
  494. if ($count == 1) {
  495. // items have been removed, update the offset
  496. $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
  497. $value = $trimmedValue;
  498. }
  499. // remove start of the document marker (---)
  500. $trimmedValue = preg_replace('#^\-\-\-.*?\n#s', '', $value, -1, $count);
  501. if ($count == 1) {
  502. // items have been removed, update the offset
  503. $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
  504. $value = $trimmedValue;
  505. // remove end of the document marker (...)
  506. $value = preg_replace('#\.\.\.\s*$#s', '', $value);
  507. }
  508. return $value;
  509. }
  510. /**
  511. * Returns true if the next line starts unindented collection
  512. *
  513. * @return Boolean Returns true if the next line starts unindented collection, false otherwise
  514. */
  515. private function isNextLineUnIndentedCollection()
  516. {
  517. $currentIndentation = $this->getCurrentLineIndentation();
  518. $notEOF = $this->moveToNextLine();
  519. while ($notEOF && $this->isCurrentLineEmpty()) {
  520. $notEOF = $this->moveToNextLine();
  521. }
  522. if (false === $notEOF) {
  523. return false;
  524. }
  525. $ret = false;
  526. if (
  527. $this->getCurrentLineIndentation() == $currentIndentation
  528. &&
  529. $this->isStringUnIndentedCollectionItem($this->currentLine)
  530. ) {
  531. $ret = true;
  532. }
  533. $this->moveToPreviousLine();
  534. return $ret;
  535. }
  536. /**
  537. * Returns true if the string is un-indented collection item
  538. *
  539. * @return Boolean Returns true if the string is un-indented collection item, false otherwise
  540. */
  541. private function isStringUnIndentedCollectionItem()
  542. {
  543. return (0 === strpos($this->currentLine, '- '));
  544. }
  545. }