1 //===--------------------- StringLexer.cpp -----------------------*- C++-*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Utility/StringLexer.h" 10 11 #include <algorithm> 12 #include <assert.h> 13 14 using namespace lldb_private; 15 16 StringLexer::StringLexer(std::string s) : m_data(s), m_position(0) {} 17 18 StringLexer::Character StringLexer::Peek() { return m_data[m_position]; } 19 20 bool StringLexer::NextIf(Character c) { 21 auto val = Peek(); 22 if (val == c) { 23 Next(); 24 return true; 25 } 26 return false; 27 } 28 29 std::pair<bool, StringLexer::Character> 30 StringLexer::NextIf(std::initializer_list<Character> cs) { 31 auto val = Peek(); 32 for (auto c : cs) { 33 if (val == c) { 34 Next(); 35 return {true, c}; 36 } 37 } 38 return {false, 0}; 39 } 40 41 bool StringLexer::AdvanceIf(const std::string &token) { 42 auto pos = m_position; 43 bool matches = true; 44 for (auto c : token) { 45 if (!NextIf(c)) { 46 matches = false; 47 break; 48 } 49 } 50 if (!matches) { 51 m_position = pos; 52 return false; 53 } 54 return true; 55 } 56 57 StringLexer::Character StringLexer::Next() { 58 auto val = Peek(); 59 Consume(); 60 return val; 61 } 62 63 bool StringLexer::HasAtLeast(Size s) { 64 return (m_data.size() - m_position) >= s; 65 } 66 67 void StringLexer::PutBack(Size s) { 68 assert(m_position >= s); 69 m_position -= s; 70 } 71 72 std::string StringLexer::GetUnlexed() { 73 return std::string(m_data, m_position); 74 } 75 76 void StringLexer::Consume() { m_position++; } 77 78 StringLexer &StringLexer::operator=(const StringLexer &rhs) { 79 if (this != &rhs) { 80 m_data = rhs.m_data; 81 m_position = rhs.m_position; 82 } 83 return *this; 84 } 85