Got rid of useless junk + fixed parser bug

This commit is contained in:
Андреев Григорий 2024-09-01 17:25:50 +03:00
parent 7f8bc8a93a
commit 69874f45ea
2 changed files with 19 additions and 29 deletions

View File

@ -43,7 +43,7 @@ namespace json {
break; break;
skip(pctx); skip(pctx);
if (ch == '0' && d == 0) if (ch == '0' && d == 0)
break; return;
if (d < 18) { if (d < 18) {
mantis_max18 = mantis_max18 * 10 + (ch - '0'); mantis_max18 = mantis_max18 * 10 + (ch - '0');
d++; d++;
@ -208,12 +208,13 @@ namespace json {
return NULL; return NULL;
} }
int parse_str(const std::string& text, JSON& ret_ans, WrongSyntax& ret_error) { JSON parse_str_flawless(const std::string &text) {
assert(ret_ans.isNull()); WrongSyntax wsErr;
ParserContext pctx(text); ParserContext pctx(text);
try { JSON result;
std::vector<std::unique_ptr<ParsingCall>> callStack; std::vector<std::unique_ptr<ParsingCall>> callStack;
callStack.push_back(std::make_unique<ValueParseCall>(ret_ans)); callStack.push_back(std::make_unique<ValueParseCall>(result));
while (!callStack.empty()) { while (!callStack.empty()) {
std::unique_ptr<ParsingCall> rt = callStack.back()->here(pctx); std::unique_ptr<ParsingCall> rt = callStack.back()->here(pctx);
if (rt) { if (rt) {
@ -225,20 +226,7 @@ namespace json {
skipWhitespaces(pctx); skipWhitespaces(pctx);
if (!isEof(pctx)) if (!isEof(pctx))
throw bad_syntax(); throw bad_syntax();
return 0;
} catch (bad_syntax&) {
ret_error.line = pctx.line;
ret_error.column = pctx.column;
return -1;
}
}
JSON parse_str_flawless(const std::string &text) {
WrongSyntax wsErr;
JSON result;
int ret = parse_str(text, result, wsErr);
if (ret < 0)
throw misuse("JSON parsing error");
return result; return result;
} }
} }

View File

@ -48,6 +48,8 @@ void ftest(int i) {
} }
int main(){ int main(){
prettyprint_json(parse_str_flawless("{\"C\":{\"L\":0}}"));
json::JSON A; json::JSON A;
A[1].asString(); A[1].asString();
A[0].asInteger(); A[0].asInteger();