Chris Lattner | a805874 | 2007-11-18 02:57:27 +0000 | [diff] [blame] | 1 | //===- TGLexer.h - Lexer for TableGen Files ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 3060910 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | a805874 | 2007-11-18 02:57:27 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This class represents the Lexer for tablegen files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef TGLEXER_H |
| 15 | #define TGLEXER_H |
| 16 | |
Argyrios Kyrtzidis | bb9d18c | 2008-10-22 09:54:13 +0000 | [diff] [blame] | 17 | #include "llvm/Support/DataTypes.h" |
Chris Lattner | a805874 | 2007-11-18 02:57:27 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | #include <string> |
| 20 | #include <iosfwd> |
Chris Lattner | 8dcf751 | 2007-11-22 23:19:05 +0000 | [diff] [blame] | 21 | #include <cassert> |
Chris Lattner | a805874 | 2007-11-18 02:57:27 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
| 24 | class MemoryBuffer; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 25 | |
| 26 | namespace tgtok { |
| 27 | enum TokKind { |
| 28 | // Markers |
| 29 | Eof, Error, |
| 30 | |
| 31 | // Tokens with no info. |
| 32 | minus, plus, // - + |
| 33 | l_square, r_square, // [ ] |
| 34 | l_brace, r_brace, // { } |
| 35 | l_paren, r_paren, // ( ) |
| 36 | less, greater, // < > |
| 37 | colon, semi, // ; : |
| 38 | comma, period, // , . |
| 39 | equal, question, // = ? |
| 40 | |
| 41 | // Keywords. |
| 42 | Bit, Bits, Class, Code, Dag, Def, Defm, Field, In, Int, Let, List, |
| 43 | MultiClass, String, |
| 44 | |
| 45 | // !keywords. |
| 46 | XConcat, XSRA, XSRL, XSHL, XStrConcat, |
| 47 | |
| 48 | // Integer value. |
| 49 | IntVal, |
| 50 | |
| 51 | // String valued tokens. |
| 52 | Id, StrVal, VarName, CodeFragment |
| 53 | }; |
| 54 | } |
Chris Lattner | a805874 | 2007-11-18 02:57:27 +0000 | [diff] [blame] | 55 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 56 | /// TGLexer - TableGen Lexer class. |
Chris Lattner | a805874 | 2007-11-18 02:57:27 +0000 | [diff] [blame] | 57 | class TGLexer { |
| 58 | const char *CurPtr; |
| 59 | unsigned CurLineNo; |
| 60 | MemoryBuffer *CurBuf; |
| 61 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 62 | // Information about the current token. |
| 63 | const char *TokStart; |
| 64 | tgtok::TokKind CurCode; |
| 65 | std::string CurStrVal; // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT |
Dan Gohman | 63f9720 | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 66 | int64_t CurIntVal; // This is valid for INTVAL. |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 67 | |
Chris Lattner | a805874 | 2007-11-18 02:57:27 +0000 | [diff] [blame] | 68 | /// IncludeRec / IncludeStack - This captures the current set of include |
| 69 | /// directives we are nested within. |
| 70 | struct IncludeRec { |
| 71 | MemoryBuffer *Buffer; |
| 72 | const char *CurPtr; |
| 73 | unsigned LineNo; |
| 74 | IncludeRec(MemoryBuffer *buffer, const char *curPtr, unsigned lineNo) |
| 75 | : Buffer(buffer), CurPtr(curPtr), LineNo(lineNo) {} |
| 76 | }; |
| 77 | std::vector<IncludeRec> IncludeStack; |
| 78 | |
| 79 | // IncludeDirectories - This is the list of directories we should search for |
| 80 | // include files in. |
| 81 | std::vector<std::string> IncludeDirectories; |
Chris Lattner | a805874 | 2007-11-18 02:57:27 +0000 | [diff] [blame] | 82 | public: |
| 83 | TGLexer(MemoryBuffer *StartBuf); |
| 84 | ~TGLexer(); |
| 85 | |
| 86 | void setIncludeDirs(const std::vector<std::string> &Dirs) { |
| 87 | IncludeDirectories = Dirs; |
| 88 | } |
| 89 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 90 | tgtok::TokKind Lex() { |
| 91 | return CurCode = LexToken(); |
| 92 | } |
Chris Lattner | a805874 | 2007-11-18 02:57:27 +0000 | [diff] [blame] | 93 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 94 | tgtok::TokKind getCode() const { return CurCode; } |
| 95 | |
| 96 | const std::string &getCurStrVal() const { |
| 97 | assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal || |
| 98 | CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) && |
| 99 | "This token doesn't have a string value"); |
| 100 | return CurStrVal; |
| 101 | } |
Dan Gohman | 63f9720 | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 102 | int64_t getCurIntVal() const { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 103 | assert(CurCode == tgtok::IntVal && "This token isn't an integer"); |
| 104 | return CurIntVal; |
| 105 | } |
| 106 | |
| 107 | typedef const char* LocTy; |
| 108 | LocTy getLoc() const { return TokStart; } |
| 109 | |
| 110 | void PrintError(LocTy Loc, const std::string &Msg) const; |
| 111 | |
Chris Lattner | c8a9bbc | 2007-11-19 07:38:58 +0000 | [diff] [blame] | 112 | void PrintIncludeStack(std::ostream &OS) const; |
| 113 | |
Chris Lattner | a805874 | 2007-11-18 02:57:27 +0000 | [diff] [blame] | 114 | private: |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 115 | /// LexToken - Read the next token and return its code. |
| 116 | tgtok::TokKind LexToken(); |
| 117 | |
| 118 | tgtok::TokKind ReturnError(const char *Loc, const std::string &Msg); |
Chris Lattner | c8a9bbc | 2007-11-19 07:38:58 +0000 | [diff] [blame] | 119 | |
Chris Lattner | a805874 | 2007-11-18 02:57:27 +0000 | [diff] [blame] | 120 | int getNextChar(); |
| 121 | void SkipBCPLComment(); |
| 122 | bool SkipCComment(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 123 | tgtok::TokKind LexIdentifier(); |
Chris Lattner | a805874 | 2007-11-18 02:57:27 +0000 | [diff] [blame] | 124 | bool LexInclude(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 125 | tgtok::TokKind LexString(); |
| 126 | tgtok::TokKind LexVarName(); |
| 127 | tgtok::TokKind LexNumber(); |
| 128 | tgtok::TokKind LexBracket(); |
| 129 | tgtok::TokKind LexExclaim(); |
Chris Lattner | a805874 | 2007-11-18 02:57:27 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | } // end namespace llvm |
| 133 | |
| 134 | #endif |