MC/AsmParser: Handle exponents in floating point literals.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@114861 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar 2010-09-27 20:12:52 +00:00
parent 08342f2d11
commit 4f2afe3d39
3 changed files with 48 additions and 9 deletions

View File

@ -61,6 +61,7 @@ private:
AsmToken LexLineComment(); AsmToken LexLineComment();
AsmToken LexDigit(); AsmToken LexDigit();
AsmToken LexQuote(); AsmToken LexQuote();
AsmToken LexFloatLiteral();
}; };
} // end namespace llvm } // end namespace llvm

View File

@ -64,6 +64,31 @@ int AsmLexer::getNextChar() {
} }
} }
/// LexFloatLiteral: [0-9]*[.][0-9]*([eE][+-]?[0-9]*)?
///
/// The leading integral digit sequence and dot should have already been
/// consumed, some or all of the fractional digit sequence *can* have been
/// consumed.
AsmToken AsmLexer::LexFloatLiteral() {
// Skip the fractional digit sequence.
while (isdigit(*CurPtr))
++CurPtr;
// Check for exponent; we intentionally accept a slighlty wider set of
// literals here and rely on the upstream client to reject invalid ones (e.g.,
// "1e+").
if (*CurPtr == 'e' || *CurPtr == 'E') {
++CurPtr;
if (*CurPtr == '-' || *CurPtr == '+')
++CurPtr;
while (isdigit(*CurPtr))
++CurPtr;
}
return AsmToken(AsmToken::Real,
StringRef(TokStart, CurPtr - TokStart));
}
/// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]* /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
static bool IsIdentifierChar(char c) { static bool IsIdentifierChar(char c) {
return isalnum(c) || c == '_' || c == '$' || c == '.' || c == '@'; return isalnum(c) || c == '_' || c == '$' || c == '.' || c == '@';
@ -71,12 +96,11 @@ static bool IsIdentifierChar(char c) {
AsmToken AsmLexer::LexIdentifier() { AsmToken AsmLexer::LexIdentifier() {
// Check for floating point literals. // Check for floating point literals.
if (CurPtr[-1] == '.' && isdigit(*CurPtr)) { if (CurPtr[-1] == '.' && isdigit(*CurPtr)) {
// Disambiguate a .1243foo identifier from a floating literal.
while (isdigit(*CurPtr)) while (isdigit(*CurPtr))
++CurPtr; ++CurPtr;
if (!IsIdentifierChar(*CurPtr)) { if (*CurPtr == 'e' || *CurPtr == 'E' || !IsIdentifierChar(*CurPtr))
return AsmToken(AsmToken::Real, return LexFloatLiteral();
StringRef(TokStart, CurPtr - TokStart));
}
} }
while (IsIdentifierChar(*CurPtr)) while (IsIdentifierChar(*CurPtr))
@ -150,12 +174,9 @@ AsmToken AsmLexer::LexDigit() {
++CurPtr; ++CurPtr;
// Check for floating point literals. // Check for floating point literals.
if (*CurPtr == '.') { if (*CurPtr == '.' || *CurPtr == 'e') {
++CurPtr; ++CurPtr;
while (isdigit(*CurPtr)) return LexFloatLiteral();
++CurPtr;
return AsmToken(AsmToken::Real, StringRef(TokStart, CurPtr - TokStart));
} }
StringRef Result(TokStart, CurPtr - TokStart); StringRef Result(TokStart, CurPtr - TokStart);

View File

@ -13,3 +13,20 @@
# CHECK: .quad 0 # CHECK: .quad 0
.double 0.0 .double 0.0
# CHECK: .quad -4570379565595099136
.double -1.2e3
# CHECK: .quad -4690170861623122860
.double -1.2e-5
# CHECK: .quad -4465782973978902528
.double -1.2e+10
# CHECK: .quad 4681608360884174848
.double 1e5
# CHECK: .quad 4681608360884174848
.double 1.e5
# CHECK: .quad 4611686018427387904
.double 2.
// APFloat should reject these with an error, not crash:
//.double -1.2e+
//.double -1.2e