blob: 9f1b535cd404ca3b35b15e660053c2ff1b58e4a4 [file] [log] [blame]
Chris Lattnera8058742007-11-18 02:57:27 +00001//===- TGLexer.h - Lexer for TableGen Files ---------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class represents the Lexer for tablegen files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef TGLEXER_H
15#define TGLEXER_H
16
17#include <vector>
18#include <string>
19#include <iosfwd>
20
21namespace llvm {
22class MemoryBuffer;
Chris Lattnerf4601652007-11-22 20:49:04 +000023
24namespace tgtok {
25 enum TokKind {
26 // Markers
27 Eof, Error,
28
29 // Tokens with no info.
30 minus, plus, // - +
31 l_square, r_square, // [ ]
32 l_brace, r_brace, // { }
33 l_paren, r_paren, // ( )
34 less, greater, // < >
35 colon, semi, // ; :
36 comma, period, // , .
37 equal, question, // = ?
38
39 // Keywords.
40 Bit, Bits, Class, Code, Dag, Def, Defm, Field, In, Int, Let, List,
41 MultiClass, String,
42
43 // !keywords.
44 XConcat, XSRA, XSRL, XSHL, XStrConcat,
45
46 // Integer value.
47 IntVal,
48
49 // String valued tokens.
50 Id, StrVal, VarName, CodeFragment
51 };
52}
Chris Lattnera8058742007-11-18 02:57:27 +000053
Chris Lattnerf4601652007-11-22 20:49:04 +000054/// TGLexer - TableGen Lexer class.
Chris Lattnera8058742007-11-18 02:57:27 +000055class TGLexer {
56 const char *CurPtr;
57 unsigned CurLineNo;
58 MemoryBuffer *CurBuf;
59
Chris Lattnerf4601652007-11-22 20:49:04 +000060 // Information about the current token.
61 const char *TokStart;
62 tgtok::TokKind CurCode;
63 std::string CurStrVal; // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT
64 int CurIntVal; // This is valid for INTVAL.
65
Chris Lattnera8058742007-11-18 02:57:27 +000066 /// IncludeRec / IncludeStack - This captures the current set of include
67 /// directives we are nested within.
68 struct IncludeRec {
69 MemoryBuffer *Buffer;
70 const char *CurPtr;
71 unsigned LineNo;
72 IncludeRec(MemoryBuffer *buffer, const char *curPtr, unsigned lineNo)
73 : Buffer(buffer), CurPtr(curPtr), LineNo(lineNo) {}
74 };
75 std::vector<IncludeRec> IncludeStack;
76
77 // IncludeDirectories - This is the list of directories we should search for
78 // include files in.
79 std::vector<std::string> IncludeDirectories;
Chris Lattnera8058742007-11-18 02:57:27 +000080public:
81 TGLexer(MemoryBuffer *StartBuf);
82 ~TGLexer();
83
84 void setIncludeDirs(const std::vector<std::string> &Dirs) {
85 IncludeDirectories = Dirs;
86 }
87
Chris Lattnerf4601652007-11-22 20:49:04 +000088 tgtok::TokKind Lex() {
89 return CurCode = LexToken();
90 }
Chris Lattnera8058742007-11-18 02:57:27 +000091
Chris Lattnerf4601652007-11-22 20:49:04 +000092 tgtok::TokKind getCode() const { return CurCode; }
93
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");
98 return CurStrVal;
99 }
100 int getCurIntVal() const {
101 assert(CurCode == tgtok::IntVal && "This token isn't an integer");
102 return CurIntVal;
103 }
104
105 typedef const char* LocTy;
106 LocTy getLoc() const { return TokStart; }
107
108 void PrintError(LocTy Loc, const std::string &Msg) const;
109
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000110 void PrintIncludeStack(std::ostream &OS) const;
111
Chris Lattnera8058742007-11-18 02:57:27 +0000112private:
Chris Lattnerf4601652007-11-22 20:49:04 +0000113 /// LexToken - Read the next token and return its code.
114 tgtok::TokKind LexToken();
115
116 tgtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000117
Chris Lattnera8058742007-11-18 02:57:27 +0000118 int getNextChar();
119 void SkipBCPLComment();
120 bool SkipCComment();
Chris Lattnerf4601652007-11-22 20:49:04 +0000121 tgtok::TokKind LexIdentifier();
Chris Lattnera8058742007-11-18 02:57:27 +0000122 bool LexInclude();
Chris Lattnerf4601652007-11-22 20:49:04 +0000123 tgtok::TokKind LexString();
124 tgtok::TokKind LexVarName();
125 tgtok::TokKind LexNumber();
126 tgtok::TokKind LexBracket();
127 tgtok::TokKind LexExclaim();
Chris Lattnera8058742007-11-18 02:57:27 +0000128};
129
130} // end namespace llvm
131
132#endif