added from identifier or keyword handling of fixed types

This commit is contained in:
RJ Catalano 2016-03-07 12:29:41 -06:00 committed by chriseth
parent 29b74be413
commit 953e92b6f5
2 changed files with 37 additions and 6 deletions

View File

@ -66,6 +66,13 @@ void ElementaryTypeNameToken::assertDetails(Token::Value _baseType, unsigned con
"No elementary type " + string(Token::toString(_baseType)) + to_string(_first) + "."
);
}
else if (_baseType == Token::UFixedMxN || _baseType == Token::FixedMxN)
{
solAssert(
_first + _second <= 256 && _first % 8 == 0 && _second % 8 == 0,
"No elementary type " + string(Token::toString(_baseType)) + to_string(_first) + "x" + to_string(_second) + "."
);
}
m_token = _baseType;
m_firstNumber = _first;
m_secondNumber = _second;
@ -101,7 +108,7 @@ char const Token::m_tokenType[] =
{
TOKEN_LIST(KT, KK)
};
unsigned Token::extractM(string const& _literal)
unsigned Token::extractUnsigned(string const& _literal)
{
try
{
@ -112,6 +119,10 @@ unsigned Token::extractM(string const& _literal)
{
return 0;
}
catch (invalid_argument& e)
{
return 1;
}
}
tuple<Token::Value, unsigned short, unsigned short> Token::fromIdentifierOrKeyword(string const& _literal)
{
@ -120,7 +131,7 @@ tuple<Token::Value, unsigned short, unsigned short> Token::fromIdentifierOrKeywo
{
string baseType(_literal.begin(), positionM);
auto positionX = find_if_not(positionM, _literal.end(), ::isdigit);
unsigned short m = extractM(string(positionM, positionX));
unsigned short m = extractUnsigned(string(positionM, positionX));
Token::Value keyword = keywordByName(baseType);
if (keyword == Token::Bytes)
{
@ -137,6 +148,25 @@ tuple<Token::Value, unsigned short, unsigned short> Token::fromIdentifierOrKeywo
return make_tuple(Token::IntM, m, 0);
}
}
else if (keyword == Token::UFixed || keyword == Token::Fixed)
{
auto positionN = find_if_not(positionX + 1, _literal.end(), ::isdigit);
unsigned short n = extractUnsigned(string(positionX + 1, positionN));
if (
0 < m + n &&
m + n <= 256 &&
m % 8 == 0 &&
n % 8 == 0 &&
positionN == _literal.end() &&
*positionX == 'x'
)
{
if (keyword == Token::UFixed)
return make_tuple(Token::UFixed, m, n);
else
return make_tuple(Token::Fixed, m, n);
}
}
return make_tuple(Token::Identifier, 0, 0);
}
return make_tuple(keywordByName(_literal), 0, 0);

View File

@ -305,10 +305,11 @@ public:
static std::tuple<Token::Value, unsigned short, unsigned short> fromIdentifierOrKeyword(std::string const& _literal);
private:
// extractM provides a safe way to extract numbers,
// if out_of_range error is thrown, they returns 0s, therefore securing
// the variable's identity as an identifier.
static unsigned extractM(std::string const& _literal);
// extractUnsigned provides a safe way to extract numbers,
// the variable's identity as an identifier. If an invalid conversion
// error is thrown (usually in the case of grabbing N from a fixed type)
// then a 1 is thrown to purposely ensure that it will declare itself as an identifier
static unsigned extractUnsigned(std::string const& _literal);
// @returns the keyword with name @a _name or Token::Identifier of no such keyword exists.
static Token::Value keywordByName(std::string const& _name);
static char const* const m_name[NUM_TOKENS];