Merge pull request #22590 from akien-mga/expression-dot-numbers

Add support for '.[0-9]' numbers in Expression
This commit is contained in:
Rémi Verschelde 2018-10-01 23:46:23 +02:00 committed by GitHub
commit fb5e732f78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 14 deletions

View File

@ -756,6 +756,10 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant
//////// ////////
static bool _is_number(CharType c) {
return (c >= '0' && c <= '9');
}
Error Expression::_get_token(Token &r_token) { Error Expression::_get_token(Token &r_token) {
while (true) { while (true) {
@ -813,17 +817,12 @@ Error Expression::_get_token(Token &r_token) {
r_token.type = TK_COLON; r_token.type = TK_COLON;
return OK; return OK;
}; };
case '.': {
r_token.type = TK_PERIOD;
return OK;
};
case '$': { case '$': {
r_token.type = TK_INPUT; r_token.type = TK_INPUT;
int index = 0; int index = 0;
do { do {
if (expression[str_ofs] < '0' || expression[str_ofs] > '9') { if (!_is_number(expression[str_ofs])) {
_set_error("Expected number after '$'"); _set_error("Expected number after '$'");
r_token.type = TK_ERROR; r_token.type = TK_ERROR;
return ERR_PARSE_ERROR; return ERR_PARSE_ERROR;
@ -832,7 +831,7 @@ Error Expression::_get_token(Token &r_token) {
index += expression[str_ofs] - '0'; index += expression[str_ofs] - '0';
str_ofs++; str_ofs++;
} while (expression[str_ofs] >= '0' && expression[str_ofs] <= '9'); } while (_is_number(expression[str_ofs]));
r_token.value = index; r_token.value = index;
return OK; return OK;
@ -979,14 +978,14 @@ Error Expression::_get_token(Token &r_token) {
r_token.type = TK_ERROR; r_token.type = TK_ERROR;
return ERR_PARSE_ERROR; return ERR_PARSE_ERROR;
} }
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) { if (!(_is_number(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
_set_error("Malformed hex constant in string"); _set_error("Malformed hex constant in string");
r_token.type = TK_ERROR; r_token.type = TK_ERROR;
return ERR_PARSE_ERROR; return ERR_PARSE_ERROR;
} }
CharType v; CharType v;
if (c >= '0' && c <= '9') { if (_is_number(c)) {
v = c - '0'; v = c - '0';
} else if (c >= 'a' && c <= 'f') { } else if (c >= 'a' && c <= 'f') {
v = c - 'a'; v = c - 'a';
@ -1032,7 +1031,8 @@ Error Expression::_get_token(Token &r_token) {
break; break;
} }
if (cchar >= '0' && cchar <= '9') { CharType next_char = (str_ofs >= expression.length()) ? 0 : expression[str_ofs];
if (_is_number(cchar) || (cchar == '.' && _is_number(next_char))) {
//a number //a number
String num; String num;
@ -1053,7 +1053,7 @@ Error Expression::_get_token(Token &r_token) {
switch (reading) { switch (reading) {
case READING_INT: { case READING_INT: {
if (c >= '0' && c <= '9') { if (_is_number(c)) {
//pass //pass
} else if (c == '.') { } else if (c == '.') {
reading = READING_DEC; reading = READING_DEC;
@ -1067,7 +1067,7 @@ Error Expression::_get_token(Token &r_token) {
} break; } break;
case READING_DEC: { case READING_DEC: {
if (c >= '0' && c <= '9') { if (_is_number(c)) {
} else if (c == 'e') { } else if (c == 'e') {
reading = READING_EXP; reading = READING_EXP;
@ -1079,7 +1079,7 @@ Error Expression::_get_token(Token &r_token) {
} break; } break;
case READING_EXP: { case READING_EXP: {
if (c >= '0' && c <= '9') { if (_is_number(c)) {
exp_beg = true; exp_beg = true;
} else if ((c == '-' || c == '+') && !exp_sign && !exp_beg) { } else if ((c == '-' || c == '+') && !exp_sign && !exp_beg) {
@ -1114,7 +1114,7 @@ Error Expression::_get_token(Token &r_token) {
String id; String id;
bool first = true; bool first = true;
while ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_' || (!first && cchar >= '0' && cchar <= '9')) { while ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_' || (!first && _is_number(cchar))) {
id += String::chr(cchar); id += String::chr(cchar);
cchar = GET_CHAR(); cchar = GET_CHAR();
@ -1176,6 +1176,12 @@ Error Expression::_get_token(Token &r_token) {
} }
return OK; return OK;
} else if (cchar == '.') {
// Handled down there as we support '.[0-9]' as numbers above
r_token.type = TK_PERIOD;
return OK;
} else { } else {
_set_error("Unexpected character."); _set_error("Unexpected character.");
r_token.type = TK_ERROR; r_token.type = TK_ERROR;
@ -1183,6 +1189,7 @@ Error Expression::_get_token(Token &r_token) {
} }
} }
} }
#undef GET_CHAR
} }
r_token.type = TK_ERROR; r_token.type = TK_ERROR;