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