blob: 38a1d2f2ee0959fa10f6d2ed9b56db6a5adade76 [file] [log] [blame]
Chris Lattner53e7a272007-11-18 02:57:27 +00001//===- TGLexer.h - Lexer for TableGen Files ---------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerfd6c2f02007-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 Lattner53e7a272007-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
Argiris Kirtzidis9fe58162008-10-22 09:54:13 +000017#include "llvm/Support/DataTypes.h"
Chris Lattner53e7a272007-11-18 02:57:27 +000018#include <vector>
19#include <string>
20#include <iosfwd>
Chris Lattnere75e8c02007-11-22 23:19:05 +000021#include <cassert>
Chris Lattner53e7a272007-11-18 02:57:27 +000022
23namespace llvm {
24class MemoryBuffer;
Chris Lattnercfc6b152009-06-21 03:41:50 +000025class SourceMgr;
Chris Lattner37b49f32009-06-21 03:39:35 +000026class SMLoc;
Chris Lattner6b7d76a2007-11-22 20:49:04 +000027
28namespace tgtok {
29 enum TokKind {
30 // Markers
31 Eof, Error,
32
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, // = ?
42
43 // Keywords.
44 Bit, Bits, Class, Code, Dag, Def, Defm, Field, In, Int, Let, List,
45 MultiClass, String,
46
47 // !keywords.
David Greene4f124db2009-05-14 21:54:42 +000048 XConcat, XSRA, XSRL, XSHL, XStrConcat, XNameConcat, XCast, XSubst,
David Greenef5562a52009-06-09 18:31:17 +000049 XForEach, XCar, XCdr, XNull, XIf,
David Greene790e1222009-04-22 20:18:10 +000050
Chris Lattner6b7d76a2007-11-22 20:49:04 +000051 // Integer value.
52 IntVal,
53
54 // String valued tokens.
55 Id, StrVal, VarName, CodeFragment
56 };
57}
Chris Lattner53e7a272007-11-18 02:57:27 +000058
Chris Lattner6b7d76a2007-11-22 20:49:04 +000059/// TGLexer - TableGen Lexer class.
Chris Lattner53e7a272007-11-18 02:57:27 +000060class TGLexer {
Chris Lattnercfc6b152009-06-21 03:41:50 +000061 SourceMgr &SrcMgr;
Chris Lattnerff99dd32009-03-13 07:05:43 +000062
Chris Lattner53e7a272007-11-18 02:57:27 +000063 const char *CurPtr;
Chris Lattnerff99dd32009-03-13 07:05:43 +000064 const MemoryBuffer *CurBuf;
Chris Lattner53e7a272007-11-18 02:57:27 +000065
Chris Lattner6b7d76a2007-11-22 20:49:04 +000066 // 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
Dan Gohman5a5e6e92008-10-17 01:33:43 +000070 int64_t CurIntVal; // This is valid for INTVAL.
Chris Lattnerff99dd32009-03-13 07:05:43 +000071
72 /// CurBuffer - This is the current buffer index we're lexing from as managed
73 /// by the SourceMgr object.
74 int CurBuffer;
Chris Lattner53e7a272007-11-18 02:57:27 +000075
76 // IncludeDirectories - This is the list of directories we should search for
77 // include files in.
78 std::vector<std::string> IncludeDirectories;
Chris Lattner53e7a272007-11-18 02:57:27 +000079public:
Chris Lattnercfc6b152009-06-21 03:41:50 +000080 TGLexer(SourceMgr &SrcMgr);
Chris Lattnerff99dd32009-03-13 07:05:43 +000081 ~TGLexer() {}
Chris Lattner53e7a272007-11-18 02:57:27 +000082
83 void setIncludeDirs(const std::vector<std::string> &Dirs) {
84 IncludeDirectories = Dirs;
85 }
86
Chris Lattner6b7d76a2007-11-22 20:49:04 +000087 tgtok::TokKind Lex() {
88 return CurCode = LexToken();
89 }
Chris Lattner53e7a272007-11-18 02:57:27 +000090
Chris Lattner6b7d76a2007-11-22 20:49:04 +000091 tgtok::TokKind getCode() const { return CurCode; }
92
93 const std::string &getCurStrVal() const {
94 assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal ||
95 CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
96 "This token doesn't have a string value");
97 return CurStrVal;
98 }
Dan Gohman5a5e6e92008-10-17 01:33:43 +000099 int64_t getCurIntVal() const {
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000100 assert(CurCode == tgtok::IntVal && "This token isn't an integer");
101 return CurIntVal;
102 }
103
Chris Lattner37b49f32009-06-21 03:39:35 +0000104 SMLoc getLoc() const;
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000105
Chris Lattnere7205cd2009-03-13 16:01:53 +0000106 void PrintError(const char *Loc, const std::string &Msg) const;
Chris Lattner37b49f32009-06-21 03:39:35 +0000107 void PrintError(SMLoc Loc, const std::string &Msg) const;
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000108
Chris Lattner53e7a272007-11-18 02:57:27 +0000109private:
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000110 /// LexToken - Read the next token and return its code.
111 tgtok::TokKind LexToken();
112
113 tgtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
Chris Lattnerce975c92007-11-19 07:38:58 +0000114
Chris Lattner53e7a272007-11-18 02:57:27 +0000115 int getNextChar();
116 void SkipBCPLComment();
117 bool SkipCComment();
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000118 tgtok::TokKind LexIdentifier();
Chris Lattner53e7a272007-11-18 02:57:27 +0000119 bool LexInclude();
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000120 tgtok::TokKind LexString();
121 tgtok::TokKind LexVarName();
122 tgtok::TokKind LexNumber();
123 tgtok::TokKind LexBracket();
124 tgtok::TokKind LexExclaim();
Chris Lattner53e7a272007-11-18 02:57:27 +0000125};
126
127} // end namespace llvm
128
129#endif