add some more punctuator characters and // comments.

Swift SVN r9
This commit is contained in:
Chris Lattner
2010-07-18 01:44:13 +00:00
parent 461f41edb1
commit 80ba9c7e66
3 changed files with 62 additions and 3 deletions

View File

@@ -40,7 +40,11 @@ public:
private:
void Warning(const char *Loc, const char *Message);
void Error(const char *Loc, const char *Message);
void FormToken(tok::TokenKind Kind, const char *TokStart, Token &Result);
void SkipSlashSlashComment();
};

View File

@@ -49,6 +49,10 @@ namespace tok {
PUNCTUATOR(colon, ":")
PUNCTUATOR(semi, ";")
PUNCTUATOR(equal, "=")
PUNCTUATOR(plus, "+")
PUNCTUATOR(minus, "-")
PUNCTUATOR(star, "*")
PUNCTUATOR(slash, "/")
PUNCTUATOR(l_paren, "(")
PUNCTUATOR(r_paren, ")")

View File

@@ -25,11 +25,48 @@ Lexer::Lexer(unsigned BufferID, llvm::SourceMgr &SM) : SourceMgr(SM) {
CurPtr = Buffer->getBufferStart();
}
void Lexer::Warning(const char *Loc, const char *Message) {
SourceMgr.PrintMessage(llvm::SMLoc::getFromPointer(Loc), Message, "warning");
}
void Lexer::Error(const char *Loc, const char *Message) {
SourceMgr.PrintMessage(llvm::SMLoc::getFromPointer(Loc), Message, "error");
}
void Lexer::FormToken(tok::TokenKind Kind, const char *TokStart, Token &Result){
Result.setToken(Kind, llvm::StringRef(TokStart, CurPtr-TokStart));
}
/// SkipSlashSlashComment - Skip to the end of the line of a // comment.
void Lexer::SkipSlashSlashComment() {
assert(CurPtr[0] == '/' && CurPtr[-1] == '/' && "Not a // comment");
while (1) {
switch (*CurPtr++) {
case '\n':
case '\r':
return; // If we found the end of the line, return.
default:
break; // Otherwise, eat other characters.
case 0:
// If this is a random nul character in the middle of a buffer, skip it as
// whitespace.
if (CurPtr-1 != Buffer->getBufferEnd()) {
Warning(CurPtr-1, "nul character embedded in middle of file");
break;
}
// Otherwise, we have a // comment at end of file, warn and return.
--CurPtr;
Warning(CurPtr-1, "no newline at end of // comment");
return;
}
}
}
void Lexer::Lex(Token &Result) {
assert(CurPtr >= Buffer->getBufferStart() &&
CurPtr <= Buffer->getBufferEnd() && "Cur Char Pointer out of range!");
@@ -39,25 +76,39 @@ Restart:
switch (*CurPtr++) {
default:
// Error!
Error(CurPtr-1, "invalid character in source file");
return FormToken(tok::unknown, TokStart, Result);
case ' ':
case '\t':
case '\n':
case '\r':
goto Restart; // Skip whitespace.
case 0:
// If this is a random nul character in the middle of a buffer, skip it as
// whitespace.
if (CurPtr-1 != Buffer->getBufferEnd())
// TODO: WARN.
if (CurPtr-1 != Buffer->getBufferEnd()) {
Warning(CurPtr-1, "nul character embedded in middle of file");
goto Restart;
}
// Otherwise, this is the end of the buffer. Return EOF.
return FormToken(tok::eof, TokStart, Result);
case ',': return FormToken(tok::comma, TokStart, Result);
case ':': return FormToken(tok::colon, TokStart, Result);
case ';': return FormToken(tok::semi, TokStart, Result);
case '=': return FormToken(tok::equal, TokStart, Result);
case '+': return FormToken(tok::plus, TokStart, Result);
case '-': return FormToken(tok::minus, TokStart, Result);
case '*': return FormToken(tok::star, TokStart, Result);
case '/':
if (CurPtr[0] == '/') {
SkipSlashSlashComment();
goto Restart;
}
return FormToken(tok::slash, TokStart, Result);
}
}