blob: c5d1722e491db90e06932e76286f55791b8c1d6f [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
Chris Lattner10a907d2009-06-21 19:56:35 +000031 // String values.
Chris Lattnera59e8772009-06-21 07:19:10 +000032 Identifier,
Chris Lattner4651bca2009-06-21 19:21:25 +000033 Register,
Chris Lattner10a907d2009-06-21 19:56:35 +000034 String,
35
36 // Integer values.
Chris Lattnera59e8772009-06-21 07:19:10 +000037 IntVal,
38
Chris Lattner10a907d2009-06-21 19:56:35 +000039 // No-value.
Chris Lattner4651bca2009-06-21 19:21:25 +000040 EndOfStatement,
Chris Lattnera59e8772009-06-21 07:19:10 +000041 Colon,
42 Plus,
Chris Lattner4651bca2009-06-21 19:21:25 +000043 Minus,
44 Slash, // '/'
45 LParen, RParen,
46 Star, Comma, Dollar
Chris Lattnera59e8772009-06-21 07:19:10 +000047 };
48}
49
50/// AsmLexer - Lexer class for assembly files.
51class AsmLexer {
52 SourceMgr &SrcMgr;
53
54 const char *CurPtr;
55 const MemoryBuffer *CurBuf;
56
57 // Information about the current token.
58 const char *TokStart;
59 asmtok::TokKind CurKind;
60 std::string CurStrVal; // This is valid for Identifier.
61 int64_t CurIntVal;
62
63 /// CurBuffer - This is the current buffer index we're lexing from as managed
64 /// by the SourceMgr object.
65 int CurBuffer;
66
67public:
68 AsmLexer(SourceMgr &SrcMgr);
69 ~AsmLexer() {}
70
71 asmtok::TokKind Lex() {
72 return CurKind = LexToken();
73 }
74
75 asmtok::TokKind getKind() const { return CurKind; }
76
77 const std::string &getCurStrVal() const {
Chris Lattner10a907d2009-06-21 19:56:35 +000078 assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register ||
79 CurKind == asmtok::String) &&
Chris Lattnera59e8772009-06-21 07:19:10 +000080 "This token doesn't have a string value");
81 return CurStrVal;
82 }
83 int64_t getCurIntVal() const {
84 assert(CurKind == asmtok::IntVal && "This token isn't an integer");
85 return CurIntVal;
86 }
87
88 SMLoc getLoc() const;
89
90 void PrintError(const char *Loc, const std::string &Msg) const;
91 void PrintError(SMLoc Loc, const std::string &Msg) const;
92
93private:
94 int getNextChar();
Chris Lattner4651bca2009-06-21 19:21:25 +000095 asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
Chris Lattnera59e8772009-06-21 07:19:10 +000096
97 /// LexToken - Read the next token and return its code.
98 asmtok::TokKind LexToken();
Chris Lattner4651bca2009-06-21 19:21:25 +000099 asmtok::TokKind LexIdentifier();
100 asmtok::TokKind LexPercent();
101 asmtok::TokKind LexSlash();
102 asmtok::TokKind LexHash();
103 asmtok::TokKind LexDigit();
Chris Lattner10a907d2009-06-21 19:56:35 +0000104 asmtok::TokKind LexQuote();
Chris Lattnera59e8772009-06-21 07:19:10 +0000105};
106
107} // end namespace llvm
108
109#endif