aboutsummaryrefslogtreecommitdiffstats
path: root/tools/llvm-mc/AsmLexer.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2009-06-21 07:19:10 +0000
committerChris Lattner <sabre@nondot.org>2009-06-21 07:19:10 +0000
commita59e8779964992457ada1af6a5f48068523cfd42 (patch)
tree936d4fbc4b784a3eb0fdbf0b65f91f3d1a66cdbb /tools/llvm-mc/AsmLexer.cpp
parentd926e048c1409d3105e1ccd166e9369ab454a81d (diff)
downloadexternal_llvm-a59e8779964992457ada1af6a5f48068523cfd42.zip
external_llvm-a59e8779964992457ada1af6a5f48068523cfd42.tar.gz
external_llvm-a59e8779964992457ada1af6a5f48068523cfd42.tar.bz2
some baby steps.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73848 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/llvm-mc/AsmLexer.cpp')
-rw-r--r--tools/llvm-mc/AsmLexer.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/tools/llvm-mc/AsmLexer.cpp b/tools/llvm-mc/AsmLexer.cpp
new file mode 100644
index 0000000..da86465
--- /dev/null
+++ b/tools/llvm-mc/AsmLexer.cpp
@@ -0,0 +1,87 @@
+//===- AsmLexer.cpp - Lexer for Assembly Files ----------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This class implements the lexer for assembly files.
+//
+//===----------------------------------------------------------------------===//
+
+#include "AsmLexer.h"
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/MemoryBuffer.h"
+using namespace llvm;
+
+AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
+ CurBuffer = 0;
+ CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
+ CurPtr = CurBuf->getBufferStart();
+ TokStart = 0;
+}
+
+void AsmLexer::PrintError(const char *Loc, const std::string &Msg) const {
+ SrcMgr.PrintError(SMLoc::getFromPointer(Loc), Msg);
+}
+
+void AsmLexer::PrintError(SMLoc Loc, const std::string &Msg) const {
+ SrcMgr.PrintError(Loc, Msg);
+}
+
+int AsmLexer::getNextChar() {
+ char CurChar = *CurPtr++;
+ switch (CurChar) {
+ default:
+ return (unsigned char)CurChar;
+ case 0: {
+ // A nul character in the stream is either the end of the current buffer or
+ // a random nul in the file. Disambiguate that here.
+ if (CurPtr-1 != CurBuf->getBufferEnd())
+ return 0; // Just whitespace.
+
+ // If this is the end of an included file, pop the parent file off the
+ // include stack.
+ SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
+ if (ParentIncludeLoc != SMLoc()) {
+ CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
+ CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
+ CurPtr = ParentIncludeLoc.getPointer();
+ return getNextChar();
+ }
+
+ // Otherwise, return end of file.
+ --CurPtr; // Another call to lex will return EOF again.
+ return EOF;
+ }
+ }
+}
+
+asmtok::TokKind AsmLexer::LexToken() {
+ TokStart = CurPtr;
+ // This always consumes at least one character.
+ int CurChar = getNextChar();
+
+ switch (CurChar) {
+ default:
+ // Handle letters: [a-zA-Z_]
+// if (isalpha(CurChar) || CurChar == '_' || CurChar == '#')
+// return LexIdentifier();
+
+ // Unknown character, emit an error.
+ return asmtok::Error;
+ case EOF: return asmtok::Eof;
+ case 0:
+ case ' ':
+ case '\t':
+ case '\n':
+ case '\r':
+ // Ignore whitespace.
+ return LexToken();
+ case ':': return asmtok::Colon;
+ case '+': return asmtok::Plus;
+ case '-': return asmtok::Minus;
+ }
+} \ No newline at end of file