1//===- TGLexer.h - Lexer for TableGen Files ---------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class represents the Lexer for tablegen files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef TGLEXER_H
15#define TGLEXER_H
16
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/DataTypes.h"
19#include "llvm/Support/SMLoc.h"
20#include <cassert>
21#include <map>
22#include <string>
23
24namespace llvm {
25class SourceMgr;
26class SMLoc;
27class Twine;
28
29namespace 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,      // < >
40    colon, semi,        // : ;
41    comma, period,      // , .
42    equal, question,    // = ?
43    paste,              // #
44
45    // Keywords.
46    Bit, Bits, Class, Code, Dag, Def, Foreach, Defm, Field, In, Int, Let, List,
47    MultiClass, String,
48
49    // !keywords.
50    XConcat, XADD, XSRA, XSRL, XSHL, XListConcat, XStrConcat, XCast, XSubst,
51    XForEach, XHead, XTail, XEmpty, XIf, XEq,
52
53    // Integer value.
54    IntVal,
55
56    // String valued tokens.
57    Id, StrVal, VarName, CodeFragment
58  };
59}
60
61/// TGLexer - TableGen Lexer class.
62class TGLexer {
63  SourceMgr &SrcMgr;
64
65  const char *CurPtr;
66  StringRef CurBuf;
67
68  // 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
72  int64_t CurIntVal;      // This is valid for INTVAL.
73
74  /// CurBuffer - This is the current buffer index we're lexing from as managed
75  /// by the SourceMgr object.
76  unsigned CurBuffer;
77
78public:
79  typedef std::map<std::string, SMLoc> DependenciesMapTy;
80private:
81  /// Dependencies - This is the list of all included files.
82  DependenciesMapTy Dependencies;
83
84public:
85  TGLexer(SourceMgr &SrcMgr);
86  ~TGLexer() {}
87
88  tgtok::TokKind Lex() {
89    return CurCode = LexToken();
90  }
91
92  const DependenciesMapTy &getDependencies() const {
93    return Dependencies;
94  }
95
96  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  }
104  int64_t getCurIntVal() const {
105    assert(CurCode == tgtok::IntVal && "This token isn't an integer");
106    return CurIntVal;
107  }
108
109  SMLoc getLoc() const;
110
111private:
112  /// LexToken - Read the next token and return its code.
113  tgtok::TokKind LexToken();
114
115  tgtok::TokKind ReturnError(const char *Loc, const Twine &Msg);
116
117  int getNextChar();
118  int peekNextChar(int Index);
119  void SkipBCPLComment();
120  bool SkipCComment();
121  tgtok::TokKind LexIdentifier();
122  bool LexInclude();
123  tgtok::TokKind LexString();
124  tgtok::TokKind LexVarName();
125  tgtok::TokKind LexNumber();
126  tgtok::TokKind LexBracket();
127  tgtok::TokKind LexExclaim();
128};
129
130} // end namespace llvm
131
132#endif
133