Merge pull request #14104 from ethereum/extract-parse-literal

Refactor literal parsing before adding suffixes
This commit is contained in:
Kamil Śliwak 2023-04-12 14:52:04 +02:00 committed by GitHub
commit 593e207094
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 48 deletions

View File

@ -36,10 +36,12 @@
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <cctype>
#include <vector>
#include <memory>
#include <regex>
#include <tuple>
#include <vector>
using namespace std;
using namespace solidity::langutil;
@ -1989,6 +1991,56 @@ ASTPointer<Expression> Parser::parseLeftHandSideExpression(
}
}
ASTPointer<Expression> Parser::parseLiteral()
{
RecursionGuard recursionGuard(*this);
ASTNodeFactory nodeFactory(*this);
Token initialToken = m_scanner->currentToken();
ASTPointer<ASTString> value = make_shared<string>(m_scanner->currentLiteral());
switch (initialToken)
{
case Token::TrueLiteral:
case Token::FalseLiteral:
case Token::Number:
{
nodeFactory.markEndPosition();
advance();
break;
}
case Token::StringLiteral:
case Token::UnicodeStringLiteral:
case Token::HexStringLiteral:
{
while (m_scanner->peekNextToken() == initialToken)
{
advance();
*value += m_scanner->currentLiteral();
}
nodeFactory.markEndPosition();
advance();
if (m_scanner->currentToken() == Token::Illegal)
fatalParserError(5428_error, to_string(m_scanner->currentError()));
break;
}
default:
solAssert(false);
}
if (initialToken == Token::Number && (
TokenTraits::isEtherSubdenomination(m_scanner->currentToken()) ||
TokenTraits::isTimeSubdenomination(m_scanner->currentToken())
))
{
nodeFactory.markEndPosition();
Literal::SubDenomination subDenomination = static_cast<Literal::SubDenomination>(m_scanner->currentToken());
advance();
return nodeFactory.createNode<Literal>(initialToken, std::move(value), subDenomination);
}
return nodeFactory.createNode<Literal>(initialToken, std::move(value), Literal::SubDenomination::None);
}
ASTPointer<Expression> Parser::parsePrimaryExpression()
{
RecursionGuard recursionGuard(*this);
@ -2000,50 +2052,12 @@ ASTPointer<Expression> Parser::parsePrimaryExpression()
{
case Token::TrueLiteral:
case Token::FalseLiteral:
nodeFactory.markEndPosition();
expression = nodeFactory.createNode<Literal>(token, getLiteralAndAdvance());
break;
case Token::Number:
if (TokenTraits::isEtherSubdenomination(m_scanner->peekNextToken()))
{
ASTPointer<ASTString> literal = getLiteralAndAdvance();
nodeFactory.markEndPosition();
Literal::SubDenomination subdenomination = static_cast<Literal::SubDenomination>(m_scanner->currentToken());
advance();
expression = nodeFactory.createNode<Literal>(token, literal, subdenomination);
}
else if (TokenTraits::isTimeSubdenomination(m_scanner->peekNextToken()))
{
ASTPointer<ASTString> literal = getLiteralAndAdvance();
nodeFactory.markEndPosition();
Literal::SubDenomination subdenomination = static_cast<Literal::SubDenomination>(m_scanner->currentToken());
advance();
expression = nodeFactory.createNode<Literal>(token, literal, subdenomination);
}
else
{
nodeFactory.markEndPosition();
expression = nodeFactory.createNode<Literal>(token, getLiteralAndAdvance());
}
break;
case Token::StringLiteral:
case Token::UnicodeStringLiteral:
case Token::HexStringLiteral:
{
string literal = m_scanner->currentLiteral();
Token firstToken = m_scanner->currentToken();
while (m_scanner->peekNextToken() == firstToken)
{
advance();
literal += m_scanner->currentLiteral();
}
nodeFactory.markEndPosition();
advance();
if (m_scanner->currentToken() == Token::Illegal)
fatalParserError(5428_error, to_string(m_scanner->currentError()));
expression = nodeFactory.createNode<Literal>(token, make_shared<ASTString>(literal));
expression = parseLiteral();
break;
}
case Token::Identifier:
nodeFactory.markEndPosition();
expression = nodeFactory.createNode<Identifier>(getLiteralAndAdvance());

View File

@ -159,6 +159,7 @@ private:
ASTPointer<Expression> parseLeftHandSideExpression(
ASTPointer<Expression> const& _partiallyParsedExpression = ASTPointer<Expression>()
);
ASTPointer<Expression> parseLiteral();
ASTPointer<Expression> parsePrimaryExpression();
std::vector<ASTPointer<Expression>> parseFunctionCallListArguments();

View File

@ -1,7 +1,7 @@
contract C {
function f() {
uint x = 1 finney;
}
function f() public {
uint x = 1 finney;
}
}
// ----
// ParserError 2314: (45-51): Expected ';' but got identifier
// ParserError 2314: (58-64): Expected ';' but got identifier

View File

@ -1,7 +1,7 @@
contract C {
function f() {
uint x = 1 szabo;
}
function f() public {
uint x = 1 szabo;
}
}
// ----
// ParserError 2314: (45-50): Expected ';' but got identifier
// ParserError 2314: (58-63): Expected ';' but got identifier