Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame^] | 1 | //===- AsmLexer.h - Lexer for Assembly Files --------------------*- C++ -*-===// |
| 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 declares the lexer for assembly files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef ASMLEXER_H |
| 15 | #define ASMLEXER_H |
| 16 | |
| 17 | #include "llvm/Support/DataTypes.h" |
| 18 | #include <string> |
| 19 | #include <cassert> |
| 20 | |
| 21 | namespace llvm { |
| 22 | class MemoryBuffer; |
| 23 | class SourceMgr; |
| 24 | class SMLoc; |
| 25 | |
| 26 | namespace asmtok { |
| 27 | enum TokKind { |
| 28 | // Markers |
| 29 | Eof, Error, |
| 30 | |
| 31 | Identifier, |
| 32 | IntVal, |
| 33 | |
| 34 | |
| 35 | Colon, |
| 36 | Plus, |
| 37 | Minus |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | /// AsmLexer - Lexer class for assembly files. |
| 42 | class AsmLexer { |
| 43 | SourceMgr &SrcMgr; |
| 44 | |
| 45 | const char *CurPtr; |
| 46 | const MemoryBuffer *CurBuf; |
| 47 | |
| 48 | // Information about the current token. |
| 49 | const char *TokStart; |
| 50 | asmtok::TokKind CurKind; |
| 51 | std::string CurStrVal; // This is valid for Identifier. |
| 52 | int64_t CurIntVal; |
| 53 | |
| 54 | /// CurBuffer - This is the current buffer index we're lexing from as managed |
| 55 | /// by the SourceMgr object. |
| 56 | int CurBuffer; |
| 57 | |
| 58 | public: |
| 59 | AsmLexer(SourceMgr &SrcMgr); |
| 60 | ~AsmLexer() {} |
| 61 | |
| 62 | asmtok::TokKind Lex() { |
| 63 | return CurKind = LexToken(); |
| 64 | } |
| 65 | |
| 66 | asmtok::TokKind getKind() const { return CurKind; } |
| 67 | |
| 68 | const std::string &getCurStrVal() const { |
| 69 | assert(CurKind == asmtok::Identifier && |
| 70 | "This token doesn't have a string value"); |
| 71 | return CurStrVal; |
| 72 | } |
| 73 | int64_t getCurIntVal() const { |
| 74 | assert(CurKind == asmtok::IntVal && "This token isn't an integer"); |
| 75 | return CurIntVal; |
| 76 | } |
| 77 | |
| 78 | SMLoc getLoc() const; |
| 79 | |
| 80 | void PrintError(const char *Loc, const std::string &Msg) const; |
| 81 | void PrintError(SMLoc Loc, const std::string &Msg) const; |
| 82 | |
| 83 | private: |
| 84 | int getNextChar(); |
| 85 | |
| 86 | /// LexToken - Read the next token and return its code. |
| 87 | asmtok::TokKind LexToken(); |
| 88 | }; |
| 89 | |
| 90 | } // end namespace llvm |
| 91 | |
| 92 | #endif |