mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
add some more punctuator characters and // comments.
Swift SVN r9
This commit is contained in:
@@ -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();
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -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, ")")
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user