introduce consume_garbage()

new routine that consumes whitespaces and comments.
activated by JSON11_COMMENTS pre-processor flag.
mutable-v2
Antonio Cervone 2015-11-27 16:41:05 +01:00
parent 2d1d176a55
commit 08c391f89a
2 changed files with 17 additions and 3 deletions

View File

@ -391,13 +391,25 @@ struct JsonParser {
}
}
/* consume_garbage()
*
* Advance until the current character is non-whitespace and non-comment.
*/
void consume_garbage() {
consume_whitespace();
#ifdef JSON11_COMMENTS
consume_comment();
consume_whitespace();
#endif
}
/* get_next_token()
*
* Return the next non-whitespace character. If the end of the input is reached,
* flag an error and return 0.
*/
char get_next_token() {
consume_whitespace();
consume_garbage();
if (i == str.size())
return fail("unexpected end of input", 0);
@ -689,7 +701,7 @@ Json Json::parse(const string &in, string &err) {
Json result = parser.parse_json(0);
// Check for any trailing garbage
parser.consume_whitespace();
parser.consume_garbage();
if (parser.i != in.size())
return parser.fail("unexpected trailing " + esc(in[parser.i]));
@ -704,7 +716,7 @@ vector<Json> Json::parse_multi(const string &in, string &err) {
while (parser.i != in.size() && !parser.failed) {
json_vec.push_back(parser.parse_json(0));
// Check for another object
parser.consume_whitespace();
parser.consume_garbage();
}
return json_vec;
}

View File

@ -56,6 +56,8 @@
#include <memory>
#include <initializer_list>
#define JSON11_COMMENTS 1
namespace json11 {
class JsonValue;