1226584Sdim//===- TGLexer.h - Lexer for TableGen Files ---------------------*- C++ -*-===//
2226584Sdim//
3226584Sdim//                     The LLVM Compiler Infrastructure
4226584Sdim//
5226584Sdim// This file is distributed under the University of Illinois Open Source
6226584Sdim// License. See LICENSE.TXT for details.
7226584Sdim//
8226584Sdim//===----------------------------------------------------------------------===//
9226584Sdim//
10226584Sdim// This class represents the Lexer for tablegen files.
11226584Sdim//
12226584Sdim//===----------------------------------------------------------------------===//
13226584Sdim
14226584Sdim#ifndef TGLEXER_H
15226584Sdim#define TGLEXER_H
16226584Sdim
17226584Sdim#include "llvm/Support/DataTypes.h"
18249423Sdim#include "llvm/Support/SMLoc.h"
19249423Sdim#include <cassert>
20249423Sdim#include <map>
21226584Sdim#include <string>
22226584Sdim
23226584Sdimnamespace llvm {
24226584Sdimclass MemoryBuffer;
25226584Sdimclass SourceMgr;
26226584Sdimclass SMLoc;
27226584Sdimclass Twine;
28226584Sdim
29226584Sdimnamespace tgtok {
30226584Sdim  enum TokKind {
31226584Sdim    // Markers
32226584Sdim    Eof, Error,
33226584Sdim
34226584Sdim    // Tokens with no info.
35226584Sdim    minus, plus,        // - +
36226584Sdim    l_square, r_square, // [ ]
37226584Sdim    l_brace, r_brace,   // { }
38226584Sdim    l_paren, r_paren,   // ( )
39226584Sdim    less, greater,      // < >
40226584Sdim    colon, semi,        // : ;
41226584Sdim    comma, period,      // , .
42226584Sdim    equal, question,    // = ?
43234353Sdim    paste,              // #
44234353Sdim
45226584Sdim    // Keywords.
46234353Sdim    Bit, Bits, Class, Code, Dag, Def, Foreach, Defm, Field, In, Int, Let, List,
47226584Sdim    MultiClass, String,
48226584Sdim
49226584Sdim    // !keywords.
50249423Sdim    XConcat, XADD, XSRA, XSRL, XSHL, XStrConcat, XCast, XSubst,
51226584Sdim    XForEach, XHead, XTail, XEmpty, XIf, XEq,
52226584Sdim
53226584Sdim    // Integer value.
54226584Sdim    IntVal,
55226584Sdim
56226584Sdim    // String valued tokens.
57226584Sdim    Id, StrVal, VarName, CodeFragment
58226584Sdim  };
59226584Sdim}
60226584Sdim
61226584Sdim/// TGLexer - TableGen Lexer class.
62226584Sdimclass TGLexer {
63226584Sdim  SourceMgr &SrcMgr;
64226584Sdim
65226584Sdim  const char *CurPtr;
66226584Sdim  const MemoryBuffer *CurBuf;
67226584Sdim
68226584Sdim  // Information about the current token.
69226584Sdim  const char *TokStart;
70226584Sdim  tgtok::TokKind CurCode;
71226584Sdim  std::string CurStrVal;  // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT
72226584Sdim  int64_t CurIntVal;      // This is valid for INTVAL.
73226584Sdim
74226584Sdim  /// CurBuffer - This is the current buffer index we're lexing from as managed
75226584Sdim  /// by the SourceMgr object.
76226584Sdim  int CurBuffer;
77249423Sdim
78249423Sdimpublic:
79249423Sdim  typedef std::map<std::string, SMLoc> DependenciesMapTy;
80249423Sdimprivate:
81226584Sdim  /// Dependencies - This is the list of all included files.
82249423Sdim  DependenciesMapTy Dependencies;
83249423Sdim
84226584Sdimpublic:
85226584Sdim  TGLexer(SourceMgr &SrcMgr);
86226584Sdim  ~TGLexer() {}
87226584Sdim
88226584Sdim  tgtok::TokKind Lex() {
89226584Sdim    return CurCode = LexToken();
90226584Sdim  }
91226584Sdim
92249423Sdim  const DependenciesMapTy &getDependencies() const {
93226584Sdim    return Dependencies;
94226584Sdim  }
95226584Sdim
96226584Sdim  tgtok::TokKind getCode() const { return CurCode; }
97226584Sdim
98226584Sdim  const std::string &getCurStrVal() const {
99226584Sdim    assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal ||
100226584Sdim            CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
101226584Sdim           "This token doesn't have a string value");
102226584Sdim    return CurStrVal;
103226584Sdim  }
104226584Sdim  int64_t getCurIntVal() const {
105226584Sdim    assert(CurCode == tgtok::IntVal && "This token isn't an integer");
106226584Sdim    return CurIntVal;
107226584Sdim  }
108226584Sdim
109226584Sdim  SMLoc getLoc() const;
110226584Sdim
111226584Sdimprivate:
112226584Sdim  /// LexToken - Read the next token and return its code.
113226584Sdim  tgtok::TokKind LexToken();
114226584Sdim
115226584Sdim  tgtok::TokKind ReturnError(const char *Loc, const Twine &Msg);
116226584Sdim
117226584Sdim  int getNextChar();
118234353Sdim  int peekNextChar(int Index);
119226584Sdim  void SkipBCPLComment();
120226584Sdim  bool SkipCComment();
121226584Sdim  tgtok::TokKind LexIdentifier();
122226584Sdim  bool LexInclude();
123226584Sdim  tgtok::TokKind LexString();
124226584Sdim  tgtok::TokKind LexVarName();
125226584Sdim  tgtok::TokKind LexNumber();
126226584Sdim  tgtok::TokKind LexBracket();
127226584Sdim  tgtok::TokKind LexExclaim();
128226584Sdim};
129226584Sdim
130226584Sdim} // end namespace llvm
131226584Sdim
132226584Sdim#endif
133