1*f678e45dSDimitry Andric //===--------------------- StringLexer.cpp -----------------------*- C++-*-===// 20127ef0fSEd Maste // 30127ef0fSEd Maste // The LLVM Compiler Infrastructure 40127ef0fSEd Maste // 50127ef0fSEd Maste // This file is distributed under the University of Illinois Open Source 60127ef0fSEd Maste // License. See LICENSE.TXT for details. 70127ef0fSEd Maste // 80127ef0fSEd Maste //===----------------------------------------------------------------------===// 90127ef0fSEd Maste 100127ef0fSEd Maste #include "lldb/Utility/StringLexer.h" 110127ef0fSEd Maste 120127ef0fSEd Maste #include <algorithm> 137aa51b79SEd Maste #include <assert.h> 140127ef0fSEd Maste 150127ef0fSEd Maste using namespace lldb_utility; 160127ef0fSEd Maste StringLexer(std::string s)17435933ddSDimitry AndricStringLexer::StringLexer(std::string s) : m_data(s), m_position(0) {} 180127ef0fSEd Maste StringLexer(const StringLexer & rhs)19435933ddSDimitry AndricStringLexer::StringLexer(const StringLexer &rhs) 20435933ddSDimitry Andric : m_data(rhs.m_data), m_position(rhs.m_position) {} 210127ef0fSEd Maste Peek()22435933ddSDimitry AndricStringLexer::Character StringLexer::Peek() { return m_data[m_position]; } 230127ef0fSEd Maste NextIf(Character c)24435933ddSDimitry Andricbool StringLexer::NextIf(Character c) { 250127ef0fSEd Maste auto val = Peek(); 26435933ddSDimitry Andric if (val == c) { 270127ef0fSEd Maste Next(); 280127ef0fSEd Maste return true; 290127ef0fSEd Maste } 300127ef0fSEd Maste return false; 310127ef0fSEd Maste } 320127ef0fSEd Maste 337aa51b79SEd Maste std::pair<bool, StringLexer::Character> NextIf(std::initializer_list<Character> cs)34435933ddSDimitry AndricStringLexer::NextIf(std::initializer_list<Character> cs) { 357aa51b79SEd Maste auto val = Peek(); 36435933ddSDimitry Andric for (auto c : cs) { 37435933ddSDimitry Andric if (val == c) { 387aa51b79SEd Maste Next(); 397aa51b79SEd Maste return {true, c}; 407aa51b79SEd Maste } 417aa51b79SEd Maste } 427aa51b79SEd Maste return {false, 0}; 437aa51b79SEd Maste } 447aa51b79SEd Maste AdvanceIf(const std::string & token)45435933ddSDimitry Andricbool StringLexer::AdvanceIf(const std::string &token) { 467aa51b79SEd Maste auto pos = m_position; 477aa51b79SEd Maste bool matches = true; 48435933ddSDimitry Andric for (auto c : token) { 49435933ddSDimitry Andric if (!NextIf(c)) { 507aa51b79SEd Maste matches = false; 517aa51b79SEd Maste break; 527aa51b79SEd Maste } 537aa51b79SEd Maste } 54435933ddSDimitry Andric if (!matches) { 557aa51b79SEd Maste m_position = pos; 567aa51b79SEd Maste return false; 577aa51b79SEd Maste } 587aa51b79SEd Maste return true; 597aa51b79SEd Maste } 607aa51b79SEd Maste Next()61435933ddSDimitry AndricStringLexer::Character StringLexer::Next() { 620127ef0fSEd Maste auto val = Peek(); 630127ef0fSEd Maste Consume(); 640127ef0fSEd Maste return val; 650127ef0fSEd Maste } 660127ef0fSEd Maste HasAtLeast(Size s)67435933ddSDimitry Andricbool StringLexer::HasAtLeast(Size s) { 687aa51b79SEd Maste return (m_data.size() - m_position) >= s; 690127ef0fSEd Maste } 700127ef0fSEd Maste PutBack(Size s)71435933ddSDimitry Andricvoid StringLexer::PutBack(Size s) { 727aa51b79SEd Maste assert(m_position >= s); 737aa51b79SEd Maste m_position -= s; 740127ef0fSEd Maste } 750127ef0fSEd Maste GetUnlexed()76435933ddSDimitry Andricstd::string StringLexer::GetUnlexed() { 777aa51b79SEd Maste return std::string(m_data, m_position); 787aa51b79SEd Maste } 797aa51b79SEd Maste Consume()80435933ddSDimitry Andricvoid StringLexer::Consume() { m_position++; } 810127ef0fSEd Maste operator =(const StringLexer & rhs)82435933ddSDimitry AndricStringLexer &StringLexer::operator=(const StringLexer &rhs) { 83435933ddSDimitry Andric if (this != &rhs) { 840127ef0fSEd Maste m_data = rhs.m_data; 850127ef0fSEd Maste m_position = rhs.m_position; 860127ef0fSEd Maste } 870127ef0fSEd Maste return *this; 880127ef0fSEd Maste } 89