summaryrefslogtreecommitdiff
path: root/source/Utility/StringLexer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/Utility/StringLexer.cpp')
-rw-r--r--source/Utility/StringLexer.cpp78
1 files changed, 53 insertions, 25 deletions
diff --git a/source/Utility/StringLexer.cpp b/source/Utility/StringLexer.cpp
index bde2fc6a4202..2f62d2cedb40 100644
--- a/source/Utility/StringLexer.cpp
+++ b/source/Utility/StringLexer.cpp
@@ -10,28 +10,24 @@
#include "lldb/Utility/StringLexer.h"
#include <algorithm>
+#include <assert.h>
using namespace lldb_utility;
StringLexer::StringLexer (std::string s) :
-m_data(s),
-m_position(0),
-m_putback_data()
+ m_data(s),
+ m_position(0)
{ }
StringLexer::StringLexer (const StringLexer& rhs) :
-m_data(rhs.m_data),
-m_position(rhs.m_position),
-m_putback_data(rhs.m_putback_data)
+ m_data(rhs.m_data),
+ m_position(rhs.m_position)
{ }
StringLexer::Character
StringLexer::Peek ()
{
- if (m_putback_data.empty())
- return m_data[m_position];
- else
- return m_putback_data.front();
+ return m_data[m_position];
}
bool
@@ -46,6 +42,42 @@ StringLexer::NextIf (Character c)
return false;
}
+std::pair<bool, StringLexer::Character>
+StringLexer::NextIf (std::initializer_list<Character> cs)
+{
+ auto val = Peek();
+ for (auto c : cs)
+ {
+ if (val == c)
+ {
+ Next();
+ return {true,c};
+ }
+ }
+ return {false,0};
+}
+
+bool
+StringLexer::AdvanceIf (const std::string& token)
+{
+ auto pos = m_position;
+ bool matches = true;
+ for (auto c : token)
+ {
+ if (!NextIf(c))
+ {
+ matches = false;
+ break;
+ }
+ }
+ if (!matches)
+ {
+ m_position = pos;
+ return false;
+ }
+ return true;
+}
+
StringLexer::Character
StringLexer::Next ()
{
@@ -57,35 +89,32 @@ StringLexer::Next ()
bool
StringLexer::HasAtLeast (Size s)
{
- auto in_m_data = m_data.size()-m_position;
- auto in_putback = m_putback_data.size();
- return (in_m_data + in_putback >= s);
+ return (m_data.size() - m_position) >= s;
}
-
void
-StringLexer::PutBack (Character c)
+StringLexer::PutBack (Size s)
{
- m_putback_data.push_back(c);
+ assert (m_position >= s);
+ m_position -= s;
}
bool
StringLexer::HasAny (Character c)
{
- const auto begin(m_putback_data.begin());
- const auto end(m_putback_data.end());
- if (std::find(begin, end, c) != end)
- return true;
return m_data.find(c, m_position) != std::string::npos;
}
+std::string
+StringLexer::GetUnlexed ()
+{
+ return std::string(m_data, m_position);
+}
+
void
StringLexer::Consume()
{
- if (m_putback_data.empty())
- m_position++;
- else
- m_putback_data.pop_front();
+ m_position++;
}
StringLexer&
@@ -95,7 +124,6 @@ StringLexer::operator = (const StringLexer& rhs)
{
m_data = rhs.m_data;
m_position = rhs.m_position;
- m_putback_data = rhs.m_putback_data;
}
return *this;
}