This repository was archived by the owner on May 21, 2019. It is now read-only.
forked from llvm-mirror/lldb
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRustLex.h
168 lines (139 loc) · 3.21 KB
/
RustLex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//===-- RustLex.h -------------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_RustLex_h
#define liblldb_RustLex_h
#include <memory>
#include "lldb/lldb-forward.h"
#include "lldb/lldb-private.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include "lldb/Utility/Stream.h"
namespace lldb_private {
namespace rust {
// Note that single-character tokens are represented by the character
// itself.
enum TokenKind {
STRING = 128,
BYTESTRING,
CHAR,
BYTE,
FLOAT,
INTEGER,
AS,
TRUE,
FALSE,
SUPER,
SELF,
MUT,
CONST,
FN,
SIZEOF,
DOTDOT,
DOTDOTEQ,
OROR,
ANDAND,
EQEQ,
NOTEQ,
LTEQ,
GTEQ,
LSH,
RSH,
PLUS_EQ,
MINUS_EQ,
SLASH_EQ,
STAR_EQ,
PERCENT_EQ,
RSH_EQ,
LSH_EQ,
AND_EQ,
OR_EQ,
XOR_EQ,
COLONCOLON,
ARROW,
IDENTIFIER,
INVALID,
THATSALLFOLKS
};
void PrintTokenKind(Stream &stream, int kind);
struct Token {
int kind;
llvm::Optional<uint64_t> uinteger;
llvm::Optional<double> dvalue;
// This can be NULL if no suffix was specified.
const char *number_suffix = nullptr;
std::string str;
explicit Token(int kind_)
: kind(kind_)
{
}
Token(int kind_, uint64_t val_, const char *suffix_ = nullptr)
: kind(kind_),
uinteger(val_),
number_suffix(suffix_)
{
}
Token(int kind_, double val_, const char *suffix_ = nullptr)
: kind(kind_),
dvalue(val_),
number_suffix(suffix_)
{
}
Token(int kind_, std::string &&str_)
: kind(kind_),
str(std::move(str_))
{
}
bool operator==(const Token &other) const {
if (kind != other.kind ||
uinteger != other.uinteger ||
dvalue != other.dvalue ||
str != other.str) {
return false;
}
if (number_suffix == nullptr) {
return other.number_suffix == nullptr;
}
if (other.number_suffix == nullptr) {
return false;
}
return strcmp(number_suffix, other.number_suffix) == 0;
}
};
class Lexer {
public:
explicit Lexer(llvm::StringRef ref)
: m_iter(ref.begin()),
m_end(ref.end())
{
}
Token Next();
private:
const char *CheckSuffix(const char *const *suffixes);
bool BasicInteger(int *radix_out, std::string *value);
Token MaybeByteLiteral();
Token MaybeRawString(bool is_byte = false);
Token String(bool is_byte = false);
Token Character(bool is_byte = false);
Token Operator();
int Float(std::string *value);
Token Number();
Token Identifier();
bool AppendEscape(std::string *result, bool is_byte);
bool ParseHex(uint64_t *result, int min_digits, int max_digits);
bool ParseEscape(uint64_t *result, bool is_byte);
bool Lookup(const ::llvm::StringRef &str, int *result);
llvm::StringRef::iterator m_iter;
llvm::StringRef::iterator m_end;
size_t Remaining() const { return m_end - m_iter; }
static llvm::StringMap<TokenKind> *Keywords();
static llvm::StringMap<TokenKind> *m_keywords;
};
} // namespace rust
} // namespace lldb_private
#endif // liblldb_RustLex_h