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

View File

@ -10,28 +10,24 @@
#include "lldb/Utility/StringLexer.h" #include "lldb/Utility/StringLexer.h"
#include <algorithm> #include <algorithm>
#include <assert.h>
using namespace lldb_utility; using namespace lldb_utility;
StringLexer::StringLexer (std::string s) : StringLexer::StringLexer (std::string s) :
m_data(s), m_data(s),
m_position(0), m_position(0)
m_putback_data()
{ } { }
StringLexer::StringLexer (const StringLexer& rhs) : StringLexer::StringLexer (const StringLexer& rhs) :
m_data(rhs.m_data), m_data(rhs.m_data),
m_position(rhs.m_position), m_position(rhs.m_position)
m_putback_data(rhs.m_putback_data)
{ } { }
StringLexer::Character StringLexer::Character
StringLexer::Peek () StringLexer::Peek ()
{ {
if (m_putback_data.empty()) return m_data[m_position];
return m_data[m_position];
else
return m_putback_data.front();
} }
bool bool
@ -57,35 +53,26 @@ StringLexer::Next ()
bool bool
StringLexer::HasAtLeast (Size s) StringLexer::HasAtLeast (Size s)
{ {
auto in_m_data = m_data.size()-m_position; return (m_data.size() - m_position) >= s;
auto in_putback = m_putback_data.size();
return (in_m_data + in_putback >= s);
} }
void void
StringLexer::PutBack (Character c) StringLexer::PutBack (Size s)
{ {
m_putback_data.push_back(c); assert (m_position >= s);
m_position -= s;
} }
bool bool
StringLexer::HasAny (Character c) 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; return m_data.find(c, m_position) != std::string::npos;
} }
void void
StringLexer::Consume() StringLexer::Consume()
{ {
if (m_putback_data.empty()) m_position++;
m_position++;
else
m_putback_data.pop_front();
} }
StringLexer& StringLexer&
@ -95,7 +82,6 @@ StringLexer::operator = (const StringLexer& rhs)
{ {
m_data = rhs.m_data; m_data = rhs.m_data;
m_position = rhs.m_position; m_position = rhs.m_position;
m_putback_data = rhs.m_putback_data;
} }
return *this; return *this;
} }