aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/server/lexer/input.hpp
blob: 61fdb030ad04bba4f328f3c070bf807abbff5907 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2019 Egor Tensin <egor@tensin.name>
// This file is part of the "math-server" project.
// For details, see https://github.com/egor-tensin/math-server
// Distributed under the MIT License.

#pragma once

#include "error.hpp"

#include <cstddef>
#include <string_view>

namespace math::server::lexer {

class Input {
public:
    explicit Input(std::string_view input) : m_pos{0}, m_input{input} {}

    std::string_view get_input() const {
        return m_input;
    }

    std::size_t get_pos() const {
        return m_pos;
    }

    std::size_t get_length() const {
        return m_input.length();
    }

    bool empty() const {
        return m_input.empty();
    }

    void consume(std::size_t len) {
        if (m_input.length() < len) {
            throw LexerError{"internal: not enough input to consume"};
        }
        m_pos += len;
        m_input.remove_prefix(len);
    }

    void consume(std::string_view sub) {
        consume(sub.length());
    }

private:
    std::size_t m_pos;
    std::string_view m_input;
};

} // namespace math::server::lexer