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

8
src/util/json/lexer.h

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

42
src/util/json/parser.cpp

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

10
src/util/json/parser.h

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

Loading…
Cancel
Save