xref: /src/contrib/llvm-project/lldb/source/Utility/StringLexer.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1cfca06d7SDimitry Andric //===-- StringLexer.cpp ---------------------------------------------------===//
20cac4ca3SEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60cac4ca3SEd Maste //
70cac4ca3SEd Maste //===----------------------------------------------------------------------===//
80cac4ca3SEd Maste 
90cac4ca3SEd Maste #include "lldb/Utility/StringLexer.h"
100cac4ca3SEd Maste 
110cac4ca3SEd Maste #include <algorithm>
12344a3780SDimitry Andric #include <cassert>
13b60736ecSDimitry Andric #include <utility>
140cac4ca3SEd Maste 
15ead24645SDimitry Andric using namespace lldb_private;
160cac4ca3SEd Maste 
StringLexer(std::string s)17b60736ecSDimitry Andric StringLexer::StringLexer(std::string s) : m_data(std::move(s)), m_position(0) {}
180cac4ca3SEd Maste 
Peek()1914f1b3e8SDimitry Andric StringLexer::Character StringLexer::Peek() { return m_data[m_position]; }
200cac4ca3SEd Maste 
NextIf(Character c)2114f1b3e8SDimitry Andric bool StringLexer::NextIf(Character c) {
220cac4ca3SEd Maste   auto val = Peek();
2314f1b3e8SDimitry Andric   if (val == c) {
240cac4ca3SEd Maste     Next();
250cac4ca3SEd Maste     return true;
260cac4ca3SEd Maste   }
270cac4ca3SEd Maste   return false;
280cac4ca3SEd Maste }
290cac4ca3SEd Maste 
30205afe67SEd Maste std::pair<bool, StringLexer::Character>
NextIf(std::initializer_list<Character> cs)3114f1b3e8SDimitry Andric StringLexer::NextIf(std::initializer_list<Character> cs) {
32205afe67SEd Maste   auto val = Peek();
3314f1b3e8SDimitry Andric   for (auto c : cs) {
3414f1b3e8SDimitry Andric     if (val == c) {
35205afe67SEd Maste       Next();
36205afe67SEd Maste       return {true, c};
37205afe67SEd Maste     }
38205afe67SEd Maste   }
39205afe67SEd Maste   return {false, 0};
40205afe67SEd Maste }
41205afe67SEd Maste 
AdvanceIf(const std::string & token)4214f1b3e8SDimitry Andric bool StringLexer::AdvanceIf(const std::string &token) {
43205afe67SEd Maste   auto pos = m_position;
44205afe67SEd Maste   bool matches = true;
4514f1b3e8SDimitry Andric   for (auto c : token) {
4614f1b3e8SDimitry Andric     if (!NextIf(c)) {
47205afe67SEd Maste       matches = false;
48205afe67SEd Maste       break;
49205afe67SEd Maste     }
50205afe67SEd Maste   }
5114f1b3e8SDimitry Andric   if (!matches) {
52205afe67SEd Maste     m_position = pos;
53205afe67SEd Maste     return false;
54205afe67SEd Maste   }
55205afe67SEd Maste   return true;
56205afe67SEd Maste }
57205afe67SEd Maste 
Next()5814f1b3e8SDimitry Andric StringLexer::Character StringLexer::Next() {
590cac4ca3SEd Maste   auto val = Peek();
600cac4ca3SEd Maste   Consume();
610cac4ca3SEd Maste   return val;
620cac4ca3SEd Maste }
630cac4ca3SEd Maste 
HasAtLeast(Size s)6414f1b3e8SDimitry Andric bool StringLexer::HasAtLeast(Size s) {
65205afe67SEd Maste   return (m_data.size() - m_position) >= s;
660cac4ca3SEd Maste }
670cac4ca3SEd Maste 
PutBack(Size s)6814f1b3e8SDimitry Andric void StringLexer::PutBack(Size s) {
69205afe67SEd Maste   assert(m_position >= s);
70205afe67SEd Maste   m_position -= s;
710cac4ca3SEd Maste }
720cac4ca3SEd Maste 
GetUnlexed()7314f1b3e8SDimitry Andric std::string StringLexer::GetUnlexed() {
74205afe67SEd Maste   return std::string(m_data, m_position);
75205afe67SEd Maste }
76205afe67SEd Maste 
Consume()7714f1b3e8SDimitry Andric void StringLexer::Consume() { m_position++; }
780cac4ca3SEd Maste 
operator =(const StringLexer & rhs)7914f1b3e8SDimitry Andric StringLexer &StringLexer::operator=(const StringLexer &rhs) {
8014f1b3e8SDimitry Andric   if (this != &rhs) {
810cac4ca3SEd Maste     m_data = rhs.m_data;
820cac4ca3SEd Maste     m_position = rhs.m_position;
830cac4ca3SEd Maste   }
840cac4ca3SEd Maste   return *this;
850cac4ca3SEd Maste }
86