Parser.php 21 KB

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