blob: 9e694c7a3010363b67f418b2c76d3d60c8177b70 [file] [log] [blame]
Chris Lattnera59e8772009-06-21 07:19:10 +00001//===- AsmLexer.h - Lexer for Assembly 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 declares the lexer for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef ASMLEXER_H
15#define ASMLEXER_H
16
17#include "llvm/Support/DataTypes.h"
18#include <string>
19#include <cassert>
20
21namespace llvm {
22class MemoryBuffer;
23class SourceMgr;
24class SMLoc;
25
26namespace asmtok {
27 enum TokKind {
28 // Markers
29 Eof, Error,
30
31 Identifier,
Chris Lattner4651bca2009-06-21 19:21:25 +000032 Register,
Chris Lattnera59e8772009-06-21 07:19:10 +000033 IntVal,
34
Chris Lattner4651bca2009-06-21 19:21:25 +000035 EndOfStatement,
Chris Lattnera59e8772009-06-21 07:19:10 +000036 Colon,
37 Plus,
Chris Lattner4651bca2009-06-21 19:21:25 +000038 Minus,
39 Slash, // '/'
40 LParen, RParen,
41 Star, Comma, Dollar
Chris Lattnera59e8772009-06-21 07:19:10 +000042 };
43}
44
45/// AsmLexer - Lexer class for assembly files.
46class AsmLexer {
47 SourceMgr &SrcMgr;
48
49 const char *CurPtr;
50 const MemoryBuffer *CurBuf;
51
52 // Information about the current token.
53 const char *TokStart;
54 asmtok::TokKind CurKind;
55 std::string CurStrVal; // This is valid for Identifier.
56 int64_t CurIntVal;
57
58 /// CurBuffer - This is the current buffer index we're lexing from as managed
59 /// by the SourceMgr object.
60 int CurBuffer;
61
62public:
63 AsmLexer(SourceMgr &SrcMgr);
64 ~AsmLexer() {}
65
66 asmtok::TokKind Lex() {
67 return CurKind = LexToken();
68 }
69
70 asmtok::TokKind getKind() const { return CurKind; }
71
72 const std::string &getCurStrVal() const {
Chris Lattner4651bca2009-06-21 19:21:25 +000073 assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register) &&
Chris Lattnera59e8772009-06-21 07:19:10 +000074 "This token doesn't have a string value");
75 return CurStrVal;
76 }
77 int64_t getCurIntVal() const {
78 assert(CurKind == asmtok::IntVal && "This token isn't an integer");
79 return CurIntVal;
80 }
81
82 SMLoc getLoc() const;
83
84 void PrintError(const char *Loc, const std::string &Msg) const;
85 void PrintError(SMLoc Loc, const std::string &Msg) const;
86
87private:
88 int getNextChar();
Chris Lattner4651bca2009-06-21 19:21:25 +000089 asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
Chris Lattnera59e8772009-06-21 07:19:10 +000090
91 /// LexToken - Read the next token and return its code.
92 asmtok::TokKind LexToken();
Chris Lattner4651bca2009-06-21 19:21:25 +000093 asmtok::TokKind LexIdentifier();
94 asmtok::TokKind LexPercent();
95 asmtok::TokKind LexSlash();
96 asmtok::TokKind LexHash();
97 asmtok::TokKind LexDigit();
Chris Lattnera59e8772009-06-21 07:19:10 +000098};
99
100} // end namespace llvm
101
102#endif