@@ -129,7 +129,7 @@ bool Reader::parse(const char* beginDoc, const char* endDoc, Value& root,
129
129
130
130
bool successful = readValue ();
131
131
Token token;
132
- skipCommentTokens (token);
132
+ readTokenSkippingComments (token);
133
133
if (collectComments_ && !commentsBefore_.empty ())
134
134
root.setComment (commentsBefore_, commentAfter);
135
135
if (features_.strictRoot_ ) {
@@ -157,7 +157,7 @@ bool Reader::readValue() {
157
157
throwRuntimeError (" Exceeded stackLimit in readValue()." );
158
158
159
159
Token token;
160
- skipCommentTokens (token);
160
+ readTokenSkippingComments (token);
161
161
bool successful = true ;
162
162
163
163
if (collectComments_ && !commentsBefore_.empty ()) {
@@ -225,14 +225,14 @@ bool Reader::readValue() {
225
225
return successful;
226
226
}
227
227
228
- void Reader::skipCommentTokens (Token& token) {
228
+ bool Reader::readTokenSkippingComments (Token& token) {
229
+ bool success = readToken (token);
229
230
if (features_.allowComments_ ) {
230
- do {
231
- readToken (token);
232
- } while (token.type_ == tokenComment);
233
- } else {
234
- readToken (token);
231
+ while (success && token.type_ == tokenComment) {
232
+ success = readToken (token);
233
+ }
235
234
}
235
+ return success;
236
236
}
237
237
238
238
bool Reader::readToken (Token& token) {
@@ -446,12 +446,7 @@ bool Reader::readObject(Token& token) {
446
446
Value init (objectValue);
447
447
currentValue ().swapPayload (init);
448
448
currentValue ().setOffsetStart (token.start_ - begin_);
449
- while (readToken (tokenName)) {
450
- bool initialTokenOk = true ;
451
- while (tokenName.type_ == tokenComment && initialTokenOk)
452
- initialTokenOk = readToken (tokenName);
453
- if (!initialTokenOk)
454
- break ;
449
+ while (readTokenSkippingComments (tokenName)) {
455
450
if (tokenName.type_ == tokenObjectEnd && name.empty ()) // empty object
456
451
return true ;
457
452
name.clear ();
@@ -480,15 +475,11 @@ bool Reader::readObject(Token& token) {
480
475
return recoverFromError (tokenObjectEnd);
481
476
482
477
Token comma;
483
- if (!readToken (comma) ||
484
- (comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator &&
485
- comma.type_ != tokenComment)) {
478
+ if (!readTokenSkippingComments (comma) ||
479
+ (comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator)) {
486
480
return addErrorAndRecover (" Missing ',' or '}' in object declaration" ,
487
481
comma, tokenObjectEnd);
488
482
}
489
- bool finalizeTokenOk = true ;
490
- while (comma.type_ == tokenComment && finalizeTokenOk)
491
- finalizeTokenOk = readToken (comma);
492
483
if (comma.type_ == tokenObjectEnd)
493
484
return true ;
494
485
}
@@ -518,10 +509,7 @@ bool Reader::readArray(Token& token) {
518
509
519
510
Token currentToken;
520
511
// Accept Comment after last item in the array.
521
- ok = readToken (currentToken);
522
- while (currentToken.type_ == tokenComment && ok) {
523
- ok = readToken (currentToken);
524
- }
512
+ ok = readTokenSkippingComments (currentToken);
525
513
bool badTokenType = (currentToken.type_ != tokenArraySeparator &&
526
514
currentToken.type_ != tokenArrayEnd);
527
515
if (!ok || badTokenType) {
@@ -943,6 +931,7 @@ class OurReader {
943
931
using Errors = std::deque<ErrorInfo>;
944
932
945
933
bool readToken (Token& token);
934
+ bool readTokenSkippingComments (Token& token);
946
935
void skipSpaces ();
947
936
void skipBom (bool skipBom);
948
937
bool match (const Char* pattern, int patternLength);
@@ -976,7 +965,6 @@ class OurReader {
976
965
int & column) const ;
977
966
String getLocationLineAndColumn (Location location) const ;
978
967
void addComment (Location begin, Location end, CommentPlacement placement);
979
- void skipCommentTokens (Token& token);
980
968
981
969
static String normalizeEOL (Location begin, Location end);
982
970
static bool containsNewLine (Location begin, Location end);
@@ -1030,7 +1018,7 @@ bool OurReader::parse(const char* beginDoc, const char* endDoc, Value& root,
1030
1018
bool successful = readValue ();
1031
1019
nodes_.pop ();
1032
1020
Token token;
1033
- skipCommentTokens (token);
1021
+ readTokenSkippingComments (token);
1034
1022
if (features_.failIfExtra_ && (token.type_ != tokenEndOfStream)) {
1035
1023
addError (" Extra non-whitespace after JSON value." , token);
1036
1024
return false ;
@@ -1058,7 +1046,7 @@ bool OurReader::readValue() {
1058
1046
if (nodes_.size () > features_.stackLimit_ )
1059
1047
throwRuntimeError (" Exceeded stackLimit in readValue()." );
1060
1048
Token token;
1061
- skipCommentTokens (token);
1049
+ readTokenSkippingComments (token);
1062
1050
bool successful = true ;
1063
1051
1064
1052
if (collectComments_ && !commentsBefore_.empty ()) {
@@ -1145,14 +1133,14 @@ bool OurReader::readValue() {
1145
1133
return successful;
1146
1134
}
1147
1135
1148
- void OurReader::skipCommentTokens (Token& token) {
1136
+ bool OurReader::readTokenSkippingComments (Token& token) {
1137
+ bool success = readToken (token);
1149
1138
if (features_.allowComments_ ) {
1150
- do {
1151
- readToken (token);
1152
- } while (token.type_ == tokenComment);
1153
- } else {
1154
- readToken (token);
1139
+ while (success && token.type_ == tokenComment) {
1140
+ success = readToken (token);
1141
+ }
1155
1142
}
1143
+ return success;
1156
1144
}
1157
1145
1158
1146
bool OurReader::readToken (Token& token) {
@@ -1449,12 +1437,7 @@ bool OurReader::readObject(Token& token) {
1449
1437
Value init (objectValue);
1450
1438
currentValue ().swapPayload (init);
1451
1439
currentValue ().setOffsetStart (token.start_ - begin_);
1452
- while (readToken (tokenName)) {
1453
- bool initialTokenOk = true ;
1454
- while (tokenName.type_ == tokenComment && initialTokenOk)
1455
- initialTokenOk = readToken (tokenName);
1456
- if (!initialTokenOk)
1457
- break ;
1440
+ while (readTokenSkippingComments (tokenName)) {
1458
1441
if (tokenName.type_ == tokenObjectEnd &&
1459
1442
(name.empty () ||
1460
1443
features_.allowTrailingCommas_ )) // empty object or trailing comma
@@ -1491,15 +1474,11 @@ bool OurReader::readObject(Token& token) {
1491
1474
return recoverFromError (tokenObjectEnd);
1492
1475
1493
1476
Token comma;
1494
- if (!readToken (comma) ||
1495
- (comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator &&
1496
- comma.type_ != tokenComment)) {
1477
+ if (!readTokenSkippingComments (comma) ||
1478
+ (comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator)) {
1497
1479
return addErrorAndRecover (" Missing ',' or '}' in object declaration" ,
1498
1480
comma, tokenObjectEnd);
1499
1481
}
1500
- bool finalizeTokenOk = true ;
1501
- while (comma.type_ == tokenComment && finalizeTokenOk)
1502
- finalizeTokenOk = readToken (comma);
1503
1482
if (comma.type_ == tokenObjectEnd)
1504
1483
return true ;
1505
1484
}
@@ -1533,10 +1512,7 @@ bool OurReader::readArray(Token& token) {
1533
1512
1534
1513
Token currentToken;
1535
1514
// Accept Comment after last item in the array.
1536
- ok = readToken (currentToken);
1537
- while (currentToken.type_ == tokenComment && ok) {
1538
- ok = readToken (currentToken);
1539
- }
1515
+ ok = readTokenSkippingComments (currentToken);
1540
1516
bool badTokenType = (currentToken.type_ != tokenArraySeparator &&
1541
1517
currentToken.type_ != tokenArrayEnd);
1542
1518
if (!ok || badTokenType) {
0 commit comments