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,37 +208,25 @@ namespace json {
return NULL; return NULL;
} }
int parse_str(const std::string& text, JSON& ret_ans, WrongSyntax& ret_error) {
assert(ret_ans.isNull());
ParserContext pctx(text);
try {
std::vector<std::unique_ptr<ParsingCall>> callStack;
callStack.push_back(std::make_unique<ValueParseCall>(ret_ans));
while (!callStack.empty()) {
std::unique_ptr<ParsingCall> rt = callStack.back()->here(pctx);
if (rt) {
callStack.push_back(std::move(rt));
} else {
callStack.pop_back();
}
}
skipWhitespaces(pctx);
if (!isEof(pctx))
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) { JSON parse_str_flawless(const std::string &text) {
WrongSyntax wsErr; WrongSyntax wsErr;
ParserContext pctx(text);
JSON result; JSON result;
int ret = parse_str(text, result, wsErr);
if (ret < 0) std::vector<std::unique_ptr<ParsingCall>> callStack;
throw misuse("JSON parsing error"); callStack.push_back(std::make_unique<ValueParseCall>(result));
while (!callStack.empty()) {
std::unique_ptr<ParsingCall> rt = callStack.back()->here(pctx);
if (rt) {
callStack.push_back(std::move(rt));
} else {
callStack.pop_back();
}
}
skipWhitespaces(pctx);
if (!isEof(pctx))
throw bad_syntax();
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();