blob: b4fa97cc0864cb3114237ab4268b16e29bab7d72 [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
17#include <vector>
18#include <string>
19#include <iosfwd>
Chris Lattner8dcf7512007-11-22 23:19:05 +000020#include <cassert>
Chris Lattnera8058742007-11-18 02:57:27 +000021
22namespace llvm {
23class MemoryBuffer;
Chris Lattnerf4601652007-11-22 20:49:04 +000024
25namespace tgtok {
26 enum TokKind {
27 // Markers
28 Eof, Error,
29
30 // Tokens with no info.
31 minus, plus, // - +
32 l_square, r_square, // [ ]
33 l_brace, r_brace, // { }
34 l_paren, r_paren, // ( )
35 less, greater, // < >
36 colon, semi, // ; :
37 comma, period, // , .
38 equal, question, // = ?
39
40 // Keywords.
41 Bit, Bits, Class, Code, Dag, Def, Defm, Field, In, Int, Let, List,
42 MultiClass, String,
43
44 // !keywords.
45 XConcat, XSRA, XSRL, XSHL, XStrConcat,
46
47 // Integer value.
48 IntVal,
49
50 // String valued tokens.
51 Id, StrVal, VarName, CodeFragment
52 };
53}
Chris Lattnera8058742007-11-18 02:57:27 +000054
Chris Lattnerf4601652007-11-22 20:49:04 +000055/// TGLexer - TableGen Lexer class.
Chris Lattnera8058742007-11-18 02:57:27 +000056class TGLexer {
57 const char *CurPtr;
58 unsigned CurLineNo;
59 MemoryBuffer *CurBuf;
60
Chris Lattnerf4601652007-11-22 20:49:04 +000061 // Information about the current token.
62 const char *TokStart;
63 tgtok::TokKind CurCode;
64 std::string CurStrVal; // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT
65 int CurIntVal; // This is valid for INTVAL.
66
Chris Lattnera8058742007-11-18 02:57:27 +000067 /// IncludeRec / IncludeStack - This captures the current set of include
68 /// directives we are nested within.
69 struct IncludeRec {
70 MemoryBuffer *Buffer;
71 const char *CurPtr;
72 unsigned LineNo;
73 IncludeRec(MemoryBuffer *buffer, const char *curPtr, unsigned lineNo)
74 : Buffer(buffer), CurPtr(curPtr), LineNo(lineNo) {}
75 };
76 std::vector<IncludeRec> IncludeStack;
77
78 // IncludeDirectories - This is the list of directories we should search for
79 // include files in.
80 std::vector<std::string> IncludeDirectories;
Chris Lattnera8058742007-11-18 02:57:27 +000081public:
82 TGLexer(MemoryBuffer *StartBuf);
83 ~TGLexer();
84
85 void setIncludeDirs(const std::vector<std::string> &Dirs) {
86 IncludeDirectories = Dirs;
87 }
88
Chris Lattnerf4601652007-11-22 20:49:04 +000089 tgtok::TokKind Lex() {
90 return CurCode = LexToken();
91 }
Chris Lattnera8058742007-11-18 02:57:27 +000092
Chris Lattnerf4601652007-11-22 20:49:04 +000093 tgtok::TokKind getCode() const { return CurCode; }
94
95 const std::string &getCurStrVal() const {
96 assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal ||
97 CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
98 "This token doesn't have a string value");
99 return CurStrVal;
100 }
101 int getCurIntVal() const {
102 assert(CurCode == tgtok::IntVal && "This token isn't an integer");
103 return CurIntVal;
104 }
105
106 typedef const char* LocTy;
107 LocTy getLoc() const { return TokStart; }
108
109 void PrintError(LocTy Loc, const std::string &Msg) const;
110
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000111 void PrintIncludeStack(std::ostream &OS) const;
112
Chris Lattnera8058742007-11-18 02:57:27 +0000113private:
Chris Lattnerf4601652007-11-22 20:49:04 +0000114 /// LexToken - Read the next token and return its code.
115 tgtok::TokKind LexToken();
116
117 tgtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000118
Chris Lattnera8058742007-11-18 02:57:27 +0000119 int getNextChar();
120 void SkipBCPLComment();
121 bool SkipCComment();
Chris Lattnerf4601652007-11-22 20:49:04 +0000122 tgtok::TokKind LexIdentifier();
Chris Lattnera8058742007-11-18 02:57:27 +0000123 bool LexInclude();
Chris Lattnerf4601652007-11-22 20:49:04 +0000124 tgtok::TokKind LexString();
125 tgtok::TokKind LexVarName();
126 tgtok::TokKind LexNumber();
127 tgtok::TokKind LexBracket();
128 tgtok::TokKind LexExclaim();
Chris Lattnera8058742007-11-18 02:57:27 +0000129};
130
131} // end namespace llvm
132
133#endif