Support using DebugLoc's in a DenseMap.
[llvm/stm8.git] / utils / TableGen / TGLexer.h
blob8859479874020dfa75c1a1695150ddeb9caa8023
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.
12 //===----------------------------------------------------------------------===//
14 #ifndef TGLEXER_H
15 #define TGLEXER_H
17 #include "llvm/Support/DataTypes.h"
18 #include <vector>
19 #include <string>
20 #include <cassert>
22 namespace llvm {
23 class MemoryBuffer;
24 class SourceMgr;
25 class SMLoc;
26 class Twine;
28 namespace tgtok {
29 enum TokKind {
30 // Markers
31 Eof, Error,
33 // Tokens with no info.
34 minus, plus, // - +
35 l_square, r_square, // [ ]
36 l_brace, r_brace, // { }
37 l_paren, r_paren, // ( )
38 less, greater, // < >
39 colon, semi, // : ;
40 comma, period, // , .
41 equal, question, // = ?
43 // Keywords.
44 Bit, Bits, Class, Code, Dag, Def, Defm, Field, In, Int, Let, List,
45 MultiClass, String,
47 // !keywords.
48 XConcat, XSRA, XSRL, XSHL, XStrConcat, XCast, XSubst,
49 XForEach, XHead, XTail, XEmpty, XIf, XEq,
51 // Integer value.
52 IntVal,
54 // String valued tokens.
55 Id, StrVal, VarName, CodeFragment
59 /// TGLexer - TableGen Lexer class.
60 class TGLexer {
61 SourceMgr &SrcMgr;
63 const char *CurPtr;
64 const MemoryBuffer *CurBuf;
66 // Information about the current token.
67 const char *TokStart;
68 tgtok::TokKind CurCode;
69 std::string CurStrVal; // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT
70 int64_t CurIntVal; // This is valid for INTVAL.
72 /// CurBuffer - This is the current buffer index we're lexing from as managed
73 /// by the SourceMgr object.
74 int CurBuffer;
76 public:
77 TGLexer(SourceMgr &SrcMgr);
78 ~TGLexer() {}
80 tgtok::TokKind Lex() {
81 return CurCode = LexToken();
84 tgtok::TokKind getCode() const { return CurCode; }
86 const std::string &getCurStrVal() const {
87 assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal ||
88 CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
89 "This token doesn't have a string value");
90 return CurStrVal;
92 int64_t getCurIntVal() const {
93 assert(CurCode == tgtok::IntVal && "This token isn't an integer");
94 return CurIntVal;
97 SMLoc getLoc() const;
99 void PrintError(const char *Loc, const Twine &Msg) const;
100 void PrintError(SMLoc Loc, const Twine &Msg) const;
102 private:
103 /// LexToken - Read the next token and return its code.
104 tgtok::TokKind LexToken();
106 tgtok::TokKind ReturnError(const char *Loc, const Twine &Msg);
108 int getNextChar();
109 void SkipBCPLComment();
110 bool SkipCComment();
111 tgtok::TokKind LexIdentifier();
112 bool LexInclude();
113 tgtok::TokKind LexString();
114 tgtok::TokKind LexVarName();
115 tgtok::TokKind LexNumber();
116 tgtok::TokKind LexBracket();
117 tgtok::TokKind LexExclaim();
120 } // end namespace llvm
122 #endif