LLDB
mainline
llvm-project
lldb
source
Utility
StringLexer.cpp
Go to the documentation of this file.
1
//===-- StringLexer.cpp ---------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "
lldb/Utility/StringLexer.h
"
10
11
#include <algorithm>
12
#include <cassert>
13
#include <utility>
14
15
using namespace
lldb_private
;
16
17
StringLexer::StringLexer
(std::string s) :
m_data
(std::move(s)),
m_position
(0) {}
18
19
StringLexer::Character
StringLexer::Peek
() {
return
m_data
[
m_position
]; }
20
21
bool
StringLexer::NextIf
(
Character
c) {
22
auto
val =
Peek
();
23
if
(val == c) {
24
Next
();
25
return
true
;
26
}
27
return
false
;
28
}
29
30
std::pair<bool, StringLexer::Character>
31
StringLexer::NextIf
(std::initializer_list<Character> cs) {
32
auto
val =
Peek
();
33
for
(
auto
c : cs) {
34
if
(val == c) {
35
Next
();
36
return
{
true
, c};
37
}
38
}
39
return
{
false
, 0};
40
}
41
42
bool
StringLexer::AdvanceIf
(
const
std::string &token) {
43
auto
pos =
m_position
;
44
bool
matches =
true
;
45
for
(
auto
c : token) {
46
if
(!
NextIf
(c)) {
47
matches =
false
;
48
break
;
49
}
50
}
51
if
(!matches) {
52
m_position
= pos;
53
return
false
;
54
}
55
return
true
;
56
}
57
58
StringLexer::Character
StringLexer::Next
() {
59
auto
val =
Peek
();
60
Consume
();
61
return
val;
62
}
63
64
bool
StringLexer::HasAtLeast
(
Size
s) {
65
return
(
m_data
.size() -
m_position
) >= s;
66
}
67
68
void
StringLexer::PutBack
(
Size
s) {
69
assert(
m_position
>= s);
70
m_position
-= s;
71
}
72
73
std::string
StringLexer::GetUnlexed
() {
74
return
std::string(
m_data
,
m_position
);
75
}
76
77
void
StringLexer::Consume
() {
m_position
++; }
78
79
StringLexer
&
StringLexer::operator=
(
const
StringLexer
&rhs) {
80
if
(
this
!= &rhs) {
81
m_data
= rhs.
m_data
;
82
m_position
= rhs.
m_position
;
83
}
84
return
*
this
;
85
}
StringLexer.h
lldb_private::StringLexer::AdvanceIf
bool AdvanceIf(const std::string &token)
Definition
StringLexer.cpp:42
lldb_private::StringLexer::m_position
Position m_position
Definition
StringLexer.h:49
lldb_private::StringLexer::m_data
std::string m_data
Definition
StringLexer.h:48
lldb_private::StringLexer::Peek
Character Peek()
Definition
StringLexer.cpp:19
lldb_private::StringLexer::operator=
StringLexer & operator=(const StringLexer &rhs)
Definition
StringLexer.cpp:79
lldb_private::StringLexer::GetUnlexed
std::string GetUnlexed()
Definition
StringLexer.cpp:73
lldb_private::StringLexer::Character
std::string::value_type Character
Definition
StringLexer.h:23
lldb_private::StringLexer::NextIf
bool NextIf(Character c)
Definition
StringLexer.cpp:21
lldb_private::StringLexer::Consume
void Consume()
Definition
StringLexer.cpp:77
lldb_private::StringLexer::Size
std::string::size_type Size
Definition
StringLexer.h:21
lldb_private::StringLexer::HasAtLeast
bool HasAtLeast(Size s)
Definition
StringLexer.cpp:64
lldb_private::StringLexer::PutBack
void PutBack(Size s)
Definition
StringLexer.cpp:68
lldb_private::StringLexer::Next
Character Next()
Definition
StringLexer.cpp:58
lldb_private::StringLexer::StringLexer
StringLexer(std::string s)
Definition
StringLexer.cpp:17
lldb_private
A class that represents a running process on the host machine.
Definition
SBAddressRange.h:14
Generated on
for LLDB by
1.14.0