blob: 958a9580db7ac0de8eb1f931f76fa69ae866fdb6 [file] [log] [blame]
Inna Palantff3f07a2019-07-11 16:15:26 -07001//===--------------------- StringLexer.cpp -----------------------*- C++-*-===//
2//
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -08003// 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
Inna Palantff3f07a2019-07-11 16:15:26 -07006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Utility/StringLexer.h"
10
11#include <algorithm>
12#include <assert.h>
13
14using namespace lldb_utility;
15
16StringLexer::StringLexer(std::string s) : m_data(s), m_position(0) {}
17
Inna Palantff3f07a2019-07-11 16:15:26 -070018StringLexer::Character StringLexer::Peek() { return m_data[m_position]; }
19
20bool StringLexer::NextIf(Character c) {
21 auto val = Peek();
22 if (val == c) {
23 Next();
24 return true;
25 }
26 return false;
27}
28
29std::pair<bool, StringLexer::Character>
30StringLexer::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
41bool 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
57StringLexer::Character StringLexer::Next() {
58 auto val = Peek();
59 Consume();
60 return val;
61}
62
63bool StringLexer::HasAtLeast(Size s) {
64 return (m_data.size() - m_position) >= s;
65}
66
67void StringLexer::PutBack(Size s) {
68 assert(m_position >= s);
69 m_position -= s;
70}
71
72std::string StringLexer::GetUnlexed() {
73 return std::string(m_data, m_position);
74}
75
76void StringLexer::Consume() { m_position++; }
77
78StringLexer &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}