blob: 0e67f680557dffd1ca96c8782391e50205e29d53 [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//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera8058742007-11-18 02:57:27 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This class represents the Lexer for tablegen files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef TGLEXER_H
15#define TGLEXER_H
16
Argyrios Kyrtzidisbb9d18c2008-10-22 09:54:13 +000017#include "llvm/Support/DataTypes.h"
Chris Lattnera8058742007-11-18 02:57:27 +000018#include <vector>
19#include <string>
20#include <iosfwd>
Chris Lattner8dcf7512007-11-22 23:19:05 +000021#include <cassert>
Chris Lattnera8058742007-11-18 02:57:27 +000022
23namespace llvm {
24class MemoryBuffer;
Chris Lattnerf4601652007-11-22 20:49:04 +000025
26namespace 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 Lattnera8058742007-11-18 02:57:27 +000055
Chris Lattnerf4601652007-11-22 20:49:04 +000056/// TGLexer - TableGen Lexer class.
Chris Lattnera8058742007-11-18 02:57:27 +000057class TGLexer {
58 const char *CurPtr;
59 unsigned CurLineNo;
60 MemoryBuffer *CurBuf;
61
Chris Lattnerf4601652007-11-22 20:49:04 +000062 // 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 Gohman63f97202008-10-17 01:33:43 +000066 int64_t CurIntVal; // This is valid for INTVAL.
Chris Lattnerf4601652007-11-22 20:49:04 +000067
Chris Lattnera8058742007-11-18 02:57:27 +000068 /// 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 Lattnera8058742007-11-18 02:57:27 +000082public:
83 TGLexer(MemoryBuffer *StartBuf);
84 ~TGLexer();
85
86 void setIncludeDirs(const std::vector<std::string> &Dirs) {
87 IncludeDirectories = Dirs;
88 }
89
Chris Lattnerf4601652007-11-22 20:49:04 +000090 tgtok::TokKind Lex() {
91 return CurCode = LexToken();
92 }
Chris Lattnera8058742007-11-18 02:57:27 +000093
Chris Lattnerf4601652007-11-22 20:49:04 +000094 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 Gohman63f97202008-10-17 01:33:43 +0000102 int64_t getCurIntVal() const {
Chris Lattnerf4601652007-11-22 20:49:04 +0000103 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 Lattnerc8a9bbc2007-11-19 07:38:58 +0000112 void PrintIncludeStack(std::ostream &OS) const;
113
Chris Lattnera8058742007-11-18 02:57:27 +0000114private:
Chris Lattnerf4601652007-11-22 20:49:04 +0000115 /// 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 Lattnerc8a9bbc2007-11-19 07:38:58 +0000119
Chris Lattnera8058742007-11-18 02:57:27 +0000120 int getNextChar();
121 void SkipBCPLComment();
122 bool SkipCComment();
Chris Lattnerf4601652007-11-22 20:49:04 +0000123 tgtok::TokKind LexIdentifier();
Chris Lattnera8058742007-11-18 02:57:27 +0000124 bool LexInclude();
Chris Lattnerf4601652007-11-22 20:49:04 +0000125 tgtok::TokKind LexString();
126 tgtok::TokKind LexVarName();
127 tgtok::TokKind LexNumber();
128 tgtok::TokKind LexBracket();
129 tgtok::TokKind LexExclaim();
Chris Lattnera8058742007-11-18 02:57:27 +0000130};
131
132} // end namespace llvm
133
134#endif