Skip to content

Commit 9bdd16c

Browse files
committed
Add messages to parse exceptions
1 parent c5f0d1b commit 9bdd16c

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/Parser.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ public static function parseDictionary(string $string): \stdClass
2929
}
3030

3131
if (!preg_match('/^(, *)/', $string, $comma_matches)) {
32-
throw new ParseException();
32+
throw new ParseException('Expected comma');
3333
}
3434

3535
$string = substr($string, strlen($comma_matches[1]));
3636

3737
if (empty($string)) {
38-
throw new ParseException();
38+
throw new ParseException('Unexpected end of input');
3939
}
4040
}
4141

@@ -58,13 +58,13 @@ public static function parseList(string $string): array
5858
}
5959

6060
if (!preg_match('/^(, *)/', $string, $comma_matches)) {
61-
throw new ParseException();
61+
throw new ParseException('Expected comma');
6262
}
6363

6464
$string = substr($string, strlen($comma_matches[1]));
6565

6666
if (empty($string)) {
67-
throw new ParseException();
67+
throw new ParseException('Unexpected end of input');
6868
}
6969
}
7070

@@ -100,11 +100,11 @@ private static function parseInnerList(string &$string): array
100100
$value[] = self::doParseItem($string);
101101

102102
if (!empty($string) && !in_array($string[0], [' ', ')'])) {
103-
throw new ParseException();
103+
throw new ParseException('Unexpected character in inner list');
104104
}
105105
}
106106

107-
throw new ParseException();
107+
throw new ParseException('Unexpected end of input');
108108
}
109109

110110
/**
@@ -123,7 +123,7 @@ public static function parseItem(string $string): array
123123
return $value;
124124
}
125125

126-
throw new ParseException();
126+
throw new ParseException('Unexpected characters at end of input');
127127
}
128128

129129
/**
@@ -153,7 +153,7 @@ private static function parseBareItem(string &$string)
153153
$value = null;
154154

155155
if ($string === "") {
156-
throw new ParseException();
156+
throw new ParseException('Unexpected empty input');
157157
} elseif (preg_match('/^(-|\d)/', $string)) {
158158
$value = self::parseNumber($string);
159159
} elseif ($string[0] == '"') {
@@ -165,7 +165,7 @@ private static function parseBareItem(string &$string)
165165
} elseif (preg_match('/^(\*|[a-z])/i', $string)) {
166166
$value = self::parseToken($string);
167167
} else {
168-
throw new ParseException();
168+
throw new ParseException('Unknown item type');
169169
}
170170

171171
return $value;
@@ -198,13 +198,13 @@ private static function parseKey(string &$string): string
198198
return $matches[0];
199199
}
200200

201-
throw new ParseException();
201+
throw new ParseException('Invalid character in key');
202202
}
203203

204204
private static function parseBoolean(string &$string): bool
205205
{
206206
if (!preg_match('/^\?[01]/', $string)) {
207-
throw new ParseException();
207+
throw new ParseException('Invalid character in boolean');
208208
}
209209

210210
$value = $string[1] === '1';
@@ -224,19 +224,19 @@ private static function parseNumber(string &$string)
224224
$input_number = $number_matches[1];
225225

226226
if (preg_match('/^(-?\d{1,12}\.\d{1,3})$/', $input_number, $decimal_matches)) {
227-
if (strlen($decimal_matches[0]) <= 16) {
228-
$string = substr($string, strlen($decimal_matches[0]));
227+
$string = substr($string, strlen($decimal_matches[0]));
229228

230-
return (float) $decimal_matches[0];
231-
}
229+
return (float) $decimal_matches[0];
232230
} elseif (preg_match('/^-?\d{1,15}$/', $input_number, $integer_matches)) {
233231
$string = substr($string, strlen($integer_matches[0]));
234232

235233
return (int) $integer_matches[0];
234+
} else {
235+
throw new ParseException('Number contains too many digits');
236236
}
237237
}
238238

239-
throw new ParseException();
239+
throw new ParseException('Invalid number format');
240240
}
241241

242242
private static function parseString(string &$string): string
@@ -246,21 +246,21 @@ private static function parseString(string &$string): string
246246

247247
// Newlines and Tabs are not allowed; string cannot end in escape character.
248248
if (preg_match('/(?<!\\\)\\\([nt]|$)/', $matches[1])) {
249-
throw new ParseException();
249+
throw new ParseException('Invalid whitespace in string');
250250
}
251251
// Only quotes and backslashes should be escaped.
252252
if (preg_match_all('/(?<!\\\)\\\./', $matches[1], $quoted_matches, PREG_PATTERN_ORDER)) {
253253
foreach ($quoted_matches[0] as $quoted_match) {
254254
if (!in_array($quoted_match, ['\\"', '\\\\'])) {
255-
throw new ParseException();
255+
throw new ParseException('Invalid escaped character in string');
256256
}
257257
}
258258
}
259259

260260
// Unescape quotes and backslashes.
261261
$output_string = preg_replace('/\\\(["\\\])/', '$1', $matches[1]);
262262
} else {
263-
throw new ParseException();
263+
throw new ParseException('Invalid character in string');
264264
}
265265

266266
return $output_string;
@@ -278,7 +278,7 @@ private static function parseToken(string &$string): Token
278278
return new Token($matches[1]);
279279
}
280280

281-
throw new ParseException();
281+
throw new ParseException('Invalid character in token');
282282
}
283283

284284
/**
@@ -295,6 +295,6 @@ private static function parseByteSequence(string &$string): Bytes
295295
return new Bytes(base64_decode($matches[1]));
296296
}
297297

298-
throw new ParseException();
298+
throw new ParseException('Invalid character in byte sequence');
299299
}
300300
}

0 commit comments

Comments
 (0)