blob: 44831d8fafd6ed9d46ef468967ebb410777b6226 (
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
// Copyright (c) 2019 Egor Tensin <Egor.Tensin@gmail.com>
// 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 "input.hpp"
#include "token.hpp"
#include "token_type.hpp"
#include <functional>
#include <optional>
#include <string_view>
#include <vector>
namespace math::server {
namespace lexer::details {
// Exposed for testing:
std::string_view parse_whitespace(const std::string_view&);
std::optional<double> parse_number(const std::string_view&);
std::optional<token::Type> parse_const_token(const std::string_view&);
}
class Lexer {
public:
explicit Lexer(const std::string_view& input);
explicit Lexer(const lexer::Input& input);
using Token = lexer::Token;
using ParsedToken = lexer::ParsedToken;
using Type = Token::Type;
using TokenProcessor = std::function<bool (const ParsedToken&)>;
bool for_each_token(const TokenProcessor& process);
std::vector<ParsedToken> get_tokens();
bool has_token() const {
return peek_token().has_value();
}
std::optional<ParsedToken> peek_token() const {
return m_token_buffer;
}
void drop_token();
std::optional<ParsedToken> drop_token_of_type(Type type);
private:
std::optional<ParsedToken> parse_whitespace() const;
std::optional<ParsedToken> parse_const_token() const;
std::optional<ParsedToken> parse_number() const;
ParsedToken parse_token() const;
void consume_whitespace();
void consume_token();
lexer::Input m_input;
std::optional<ParsedToken> m_token_buffer;
};
}
|