Browse Source

Util: Rename Lexer/Parser get functions to consume

master
Riyyi 2 years ago
parent
commit
42143fed71
  1. 18
      src/util/json/lexer.cpp
  2. 8
      src/util/json/lexer.h
  3. 42
      src/util/json/parser.cpp
  4. 10
      src/util/json/parser.h

18
src/util/json/lexer.cpp

@ -48,7 +48,7 @@ void Lexer::analyze()
m_tokens->push_back({ Token::Type::Comma, m_line, m_column, "," }); m_tokens->push_back({ Token::Type::Comma, m_line, m_column, "," });
break; break;
case '"': case '"':
if (!getString()) { if (!consumeString()) {
return; return;
} }
break; break;
@ -63,7 +63,7 @@ void Lexer::analyze()
case '7': case '7':
case '8': case '8':
case '9': case '9':
if (!getNumber()) { if (!consumeNumber()) {
return; return;
} }
break; break;
@ -93,7 +93,7 @@ void Lexer::analyze()
case 'x': case 'x':
case 'y': case 'y':
case 'z': case 'z':
if (!getLiteral()) { if (!consumeLiteral()) {
return; return;
} }
break; break;
@ -127,7 +127,7 @@ void Lexer::analyze()
// ----------------------------------------- // -----------------------------------------
bool Lexer::getString() bool Lexer::consumeString()
{ {
size_t column = m_column; size_t column = m_column;
std::string symbol = ""; std::string symbol = "";
@ -170,7 +170,7 @@ bool Lexer::getString()
return true; return true;
} }
bool Lexer::getNumberOrLiteral(Token::Type type) bool Lexer::consumeNumberOrLiteral(Token::Type type)
{ {
size_t index = m_index; size_t index = m_index;
size_t column = m_column; size_t column = m_column;
@ -204,14 +204,14 @@ bool Lexer::getNumberOrLiteral(Token::Type type)
return true; return true;
} }
bool Lexer::getNumber() bool Lexer::consumeNumber()
{ {
return getNumberOrLiteral(Token::Type::Number); return consumeNumberOrLiteral(Token::Type::Number);
} }
bool Lexer::getLiteral() bool Lexer::consumeLiteral()
{ {
return getNumberOrLiteral(Token::Type::Literal); return consumeNumberOrLiteral(Token::Type::Literal);
} }
} // namespace Json } // namespace Json

8
src/util/json/lexer.h

@ -49,10 +49,10 @@ public:
void analyze(); void analyze();
private: private:
bool getString(); bool consumeString();
bool getNumberOrLiteral(Token::Type type); bool consumeNumberOrLiteral(Token::Type type);
bool getNumber(); bool consumeNumber();
bool getLiteral(); bool consumeLiteral();
Job* m_job { nullptr }; Job* m_job { nullptr };

42
src/util/json/parser.cpp

@ -44,19 +44,19 @@ Value Parser::parse()
Token token = peek(); Token token = peek();
switch (token.type) { switch (token.type) {
case Token::Type::Literal: case Token::Type::Literal:
result = getLiteral(); result = consumeLiteral();
break; break;
case Token::Type::Number: case Token::Type::Number:
result = getNumber(); result = consumeNumber();
break; break;
case Token::Type::String: case Token::Type::String:
result = getString(); result = consumeString();
break; break;
case Token::Type::BracketOpen: case Token::Type::BracketOpen:
result = getArray(); result = consumeArray();
break; break;
case Token::Type::BraceOpen: case Token::Type::BraceOpen:
result = getObject(); result = consumeObject();
break; break;
case Token::Type::BracketClose: case Token::Type::BracketClose:
m_job->printErrorLine(token, "expecting value, not ']'"); m_job->printErrorLine(token, "expecting value, not ']'");
@ -110,7 +110,7 @@ Token Parser::consume()
return token; return token;
} }
Value Parser::getLiteral() Value Parser::consumeLiteral()
{ {
Token token = consume(); Token token = consume();
@ -128,7 +128,7 @@ Value Parser::getLiteral()
return nullptr; return nullptr;
} }
Value Parser::getNumber() Value Parser::consumeNumber()
{ {
Token token = consume(); Token token = consume();
@ -231,7 +231,7 @@ Value Parser::getNumber()
return std::stod(token.symbol); return std::stod(token.symbol);
} }
Value Parser::getString() Value Parser::consumeString()
{ {
Token token = consume(); Token token = consume();
@ -305,7 +305,7 @@ Value Parser::getString()
return string; return string;
} }
Value Parser::getArray() Value Parser::consumeArray()
{ {
m_index++; m_index++;
@ -327,19 +327,19 @@ Value Parser::getArray()
token = peek(); token = peek();
if (token.type == Token::Type::Literal) { if (token.type == Token::Type::Literal) {
array.emplace_back(getLiteral()); array.emplace_back(consumeLiteral());
} }
else if (token.type == Token::Type::Number) { else if (token.type == Token::Type::Number) {
array.emplace_back(getNumber()); array.emplace_back(consumeNumber());
} }
else if (token.type == Token::Type::String) { else if (token.type == Token::Type::String) {
array.emplace_back(getString()); array.emplace_back(consumeString());
} }
else if (token.type == Token::Type::BracketOpen) { else if (token.type == Token::Type::BracketOpen) {
array.emplace_back(getArray()); array.emplace_back(consumeArray());
} }
else if (token.type == Token::Type::BraceOpen) { else if (token.type == Token::Type::BraceOpen) {
array.emplace_back(getObject()); array.emplace_back(consumeObject());
} }
else if (token.type == Token::Type::BracketClose) { else if (token.type == Token::Type::BracketClose) {
// Trailing comma // Trailing comma
@ -376,7 +376,7 @@ Value Parser::getArray()
return array; return array;
} }
Value Parser::getObject() Value Parser::consumeObject()
{ {
m_index++; m_index++;
@ -414,7 +414,7 @@ Value Parser::getObject()
// Find member name // Find member name
m_index--; m_index--;
Value tmpName = getString(); Value tmpName = consumeString();
if (tmpName.m_type != Value::Type::String) { if (tmpName.m_type != Value::Type::String) {
seekForward(Token::Type::BraceClose); seekForward(Token::Type::BraceClose);
break; break;
@ -453,19 +453,19 @@ Value Parser::getObject()
// Add member (name:value pair) to object // Add member (name:value pair) to object
token = peek(); token = peek();
if (token.type == Token::Type::Literal) { if (token.type == Token::Type::Literal) {
object.emplace(name, getLiteral()); object.emplace(name, consumeLiteral());
} }
else if (token.type == Token::Type::Number) { else if (token.type == Token::Type::Number) {
object.emplace(name, getNumber()); object.emplace(name, consumeNumber());
} }
else if (token.type == Token::Type::String) { else if (token.type == Token::Type::String) {
object.emplace(name, getString()); object.emplace(name, consumeString());
} }
else if (token.type == Token::Type::BracketOpen) { else if (token.type == Token::Type::BracketOpen) {
object.emplace(name, getArray()); object.emplace(name, consumeArray());
} }
else if (token.type == Token::Type::BraceOpen) { else if (token.type == Token::Type::BraceOpen) {
object.emplace(name, getObject()); object.emplace(name, consumeObject());
} }
else { else {
reportError(token, "expecting value, not '" + token.symbol + "'"); reportError(token, "expecting value, not '" + token.symbol + "'");

10
src/util/json/parser.h

@ -31,11 +31,11 @@ private:
Token peek(); Token peek();
Token consume(); Token consume();
Value getLiteral(); Value consumeLiteral();
Value getNumber(); Value consumeNumber();
Value getString(); Value consumeString();
Value getArray(); Value consumeArray();
Value getObject(); Value consumeObject();
Job* m_job { nullptr }; Job* m_job { nullptr };

Loading…
Cancel
Save