blob: 82f312a3388e260433c96971064e1c5ecd374e42 [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 Lattner1e3a8a42009-06-21 03:39:35 +000026class SMLoc;
Chris Lattnerf4601652007-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 Greene4afc5092009-05-14 21:54:42 +000048 XConcat, XSRA, XSRL, XSHL, XStrConcat, XNameConcat, XCast, XSubst,
David Greeneffc0ab62009-06-09 18:31:17 +000049 XForEach, XCar, XCdr, XNull, XIf,
David Greenec7cafcd2009-04-22 20:18:10 +000050
Chris Lattnerf4601652007-11-22 20:49:04 +000051 // Integer value.
52 IntVal,
53
54 // String valued tokens.
55 Id, StrVal, VarName, CodeFragment
56 };
57}
Chris Lattnera8058742007-11-18 02:57:27 +000058
Chris Lattnerf4601652007-11-22 20:49:04 +000059/// TGLexer - TableGen Lexer class.
Chris Lattnera8058742007-11-18 02:57:27 +000060class TGLexer {
Chris Lattneraa739d22009-03-13 07:05:43 +000061 TGSourceMgr &SrcMgr;
62
Chris Lattnera8058742007-11-18 02:57:27 +000063 const char *CurPtr;
Chris Lattneraa739d22009-03-13 07:05:43 +000064 const MemoryBuffer *CurBuf;
Chris Lattnera8058742007-11-18 02:57:27 +000065
Chris Lattnerf4601652007-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 Gohman63f97202008-10-17 01:33:43 +000070 int64_t CurIntVal; // This is valid for INTVAL.
Chris Lattneraa739d22009-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 Lattnera8058742007-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 Lattnera8058742007-11-18 02:57:27 +000079public:
Chris Lattneraa739d22009-03-13 07:05:43 +000080 TGLexer(TGSourceMgr &SrcMgr);
81 ~TGLexer() {}
Chris Lattnera8058742007-11-18 02:57:27 +000082
83 void setIncludeDirs(const std::vector<std::string> &Dirs) {
84 IncludeDirectories = Dirs;
85 }
86
Chris Lattnerf4601652007-11-22 20:49:04 +000087 tgtok::TokKind Lex() {
88 return CurCode = LexToken();
89 }
Chris Lattnera8058742007-11-18 02:57:27 +000090
Chris Lattnerf4601652007-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 Gohman63f97202008-10-17 01:33:43 +000099 int64_t getCurIntVal() const {
Chris Lattnerf4601652007-11-22 20:49:04 +0000100 assert(CurCode == tgtok::IntVal && "This token isn't an integer");
101 return CurIntVal;
102 }
103
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000104 SMLoc getLoc() const;
Chris Lattnerf4601652007-11-22 20:49:04 +0000105
Chris Lattner1c8ae592009-03-13 16:01:53 +0000106 void PrintError(const char *Loc, const std::string &Msg) const;
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000107 void PrintError(SMLoc Loc, const std::string &Msg) const;
Chris Lattnerf4601652007-11-22 20:49:04 +0000108
Chris Lattnera8058742007-11-18 02:57:27 +0000109private:
Chris Lattnerf4601652007-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 Lattnerc8a9bbc2007-11-19 07:38:58 +0000114
Chris Lattnera8058742007-11-18 02:57:27 +0000115 int getNextChar();
116 void SkipBCPLComment();
117 bool SkipCComment();
Chris Lattnerf4601652007-11-22 20:49:04 +0000118 tgtok::TokKind LexIdentifier();
Chris Lattnera8058742007-11-18 02:57:27 +0000119 bool LexInclude();
Chris Lattnerf4601652007-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 Lattnera8058742007-11-18 02:57:27 +0000125};
126
127} // end namespace llvm
128
129#endif