• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- TGLexer.h - Lexer for TableGen 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 represents the Lexer for tablegen files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_TABLEGEN_TGLEXER_H
15 #define LLVM_LIB_TABLEGEN_TGLEXER_H
16 
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/DataTypes.h"
19 #include "llvm/Support/SMLoc.h"
20 #include <cassert>
21 #include <map>
22 #include <string>
23 
24 namespace llvm {
25 class SourceMgr;
26 class SMLoc;
27 class Twine;
28 
29 namespace tgtok {
30   enum TokKind {
31     // Markers
32     Eof, Error,
33 
34     // Tokens with no info.
35     minus, plus,        // - +
36     l_square, r_square, // [ ]
37     l_brace, r_brace,   // { }
38     l_paren, r_paren,   // ( )
39     less, greater,      // < >
40     colon, semi,        // : ;
41     comma, period,      // , .
42     equal, question,    // = ?
43     paste,              // #
44 
45     // Keywords.
46     Bit, Bits, Class, Code, Dag, Def, Foreach, Defm, Field, In, Int, Let, List,
47     MultiClass, String, Defset,
48 
49     // !keywords.
50     XConcat, XADD, XAND, XOR, XSRA, XSRL, XSHL, XListConcat, XStrConcat, XCast,
51     XSubst, XForEach, XFoldl, XHead, XTail, XSize, XEmpty, XIf, XEq, XIsA, XDag,
52     XNe, XLe, XLt, XGe, XGt,
53 
54     // Integer value.
55     IntVal,
56 
57     // Binary constant.  Note that these are sized according to the number of
58     // bits given.
59     BinaryIntVal,
60 
61     // String valued tokens.
62     Id, StrVal, VarName, CodeFragment
63   };
64 }
65 
66 /// TGLexer - TableGen Lexer class.
67 class TGLexer {
68   SourceMgr &SrcMgr;
69 
70   const char *CurPtr;
71   StringRef CurBuf;
72 
73   // Information about the current token.
74   const char *TokStart;
75   tgtok::TokKind CurCode;
76   std::string CurStrVal;  // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT
77   int64_t CurIntVal;      // This is valid for INTVAL.
78 
79   /// CurBuffer - This is the current buffer index we're lexing from as managed
80   /// by the SourceMgr object.
81   unsigned CurBuffer;
82 
83 public:
84   typedef std::map<std::string, SMLoc> DependenciesMapTy;
85 private:
86   /// Dependencies - This is the list of all included files.
87   DependenciesMapTy Dependencies;
88 
89 public:
90   TGLexer(SourceMgr &SrcMgr);
91 
Lex()92   tgtok::TokKind Lex() {
93     return CurCode = LexToken();
94   }
95 
getDependencies()96   const DependenciesMapTy &getDependencies() const {
97     return Dependencies;
98   }
99 
getCode()100   tgtok::TokKind getCode() const { return CurCode; }
101 
getCurStrVal()102   const std::string &getCurStrVal() const {
103     assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal ||
104             CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
105            "This token doesn't have a string value");
106     return CurStrVal;
107   }
getCurIntVal()108   int64_t getCurIntVal() const {
109     assert(CurCode == tgtok::IntVal && "This token isn't an integer");
110     return CurIntVal;
111   }
getCurBinaryIntVal()112   std::pair<int64_t, unsigned> getCurBinaryIntVal() const {
113     assert(CurCode == tgtok::BinaryIntVal &&
114            "This token isn't a binary integer");
115     return std::make_pair(CurIntVal, (CurPtr - TokStart)-2);
116   }
117 
118   SMLoc getLoc() const;
119 
120 private:
121   /// LexToken - Read the next token and return its code.
122   tgtok::TokKind LexToken();
123 
124   tgtok::TokKind ReturnError(const char *Loc, const Twine &Msg);
125 
126   int getNextChar();
127   int peekNextChar(int Index);
128   void SkipBCPLComment();
129   bool SkipCComment();
130   tgtok::TokKind LexIdentifier();
131   bool LexInclude();
132   tgtok::TokKind LexString();
133   tgtok::TokKind LexVarName();
134   tgtok::TokKind LexNumber();
135   tgtok::TokKind LexBracket();
136   tgtok::TokKind LexExclaim();
137 };
138 
139 } // end namespace llvm
140 
141 #endif
142