some baby steps.
[oota-llvm.git] / tools / llvm-mc / AsmLexer.cpp
1 //===- AsmLexer.cpp - Lexer for Assembly Files ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class implements the lexer for assembly files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "AsmLexer.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 using namespace llvm;
18
19 AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
20   CurBuffer = 0;
21   CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
22   CurPtr = CurBuf->getBufferStart();
23   TokStart = 0;
24 }
25
26 void AsmLexer::PrintError(const char *Loc, const std::string &Msg) const {
27   SrcMgr.PrintError(SMLoc::getFromPointer(Loc), Msg);
28 }
29
30 void AsmLexer::PrintError(SMLoc Loc, const std::string &Msg) const {
31   SrcMgr.PrintError(Loc, Msg);
32 }
33
34 int AsmLexer::getNextChar() {
35   char CurChar = *CurPtr++;
36   switch (CurChar) {
37   default:
38     return (unsigned char)CurChar;
39   case 0: {
40     // A nul character in the stream is either the end of the current buffer or
41     // a random nul in the file.  Disambiguate that here.
42     if (CurPtr-1 != CurBuf->getBufferEnd())
43       return 0;  // Just whitespace.
44     
45     // If this is the end of an included file, pop the parent file off the
46     // include stack.
47     SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
48     if (ParentIncludeLoc != SMLoc()) {
49       CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
50       CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
51       CurPtr = ParentIncludeLoc.getPointer();
52       return getNextChar();
53     }
54     
55     // Otherwise, return end of file.
56     --CurPtr;  // Another call to lex will return EOF again.  
57     return EOF;
58   }
59   }
60 }
61
62 asmtok::TokKind AsmLexer::LexToken() {
63   TokStart = CurPtr;
64   // This always consumes at least one character.
65   int CurChar = getNextChar();
66   
67   switch (CurChar) {
68   default:
69     // Handle letters: [a-zA-Z_]
70 //    if (isalpha(CurChar) || CurChar == '_' || CurChar == '#')
71 //      return LexIdentifier();
72     
73     // Unknown character, emit an error.
74     return asmtok::Error;
75   case EOF: return asmtok::Eof;
76   case 0:
77   case ' ':
78   case '\t':
79   case '\n':
80   case '\r':
81     // Ignore whitespace.
82     return LexToken();
83   case ':': return asmtok::Colon;
84   case '+': return asmtok::Plus;
85   case '-': return asmtok::Minus;
86   }
87 }