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 17 StringLexer::StringLexer (std::string s) : 18 m_data(s), 19 m_position(0) 20 { } 21 22 StringLexer::StringLexer (const StringLexer& rhs) : 23 m_data(rhs.m_data), 24 m_position(rhs.m_position) 25 { } 26 27 StringLexer::Character 28 StringLexer::Peek () 29 { 30 return m_data[m_position]; 31 } 32 33 bool 34 StringLexer::NextIf (Character c) 35 { 36 auto val = Peek(); 37 if (val == c) 38 { 39 Next(); 40 return true; 41 } 42 return false; 43 } 44 45 std::pair<bool, StringLexer::Character> 46 StringLexer::NextIf (std::initializer_list<Character> cs) 47 { 48 auto val = Peek(); 49 for (auto c : cs) 50 { 51 if (val == c) 52 { 53 Next(); 54 return {true,c}; 55 } 56 } 57 return {false,0}; 58 } 59 60 bool 61 StringLexer::AdvanceIf (const std::string& token) 62 { 63 auto pos = m_position; 64 bool matches = true; 65 for (auto c : token) 66 { 67 if (!NextIf(c)) 68 { 69 matches = false; 70 break; 71 } 72 } 73 if (!matches) 74 { 75 m_position = pos; 76 return false; 77 } 78 return true; 79 } 80 81 StringLexer::Character 82 StringLexer::Next () 83 { 84 auto val = Peek(); 85 Consume(); 86 return val; 87 } 88 89 bool 90 StringLexer::HasAtLeast (Size s) 91 { 92 return (m_data.size() - m_position) >= s; 93 } 94 95 void 96 StringLexer::PutBack (Size s) 97 { 98 assert (m_position >= s); 99 m_position -= s; 100 } 101 102 bool 103 StringLexer::HasAny (Character c) 104 { 105 return m_data.find(c, m_position) != std::string::npos; 106 } 107 108 std::string 109 StringLexer::GetUnlexed () 110 { 111 return std::string(m_data, m_position); 112 } 113 114 void 115 StringLexer::Consume() 116 { 117 m_position++; 118 } 119 120 StringLexer& 121 StringLexer::operator = (const StringLexer& rhs) 122 { 123 if (this != &rhs) 124 { 125 m_data = rhs.m_data; 126 m_position = rhs.m_position; 127 } 128 return *this; 129 } 130