2016-09-07 04:57:50 +08:00
|
|
|
//===--------------------- StringLexer.cpp -----------------------*- C++
|
|
|
|
//-*-===//
|
2014-08-08 03:22:14 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Utility/StringLexer.h"
|
|
|
|
|
2014-08-24 02:22:09 +08:00
|
|
|
#include <algorithm>
|
2014-11-11 07:20:52 +08:00
|
|
|
#include <assert.h>
|
2014-08-24 02:22:09 +08:00
|
|
|
|
2014-08-08 03:22:14 +08:00
|
|
|
using namespace lldb_utility;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
StringLexer::StringLexer(std::string s) : m_data(s), m_position(0) {}
|
2014-08-08 03:22:14 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
StringLexer::StringLexer(const StringLexer &rhs)
|
|
|
|
: m_data(rhs.m_data), m_position(rhs.m_position) {}
|
2014-08-08 03:22:14 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
StringLexer::Character StringLexer::Peek() { return m_data[m_position]; }
|
2014-08-08 03:22:14 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool StringLexer::NextIf(Character c) {
|
|
|
|
auto val = Peek();
|
|
|
|
if (val == c) {
|
|
|
|
Next();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-08-08 03:22:14 +08:00
|
|
|
}
|
|
|
|
|
2014-11-15 06:58:11 +08:00
|
|
|
std::pair<bool, StringLexer::Character>
|
2016-09-07 04:57:50 +08:00
|
|
|
StringLexer::NextIf(std::initializer_list<Character> cs) {
|
|
|
|
auto val = Peek();
|
|
|
|
for (auto c : cs) {
|
|
|
|
if (val == c) {
|
|
|
|
Next();
|
|
|
|
return {true, c};
|
2014-11-15 06:58:11 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
return {false, 0};
|
2014-11-15 06:58:11 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool StringLexer::AdvanceIf(const std::string &token) {
|
|
|
|
auto pos = m_position;
|
|
|
|
bool matches = true;
|
|
|
|
for (auto c : token) {
|
|
|
|
if (!NextIf(c)) {
|
|
|
|
matches = false;
|
|
|
|
break;
|
2014-11-15 06:58:11 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
if (!matches) {
|
|
|
|
m_position = pos;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-11-15 06:58:11 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
StringLexer::Character StringLexer::Next() {
|
|
|
|
auto val = Peek();
|
|
|
|
Consume();
|
|
|
|
return val;
|
2014-08-08 03:22:14 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool StringLexer::HasAtLeast(Size s) {
|
|
|
|
return (m_data.size() - m_position) >= s;
|
2014-08-08 03:22:14 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void StringLexer::PutBack(Size s) {
|
|
|
|
assert(m_position >= s);
|
|
|
|
m_position -= s;
|
2014-08-08 03:22:14 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool StringLexer::HasAny(Character c) {
|
|
|
|
return m_data.find(c, m_position) != std::string::npos;
|
2014-08-08 03:22:14 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
std::string StringLexer::GetUnlexed() {
|
|
|
|
return std::string(m_data, m_position);
|
2014-11-15 06:58:11 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void StringLexer::Consume() { m_position++; }
|
2014-08-08 03:22:14 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
StringLexer &StringLexer::operator=(const StringLexer &rhs) {
|
|
|
|
if (this != &rhs) {
|
|
|
|
m_data = rhs.m_data;
|
|
|
|
m_position = rhs.m_position;
|
|
|
|
}
|
|
|
|
return *this;
|
2014-08-08 03:22:14 +08:00
|
|
|
}
|