|
21 | 21 | class Inline
|
22 | 22 | {
|
23 | 23 | const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\']*(?:\'\'[^\']*)*)\')';
|
24 |
| - const REGEX_SINGLE_QUOTED_STRING = '(?:\'([^\']*(?:\'\'[^\']*)*)\')(?!.*\')'; |
25 |
| - const REGEX_DOUBLE_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)")(?!.*")'; |
26 | 24 |
|
27 | 25 | /**
|
28 | 26 | * Converts a YAML string to a PHP array.
|
@@ -52,7 +50,13 @@ static public function parse($value)
|
52 | 50 | $result = self::parseMapping($value);
|
53 | 51 | break;
|
54 | 52 | default:
|
55 |
| - $result = self::parseScalar($value); |
| 53 | + $i = 0; |
| 54 | + $result = self::parseScalar($value, null, array('"', "'"), $i); |
| 55 | + |
| 56 | + // some comment can end the scalar |
| 57 | + if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) { |
| 58 | + throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i))); |
| 59 | + } |
56 | 60 | }
|
57 | 61 |
|
58 | 62 | if (isset($mbEncoding)) {
|
@@ -163,6 +167,13 @@ static public function parseScalar($scalar, $delimiters = null, $stringDelimiter
|
163 | 167 | if (in_array($scalar[$i], $stringDelimiters)) {
|
164 | 168 | // quoted scalar
|
165 | 169 | $output = self::parseQuotedScalar($scalar, $i);
|
| 170 | + |
| 171 | + if (null !== $delimiters) { |
| 172 | + $tmp = ltrim(substr($scalar, $i), ' '); |
| 173 | + if (!in_array($tmp[0], $delimiters)) { |
| 174 | + throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i))); |
| 175 | + } |
| 176 | + } |
166 | 177 | } else {
|
167 | 178 | // "normal" string
|
168 | 179 | if (!$delimiters) {
|
@@ -203,11 +214,7 @@ static private function parseQuotedScalar($scalar, &$i)
|
203 | 214 | $items = preg_split('/[\'"]\s*(?:[,:]|[}\]]\s*,)/', $subject);
|
204 | 215 | $subject = substr($subject, 0, strlen($items[0]) + 1);
|
205 | 216 |
|
206 |
| - if (($scalar[$i] == "'" |
207 |
| - && !preg_match('/'.self::REGEX_SINGLE_QUOTED_STRING.'/Au', $subject, $match)) |
208 |
| - || ($scalar[$i] == '"' |
209 |
| - && !preg_match('/'.self::REGEX_DOUBLE_QUOTED_STRING.'/Au', $subject, $match)) |
210 |
| - ) { |
| 217 | + if (!preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) { |
211 | 218 | throw new ParseException(sprintf('Malformed inline YAML string (%s).', substr($scalar, $i)));
|
212 | 219 | }
|
213 | 220 |
|
|
0 commit comments