Skip to content

Commit ae7ab8e

Browse files
authored
[Lexer] Implement lexing C99 numbers (#13)
Fixes #4
1 parent ba357af commit ae7ab8e

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

lldb/include/lldb/ValueObject/DILLexer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ class DILLexer {
223223

224224
bool Is_Word(std::string::iterator start, uint32_t& length);
225225

226+
void ConsumeNumberBody(uint32_t &length, char &prev_ch);
226227
bool Is_Number(std::string::iterator start, uint32_t& length,
227228
dil::NumberKind& kind);
228229

lldb/source/ValueObject/DILLexer.cpp

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,29 +134,44 @@ bool DILLexer::Is_Word(std::string::iterator start, uint32_t& length) {
134134
return false;
135135
}
136136

137+
void DILLexer::ConsumeNumberBody(uint32_t &length, char &prev_ch) {
138+
while (m_cur_pos != m_expr.end() &&
139+
(Is_Digit(*m_cur_pos) || Is_Letter(*m_cur_pos) || *m_cur_pos == '_')) {
140+
prev_ch = *m_cur_pos;
141+
length++;
142+
m_cur_pos++;
143+
}
144+
}
145+
137146
bool DILLexer::Is_Number(std::string::iterator start, uint32_t& length,
138147
dil::NumberKind& kind) {
139-
140-
if (Is_Digit(*start)) {
141-
while (m_cur_pos != m_expr.end() && Is_Digit(*m_cur_pos)) {
148+
char prev_ch = 0;
149+
kind = dil::NumberKind::eInteger;
150+
if (*start == '.') {
151+
auto next_pos = start + 1;
152+
if (next_pos == m_expr.end() || !Is_Digit(*next_pos))
153+
return false;
154+
}
155+
if (Is_Digit(*start) || *start == '.') {
156+
ConsumeNumberBody(length, prev_ch);
157+
// We're not at the end of the string, and we should be looking at a '.'
158+
if (*m_cur_pos == '.') {
159+
kind = dil::NumberKind::eFloat;
160+
prev_ch = *m_cur_pos;
142161
length++;
143162
m_cur_pos++;
163+
ConsumeNumberBody(length, prev_ch);
144164
}
145-
if (m_cur_pos == m_expr.end() || (*m_cur_pos != '.')) {
146-
kind = dil::NumberKind::eInteger;
147-
return true;
148-
}
149-
// We're not at the end of the string, and we should be looking at a '.'
150-
if (*m_cur_pos == '.') {
165+
// Check the exponent part
166+
if ((*m_cur_pos == '-' || *m_cur_pos == '+') &&
167+
(prev_ch == 'E' || prev_ch == 'e' || prev_ch == 'P' ||
168+
prev_ch == 'p')) {
169+
prev_ch = *m_cur_pos;
151170
length++;
152171
m_cur_pos++;
153-
while (m_cur_pos != m_expr.end() && Is_Digit(*m_cur_pos)) {
154-
length++;
155-
m_cur_pos++;
156-
}
157-
kind = dil::NumberKind::eFloat;
158-
return true;
172+
ConsumeNumberBody(length, prev_ch);
159173
}
174+
return true;
160175
}
161176
return false;
162177
}

0 commit comments

Comments
 (0)