further optimization, splitting function into pieces

generating strings on the fly, changed name, and added two tests
This commit is contained in:
RJ Catalano
2016-02-18 11:23:00 -06:00
parent d2c0712f36
commit 9b67969fd6
4 changed files with 45 additions and 16 deletions
+16 -14
View File
@@ -53,7 +53,6 @@ namespace solidity
void ElementaryTypeNameToken::assertDetails(Token::Value _baseType, unsigned const& _first, unsigned const& _second)
{
solAssert(Token::isElementaryTypeName(_baseType), "");
string tokenString = Token::toString(_baseType);
if (_baseType == Token::BytesM)
{
solAssert(_second == 0, "There should not be a second size argument to type bytesM.");
@@ -61,10 +60,10 @@ void ElementaryTypeNameToken::assertDetails(Token::Value _baseType, unsigned con
}
else if (_baseType == Token::UIntM || _baseType == Token::IntM)
{
solAssert(_second == 0, "There should not be a second size argument to type " + tokenString + ".");
solAssert(_second == 0, "There should not be a second size argument to type " + string(Token::toString(_baseType)) + ".");
solAssert(
_first <= 256 && _first % 8 == 0,
"No elementary type " + tokenString + to_string(_first) + "."
"No elementary type " + string(Token::toString(_baseType)) + to_string(_first) + "."
);
}
m_token = _baseType;
@@ -120,26 +119,30 @@ tuple<Token::Value, unsigned short, unsigned short> Token::fromIdentifierOrKeywo
if (positionM != _literal.end())
{
string baseType(_literal.begin(), positionM);
auto positionX = find(positionM, _literal.end(), 'x');
auto positionX = find_if_not(positionM, _literal.end(), ::isdigit);
unsigned short m = extractM(string(positionM, positionX));
if (baseType == toString(Token::Bytes))
Token::Value keyword = keywordByName(baseType);
if (keyword == Token::Bytes)
{
if (0 < m && m <= 32)
if (0 < m && m <= 32 && positionX == _literal.end())
return make_tuple(Token::BytesM, m, 0);
return make_tuple(Token::Identifier, 0, 0);
}
else if (baseType == toString(Token::UInt) || baseType == toString(Token::Int))
else if (keyword == Token::UInt || keyword == Token::Int)
{
if (0 < m && m <= 256 && m % 8 == 0)
if (0 < m && m <= 256 && m % 8 == 0 && positionX == _literal.end())
{
if (baseType == toString(Token::UInt))
if (keyword == Token::UInt)
return make_tuple(Token::UIntM, m, 0);
else
return make_tuple(Token::IntM, m, 0);
}
return make_tuple(Token::Identifier, 0, 0);
}
return make_tuple(Token::Identifier, 0, 0);
}
return make_tuple(keywordByName(_literal), 0, 0);
}
Token::Value Token::keywordByName(string const& _name)
{
// The following macros are used inside TOKEN_LIST and cause non-keyword tokens to be ignored
// and keywords to be put inside the keywords variable.
#define KEYWORD(name, string, precedence) {string, Token::name},
@@ -147,12 +150,11 @@ tuple<Token::Value, unsigned short, unsigned short> Token::fromIdentifierOrKeywo
static const map<string, Token::Value> keywords({TOKEN_LIST(TOKEN, KEYWORD)});
#undef KEYWORD
#undef TOKEN
auto it = keywords.find(_literal);
return it == keywords.end() ? make_tuple(Token::Identifier, 0, 0) : make_tuple(it->second, 0, 0);
auto it = keywords.find(_name);
return it == keywords.end() ? Token::Identifier : it->second;
}
#undef KT
#undef KK
}
}