// Copyright (c) 2019 Egor Tensin // 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 #include #include #include namespace math::server { namespace lexer::details { // Exposed for testing: std::string_view parse_whitespace(const std::string_view&); std::optional parse_number(const std::string_view&); std::optional 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 for_each_token(const TokenProcessor& process); std::vector get_tokens(); bool has_token() const { return peek_token().has_value(); } std::optional peek_token() const { return m_token_buffer; } void drop_token(); std::optional drop_token_of_type(Type type); private: std::optional parse_whitespace() const; std::optional parse_const_token() const; std::optional parse_number() const; ParsedToken parse_token() const; void consume_whitespace(); void consume_token(); lexer::Input m_input; std::optional m_token_buffer; }; }