blob: d1bd70d2eca427caf9778ab51118bbf171d0cdde [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
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000017#include "llvm/Support/DataTypes.h"
Sean Silvaa170f522013-02-07 04:30:39 +000018#include "llvm/Support/SMLoc.h"
Chandler Carrutha1514e22012-12-04 07:12:27 +000019#include <cassert>
Sean Silvaa170f522013-02-07 04:30:39 +000020#include <map>
Chris Lattnera8058742007-11-18 02:57:27 +000021#include <string>
Chris Lattnera8058742007-11-18 02:57:27 +000022
23namespace llvm {
24class MemoryBuffer;
Chris Lattner8070ea32009-06-21 03:41:50 +000025class SourceMgr;
Chris Lattner1e3a8a42009-06-21 03:39:35 +000026class SMLoc;
Benjamin Kramerd1e17032010-09-27 17:42:11 +000027class Twine;
28
Chris Lattnerf4601652007-11-22 20:49:04 +000029namespace tgtok {
30 enum TokKind {
31 // Markers
32 Eof, Error,
33
34 // Tokens with no info.
35 minus, plus, // - +
36 l_square, r_square, // [ ]
37 l_brace, r_brace, // { }
38 l_paren, r_paren, // ( )
39 less, greater, // < >
Francois Pichet606957f2011-03-14 02:30:32 +000040 colon, semi, // : ;
Chris Lattnerf4601652007-11-22 20:49:04 +000041 comma, period, // , .
42 equal, question, // = ?
David Greened3d1cad2011-10-19 13:04:43 +000043 paste, // #
44
Chris Lattnerf4601652007-11-22 20:49:04 +000045 // Keywords.
David Greenecebb4ee2012-02-22 16:09:41 +000046 Bit, Bits, Class, Code, Dag, Def, Foreach, Defm, Field, In, Int, Let, List,
Chris Lattnerf4601652007-11-22 20:49:04 +000047 MultiClass, String,
48
49 // !keywords.
Hal Finkeld23a41c2013-01-25 14:49:08 +000050 XConcat, XADD, XSRA, XSRL, XSHL, XStrConcat, XCast, XSubst,
David Greene1434f662011-01-07 17:05:37 +000051 XForEach, XHead, XTail, XEmpty, XIf, XEq,
David Greenec7cafcd2009-04-22 20:18:10 +000052
Chris Lattnerf4601652007-11-22 20:49:04 +000053 // Integer value.
54 IntVal,
55
56 // String valued tokens.
57 Id, StrVal, VarName, CodeFragment
58 };
59}
Chris Lattnera8058742007-11-18 02:57:27 +000060
Chris Lattnerf4601652007-11-22 20:49:04 +000061/// TGLexer - TableGen Lexer class.
Chris Lattnera8058742007-11-18 02:57:27 +000062class TGLexer {
Chris Lattner8070ea32009-06-21 03:41:50 +000063 SourceMgr &SrcMgr;
Chris Lattneraa739d22009-03-13 07:05:43 +000064
Chris Lattnera8058742007-11-18 02:57:27 +000065 const char *CurPtr;
Chris Lattneraa739d22009-03-13 07:05:43 +000066 const MemoryBuffer *CurBuf;
Chris Lattnera8058742007-11-18 02:57:27 +000067
Chris Lattnerf4601652007-11-22 20:49:04 +000068 // Information about the current token.
69 const char *TokStart;
70 tgtok::TokKind CurCode;
71 std::string CurStrVal; // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT
Dan Gohman63f97202008-10-17 01:33:43 +000072 int64_t CurIntVal; // This is valid for INTVAL.
Chris Lattneraa739d22009-03-13 07:05:43 +000073
74 /// CurBuffer - This is the current buffer index we're lexing from as managed
75 /// by the SourceMgr object.
76 int CurBuffer;
Sean Silvaa170f522013-02-07 04:30:39 +000077
78public:
79 typedef std::map<std::string, SMLoc> DependenciesMapTy;
80private:
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +000081 /// Dependencies - This is the list of all included files.
Sean Silvaa170f522013-02-07 04:30:39 +000082 DependenciesMapTy Dependencies;
83
Chris Lattnera8058742007-11-18 02:57:27 +000084public:
Chris Lattner8070ea32009-06-21 03:41:50 +000085 TGLexer(SourceMgr &SrcMgr);
Chris Lattneraa739d22009-03-13 07:05:43 +000086 ~TGLexer() {}
Chris Lattnera8058742007-11-18 02:57:27 +000087
Chris Lattnerf4601652007-11-22 20:49:04 +000088 tgtok::TokKind Lex() {
89 return CurCode = LexToken();
90 }
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +000091
Sean Silvaa170f522013-02-07 04:30:39 +000092 const DependenciesMapTy &getDependencies() const {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +000093 return Dependencies;
94 }
Chris Lattnera8058742007-11-18 02:57:27 +000095
Chris Lattnerf4601652007-11-22 20:49:04 +000096 tgtok::TokKind getCode() const { return CurCode; }
97
98 const std::string &getCurStrVal() const {
99 assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal ||
100 CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
101 "This token doesn't have a string value");
102 return CurStrVal;
103 }
Dan Gohman63f97202008-10-17 01:33:43 +0000104 int64_t getCurIntVal() const {
Chris Lattnerf4601652007-11-22 20:49:04 +0000105 assert(CurCode == tgtok::IntVal && "This token isn't an integer");
106 return CurIntVal;
107 }
108
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000109 SMLoc getLoc() const;
Chris Lattnerf4601652007-11-22 20:49:04 +0000110
Chris Lattnera8058742007-11-18 02:57:27 +0000111private:
Chris Lattnerf4601652007-11-22 20:49:04 +0000112 /// LexToken - Read the next token and return its code.
113 tgtok::TokKind LexToken();
114
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000115 tgtok::TokKind ReturnError(const char *Loc, const Twine &Msg);
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000116
Chris Lattnera8058742007-11-18 02:57:27 +0000117 int getNextChar();
David Greenea761f922011-10-19 13:03:35 +0000118 int peekNextChar(int Index);
Chris Lattnera8058742007-11-18 02:57:27 +0000119 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