View source
<?php
namespace Symfony\Component\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
class Parser {
const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\\||>)(?P<modifiers>\\+|\\-|\\d+|\\+\\d+|\\-\\d+|\\d+\\+|\\d+\\-)?(?P<comments> +#.*)?';
const FOLDED_SCALAR_PATTERN = self::BLOCK_SCALAR_HEADER_PATTERN;
private $offset = 0;
private $totalNumberOfLines;
private $lines = array();
private $currentLineNb = -1;
private $currentLine = '';
private $refs = array();
private $skippedLineNumbers = array();
private $locallySkippedLineNumbers = array();
public function __construct($offset = 0, $totalNumberOfLines = null, array $skippedLineNumbers = array()) {
$this->offset = $offset;
$this->totalNumberOfLines = $totalNumberOfLines;
$this->skippedLineNumbers = $skippedLineNumbers;
}
public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false) {
if (false === preg_match('//u', $value)) {
throw new ParseException('The YAML value does not appear to be valid UTF-8.');
}
$this->refs = array();
$mbEncoding = null;
$e = null;
$data = null;
if (2 & (int) ini_get('mbstring.func_overload')) {
$mbEncoding = mb_internal_encoding();
mb_internal_encoding('UTF-8');
}
try {
$data = $this
->doParse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
} catch (\Exception $e) {
} catch (\Throwable $e) {
}
if (null !== $mbEncoding) {
mb_internal_encoding($mbEncoding);
}
$this->lines = array();
$this->currentLine = '';
$this->refs = array();
$this->skippedLineNumbers = array();
$this->locallySkippedLineNumbers = array();
if (null !== $e) {
throw $e;
}
return $data;
}
private function doParse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false) {
$this->currentLineNb = -1;
$this->currentLine = '';
$value = $this
->cleanup($value);
$this->lines = explode("\n", $value);
$this->locallySkippedLineNumbers = array();
if (null === $this->totalNumberOfLines) {
$this->totalNumberOfLines = count($this->lines);
}
$data = array();
$context = null;
$allowOverwrite = false;
while ($this
->moveToNextLine()) {
if ($this
->isCurrentLineEmpty()) {
continue;
}
if ("\t" === $this->currentLine[0]) {
throw new ParseException('A YAML file cannot contain tabs as indentation.', $this
->getRealCurrentLineNb() + 1, $this->currentLine);
}
$isRef = $mergeNode = false;
if (self::preg_match('#^\\-((?P<leadspaces>\\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) {
if ($context && 'mapping' == $context) {
throw new ParseException('You cannot define a sequence item when in a mapping', $this
->getRealCurrentLineNb() + 1, $this->currentLine);
}
$context = 'sequence';
if (isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
$isRef = $matches['ref'];
$values['value'] = $matches['value'];
}
if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
$data[] = $this
->parseBlock($this
->getRealCurrentLineNb() + 1, $this
->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport, $objectForMap);
}
else {
if (isset($values['leadspaces']) && self::preg_match('#^(?P<key>' . Inline::REGEX_QUOTED_STRING . '|[^ \'"\\{\\[].*?) *\\:(\\s+(?P<value>.+))?$#u', rtrim($values['value']), $matches)) {
$block = $values['value'];
if ($this
->isNextLineIndented()) {
$block .= "\n" . $this
->getNextEmbedBlock($this
->getCurrentLineIndentation() + strlen($values['leadspaces']) + 1);
}
$data[] = $this
->parseBlock($this
->getRealCurrentLineNb(), $block, $exceptionOnInvalidType, $objectSupport, $objectForMap);
}
else {
$data[] = $this
->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $context);
}
}
if ($isRef) {
$this->refs[$isRef] = end($data);
}
}
elseif (self::preg_match('#^(?P<key>' . Inline::REGEX_QUOTED_STRING . '|[^ \'"\\[\\{].*?) *\\:(\\s+(?P<value>.+))?$#u', rtrim($this->currentLine), $values) && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array(
'"',
"'",
)))) {
if ($context && 'sequence' == $context) {
throw new ParseException('You cannot define a mapping item when in a sequence', $this->currentLineNb + 1, $this->currentLine);
}
$context = 'mapping';
Inline::parse(null, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
try {
$key = Inline::parseScalar($values['key']);
} catch (ParseException $e) {
$e
->setParsedLine($this
->getRealCurrentLineNb() + 1);
$e
->setSnippet($this->currentLine);
throw $e;
}
if (is_float($key)) {
$key = (string) $key;
}
if ('<<' === $key && (!isset($values['value']) || !self::preg_match('#^&(?P<ref>[^ ]+)#u', $values['value'], $refMatches))) {
$mergeNode = true;
$allowOverwrite = true;
if (isset($values['value']) && 0 === strpos($values['value'], '*')) {
$refName = substr($values['value'], 1);
if (!array_key_exists($refName, $this->refs)) {
throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this
->getRealCurrentLineNb() + 1, $this->currentLine);
}
$refValue = $this->refs[$refName];
if (!is_array($refValue)) {
throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this
->getRealCurrentLineNb() + 1, $this->currentLine);
}
$data += $refValue;
}
else {
if (isset($values['value']) && '' !== $values['value']) {
$value = $values['value'];
}
else {
$value = $this
->getNextEmbedBlock();
}
$parsed = $this
->parseBlock($this
->getRealCurrentLineNb() + 1, $value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
if (!is_array($parsed)) {
throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this
->getRealCurrentLineNb() + 1, $this->currentLine);
}
if (isset($parsed[0])) {
foreach ($parsed as $parsedItem) {
if (!is_array($parsedItem)) {
throw new ParseException('Merge items must be arrays.', $this
->getRealCurrentLineNb() + 1, $parsedItem);
}
$data += $parsedItem;
}
}
else {
$data += $parsed;
}
}
}
elseif ('<<' !== $key && isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
$isRef = $matches['ref'];
$values['value'] = $matches['value'];
}
if ($mergeNode) {
}
elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#') || '<<' === $key) {
if (!$this
->isNextLineIndented() && !$this
->isNextLineUnIndentedCollection()) {
if ($allowOverwrite || !isset($data[$key])) {
$data[$key] = null;
}
}
else {
$value = $this
->parseBlock($this
->getRealCurrentLineNb() + 1, $this
->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport, $objectForMap);
if ('<<' === $key) {
$this->refs[$refMatches['ref']] = $value;
$data += $value;
}
elseif ($allowOverwrite || !isset($data[$key])) {
$data[$key] = $value;
}
}
}
else {
$value = $this
->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $context);
if ($allowOverwrite || !isset($data[$key])) {
$data[$key] = $value;
}
}
if ($isRef) {
$this->refs[$isRef] = $data[$key];
}
}
else {
if ('---' === $this->currentLine) {
throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine);
}
if (is_string($value) && $this->lines[0] === trim($value)) {
try {
$value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
} catch (ParseException $e) {
$e
->setParsedLine($this
->getRealCurrentLineNb() + 1);
$e
->setSnippet($this->currentLine);
throw $e;
}
return $value;
}
throw new ParseException('Unable to parse.', $this
->getRealCurrentLineNb() + 1, $this->currentLine);
}
}
if ($objectForMap && !is_object($data) && 'mapping' === $context) {
$object = new \stdClass();
foreach ($data as $key => $value) {
$object->{$key} = $value;
}
$data = $object;
}
return empty($data) ? null : $data;
}
private function parseBlock($offset, $yaml, $exceptionOnInvalidType, $objectSupport, $objectForMap) {
$skippedLineNumbers = $this->skippedLineNumbers;
foreach ($this->locallySkippedLineNumbers as $lineNumber) {
if ($lineNumber < $offset) {
continue;
}
$skippedLineNumbers[] = $lineNumber;
}
$parser = new self($offset, $this->totalNumberOfLines, $skippedLineNumbers);
$parser->refs =& $this->refs;
return $parser
->doParse($yaml, $exceptionOnInvalidType, $objectSupport, $objectForMap);
}
private function getRealCurrentLineNb() {
$realCurrentLineNumber = $this->currentLineNb + $this->offset;
foreach ($this->skippedLineNumbers as $skippedLineNumber) {
if ($skippedLineNumber > $realCurrentLineNumber) {
break;
}
++$realCurrentLineNumber;
}
return $realCurrentLineNumber;
}
private function getCurrentLineIndentation() {
return strlen($this->currentLine) - strlen(ltrim($this->currentLine, ' '));
}
private function getNextEmbedBlock($indentation = null, $inSequence = false) {
$oldLineIndentation = $this
->getCurrentLineIndentation();
$blockScalarIndentations = array();
if ($this
->isBlockScalarHeader()) {
$blockScalarIndentations[] = $this
->getCurrentLineIndentation();
}
if (!$this
->moveToNextLine()) {
return;
}
if (null === $indentation) {
$newIndent = $this
->getCurrentLineIndentation();
$unindentedEmbedBlock = $this
->isStringUnIndentedCollectionItem();
if (!$this
->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
throw new ParseException('Indentation problem.', $this
->getRealCurrentLineNb() + 1, $this->currentLine);
}
}
else {
$newIndent = $indentation;
}
$data = array();
if ($this
->getCurrentLineIndentation() >= $newIndent) {
$data[] = substr($this->currentLine, $newIndent);
}
else {
$this
->moveToPreviousLine();
return;
}
if ($inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) {
$this
->moveToPreviousLine();
return;
}
$isItUnindentedCollection = $this
->isStringUnIndentedCollectionItem();
if (empty($blockScalarIndentations) && $this
->isBlockScalarHeader()) {
$blockScalarIndentations[] = $this
->getCurrentLineIndentation();
}
$previousLineIndentation = $this
->getCurrentLineIndentation();
while ($this
->moveToNextLine()) {
$indent = $this
->getCurrentLineIndentation();
if (!empty($blockScalarIndentations) && $indent < $previousLineIndentation && '' !== trim($this->currentLine)) {
foreach ($blockScalarIndentations as $key => $blockScalarIndentation) {
if ($blockScalarIndentation >= $this
->getCurrentLineIndentation()) {
unset($blockScalarIndentations[$key]);
}
}
}
if (empty($blockScalarIndentations) && !$this
->isCurrentLineComment() && $this
->isBlockScalarHeader()) {
$blockScalarIndentations[] = $this
->getCurrentLineIndentation();
}
$previousLineIndentation = $indent;
if ($isItUnindentedCollection && !$this
->isCurrentLineEmpty() && !$this
->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
$this
->moveToPreviousLine();
break;
}
if ($this
->isCurrentLineBlank()) {
$data[] = substr($this->currentLine, $newIndent);
continue;
}
if (empty($blockScalarIndentations) && $this
->isCurrentLineComment()) {
$this->locallySkippedLineNumbers[] = $this
->getRealCurrentLineNb();
continue;
}
if ($indent >= $newIndent) {
$data[] = substr($this->currentLine, $newIndent);
}
elseif (0 == $indent) {
$this
->moveToPreviousLine();
break;
}
else {
throw new ParseException('Indentation problem.', $this
->getRealCurrentLineNb() + 1, $this->currentLine);
}
}
return implode("\n", $data);
}
private function moveToNextLine() {
if ($this->currentLineNb >= count($this->lines) - 1) {
return false;
}
$this->currentLine = $this->lines[++$this->currentLineNb];
return true;
}
private function moveToPreviousLine() {
if ($this->currentLineNb < 1) {
return false;
}
$this->currentLine = $this->lines[--$this->currentLineNb];
return true;
}
private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $context) {
if (0 === strpos($value, '*')) {
if (false !== ($pos = strpos($value, '#'))) {
$value = substr($value, 1, $pos - 2);
}
else {
$value = substr($value, 1);
}
if (!array_key_exists($value, $this->refs)) {
throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine);
}
return $this->refs[$value];
}
if (self::preg_match('/^' . self::BLOCK_SCALAR_HEADER_PATTERN . '$/', $value, $matches)) {
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
return $this
->parseBlockScalar($matches['separator'], preg_replace('#\\d+#', '', $modifiers), (int) abs($modifiers));
}
try {
$parsedValue = Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
if ('mapping' === $context && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
@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);
}
return $parsedValue;
} catch (ParseException $e) {
$e
->setParsedLine($this
->getRealCurrentLineNb() + 1);
$e
->setSnippet($this->currentLine);
throw $e;
}
}
private function parseBlockScalar($style, $chomping = '', $indentation = 0) {
$notEOF = $this
->moveToNextLine();
if (!$notEOF) {
return '';
}
$isCurrentLineBlank = $this
->isCurrentLineBlank();
$blockLines = array();
while ($notEOF && $isCurrentLineBlank) {
if ($notEOF = $this
->moveToNextLine()) {
$blockLines[] = '';
$isCurrentLineBlank = $this
->isCurrentLineBlank();
}
}
if (0 === $indentation) {
if (self::preg_match('/^ +/', $this->currentLine, $matches)) {
$indentation = strlen($matches[0]);
}
}
if ($indentation > 0) {
$pattern = sprintf('/^ {%d}(.*)$/', $indentation);
while ($notEOF && ($isCurrentLineBlank || self::preg_match($pattern, $this->currentLine, $matches))) {
if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) {
$blockLines[] = substr($this->currentLine, $indentation);
}
elseif ($isCurrentLineBlank) {
$blockLines[] = '';
}
else {
$blockLines[] = $matches[1];
}
if ($notEOF = $this
->moveToNextLine()) {
$isCurrentLineBlank = $this
->isCurrentLineBlank();
}
}
}
elseif ($notEOF) {
$blockLines[] = '';
}
if ($notEOF) {
$blockLines[] = '';
$this
->moveToPreviousLine();
}
elseif (!$notEOF && !$this
->isCurrentLineLastLineInDocument()) {
$blockLines[] = '';
}
if ('>' === $style) {
$text = '';
$previousLineIndented = false;
$previousLineBlank = false;
for ($i = 0, $blockLinesCount = count($blockLines); $i < $blockLinesCount; ++$i) {
if ('' === $blockLines[$i]) {
$text .= "\n";
$previousLineIndented = false;
$previousLineBlank = true;
}
elseif (' ' === $blockLines[$i][0]) {
$text .= "\n" . $blockLines[$i];
$previousLineIndented = true;
$previousLineBlank = false;
}
elseif ($previousLineIndented) {
$text .= "\n" . $blockLines[$i];
$previousLineIndented = false;
$previousLineBlank = false;
}
elseif ($previousLineBlank || 0 === $i) {
$text .= $blockLines[$i];
$previousLineIndented = false;
$previousLineBlank = false;
}
else {
$text .= ' ' . $blockLines[$i];
$previousLineIndented = false;
$previousLineBlank = false;
}
}
}
else {
$text = implode("\n", $blockLines);
}
if ('' === $chomping) {
$text = preg_replace('/\\n+$/', "\n", $text);
}
elseif ('-' === $chomping) {
$text = preg_replace('/\\n+$/', '', $text);
}
return $text;
}
private function isNextLineIndented() {
$currentIndentation = $this
->getCurrentLineIndentation();
$EOF = !$this
->moveToNextLine();
while (!$EOF && $this
->isCurrentLineEmpty()) {
$EOF = !$this
->moveToNextLine();
}
if ($EOF) {
return false;
}
$ret = $this
->getCurrentLineIndentation() > $currentIndentation;
$this
->moveToPreviousLine();
return $ret;
}
private function isCurrentLineEmpty() {
return $this
->isCurrentLineBlank() || $this
->isCurrentLineComment();
}
private function isCurrentLineBlank() {
return '' == trim($this->currentLine, ' ');
}
private function isCurrentLineComment() {
$ltrimmedLine = ltrim($this->currentLine, ' ');
return '' !== $ltrimmedLine && '#' === $ltrimmedLine[0];
}
private function isCurrentLineLastLineInDocument() {
return $this->offset + $this->currentLineNb >= $this->totalNumberOfLines - 1;
}
private function cleanup($value) {
$value = str_replace(array(
"\r\n",
"\r",
), "\n", $value);
$count = 0;
$value = preg_replace('#^\\%YAML[: ][\\d\\.]+.*\\n#u', '', $value, -1, $count);
$this->offset += $count;
$trimmedValue = preg_replace('#^(\\#.*?\\n)+#s', '', $value, -1, $count);
if (1 == $count) {
$this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
$value = $trimmedValue;
}
$trimmedValue = preg_replace('#^\\-\\-\\-.*?\\n#s', '', $value, -1, $count);
if (1 == $count) {
$this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
$value = $trimmedValue;
$value = preg_replace('#\\.\\.\\.\\s*$#', '', $value);
}
return $value;
}
private function isNextLineUnIndentedCollection() {
$currentIndentation = $this
->getCurrentLineIndentation();
$notEOF = $this
->moveToNextLine();
while ($notEOF && $this
->isCurrentLineEmpty()) {
$notEOF = $this
->moveToNextLine();
}
if (false === $notEOF) {
return false;
}
$ret = $this
->getCurrentLineIndentation() === $currentIndentation && $this
->isStringUnIndentedCollectionItem();
$this
->moveToPreviousLine();
return $ret;
}
private function isStringUnIndentedCollectionItem() {
return '-' === rtrim($this->currentLine) || 0 === strpos($this->currentLine, '- ');
}
private function isBlockScalarHeader() {
return (bool) self::preg_match('~' . self::BLOCK_SCALAR_HEADER_PATTERN . '$~', $this->currentLine);
}
public static function preg_match($pattern, $subject, &$matches = null, $flags = 0, $offset = 0) {
if (false === ($ret = preg_match($pattern, $subject, $matches, $flags, $offset))) {
switch (preg_last_error()) {
case PREG_INTERNAL_ERROR:
$error = 'Internal PCRE error.';
break;
case PREG_BACKTRACK_LIMIT_ERROR:
$error = 'pcre.backtrack_limit reached.';
break;
case PREG_RECURSION_LIMIT_ERROR:
$error = 'pcre.recursion_limit reached.';
break;
case PREG_BAD_UTF8_ERROR:
$error = 'Malformed UTF-8 data.';
break;
case PREG_BAD_UTF8_OFFSET_ERROR:
$error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
break;
default:
$error = 'Error.';
}
throw new ParseException($error);
}
return $ret;
}
}