From 6f7b0451411174103dcf6649c94ccbcdbd8d9616 Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Wed, 20 Jul 2011 15:08:05 +0200 Subject: Improved parsing of numeric literals Change-Id: Id02b5b0b0ab590b7ea5d20098472459e1fd986a6 Reviewed-on: http://codereview.qt.nokia.com/3755 Reviewed-by: Roberto Raggi Reviewed-by: Qt Sanity Bot --- src/declarative/qml/parser/qdeclarativejslexer.cpp | 99 ++++++++++++++++++---- 1 file changed, 83 insertions(+), 16 deletions(-) (limited to 'src/declarative/qml/parser/qdeclarativejslexer.cpp') diff --git a/src/declarative/qml/parser/qdeclarativejslexer.cpp b/src/declarative/qml/parser/qdeclarativejslexer.cpp index ac4be34765..980c15cf48 100644 --- a/src/declarative/qml/parser/qdeclarativejslexer.cpp +++ b/src/declarative/qml/parser/qdeclarativejslexer.cpp @@ -407,26 +407,40 @@ again: if (_char.isDigit()) { QByteArray chars; chars.reserve(32); - while (_char.isLetterOrNumber() || _char == QLatin1Char('.')) { - if (_char == QLatin1Char('e') || _char == QLatin1Char('E')) { + + chars += ch.unicode(); // append the `.' + + while (_char.isDigit()) { + chars += _char.unicode(); + scanChar(); + } + + if (_char.toLower() == QLatin1Char('e')) { + if (_codePtr[0].isDigit() || ((_codePtr[0] == QLatin1Char('+') || _codePtr[0] == QLatin1Char('-')) && + _codePtr[1].isDigit())) { + chars += _char.unicode(); - scanChar(); // skip e + scanChar(); // consume `e' if (_char == QLatin1Char('+') || _char == QLatin1Char('-')) { chars += _char.unicode(); - scanChar(); // skip +/- + scanChar(); // consume the sign + } + + while (_char.isDigit()) { + chars += _char.unicode(); + scanChar(); } - } else { - chars += _char.unicode(); - scanChar(); } } + const char *begin = chars.constData(); const char *end = 0; bool ok = false; + _tokenValue = qstrtod(begin, &end, &ok); - if (! ok || end != chars.end()) { + if (end != chars.end()) { _errorCode = IllegalExponentIndicator; _errorMessage = QCoreApplication::translate("QDeclarativeParser", "Illegal syntax for exponential number"); return T_ERROR; @@ -676,27 +690,80 @@ again: chars.reserve(32); chars += ch.unicode(); - while (_char.isLetterOrNumber() || _char == QLatin1Char('.')) { - if (_char == QLatin1Char('e') || _char == QLatin1Char('E')) { + if (ch == QLatin1Char('0') && (_char == 'x' || _char == 'X')) { + // parse hex integer literal + + chars += _char.unicode(); + scanChar(); // consume `x' + + while (isHexDigit(_char)) { chars += _char.unicode(); - scanChar(); // skip e + scanChar(); + } + + _tokenValue = integerFromString(chars.constData(), chars.size(), 16); + return T_NUMERIC_LITERAL; + } + + // decimal integer literal + while (_char.isDigit()) { + chars += _char.unicode(); + scanChar(); // consume the digit + } + + if (_char == QLatin1Char('.')) { + chars += _char.unicode(); + scanChar(); // consume `.' + + while (_char.isDigit()) { + chars += _char.unicode(); + scanChar(); + } + + if (_char.toLower() == QLatin1Char('e')) { + if (_codePtr[0].isDigit() || ((_codePtr[0] == QLatin1Char('+') || _codePtr[0] == QLatin1Char('-')) && + _codePtr[1].isDigit())) { - if (_char == QLatin1Char('+') || _char == QLatin1Char('-')) { chars += _char.unicode(); - scanChar(); // skip +/- + scanChar(); // consume `e' + + if (_char == QLatin1Char('+') || _char == QLatin1Char('-')) { + chars += _char.unicode(); + scanChar(); // consume the sign + } + + while (_char.isDigit()) { + chars += _char.unicode(); + scanChar(); + } } - } else { + } + } else if (_char.toLower() == QLatin1Char('e')) { + if (_codePtr[0].isDigit() || ((_codePtr[0] == QLatin1Char('+') || _codePtr[0] == QLatin1Char('-')) && + _codePtr[1].isDigit())) { + chars += _char.unicode(); - scanChar(); + scanChar(); // consume `e' + + if (_char == QLatin1Char('+') || _char == QLatin1Char('-')) { + chars += _char.unicode(); + scanChar(); // consume the sign + } + + while (_char.isDigit()) { + chars += _char.unicode(); + scanChar(); + } } } + const char *begin = chars.constData(); const char *end = 0; bool ok = false; _tokenValue = qstrtod(begin, &end, &ok); - if (! ok || end != chars.end()) { + if (end != chars.end()) { _errorCode = IllegalExponentIndicator; _errorMessage = QCoreApplication::translate("QDeclarativeParser", "Illegal syntax for exponential number"); return T_ERROR; -- cgit v1.2.3