Cleaned up the StringLexer a little bit. It turns

out we only want to roll back text that was in the
buffer to begin with, so it's not necessary to
provide a pushback stack.

I'm going to use this slightly cleaner API to perform
lookahead for the Objective-C runtime type parser.

llvm-svn: 221640
This commit is contained in:
Sean Callanan 2014-11-10 23:20:52 +00:00
parent a9ae3e311c
commit 5c35f7cfd1
2 changed files with 14 additions and 27 deletions

View File

@ -27,6 +27,7 @@ public:
StringLexer (const StringLexer& rhs);
// These APIs are not bounds-checked. Use HasAtLeast() if you're not sure.
Character
Peek ();
@ -42,8 +43,9 @@ public:
bool
HasAny (Character c);
// This will assert if there are less than s characters preceding the cursor.
void
PutBack (Character c);
PutBack (Size s);
StringLexer&
operator = (const StringLexer& rhs);
@ -51,7 +53,6 @@ public:
private:
std::string m_data;
Position m_position;
std::list<Character> m_putback_data;
void
Consume();

View File

@ -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
@ -57,35 +53,26 @@ 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;
}
void
StringLexer::Consume()
{
if (m_putback_data.empty())
m_position++;
else
m_putback_data.pop_front();
m_position++;
}
StringLexer&
@ -95,7 +82,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;
}