Browse Source

Util: Change Parser switch cases to if-else

master
Riyyi 3 years ago
parent
commit
a14cd1e0a5
  1. 58
      src/util/json/parser.cpp

58
src/util/json/parser.cpp

@ -45,13 +45,13 @@ Value Parser::parse()
switch (token.type) { switch (token.type) {
case Token::Type::Literal: case Token::Type::Literal:
if (token.symbol == "null") { if (token.symbol == "null") {
result = Value {}; result = nullptr;
} }
else if (token.symbol == "true") { else if (token.symbol == "true") {
result = Value { true }; result = true;
} }
else if (token.symbol == "false") { else if (token.symbol == "false") {
result = Value { false }; result = false;
} }
m_index++; m_index++;
break; break;
@ -120,8 +120,7 @@ Value Parser::getArray()
for (;;) { for (;;) {
token = consume(); token = consume();
switch (token.type) { if (token.type == Token::Type::Literal) {
case Token::Type::Literal:
printf("Adding literal to array.. v:{%s}, t:{%d}\n", token.symbol.c_str(), (int)token.type); printf("Adding literal to array.. v:{%s}, t:{%d}\n", token.symbol.c_str(), (int)token.type);
if (token.symbol == "null") { if (token.symbol == "null") {
array.emplace_back(nullptr); array.emplace_back(nullptr);
@ -132,30 +131,33 @@ Value Parser::getArray()
else if (token.symbol == "false") { else if (token.symbol == "false") {
array.emplace_back(false); array.emplace_back(false);
} }
break; }
case Token::Type::Number: else if (token.type == Token::Type::Number) {
printf("Adding number to array.. v:{%s}, t:{%d} -> %f\n", token.symbol.c_str(), (int)token.type, std::stod(token.symbol)); printf("Adding number to array.. v:{%s}, t:{%d} -> %f\n", token.symbol.c_str(), (int)token.type, std::stod(token.symbol));
array.emplace_back(std::stod(token.symbol)); array.emplace_back(std::stod(token.symbol));
break; }
case Token::Type::String: else if (token.type == Token::Type::String) {
printf("Adding string to array.. v:{%s}, t:{%d}\n", token.symbol.c_str(), (int)token.type); printf("Adding string to array.. v:{%s}, t:{%d}\n", token.symbol.c_str(), (int)token.type);
array.emplace_back(token.symbol); array.emplace_back(token.symbol);
break; }
case Token::Type::BracketOpen: else if (token.type == Token::Type::BracketOpen) {
m_index--; m_index--;
array.emplace_back(getArray()); array.emplace_back(getArray());
break; }
case Token::Type::BraceOpen: else if (token.type == Token::Type::BraceOpen) {
m_index--; m_index--;
array.emplace_back(getObject()); array.emplace_back(getObject());
}
else if (token.type == Token::Type::BracketClose) {
break; break;
default:
// Error! // Error!
printf("Invalid JSON! array:1\n"); printf("Invalid JSON! array:1\n");
}
else {
break; break;
} }
// Find , or } // Find , or ]
token = consume(); token = consume();
if (token.type == Token::Type::Comma) { if (token.type == Token::Type::Comma) {
continue; continue;
@ -222,9 +224,9 @@ Value Parser::getObject()
break; break;
} }
// Add member to object // Add member (name:value pair) to object
switch (token.type) { token = consume();
case Token::Type::Literal: if (token.type == Token::Type::Literal) {
printf("Adding literal to object.. k:{%s}, v:{%s}, t:{%d}\n", key.c_str(), token.symbol.c_str(), (int)token.type); printf("Adding literal to object.. k:{%s}, v:{%s}, t:{%d}\n", key.c_str(), token.symbol.c_str(), (int)token.type);
if (token.symbol == "null") { if (token.symbol == "null") {
object[key] = nullptr; object[key] = nullptr;
@ -235,26 +237,26 @@ Value Parser::getObject()
else if (token.symbol == "false") { else if (token.symbol == "false") {
object[key] = false; object[key] = false;
} }
break; }
case Token::Type::Number: else if (token.type == Token::Type::Number) {
printf("Adding number to object.. k:{%s}, v:{%s}, t:{%d} -> %f\n", key.c_str(), token.symbol.c_str(), (int)token.type, std::stod(token.symbol)); printf("Adding number to object.. k:{%s}, v:{%s}, t:{%d} -> %f\n", key.c_str(), token.symbol.c_str(), (int)token.type, std::stod(token.symbol));
object[key] = std::stod(token.symbol); object[key] = std::stod(token.symbol);
break; }
case Token::Type::String: else if (token.type == Token::Type::String) {
printf("Adding string to object.. k:{%s}, v:{%s}, t:{%d}\n", key.c_str(), token.symbol.c_str(), (int)token.type); printf("Adding string to object.. k:{%s}, v:{%s}, t:{%d}\n", key.c_str(), token.symbol.c_str(), (int)token.type);
object[key] = token.symbol; object[key] = token.symbol;
break; }
case Token::Type::BracketOpen: else if (token.type == Token::Type::BracketOpen) {
m_index--; m_index--;
object[key] = getArray(); object[key] = getArray();
break; }
case Token::Type::BraceOpen: else if (token.type == Token::Type::BraceOpen) {
m_index--; m_index--;
object[key] = getObject(); object[key] = getObject();
break;
default:
// Error! // Error!
printf("Invalid JSON! 5\n"); printf("Invalid JSON! 5\n");
}
else {
break; break;
} }

Loading…
Cancel
Save