Parser.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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. use Symfony\Component\Yaml\Tag\TaggedValue;
  13. /**
  14. * Parser parses YAML strings to convert them to PHP arrays.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. *
  18. * @final
  19. */
  20. class Parser
  21. {
  22. public const TAG_PATTERN = '(?P<tag>![\w!.\/:-]+)';
  23. public const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
  24. public const REFERENCE_PATTERN = '#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u';
  25. private $filename;
  26. private $offset = 0;
  27. private $totalNumberOfLines;
  28. private $lines = [];
  29. private $currentLineNb = -1;
  30. private $currentLine = '';
  31. private $refs = [];
  32. private $skippedLineNumbers = [];
  33. private $locallySkippedLineNumbers = [];
  34. private $refsBeingParsed = [];
  35. /**
  36. * Parses a YAML file into a PHP value.
  37. *
  38. * @param string $filename The path to the YAML file to be parsed
  39. * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  40. *
  41. * @return mixed The YAML converted to a PHP value
  42. *
  43. * @throws ParseException If the file could not be read or the YAML is not valid
  44. */
  45. public function parseFile(string $filename, int $flags = 0)
  46. {
  47. if (!is_file($filename)) {
  48. throw new ParseException(sprintf('File "%s" does not exist.', $filename));
  49. }
  50. if (!is_readable($filename)) {
  51. throw new ParseException(sprintf('File "%s" cannot be read.', $filename));
  52. }
  53. $this->filename = $filename;
  54. try {
  55. return $this->parse(file_get_contents($filename), $flags);
  56. } finally {
  57. $this->filename = null;
  58. }
  59. }
  60. /**
  61. * Parses a YAML string to a PHP value.
  62. *
  63. * @param string $value A YAML string
  64. * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  65. *
  66. * @return mixed A PHP value
  67. *
  68. * @throws ParseException If the YAML is not valid
  69. */
  70. public function parse(string $value, int $flags = 0)
  71. {
  72. if (false === preg_match('//u', $value)) {
  73. throw new ParseException('The YAML value does not appear to be valid UTF-8.', -1, null, $this->filename);
  74. }
  75. $this->refs = [];
  76. $mbEncoding = null;
  77. if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
  78. $mbEncoding = mb_internal_encoding();
  79. mb_internal_encoding('UTF-8');
  80. }
  81. try {
  82. $data = $this->doParse($value, $flags);
  83. } finally {
  84. if (null !== $mbEncoding) {
  85. mb_internal_encoding($mbEncoding);
  86. }
  87. $this->lines = [];
  88. $this->currentLine = '';
  89. $this->refs = [];
  90. $this->skippedLineNumbers = [];
  91. $this->locallySkippedLineNumbers = [];
  92. $this->totalNumberOfLines = null;
  93. }
  94. return $data;
  95. }
  96. private function doParse(string $value, int $flags)
  97. {
  98. $this->currentLineNb = -1;
  99. $this->currentLine = '';
  100. $value = $this->cleanup($value);
  101. $this->lines = explode("\n", $value);
  102. $this->locallySkippedLineNumbers = [];
  103. if (null === $this->totalNumberOfLines) {
  104. $this->totalNumberOfLines = \count($this->lines);
  105. }
  106. if (!$this->moveToNextLine()) {
  107. return null;
  108. }
  109. $data = [];
  110. $context = null;
  111. $allowOverwrite = false;
  112. while ($this->isCurrentLineEmpty()) {
  113. if (!$this->moveToNextLine()) {
  114. return null;
  115. }
  116. }
  117. // Resolves the tag and returns if end of the document
  118. if (null !== ($tag = $this->getLineTag($this->currentLine, $flags, false)) && !$this->moveToNextLine()) {
  119. return new TaggedValue($tag, '');
  120. }
  121. do {
  122. if ($this->isCurrentLineEmpty()) {
  123. continue;
  124. }
  125. // tab?
  126. if ("\t" === $this->currentLine[0]) {
  127. throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  128. }
  129. Inline::initialize($flags, $this->getRealCurrentLineNb(), $this->filename);
  130. $isRef = $mergeNode = false;
  131. if ('-' === $this->currentLine[0] && self::preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) {
  132. if ($context && 'mapping' == $context) {
  133. throw new ParseException('You cannot define a sequence item when in a mapping.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  134. }
  135. $context = 'sequence';
  136. if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) {
  137. $isRef = $matches['ref'];
  138. $this->refsBeingParsed[] = $isRef;
  139. $values['value'] = $matches['value'];
  140. }
  141. if (isset($values['value'][1]) && '?' === $values['value'][0] && ' ' === $values['value'][1]) {
  142. throw new ParseException('Complex mappings are not supported.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  143. }
  144. // array
  145. if (isset($values['value']) && 0 === strpos(ltrim($values['value'], ' '), '-')) {
  146. // Inline first child
  147. $currentLineNumber = $this->getRealCurrentLineNb();
  148. $sequenceIndentation = \strlen($values['leadspaces']) + 1;
  149. $sequenceYaml = substr($this->currentLine, $sequenceIndentation);
  150. $sequenceYaml .= "\n".$this->getNextEmbedBlock($sequenceIndentation, true);
  151. $data[] = $this->parseBlock($currentLineNumber, rtrim($sequenceYaml), $flags);
  152. } elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
  153. $data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true) ?? '', $flags);
  154. } elseif (null !== $subTag = $this->getLineTag(ltrim($values['value'], ' '), $flags)) {
  155. $data[] = new TaggedValue(
  156. $subTag,
  157. $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags)
  158. );
  159. } else {
  160. if (
  161. isset($values['leadspaces'])
  162. && (
  163. '!' === $values['value'][0]
  164. || self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->trimTag($values['value']), $matches)
  165. )
  166. ) {
  167. // this is a compact notation element, add to next block and parse
  168. $block = $values['value'];
  169. if ($this->isNextLineIndented()) {
  170. $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + \strlen($values['leadspaces']) + 1);
  171. }
  172. $data[] = $this->parseBlock($this->getRealCurrentLineNb(), $block, $flags);
  173. } else {
  174. $data[] = $this->parseValue($values['value'], $flags, $context);
  175. }
  176. }
  177. if ($isRef) {
  178. $this->refs[$isRef] = end($data);
  179. array_pop($this->refsBeingParsed);
  180. }
  181. } elseif (
  182. self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(( |\t)++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
  183. && (false === strpos($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"]))
  184. ) {
  185. if ($context && 'sequence' == $context) {
  186. throw new ParseException('You cannot define a mapping item when in a sequence.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
  187. }
  188. $context = 'mapping';
  189. try {
  190. $key = Inline::parseScalar($values['key']);
  191. } catch (ParseException $e) {
  192. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  193. $e->setSnippet($this->currentLine);
  194. throw $e;
  195. }
  196. if (!\is_string($key) && !\is_int($key)) {
  197. throw new ParseException((is_numeric($key) ? 'Numeric' : 'Non-string').' keys are not supported. Quote your evaluable mapping keys instead.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  198. }
  199. // Convert float keys to strings, to avoid being converted to integers by PHP
  200. if (\is_float($key)) {
  201. $key = (string) $key;
  202. }
  203. if ('<<' === $key && (!isset($values['value']) || '&' !== $values['value'][0] || !self::preg_match('#^&(?P<ref>[^ ]+)#u', $values['value'], $refMatches))) {
  204. $mergeNode = true;
  205. $allowOverwrite = true;
  206. if (isset($values['value'][0]) && '*' === $values['value'][0]) {
  207. $refName = substr(rtrim($values['value']), 1);
  208. if (!\array_key_exists($refName, $this->refs)) {
  209. if (false !== $pos = array_search($refName, $this->refsBeingParsed, true)) {
  210. throw new ParseException(sprintf('Circular reference [%s] detected for reference "%s".', implode(', ', array_merge(\array_slice($this->refsBeingParsed, $pos), [$refName])), $refName), $this->currentLineNb + 1, $this->currentLine, $this->filename);
  211. }
  212. throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  213. }
  214. $refValue = $this->refs[$refName];
  215. if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $refValue instanceof \stdClass) {
  216. $refValue = (array) $refValue;
  217. }
  218. if (!\is_array($refValue)) {
  219. throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  220. }
  221. $data += $refValue; // array union
  222. } else {
  223. if (isset($values['value']) && '' !== $values['value']) {
  224. $value = $values['value'];
  225. } else {
  226. $value = $this->getNextEmbedBlock();
  227. }
  228. $parsed = $this->parseBlock($this->getRealCurrentLineNb() + 1, $value, $flags);
  229. if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $parsed instanceof \stdClass) {
  230. $parsed = (array) $parsed;
  231. }
  232. if (!\is_array($parsed)) {
  233. throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  234. }
  235. if (isset($parsed[0])) {
  236. // If the value associated with the merge key is a sequence, then this sequence is expected to contain mapping nodes
  237. // and each of these nodes is merged in turn according to its order in the sequence. Keys in mapping nodes earlier
  238. // in the sequence override keys specified in later mapping nodes.
  239. foreach ($parsed as $parsedItem) {
  240. if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $parsedItem instanceof \stdClass) {
  241. $parsedItem = (array) $parsedItem;
  242. }
  243. if (!\is_array($parsedItem)) {
  244. throw new ParseException('Merge items must be arrays.', $this->getRealCurrentLineNb() + 1, $parsedItem, $this->filename);
  245. }
  246. $data += $parsedItem; // array union
  247. }
  248. } else {
  249. // If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the
  250. // current mapping, unless the key already exists in it.
  251. $data += $parsed; // array union
  252. }
  253. }
  254. } elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) {
  255. $isRef = $matches['ref'];
  256. $this->refsBeingParsed[] = $isRef;
  257. $values['value'] = $matches['value'];
  258. }
  259. $subTag = null;
  260. if ($mergeNode) {
  261. // Merge keys
  262. } elseif (!isset($values['value']) || '' === $values['value'] || 0 === strpos($values['value'], '#') || (null !== $subTag = $this->getLineTag($values['value'], $flags)) || '<<' === $key) {
  263. // hash
  264. // if next line is less indented or equal, then it means that the current value is null
  265. if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
  266. // Spec: Keys MUST be unique; first one wins.
  267. // But overwriting is allowed when a merge node is used in current block.
  268. if ($allowOverwrite || !isset($data[$key])) {
  269. if (null !== $subTag) {
  270. $data[$key] = new TaggedValue($subTag, '');
  271. } else {
  272. $data[$key] = null;
  273. }
  274. } else {
  275. throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine);
  276. }
  277. } else {
  278. // remember the parsed line number here in case we need it to provide some contexts in error messages below
  279. $realCurrentLineNbKey = $this->getRealCurrentLineNb();
  280. $value = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(), $flags);
  281. if ('<<' === $key) {
  282. $this->refs[$refMatches['ref']] = $value;
  283. if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $value instanceof \stdClass) {
  284. $value = (array) $value;
  285. }
  286. $data += $value;
  287. } elseif ($allowOverwrite || !isset($data[$key])) {
  288. // Spec: Keys MUST be unique; first one wins.
  289. // But overwriting is allowed when a merge node is used in current block.
  290. if (null !== $subTag) {
  291. $data[$key] = new TaggedValue($subTag, $value);
  292. } else {
  293. $data[$key] = $value;
  294. }
  295. } else {
  296. throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $realCurrentLineNbKey + 1, $this->currentLine);
  297. }
  298. }
  299. } else {
  300. $value = $this->parseValue(rtrim($values['value']), $flags, $context);
  301. // Spec: Keys MUST be unique; first one wins.
  302. // But overwriting is allowed when a merge node is used in current block.
  303. if ($allowOverwrite || !isset($data[$key])) {
  304. $data[$key] = $value;
  305. } else {
  306. throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine);
  307. }
  308. }
  309. if ($isRef) {
  310. $this->refs[$isRef] = $data[$key];
  311. array_pop($this->refsBeingParsed);
  312. }
  313. } elseif ('"' === $this->currentLine[0] || "'" === $this->currentLine[0]) {
  314. if (null !== $context) {
  315. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  316. }
  317. try {
  318. return Inline::parse($this->lexInlineQuotedString(), $flags, $this->refs);
  319. } catch (ParseException $e) {
  320. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  321. $e->setSnippet($this->currentLine);
  322. throw $e;
  323. }
  324. } elseif ('{' === $this->currentLine[0]) {
  325. if (null !== $context) {
  326. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  327. }
  328. try {
  329. $parsedMapping = Inline::parse($this->lexInlineMapping(), $flags, $this->refs);
  330. while ($this->moveToNextLine()) {
  331. if (!$this->isCurrentLineEmpty()) {
  332. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  333. }
  334. }
  335. return $parsedMapping;
  336. } catch (ParseException $e) {
  337. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  338. $e->setSnippet($this->currentLine);
  339. throw $e;
  340. }
  341. } elseif ('[' === $this->currentLine[0]) {
  342. if (null !== $context) {
  343. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  344. }
  345. try {
  346. $parsedSequence = Inline::parse($this->lexInlineSequence(), $flags, $this->refs);
  347. while ($this->moveToNextLine()) {
  348. if (!$this->isCurrentLineEmpty()) {
  349. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  350. }
  351. }
  352. return $parsedSequence;
  353. } catch (ParseException $e) {
  354. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  355. $e->setSnippet($this->currentLine);
  356. throw $e;
  357. }
  358. } else {
  359. // multiple documents are not supported
  360. if ('---' === $this->currentLine) {
  361. throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
  362. }
  363. if ($deprecatedUsage = (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1])) {
  364. throw new ParseException('Complex mappings are not supported.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  365. }
  366. // 1-liner optionally followed by newline(s)
  367. if (\is_string($value) && $this->lines[0] === trim($value)) {
  368. try {
  369. $value = Inline::parse($this->lines[0], $flags, $this->refs);
  370. } catch (ParseException $e) {
  371. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  372. $e->setSnippet($this->currentLine);
  373. throw $e;
  374. }
  375. return $value;
  376. }
  377. // try to parse the value as a multi-line string as a last resort
  378. if (0 === $this->currentLineNb) {
  379. $previousLineWasNewline = false;
  380. $previousLineWasTerminatedWithBackslash = false;
  381. $value = '';
  382. foreach ($this->lines as $line) {
  383. if ('' !== ltrim($line) && '#' === ltrim($line)[0]) {
  384. continue;
  385. }
  386. // If the indentation is not consistent at offset 0, it is to be considered as a ParseError
  387. if (0 === $this->offset && !$deprecatedUsage && isset($line[0]) && ' ' === $line[0]) {
  388. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  389. }
  390. if (false !== strpos($line, ': ')) {
  391. @trigger_error('Support for mapping keys in multi-line blocks is deprecated since Symfony 4.3 and will throw a ParseException in 5.0.', \E_USER_DEPRECATED);
  392. }
  393. if ('' === trim($line)) {
  394. $value .= "\n";
  395. } elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
  396. $value .= ' ';
  397. }
  398. if ('' !== trim($line) && '\\' === substr($line, -1)) {
  399. $value .= ltrim(substr($line, 0, -1));
  400. } elseif ('' !== trim($line)) {
  401. $value .= trim($line);
  402. }
  403. if ('' === trim($line)) {
  404. $previousLineWasNewline = true;
  405. $previousLineWasTerminatedWithBackslash = false;
  406. } elseif ('\\' === substr($line, -1)) {
  407. $previousLineWasNewline = false;
  408. $previousLineWasTerminatedWithBackslash = true;
  409. } else {
  410. $previousLineWasNewline = false;
  411. $previousLineWasTerminatedWithBackslash = false;
  412. }
  413. }
  414. try {
  415. return Inline::parse(trim($value));
  416. } catch (ParseException $e) {
  417. // fall-through to the ParseException thrown below
  418. }
  419. }
  420. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  421. }
  422. } while ($this->moveToNextLine());
  423. if (null !== $tag) {
  424. $data = new TaggedValue($tag, $data);
  425. }
  426. if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && !\is_object($data) && 'mapping' === $context) {
  427. $object = new \stdClass();
  428. foreach ($data as $key => $value) {
  429. $object->$key = $value;
  430. }
  431. $data = $object;
  432. }
  433. return empty($data) ? null : $data;
  434. }
  435. private function parseBlock(int $offset, string $yaml, int $flags)
  436. {
  437. $skippedLineNumbers = $this->skippedLineNumbers;
  438. foreach ($this->locallySkippedLineNumbers as $lineNumber) {
  439. if ($lineNumber < $offset) {
  440. continue;
  441. }
  442. $skippedLineNumbers[] = $lineNumber;
  443. }
  444. $parser = new self();
  445. $parser->offset = $offset;
  446. $parser->totalNumberOfLines = $this->totalNumberOfLines;
  447. $parser->skippedLineNumbers = $skippedLineNumbers;
  448. $parser->refs = &$this->refs;
  449. $parser->refsBeingParsed = $this->refsBeingParsed;
  450. return $parser->doParse($yaml, $flags);
  451. }
  452. /**
  453. * Returns the current line number (takes the offset into account).
  454. *
  455. * @internal
  456. *
  457. * @return int The current line number
  458. */
  459. public function getRealCurrentLineNb(): int
  460. {
  461. $realCurrentLineNumber = $this->currentLineNb + $this->offset;
  462. foreach ($this->skippedLineNumbers as $skippedLineNumber) {
  463. if ($skippedLineNumber > $realCurrentLineNumber) {
  464. break;
  465. }
  466. ++$realCurrentLineNumber;
  467. }
  468. return $realCurrentLineNumber;
  469. }
  470. /**
  471. * Returns the current line indentation.
  472. *
  473. * @return int The current line indentation
  474. */
  475. private function getCurrentLineIndentation(): int
  476. {
  477. return \strlen($this->currentLine) - \strlen(ltrim($this->currentLine, ' '));
  478. }
  479. /**
  480. * Returns the next embed block of YAML.
  481. *
  482. * @param int|null $indentation The indent level at which the block is to be read, or null for default
  483. * @param bool $inSequence True if the enclosing data structure is a sequence
  484. *
  485. * @return string A YAML string
  486. *
  487. * @throws ParseException When indentation problem are detected
  488. */
  489. private function getNextEmbedBlock(int $indentation = null, bool $inSequence = false): string
  490. {
  491. $oldLineIndentation = $this->getCurrentLineIndentation();
  492. if (!$this->moveToNextLine()) {
  493. return '';
  494. }
  495. if (null === $indentation) {
  496. $newIndent = null;
  497. $movements = 0;
  498. do {
  499. $EOF = false;
  500. // empty and comment-like lines do not influence the indentation depth
  501. if ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) {
  502. $EOF = !$this->moveToNextLine();
  503. if (!$EOF) {
  504. ++$movements;
  505. }
  506. } else {
  507. $newIndent = $this->getCurrentLineIndentation();
  508. }
  509. } while (!$EOF && null === $newIndent);
  510. for ($i = 0; $i < $movements; ++$i) {
  511. $this->moveToPreviousLine();
  512. }
  513. $unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem();
  514. if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
  515. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  516. }
  517. } else {
  518. $newIndent = $indentation;
  519. }
  520. $data = [];
  521. if ($this->getCurrentLineIndentation() >= $newIndent) {
  522. $data[] = substr($this->currentLine, $newIndent ?? 0);
  523. } elseif ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) {
  524. $data[] = $this->currentLine;
  525. } else {
  526. $this->moveToPreviousLine();
  527. return '';
  528. }
  529. if ($inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) {
  530. // the previous line contained a dash but no item content, this line is a sequence item with the same indentation
  531. // and therefore no nested list or mapping
  532. $this->moveToPreviousLine();
  533. return '';
  534. }
  535. $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
  536. $isItComment = $this->isCurrentLineComment();
  537. while ($this->moveToNextLine()) {
  538. if ($isItComment && !$isItUnindentedCollection) {
  539. $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
  540. $isItComment = $this->isCurrentLineComment();
  541. }
  542. $indent = $this->getCurrentLineIndentation();
  543. if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
  544. $this->moveToPreviousLine();
  545. break;
  546. }
  547. if ($this->isCurrentLineBlank()) {
  548. $data[] = substr($this->currentLine, $newIndent);
  549. continue;
  550. }
  551. if ($indent >= $newIndent) {
  552. $data[] = substr($this->currentLine, $newIndent);
  553. } elseif ($this->isCurrentLineComment()) {
  554. $data[] = $this->currentLine;
  555. } elseif (0 == $indent) {
  556. $this->moveToPreviousLine();
  557. break;
  558. } else {
  559. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
  560. }
  561. }
  562. return implode("\n", $data);
  563. }
  564. private function hasMoreLines(): bool
  565. {
  566. return (\count($this->lines) - 1) > $this->currentLineNb;
  567. }
  568. /**
  569. * Moves the parser to the next line.
  570. */
  571. private function moveToNextLine(): bool
  572. {
  573. if ($this->currentLineNb >= \count($this->lines) - 1) {
  574. return false;
  575. }
  576. $this->currentLine = $this->lines[++$this->currentLineNb];
  577. return true;
  578. }
  579. /**
  580. * Moves the parser to the previous line.
  581. */
  582. private function moveToPreviousLine(): bool
  583. {
  584. if ($this->currentLineNb < 1) {
  585. return false;
  586. }
  587. $this->currentLine = $this->lines[--$this->currentLineNb];
  588. return true;
  589. }
  590. /**
  591. * Parses a YAML value.
  592. *
  593. * @param string $value A YAML value
  594. * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  595. * @param string $context The parser context (either sequence or mapping)
  596. *
  597. * @return mixed A PHP value
  598. *
  599. * @throws ParseException When reference does not exist
  600. */
  601. private function parseValue(string $value, int $flags, string $context)
  602. {
  603. if (0 === strpos($value, '*')) {
  604. if (false !== $pos = strpos($value, '#')) {
  605. $value = substr($value, 1, $pos - 2);
  606. } else {
  607. $value = substr($value, 1);
  608. }
  609. if (!\array_key_exists($value, $this->refs)) {
  610. if (false !== $pos = array_search($value, $this->refsBeingParsed, true)) {
  611. throw new ParseException(sprintf('Circular reference [%s] detected for reference "%s".', implode(', ', array_merge(\array_slice($this->refsBeingParsed, $pos), [$value])), $value), $this->currentLineNb + 1, $this->currentLine, $this->filename);
  612. }
  613. throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine, $this->filename);
  614. }
  615. return $this->refs[$value];
  616. }
  617. if (\in_array($value[0], ['!', '|', '>'], true) && self::preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
  618. $modifiers = $matches['modifiers'] ?? '';
  619. $data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), abs((int) $modifiers));
  620. if ('' !== $matches['tag'] && '!' !== $matches['tag']) {
  621. if ('!!binary' === $matches['tag']) {
  622. return Inline::evaluateBinaryScalar($data);
  623. }
  624. return new TaggedValue(substr($matches['tag'], 1), $data);
  625. }
  626. return $data;
  627. }
  628. try {
  629. if ('' !== $value && '{' === $value[0]) {
  630. $cursor = \strlen(rtrim($this->currentLine)) - \strlen(rtrim($value));
  631. return Inline::parse($this->lexInlineMapping($cursor), $flags, $this->refs);
  632. } elseif ('' !== $value && '[' === $value[0]) {
  633. $cursor = \strlen(rtrim($this->currentLine)) - \strlen(rtrim($value));
  634. return Inline::parse($this->lexInlineSequence($cursor), $flags, $this->refs);
  635. }
  636. switch ($value[0] ?? '') {
  637. case '"':
  638. case "'":
  639. $cursor = \strlen(rtrim($this->currentLine)) - \strlen(rtrim($value));
  640. $parsedValue = Inline::parse($this->lexInlineQuotedString($cursor), $flags, $this->refs);
  641. if (isset($this->currentLine[$cursor]) && preg_replace('/\s*(#.*)?$/A', '', substr($this->currentLine, $cursor))) {
  642. throw new ParseException(sprintf('Unexpected characters near "%s".', substr($this->currentLine, $cursor)));
  643. }
  644. return $parsedValue;
  645. default:
  646. $lines = [];
  647. while ($this->moveToNextLine()) {
  648. // unquoted strings end before the first unindented line
  649. if (0 === $this->getCurrentLineIndentation()) {
  650. $this->moveToPreviousLine();
  651. break;
  652. }
  653. $lines[] = trim($this->currentLine);
  654. }
  655. for ($i = 0, $linesCount = \count($lines), $previousLineBlank = false; $i < $linesCount; ++$i) {
  656. if ('' === $lines[$i]) {
  657. $value .= "\n";
  658. $previousLineBlank = true;
  659. } elseif ($previousLineBlank) {
  660. $value .= $lines[$i];
  661. $previousLineBlank = false;
  662. } else {
  663. $value .= ' '.$lines[$i];
  664. $previousLineBlank = false;
  665. }
  666. }
  667. Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
  668. $parsedValue = Inline::parse($value, $flags, $this->refs);
  669. if ('mapping' === $context && \is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
  670. throw new ParseException('A colon cannot be used in an unquoted mapping value.', $this->getRealCurrentLineNb() + 1, $value, $this->filename);
  671. }
  672. return $parsedValue;
  673. }
  674. } catch (ParseException $e) {
  675. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  676. $e->setSnippet($this->currentLine);
  677. throw $e;
  678. }
  679. }
  680. /**
  681. * Parses a block scalar.
  682. *
  683. * @param string $style The style indicator that was used to begin this block scalar (| or >)
  684. * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
  685. * @param int $indentation The indentation indicator that was used to begin this block scalar
  686. */
  687. private function parseBlockScalar(string $style, string $chomping = '', int $indentation = 0): string
  688. {
  689. $notEOF = $this->moveToNextLine();
  690. if (!$notEOF) {
  691. return '';
  692. }
  693. $isCurrentLineBlank = $this->isCurrentLineBlank();
  694. $blockLines = [];
  695. // leading blank lines are consumed before determining indentation
  696. while ($notEOF && $isCurrentLineBlank) {
  697. // newline only if not EOF
  698. if ($notEOF = $this->moveToNextLine()) {
  699. $blockLines[] = '';
  700. $isCurrentLineBlank = $this->isCurrentLineBlank();
  701. }
  702. }
  703. // determine indentation if not specified
  704. if (0 === $indentation) {
  705. $currentLineLength = \strlen($this->currentLine);
  706. for ($i = 0; $i < $currentLineLength && ' ' === $this->currentLine[$i]; ++$i) {
  707. ++$indentation;
  708. }
  709. }
  710. if ($indentation > 0) {
  711. $pattern = sprintf('/^ {%d}(.*)$/', $indentation);
  712. while (
  713. $notEOF && (
  714. $isCurrentLineBlank ||
  715. self::preg_match($pattern, $this->currentLine, $matches)
  716. )
  717. ) {
  718. if ($isCurrentLineBlank && \strlen($this->currentLine) > $indentation) {
  719. $blockLines[] = substr($this->currentLine, $indentation);
  720. } elseif ($isCurrentLineBlank) {
  721. $blockLines[] = '';
  722. } else {
  723. $blockLines[] = $matches[1];
  724. }
  725. // newline only if not EOF
  726. if ($notEOF = $this->moveToNextLine()) {
  727. $isCurrentLineBlank = $this->isCurrentLineBlank();
  728. }
  729. }
  730. } elseif ($notEOF) {
  731. $blockLines[] = '';
  732. }
  733. if ($notEOF) {
  734. $blockLines[] = '';
  735. $this->moveToPreviousLine();
  736. } elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) {
  737. $blockLines[] = '';
  738. }
  739. // folded style
  740. if ('>' === $style) {
  741. $text = '';
  742. $previousLineIndented = false;
  743. $previousLineBlank = false;
  744. for ($i = 0, $blockLinesCount = \count($blockLines); $i < $blockLinesCount; ++$i) {
  745. if ('' === $blockLines[$i]) {
  746. $text .= "\n";
  747. $previousLineIndented = false;
  748. $previousLineBlank = true;
  749. } elseif (' ' === $blockLines[$i][0]) {
  750. $text .= "\n".$blockLines[$i];
  751. $previousLineIndented = true;
  752. $previousLineBlank = false;
  753. } elseif ($previousLineIndented) {
  754. $text .= "\n".$blockLines[$i];
  755. $previousLineIndented = false;
  756. $previousLineBlank = false;
  757. } elseif ($previousLineBlank || 0 === $i) {
  758. $text .= $blockLines[$i];
  759. $previousLineIndented = false;
  760. $previousLineBlank = false;
  761. } else {
  762. $text .= ' '.$blockLines[$i];
  763. $previousLineIndented = false;
  764. $previousLineBlank = false;
  765. }
  766. }
  767. } else {
  768. $text = implode("\n", $blockLines);
  769. }
  770. // deal with trailing newlines
  771. if ('' === $chomping) {
  772. $text = preg_replace('/\n+$/', "\n", $text);
  773. } elseif ('-' === $chomping) {
  774. $text = preg_replace('/\n+$/', '', $text);
  775. }
  776. return $text;
  777. }
  778. /**
  779. * Returns true if the next line is indented.
  780. *
  781. * @return bool Returns true if the next line is indented, false otherwise
  782. */
  783. private function isNextLineIndented(): bool
  784. {
  785. $currentIndentation = $this->getCurrentLineIndentation();
  786. $movements = 0;
  787. do {
  788. $EOF = !$this->moveToNextLine();
  789. if (!$EOF) {
  790. ++$movements;
  791. }
  792. } while (!$EOF && ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()));
  793. if ($EOF) {
  794. return false;
  795. }
  796. $ret = $this->getCurrentLineIndentation() > $currentIndentation;
  797. for ($i = 0; $i < $movements; ++$i) {
  798. $this->moveToPreviousLine();
  799. }
  800. return $ret;
  801. }
  802. /**
  803. * Returns true if the current line is blank or if it is a comment line.
  804. *
  805. * @return bool Returns true if the current line is empty or if it is a comment line, false otherwise
  806. */
  807. private function isCurrentLineEmpty(): bool
  808. {
  809. return $this->isCurrentLineBlank() || $this->isCurrentLineComment();
  810. }
  811. /**
  812. * Returns true if the current line is blank.
  813. *
  814. * @return bool Returns true if the current line is blank, false otherwise
  815. */
  816. private function isCurrentLineBlank(): bool
  817. {
  818. return '' == trim($this->currentLine, ' ');
  819. }
  820. /**
  821. * Returns true if the current line is a comment line.
  822. *
  823. * @return bool Returns true if the current line is a comment line, false otherwise
  824. */
  825. private function isCurrentLineComment(): bool
  826. {
  827. //checking explicitly the first char of the trim is faster than loops or strpos
  828. $ltrimmedLine = ltrim($this->currentLine, ' ');
  829. return '' !== $ltrimmedLine && '#' === $ltrimmedLine[0];
  830. }
  831. private function isCurrentLineLastLineInDocument(): bool
  832. {
  833. return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1);
  834. }
  835. /**
  836. * Cleanups a YAML string to be parsed.
  837. *
  838. * @param string $value The input YAML string
  839. *
  840. * @return string A cleaned up YAML string
  841. */
  842. private function cleanup(string $value): string
  843. {
  844. $value = str_replace(["\r\n", "\r"], "\n", $value);
  845. // strip YAML header
  846. $count = 0;
  847. $value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#u', '', $value, -1, $count);
  848. $this->offset += $count;
  849. // remove leading comments
  850. $trimmedValue = preg_replace('#^(\#.*?\n)+#s', '', $value, -1, $count);
  851. if (1 === $count) {
  852. // items have been removed, update the offset
  853. $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
  854. $value = $trimmedValue;
  855. }
  856. // remove start of the document marker (---)
  857. $trimmedValue = preg_replace('#^\-\-\-.*?\n#s', '', $value, -1, $count);
  858. if (1 === $count) {
  859. // items have been removed, update the offset
  860. $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
  861. $value = $trimmedValue;
  862. // remove end of the document marker (...)
  863. $value = preg_replace('#\.\.\.\s*$#', '', $value);
  864. }
  865. return $value;
  866. }
  867. /**
  868. * Returns true if the next line starts unindented collection.
  869. *
  870. * @return bool Returns true if the next line starts unindented collection, false otherwise
  871. */
  872. private function isNextLineUnIndentedCollection(): bool
  873. {
  874. $currentIndentation = $this->getCurrentLineIndentation();
  875. $movements = 0;
  876. do {
  877. $EOF = !$this->moveToNextLine();
  878. if (!$EOF) {
  879. ++$movements;
  880. }
  881. } while (!$EOF && ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()));
  882. if ($EOF) {
  883. return false;
  884. }
  885. $ret = $this->getCurrentLineIndentation() === $currentIndentation && $this->isStringUnIndentedCollectionItem();
  886. for ($i = 0; $i < $movements; ++$i) {
  887. $this->moveToPreviousLine();
  888. }
  889. return $ret;
  890. }
  891. /**
  892. * Returns true if the string is un-indented collection item.
  893. *
  894. * @return bool Returns true if the string is un-indented collection item, false otherwise
  895. */
  896. private function isStringUnIndentedCollectionItem(): bool
  897. {
  898. return '-' === rtrim($this->currentLine) || 0 === strpos($this->currentLine, '- ');
  899. }
  900. /**
  901. * A local wrapper for "preg_match" which will throw a ParseException if there
  902. * is an internal error in the PCRE engine.
  903. *
  904. * This avoids us needing to check for "false" every time PCRE is used
  905. * in the YAML engine
  906. *
  907. * @throws ParseException on a PCRE internal error
  908. *
  909. * @see preg_last_error()
  910. *
  911. * @internal
  912. */
  913. public static function preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int
  914. {
  915. if (false === $ret = preg_match($pattern, $subject, $matches, $flags, $offset)) {
  916. switch (preg_last_error()) {
  917. case \PREG_INTERNAL_ERROR:
  918. $error = 'Internal PCRE error.';
  919. break;
  920. case \PREG_BACKTRACK_LIMIT_ERROR:
  921. $error = 'pcre.backtrack_limit reached.';
  922. break;
  923. case \PREG_RECURSION_LIMIT_ERROR:
  924. $error = 'pcre.recursion_limit reached.';
  925. break;
  926. case \PREG_BAD_UTF8_ERROR:
  927. $error = 'Malformed UTF-8 data.';
  928. break;
  929. case \PREG_BAD_UTF8_OFFSET_ERROR:
  930. $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
  931. break;
  932. default:
  933. $error = 'Error.';
  934. }
  935. throw new ParseException($error);
  936. }
  937. return $ret;
  938. }
  939. /**
  940. * Trim the tag on top of the value.
  941. *
  942. * Prevent values such as "!foo {quz: bar}" to be considered as
  943. * a mapping block.
  944. */
  945. private function trimTag(string $value): string
  946. {
  947. if ('!' === $value[0]) {
  948. return ltrim(substr($value, 1, strcspn($value, " \r\n", 1)), ' ');
  949. }
  950. return $value;
  951. }
  952. private function getLineTag(string $value, int $flags, bool $nextLineCheck = true): ?string
  953. {
  954. if ('' === $value || '!' !== $value[0] || 1 !== self::preg_match('/^'.self::TAG_PATTERN.' *( +#.*)?$/', $value, $matches)) {
  955. return null;
  956. }
  957. if ($nextLineCheck && !$this->isNextLineIndented()) {
  958. return null;
  959. }
  960. $tag = substr($matches['tag'], 1);
  961. // Built-in tags
  962. if ($tag && '!' === $tag[0]) {
  963. throw new ParseException(sprintf('The built-in tag "!%s" is not implemented.', $tag), $this->getRealCurrentLineNb() + 1, $value, $this->filename);
  964. }
  965. if (Yaml::PARSE_CUSTOM_TAGS & $flags) {
  966. return $tag;
  967. }
  968. throw new ParseException(sprintf('Tags support is not enabled. You must use the flag "Yaml::PARSE_CUSTOM_TAGS" to use "%s".', $matches['tag']), $this->getRealCurrentLineNb() + 1, $value, $this->filename);
  969. }
  970. private function lexInlineQuotedString(int &$cursor = 0): string
  971. {
  972. $quotation = $this->currentLine[$cursor];
  973. $value = $quotation;
  974. ++$cursor;
  975. $previousLineWasNewline = true;
  976. $previousLineWasTerminatedWithBackslash = false;
  977. $lineNumber = 0;
  978. do {
  979. if (++$lineNumber > 1) {
  980. $cursor += strspn($this->currentLine, ' ', $cursor);
  981. }
  982. if ($this->isCurrentLineBlank()) {
  983. $value .= "\n";
  984. } elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
  985. $value .= ' ';
  986. }
  987. for (; \strlen($this->currentLine) > $cursor; ++$cursor) {
  988. switch ($this->currentLine[$cursor]) {
  989. case '\\':
  990. if ("'" === $quotation) {
  991. $value .= '\\';
  992. } elseif (isset($this->currentLine[++$cursor])) {
  993. $value .= '\\'.$this->currentLine[$cursor];
  994. }
  995. break;
  996. case $quotation:
  997. ++$cursor;
  998. if ("'" === $quotation && isset($this->currentLine[$cursor]) && "'" === $this->currentLine[$cursor]) {
  999. $value .= "''";
  1000. break;
  1001. }
  1002. return $value.$quotation;
  1003. default:
  1004. $value .= $this->currentLine[$cursor];
  1005. }
  1006. }
  1007. if ($this->isCurrentLineBlank()) {
  1008. $previousLineWasNewline = true;
  1009. $previousLineWasTerminatedWithBackslash = false;
  1010. } elseif ('\\' === $this->currentLine[-1]) {
  1011. $previousLineWasNewline = false;
  1012. $previousLineWasTerminatedWithBackslash = true;
  1013. } else {
  1014. $previousLineWasNewline = false;
  1015. $previousLineWasTerminatedWithBackslash = false;
  1016. }
  1017. if ($this->hasMoreLines()) {
  1018. $cursor = 0;
  1019. }
  1020. } while ($this->moveToNextLine());
  1021. throw new ParseException('Malformed inline YAML string.');
  1022. }
  1023. private function lexUnquotedString(int &$cursor): string
  1024. {
  1025. $offset = $cursor;
  1026. $cursor += strcspn($this->currentLine, '[]{},: ', $cursor);
  1027. if ($cursor === $offset) {
  1028. throw new ParseException('Malformed unquoted YAML string.');
  1029. }
  1030. return substr($this->currentLine, $offset, $cursor - $offset);
  1031. }
  1032. private function lexInlineMapping(int &$cursor = 0): string
  1033. {
  1034. return $this->lexInlineStructure($cursor, '}');
  1035. }
  1036. private function lexInlineSequence(int &$cursor = 0): string
  1037. {
  1038. return $this->lexInlineStructure($cursor, ']');
  1039. }
  1040. private function lexInlineStructure(int &$cursor, string $closingTag): string
  1041. {
  1042. $value = $this->currentLine[$cursor];
  1043. ++$cursor;
  1044. do {
  1045. $this->consumeWhitespaces($cursor);
  1046. while (isset($this->currentLine[$cursor])) {
  1047. switch ($this->currentLine[$cursor]) {
  1048. case '"':
  1049. case "'":
  1050. $value .= $this->lexInlineQuotedString($cursor);
  1051. break;
  1052. case ':':
  1053. case ',':
  1054. $value .= $this->currentLine[$cursor];
  1055. ++$cursor;
  1056. break;
  1057. case '{':
  1058. $value .= $this->lexInlineMapping($cursor);
  1059. break;
  1060. case '[':
  1061. $value .= $this->lexInlineSequence($cursor);
  1062. break;
  1063. case $closingTag:
  1064. $value .= $this->currentLine[$cursor];
  1065. ++$cursor;
  1066. return $value;
  1067. case '#':
  1068. break 2;
  1069. default:
  1070. $value .= $this->lexUnquotedString($cursor);
  1071. }
  1072. if ($this->consumeWhitespaces($cursor)) {
  1073. $value .= ' ';
  1074. }
  1075. }
  1076. if ($this->hasMoreLines()) {
  1077. $cursor = 0;
  1078. }
  1079. } while ($this->moveToNextLine());
  1080. throw new ParseException('Malformed inline YAML string.');
  1081. }
  1082. private function consumeWhitespaces(int &$cursor): bool
  1083. {
  1084. $whitespacesConsumed = 0;
  1085. do {
  1086. $whitespaceOnlyTokenLength = strspn($this->currentLine, ' ', $cursor);
  1087. $whitespacesConsumed += $whitespaceOnlyTokenLength;
  1088. $cursor += $whitespaceOnlyTokenLength;
  1089. if (isset($this->currentLine[$cursor])) {
  1090. return 0 < $whitespacesConsumed;
  1091. }
  1092. if ($this->hasMoreLines()) {
  1093. $cursor = 0;
  1094. }
  1095. } while ($this->moveToNextLine());
  1096. return 0 < $whitespacesConsumed;
  1097. }
  1098. }