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, |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame^] | 32 | Register, |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 33 | IntVal, |
| 34 | |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame^] | 35 | EndOfStatement, |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 36 | Colon, |
| 37 | Plus, |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame^] | 38 | Minus, |
| 39 | Slash, // '/' |
| 40 | LParen, RParen, |
| 41 | Star, Comma, Dollar |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 42 | }; |
| 43 | } |
| 44 | |
| 45 | /// AsmLexer - Lexer class for assembly files. |
| 46 | class AsmLexer { |
| 47 | SourceMgr &SrcMgr; |
| 48 | |
| 49 | const char *CurPtr; |
| 50 | const MemoryBuffer *CurBuf; |
| 51 | |
| 52 | // Information about the current token. |
| 53 | const char *TokStart; |
| 54 | asmtok::TokKind CurKind; |
| 55 | std::string CurStrVal; // This is valid for Identifier. |
| 56 | int64_t CurIntVal; |
| 57 | |
| 58 | /// CurBuffer - This is the current buffer index we're lexing from as managed |
| 59 | /// by the SourceMgr object. |
| 60 | int CurBuffer; |
| 61 | |
| 62 | public: |
| 63 | AsmLexer(SourceMgr &SrcMgr); |
| 64 | ~AsmLexer() {} |
| 65 | |
| 66 | asmtok::TokKind Lex() { |
| 67 | return CurKind = LexToken(); |
| 68 | } |
| 69 | |
| 70 | asmtok::TokKind getKind() const { return CurKind; } |
| 71 | |
| 72 | const std::string &getCurStrVal() const { |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame^] | 73 | assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register) && |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 74 | "This token doesn't have a string value"); |
| 75 | return CurStrVal; |
| 76 | } |
| 77 | int64_t getCurIntVal() const { |
| 78 | assert(CurKind == asmtok::IntVal && "This token isn't an integer"); |
| 79 | return CurIntVal; |
| 80 | } |
| 81 | |
| 82 | SMLoc getLoc() const; |
| 83 | |
| 84 | void PrintError(const char *Loc, const std::string &Msg) const; |
| 85 | void PrintError(SMLoc Loc, const std::string &Msg) const; |
| 86 | |
| 87 | private: |
| 88 | int getNextChar(); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame^] | 89 | asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 90 | |
| 91 | /// LexToken - Read the next token and return its code. |
| 92 | asmtok::TokKind LexToken(); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame^] | 93 | asmtok::TokKind LexIdentifier(); |
| 94 | asmtok::TokKind LexPercent(); |
| 95 | asmtok::TokKind LexSlash(); |
| 96 | asmtok::TokKind LexHash(); |
| 97 | asmtok::TokKind LexDigit(); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } // end namespace llvm |
| 101 | |
| 102 | #endif |