#include "FloatingLexer.h"
using std::string;
string matchFloatingLiteral(const char *prefix) {
if (strcmp("0x", prefix) == 0 || strcmp("0X", prefix) == 0) {
return "";
}
string output;
bool hasDigits = false;
for (;;) {
auto c = *prefix++;
if ('0' <= c && c <= '9') {
output.push_back(c);
hasDigits = true;
} else if ('.' == c) {
if (!hasDigits) {
output.push_back('0');
}
output.push_back(c);
goto fracpart;
} else if ('E' == c || 'e' == c) {
output.push_back(c);
goto exppart;
} else {
return "";
}
}
fracpart:
hasDigits = false;
for (;;) {
auto c = *prefix++;
if ('0' <= c && c <= '9') { output.push_back(c);
hasDigits = true;
} else if ('E' == c || 'e' == c) { if (!hasDigits) {
output.push_back('0');
}
output.push_back(c);
goto exppart;
} else { if (!hasDigits) {
output.push_back('0');
}
return output;
}
}
exppart:
hasDigits = false;
for (;;) {
auto c = *prefix++;
if ('0' <= c && c <= '9') {
output.push_back(c);
hasDigits = true;
} else if (!hasDigits && (c == '+' || c == '-')) {
output.push_back(c);
hasDigits = true;
} else {
return output;
}
}
}