1 //===- TGLexer.h - Lexer for TableGen Files ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class represents the Lexer for tablegen files.
12 //===----------------------------------------------------------------------===//
29 // Tokens with no info.
31 l_square, r_square, // [ ]
32 l_brace, r_brace, // { }
33 l_paren, r_paren, // ( )
37 equal, question, // = ?
40 Bit, Bits, Class, Code, Dag, Def, Defm, Field, In, Int, Let, List,
44 XConcat, XSRA, XSRL, XSHL, XStrConcat,
49 // String valued tokens.
50 Id, StrVal, VarName, CodeFragment
54 /// TGLexer - TableGen Lexer class.
60 // Information about the current token.
62 tgtok::TokKind CurCode;
63 std::string CurStrVal; // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT
64 int CurIntVal; // This is valid for INTVAL.
66 /// IncludeRec / IncludeStack - This captures the current set of include
67 /// directives we are nested within.
72 IncludeRec(MemoryBuffer *buffer, const char *curPtr, unsigned lineNo)
73 : Buffer(buffer), CurPtr(curPtr), LineNo(lineNo) {}
75 std::vector<IncludeRec> IncludeStack;
77 // IncludeDirectories - This is the list of directories we should search for
79 std::vector<std::string> IncludeDirectories;
81 TGLexer(MemoryBuffer *StartBuf);
84 void setIncludeDirs(const std::vector<std::string> &Dirs) {
85 IncludeDirectories = Dirs;
88 tgtok::TokKind Lex() {
89 return CurCode = LexToken();
92 tgtok::TokKind getCode() const { return CurCode; }
94 const std::string &getCurStrVal() const {
95 assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal ||
96 CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
97 "This token doesn't have a string value");
100 int getCurIntVal() const {
101 assert(CurCode == tgtok::IntVal && "This token isn't an integer");
105 typedef const char* LocTy;
106 LocTy getLoc() const { return TokStart; }
108 void PrintError(LocTy Loc, const std::string &Msg) const;
110 void PrintIncludeStack(std::ostream &OS) const;
113 /// LexToken - Read the next token and return its code.
114 tgtok::TokKind LexToken();
116 tgtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
119 void SkipBCPLComment();
121 tgtok::TokKind LexIdentifier();
123 tgtok::TokKind LexString();
124 tgtok::TokKind LexVarName();
125 tgtok::TokKind LexNumber();
126 tgtok::TokKind LexBracket();
127 tgtok::TokKind LexExclaim();
130 } // end namespace llvm