blob: 734dc2651e33d397e188d61a9ac2dd8949c20393 [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 Lattner1c8ae592009-03-13 16:01:53 +000026class TGLoc;
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 Greenec7cafcd2009-04-22 20:18:10 +000048 XConcat, XSRA, XSRL, XSHL, XStrConcat, XNameConcat,
49
Chris Lattnerf4601652007-11-22 20:49:04 +000050 // Integer value.
51 IntVal,
52
53 // String valued tokens.
54 Id, StrVal, VarName, CodeFragment
55 };
56}
Chris Lattnera8058742007-11-18 02:57:27 +000057
Chris Lattnerf4601652007-11-22 20:49:04 +000058/// TGLexer - TableGen Lexer class.
Chris Lattnera8058742007-11-18 02:57:27 +000059class TGLexer {
Chris Lattneraa739d22009-03-13 07:05:43 +000060 TGSourceMgr &SrcMgr;
61
Chris Lattnera8058742007-11-18 02:57:27 +000062 const char *CurPtr;
Chris Lattneraa739d22009-03-13 07:05:43 +000063 const MemoryBuffer *CurBuf;
Chris Lattnera8058742007-11-18 02:57:27 +000064
Chris Lattnerf4601652007-11-22 20:49:04 +000065 // Information about the current token.
66 const char *TokStart;
67 tgtok::TokKind CurCode;
68 std::string CurStrVal; // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT
Dan Gohman63f97202008-10-17 01:33:43 +000069 int64_t CurIntVal; // This is valid for INTVAL.
Chris Lattneraa739d22009-03-13 07:05:43 +000070
71 /// CurBuffer - This is the current buffer index we're lexing from as managed
72 /// by the SourceMgr object.
73 int CurBuffer;
Chris Lattnera8058742007-11-18 02:57:27 +000074
75 // IncludeDirectories - This is the list of directories we should search for
76 // include files in.
77 std::vector<std::string> IncludeDirectories;
Chris Lattnera8058742007-11-18 02:57:27 +000078public:
Chris Lattneraa739d22009-03-13 07:05:43 +000079 TGLexer(TGSourceMgr &SrcMgr);
80 ~TGLexer() {}
Chris Lattnera8058742007-11-18 02:57:27 +000081
82 void setIncludeDirs(const std::vector<std::string> &Dirs) {
83 IncludeDirectories = Dirs;
84 }
85
Chris Lattnerf4601652007-11-22 20:49:04 +000086 tgtok::TokKind Lex() {
87 return CurCode = LexToken();
88 }
Chris Lattnera8058742007-11-18 02:57:27 +000089
Chris Lattnerf4601652007-11-22 20:49:04 +000090 tgtok::TokKind getCode() const { return CurCode; }
91
92 const std::string &getCurStrVal() const {
93 assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal ||
94 CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
95 "This token doesn't have a string value");
96 return CurStrVal;
97 }
Dan Gohman63f97202008-10-17 01:33:43 +000098 int64_t getCurIntVal() const {
Chris Lattnerf4601652007-11-22 20:49:04 +000099 assert(CurCode == tgtok::IntVal && "This token isn't an integer");
100 return CurIntVal;
101 }
102
Chris Lattner1c8ae592009-03-13 16:01:53 +0000103 TGLoc getLoc() const;
Chris Lattnerf4601652007-11-22 20:49:04 +0000104
Chris Lattner1c8ae592009-03-13 16:01:53 +0000105 void PrintError(const char *Loc, const std::string &Msg) const;
106 void PrintError(TGLoc Loc, const std::string &Msg) const;
Chris Lattnerf4601652007-11-22 20:49:04 +0000107
Chris Lattnera8058742007-11-18 02:57:27 +0000108private:
Chris Lattnerf4601652007-11-22 20:49:04 +0000109 /// LexToken - Read the next token and return its code.
110 tgtok::TokKind LexToken();
111
112 tgtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000113
Chris Lattnera8058742007-11-18 02:57:27 +0000114 int getNextChar();
115 void SkipBCPLComment();
116 bool SkipCComment();
Chris Lattnerf4601652007-11-22 20:49:04 +0000117 tgtok::TokKind LexIdentifier();
Chris Lattnera8058742007-11-18 02:57:27 +0000118 bool LexInclude();
Chris Lattnerf4601652007-11-22 20:49:04 +0000119 tgtok::TokKind LexString();
120 tgtok::TokKind LexVarName();
121 tgtok::TokKind LexNumber();
122 tgtok::TokKind LexBracket();
123 tgtok::TokKind LexExclaim();
Chris Lattnera8058742007-11-18 02:57:27 +0000124};
125
126} // end namespace llvm
127
128#endif