Pig: Added ::getNumber

This commit is contained in:
Paul Beckingham 2015-12-30 10:05:32 -05:00
parent b81fab4f32
commit dacff48165
2 changed files with 80 additions and 0 deletions

View file

@ -157,6 +157,85 @@ bool Pig::getDigits (int& result)
return false;
}
////////////////////////////////////////////////////////////////////////////////
// number:
// int frac? exp?
//
// int:
// (-|+)? digit+
//
// frac:
// . digit+
//
// exp:
// e digit+
//
// e:
// e|E (+|-)?
//
bool Pig::getNumber (std::string& result)
{
auto i = _cursor;
// [+-]?
if (_text[i] &&
(_text[i] == '-' ||
_text[i] == '+'))
++i;
// digit+
if (_text[i] &&
Lexer::isDigit (_text[i]))
{
++i;
while (_text[i] && Lexer::isDigit (_text[i]))
++i;
// ( . digit+ )?
if (_text[i] && _text[i] == '.')
{
++i;
while (_text[i] && Lexer::isDigit (_text[i]))
++i;
}
// ( [eE] [+-]? digit+ )?
if (_text[i] &&
(_text[i] == 'e' ||
_text[i] == 'E'))
{
++i;
if (_text[i] &&
(_text[i] == '+' ||
_text[i] == '-'))
++i;
if (_text[i] && Lexer::isDigit (_text[i]))
{
++i;
while (_text[i] && Lexer::isDigit (_text[i]))
++i;
result = _text.substr (_cursor, i - _cursor);
_cursor = i;
return true;
}
return false;
}
result = _text.substr (_cursor, i - _cursor);
_cursor = i;
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool Pig::getRemainder (std::string& result)
{

View file

@ -41,6 +41,7 @@ public:
bool getUntilWS (std::string&);
bool getDigit (int&);
bool getDigits (int&);
bool getNumber (std::string&);
bool getRemainder (std::string&);
bool eos () const;