blob: 59e9fa0ef5ffd8f949f999e0089c254bb4b69f77 [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 Lattneraa739d22009-03-13 07:05:43 +000025class TGSourceMgr;
Chris Lattnerf4601652007-11-22 20:49:04 +000026
27namespace tgtok {
28 enum TokKind {
29 // Markers
30 Eof, Error,
31
32 // Tokens with no info.
33 minus, plus, // - +
34 l_square, r_square, // [ ]
35 l_brace, r_brace, // { }
36 l_paren, r_paren, // ( )
37 less, greater, // < >
38 colon, semi, // ; :
39 comma, period, // , .
40 equal, question, // = ?
41
42 // Keywords.
43 Bit, Bits, Class, Code, Dag, Def, Defm, Field, In, Int, Let, List,
44 MultiClass, String,
45
46 // !keywords.
47 XConcat, XSRA, XSRL, XSHL, XStrConcat,
48
49 // Integer value.
50 IntVal,
51
52 // String valued tokens.
53 Id, StrVal, VarName, CodeFragment
54 };
55}
Chris Lattnera8058742007-11-18 02:57:27 +000056
Chris Lattnerf4601652007-11-22 20:49:04 +000057/// TGLexer - TableGen Lexer class.
Chris Lattnera8058742007-11-18 02:57:27 +000058class TGLexer {
Chris Lattneraa739d22009-03-13 07:05:43 +000059 TGSourceMgr &SrcMgr;
60
Chris Lattnera8058742007-11-18 02:57:27 +000061 const char *CurPtr;
Chris Lattneraa739d22009-03-13 07:05:43 +000062 const MemoryBuffer *CurBuf;
Chris Lattnera8058742007-11-18 02:57:27 +000063
Chris Lattnerf4601652007-11-22 20:49:04 +000064 // Information about the current token.
65 const char *TokStart;
66 tgtok::TokKind CurCode;
67 std::string CurStrVal; // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT
Dan Gohman63f97202008-10-17 01:33:43 +000068 int64_t CurIntVal; // This is valid for INTVAL.
Chris Lattneraa739d22009-03-13 07:05:43 +000069
70 /// CurBuffer - This is the current buffer index we're lexing from as managed
71 /// by the SourceMgr object.
72 int CurBuffer;
Chris Lattnera8058742007-11-18 02:57:27 +000073
74 // IncludeDirectories - This is the list of directories we should search for
75 // include files in.
76 std::vector<std::string> IncludeDirectories;
Chris Lattnera8058742007-11-18 02:57:27 +000077public:
Chris Lattneraa739d22009-03-13 07:05:43 +000078 TGLexer(TGSourceMgr &SrcMgr);
79 ~TGLexer() {}
Chris Lattnera8058742007-11-18 02:57:27 +000080
81 void setIncludeDirs(const std::vector<std::string> &Dirs) {
82 IncludeDirectories = Dirs;
83 }
84
Chris Lattnerf4601652007-11-22 20:49:04 +000085 tgtok::TokKind Lex() {
86 return CurCode = LexToken();
87 }
Chris Lattnera8058742007-11-18 02:57:27 +000088
Chris Lattnerf4601652007-11-22 20:49:04 +000089 tgtok::TokKind getCode() const { return CurCode; }
90
91 const std::string &getCurStrVal() const {
92 assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal ||
93 CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
94 "This token doesn't have a string value");
95 return CurStrVal;
96 }
Dan Gohman63f97202008-10-17 01:33:43 +000097 int64_t getCurIntVal() const {
Chris Lattnerf4601652007-11-22 20:49:04 +000098 assert(CurCode == tgtok::IntVal && "This token isn't an integer");
99 return CurIntVal;
100 }
101
102 typedef const char* LocTy;
103 LocTy getLoc() const { return TokStart; }
104
105 void PrintError(LocTy Loc, const std::string &Msg) const;
106
Chris Lattnera8058742007-11-18 02:57:27 +0000107private:
Chris Lattnerf4601652007-11-22 20:49:04 +0000108 /// LexToken - Read the next token and return its code.
109 tgtok::TokKind LexToken();
110
111 tgtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000112
Chris Lattnera8058742007-11-18 02:57:27 +0000113 int getNextChar();
114 void SkipBCPLComment();
115 bool SkipCComment();
Chris Lattnerf4601652007-11-22 20:49:04 +0000116 tgtok::TokKind LexIdentifier();
Chris Lattnera8058742007-11-18 02:57:27 +0000117 bool LexInclude();
Chris Lattnerf4601652007-11-22 20:49:04 +0000118 tgtok::TokKind LexString();
119 tgtok::TokKind LexVarName();
120 tgtok::TokKind LexNumber();
121 tgtok::TokKind LexBracket();
122 tgtok::TokKind LexExclaim();
Chris Lattnera8058742007-11-18 02:57:27 +0000123};
124
125} // end namespace llvm
126
127#endif