Refactor parsing of variable names (ie., %foo and @foo) since they have the same

rules. Also refactor "read string until quote" into its own function. No
functionality change!


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@132645 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Nick Lewycky 2011-06-04 18:16:26 +00:00
parent e0b87032f5
commit 9fa89334f1
2 changed files with 46 additions and 50 deletions

View File

@ -308,16 +308,8 @@ lltok::Kind LLLexer::LexAt() {
} }
// Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]* // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' || if (ReadVarName())
CurPtr[0] == '.' || CurPtr[0] == '_') {
++CurPtr;
while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
CurPtr[0] == '.' || CurPtr[0] == '_')
++CurPtr;
StrVal.assign(TokStart+1, CurPtr); // Skip @
return lltok::GlobalVar; return lltok::GlobalVar;
}
// Handle GlobalVarID: @[0-9]+ // Handle GlobalVarID: @[0-9]+
if (isdigit(CurPtr[0])) { if (isdigit(CurPtr[0])) {
@ -334,6 +326,39 @@ lltok::Kind LLLexer::LexAt() {
return lltok::Error; return lltok::Error;
} }
/// ReadString - Read a string until the closing quote.
lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
const char *Start = CurPtr;
while (1) {
int CurChar = getNextChar();
if (CurChar == EOF) {
Error("end of file in string constant");
return lltok::Error;
}
if (CurChar == '"') {
StrVal.assign(Start, CurPtr-1);
UnEscapeLexed(StrVal);
return kind;
}
}
}
/// ReadVarName - Read the rest of a token containing a variable name.
bool LLLexer::ReadVarName() {
const char *NameStart = CurPtr;
if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
CurPtr[0] == '.' || CurPtr[0] == '_') {
++CurPtr;
while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
CurPtr[0] == '.' || CurPtr[0] == '_')
++CurPtr;
StrVal.assign(NameStart, CurPtr);
return true;
}
return false;
}
/// LexPercent - Lex all tokens that start with a % character: /// LexPercent - Lex all tokens that start with a % character:
/// LocalVar ::= %\"[^\"]*\" /// LocalVar ::= %\"[^\"]*\"
@ -343,33 +368,12 @@ lltok::Kind LLLexer::LexPercent() {
// Handle LocalVarName: %\"[^\"]*\" // Handle LocalVarName: %\"[^\"]*\"
if (CurPtr[0] == '"') { if (CurPtr[0] == '"') {
++CurPtr; ++CurPtr;
return ReadString(lltok::LocalVar);
while (1) {
int CurChar = getNextChar();
if (CurChar == EOF) {
Error("end of file in string constant");
return lltok::Error;
}
if (CurChar == '"') {
StrVal.assign(TokStart+2, CurPtr-1);
UnEscapeLexed(StrVal);
return lltok::LocalVar;
}
}
} }
// Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]* // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' || if (ReadVarName())
CurPtr[0] == '.' || CurPtr[0] == '_') {
++CurPtr;
while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
CurPtr[0] == '.' || CurPtr[0] == '_')
++CurPtr;
StrVal.assign(TokStart+1, CurPtr); // Skip %
return lltok::LocalVar; return lltok::LocalVar;
}
// Handle LocalVarID: %[0-9]+ // Handle LocalVarID: %[0-9]+
if (isdigit(CurPtr[0])) { if (isdigit(CurPtr[0])) {
@ -390,27 +394,16 @@ lltok::Kind LLLexer::LexPercent() {
/// QuoteLabel "[^"]+": /// QuoteLabel "[^"]+":
/// StringConstant "[^"]*" /// StringConstant "[^"]*"
lltok::Kind LLLexer::LexQuote() { lltok::Kind LLLexer::LexQuote() {
while (1) { lltok::Kind kind = ReadString(lltok::StringConstant);
int CurChar = getNextChar(); if (kind == lltok::Error || kind == lltok::Eof)
return kind;
if (CurChar == EOF) {
Error("end of file in quoted string");
return lltok::Error;
}
if (CurChar != '"') continue;
if (CurPtr[0] != ':') {
StrVal.assign(TokStart+1, CurPtr-1);
UnEscapeLexed(StrVal);
return lltok::StringConstant;
}
if (CurPtr[0] == ':') {
++CurPtr; ++CurPtr;
StrVal.assign(TokStart+1, CurPtr-2); kind = lltok::LabelStr;
UnEscapeLexed(StrVal);
return lltok::LabelStr;
} }
return kind;
} }
static bool JustWhitespaceNewLine(const char *&Ptr) { static bool JustWhitespaceNewLine(const char *&Ptr) {

View File

@ -71,6 +71,9 @@ namespace llvm {
int getNextChar(); int getNextChar();
void SkipLineComment(); void SkipLineComment();
lltok::Kind ReadString(lltok::Kind kind);
bool ReadVarName();
lltok::Kind LexIdentifier(); lltok::Kind LexIdentifier();
lltok::Kind LexDigitOrNegative(); lltok::Kind LexDigitOrNegative();
lltok::Kind LexPositive(); lltok::Kind LexPositive();