2017-04-07 02:12:24 +08:00
|
|
|
//===--------------------- StringLexer.cpp -----------------------*- C++-*-===//
|
2014-08-08 03:22:14 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-08-08 03:22:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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
|
|
|
|
2019-08-21 08:50:46 +08:00
|
|
|
using namespace lldb_private;
|
2014-08-08 03:22:14 +08:00
|
|
|
|
|
|
|
StringLexer::StringLexer(std::string s) : m_data(s), m_position(0) {}
|
|
|
|
|
2014-11-11 07:20:52 +08:00
|
|
|
StringLexer::Character StringLexer::Peek() { return m_data[m_position]; }
|
2014-08-08 03:22:14 +08:00
|
|
|
|
|
|
|
bool StringLexer::NextIf(Character c) {
|
|
|
|
auto val = Peek();
|
|
|
|
if (val == c) {
|
|
|
|
Next();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-15 06:58:11 +08:00
|
|
|
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};
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-11-15 06:58:11 +08:00
|
|
|
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;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-11-15 06:58:11 +08:00
|
|
|
if (!matches) {
|
|
|
|
m_position = pos;
|
|
|
|
return false;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-11-15 06:58:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-08 03:22:14 +08:00
|
|
|
StringLexer::Character StringLexer::Next() {
|
|
|
|
auto val = Peek();
|
|
|
|
Consume();
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StringLexer::HasAtLeast(Size s) {
|
2014-11-11 07:20:52 +08:00
|
|
|
return (m_data.size() - m_position) >= s;
|
2014-08-08 03:22:14 +08:00
|
|
|
}
|
|
|
|
|
2014-11-11 07:20:52 +08:00
|
|
|
void StringLexer::PutBack(Size s) {
|
|
|
|
assert(m_position >= s);
|
|
|
|
m_position -= s;
|
2014-08-08 03:22:14 +08:00
|
|
|
}
|
|
|
|
|
2014-11-15 06:58:11 +08:00
|
|
|
std::string StringLexer::GetUnlexed() {
|
|
|
|
return std::string(m_data, m_position);
|
|
|
|
}
|
|
|
|
|
2014-08-08 03:22:14 +08:00
|
|
|
void StringLexer::Consume() { m_position++; }
|
|
|
|
|
|
|
|
StringLexer &StringLexer::operator=(const StringLexer &rhs) {
|
|
|
|
if (this != &rhs) {
|
|
|
|
m_data = rhs.m_data;
|
|
|
|
m_position = rhs.m_position;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|