Skip to content
Snippets Groups Projects
Commit d685437d authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Recognize the numeric literals.

parent 7356280c
No related branches found
No related tags found
No related merge requests found
...@@ -105,13 +105,105 @@ StringLiteral::~StringLiteral() ...@@ -105,13 +105,105 @@ StringLiteral::~StringLiteral()
{ } { }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
enum {
NumericLiteralIsChar,
NumericLiteralIsWideChar,
NumericLiteralIsInt,
NumericLiteralIsFloat,
NumericLiteralIsDouble,
NumericLiteralIsLongDouble,
NumericLiteralIsLong,
NumericLiteralIsLongLong,
};
NumericLiteral::NumericLiteral(const char *chars, unsigned size) NumericLiteral::NumericLiteral(const char *chars, unsigned size)
: Literal(chars, size) : Literal(chars, size), _flags(0)
{ } {
_type = NumericLiteralIsInt;
if (chars[0] == '\'') {
_type = NumericLiteralIsChar;
} else if (size > 1 && chars[0] == 'L' && chars[1] == '\'') {
_type = NumericLiteralIsWideChar;
} else if (size > 1 && chars[0] == '0' && (chars[1] == 'x' || chars[1] == 'X')) {
_isHex = true;
} else {
const char *begin = chars;
const char *end = begin + size;
bool done = false;
const char *it = end - 1;
for (; it != begin - 1 && ! done; --it) {
switch (*it) {
case 'l': case 'L': // long suffix
case 'u': case 'U': // unsigned suffix
case 'f': case 'F': // floating suffix
break;
default:
done = true;
break;
} // switch
}
for (const char *dot = it; it != begin - 1; --it) {
if (*dot == '.')
_type = NumericLiteralIsDouble;
}
for (++it; it != end; ++it) {
if (*it == 'l' || *it == 'L') {
if (_type == NumericLiteralIsDouble) {
_type = NumericLiteralIsLongDouble;
} else if (it + 1 != end && (it[1] == 'l' || it[1] == 'L')) {
++it;
_type = NumericLiteralIsLongLong;
} else {
_type = NumericLiteralIsLong;
}
} else if (*it == 'f' || *it == 'F') {
_type = NumericLiteralIsFloat;
} else if (*it == 'u' || *it == 'U') {
_isUnsigned = true;
}
}
}
}
NumericLiteral::~NumericLiteral() NumericLiteral::~NumericLiteral()
{ } { }
bool NumericLiteral::isHex() const
{ return _isHex; }
bool NumericLiteral::isUnsigned() const
{ return _isUnsigned; }
bool NumericLiteral::isChar() const
{ return _type == NumericLiteralIsChar; }
bool NumericLiteral::isWideChar() const
{ return _type == NumericLiteralIsWideChar; }
bool NumericLiteral::isInt() const
{ return _type == NumericLiteralIsInt; }
bool NumericLiteral::isFloat() const
{ return _type == NumericLiteralIsFloat; }
bool NumericLiteral::isDouble() const
{ return _type == NumericLiteralIsDouble; }
bool NumericLiteral::isLongDouble() const
{ return _type == NumericLiteralIsLongDouble; }
bool NumericLiteral::isLong() const
{ return _type == NumericLiteralIsLong; }
bool NumericLiteral::isLongLong() const
{ return _type == NumericLiteralIsLongLong; }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Identifier::Identifier(const char *chars, unsigned size) Identifier::Identifier(const char *chars, unsigned size)
: Literal(chars, size) : Literal(chars, size)
......
...@@ -101,6 +101,29 @@ class CPLUSPLUS_EXPORT NumericLiteral: public Literal ...@@ -101,6 +101,29 @@ class CPLUSPLUS_EXPORT NumericLiteral: public Literal
public: public:
NumericLiteral(const char *chars, unsigned size); NumericLiteral(const char *chars, unsigned size);
virtual ~NumericLiteral(); virtual ~NumericLiteral();
bool isChar() const;
bool isWideChar() const;
bool isInt() const;
bool isFloat() const;
bool isDouble() const;
bool isLongDouble() const;
bool isLong() const;
bool isLongLong() const;
bool isUnsigned() const;
bool isHex() const;
private:
union {
unsigned _flags;
struct {
unsigned _type : 8;
unsigned _isHex : 1;
unsigned _isUnsigned: 1;
};
};
}; };
class CPLUSPLUS_EXPORT Identifier: public Literal class CPLUSPLUS_EXPORT Identifier: public Literal
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment