BLT/include/blt/logging/fmt_tokenizer.h

189 lines
4.4 KiB
C
Raw Normal View History

2025-03-03 01:52:20 -05:00
#pragma once
/*
* Copyright (C) 2024 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_LOGGING_FMT_TOKENIZER_H
#define BLT_LOGGING_FMT_TOKENIZER_H
#include <optional>
#include <string_view>
#include <vector>
#include <blt/std/types.h>
2025-03-04 22:02:51 -05:00
#include <functional>
2025-03-03 01:52:20 -05:00
namespace blt::logging
{
2025-03-03 15:44:42 -05:00
enum class fmt_token_type : u8
{
STRING,
NUMBER,
SPACE,
COLON,
DOT,
MINUS,
2025-03-04 01:26:33 -05:00
PLUS,
2025-03-04 16:17:16 -05:00
POUND,
LEFT_CHEVRON,
RIGHT_CHEVRON,
2025-03-04 22:02:51 -05:00
OPEN_BRACKET,
CLOSE_BRACKET,
2025-03-04 16:17:16 -05:00
CARET
};
enum class fmt_align_t : u8
{
LEFT,
CENTER,
RIGHT
2025-03-03 15:44:42 -05:00
};
enum class fmt_sign_t : u8
{
SPACE,
PLUS,
MINUS
};
enum class fmt_type_t : u8
{
BINARY, // 'b'
CHAR, // 'c'
DECIMAL, // 'd'
OCTAL, // 'o'
HEX, // 'x'
HEX_FLOAT, // 'a'
EXPONENT, // 'e'
FIXED_POINT, // 'f'
2025-03-04 11:35:23 -05:00
GENERAL, // 'g'
2025-03-04 13:58:14 -05:00
TYPE, // 't'
UNSPECIFIED // default
2025-03-03 15:44:42 -05:00
};
struct fmt_spec_t
{
i64 arg_id = -1;
i64 width = -1;
i64 precision = -1;
2025-03-04 13:58:14 -05:00
fmt_type_t type = fmt_type_t::UNSPECIFIED;
2025-03-03 15:44:42 -05:00
fmt_sign_t sign = fmt_sign_t::MINUS;
2025-03-04 16:17:16 -05:00
fmt_align_t alignment = fmt_align_t::RIGHT;
std::optional<char> prefix_char;
2025-03-03 15:44:42 -05:00
bool uppercase = false;
2025-03-04 01:26:33 -05:00
bool alternate_form = false;
2025-03-03 15:44:42 -05:00
};
struct fmt_token_t
{
fmt_token_type type;
std::string_view value;
};
class fmt_tokenizer_t
{
public:
explicit fmt_tokenizer_t() = default;
static fmt_token_type get_type(char c);
std::optional<fmt_token_t> next();
std::vector<fmt_token_t> tokenize(std::string_view fmt);
private:
size_t m_pos = 0;
2025-03-04 22:02:51 -05:00
std::string_view m_fmt{};
2025-03-03 15:44:42 -05:00
};
class fmt_parser_t
{
public:
2025-03-04 22:02:51 -05:00
explicit fmt_parser_t(std::vector<std::function<void(std::ostream&, const fmt_spec_t&)>>& handlers): m_handlers(handlers)
{
}
2025-03-03 15:44:42 -05:00
2025-03-04 16:17:16 -05:00
fmt_token_t& peek(const size_t offset)
{
return m_tokens[m_pos + offset];
}
2025-03-03 15:44:42 -05:00
fmt_token_t& peek()
{
return m_tokens[m_pos];
}
[[nodiscard]] bool has_next() const
{
return m_pos < m_tokens.size();
}
2025-03-04 16:17:16 -05:00
[[nodiscard]] bool has_next(const size_t offset) const
{
return (m_pos + offset) < m_tokens.size();
}
2025-03-03 15:44:42 -05:00
[[nodiscard]] fmt_token_t& next()
{
return m_tokens[m_pos++];
}
void consume()
{
++m_pos;
}
2025-03-04 16:17:16 -05:00
void consume(const size_t amount)
{
m_pos += amount;
}
2025-03-03 15:44:42 -05:00
const fmt_spec_t& parse(std::string_view fmt);
private:
2025-03-04 16:17:16 -05:00
static bool is_align_t(fmt_token_type type);
2025-03-03 15:44:42 -05:00
void parse_fmt_field();
void parse_arg_id();
2025-03-04 22:02:51 -05:00
std::string parse_arg_or_number();
2025-03-04 01:26:33 -05:00
void parse_fmt_spec();
2025-03-04 22:02:51 -05:00
void parse_fmt_spec_fill();
2025-03-04 16:17:16 -05:00
void parse_fmt_spec_align();
2025-03-04 01:26:33 -05:00
void parse_fmt_spec_sign();
void parse_fmt_spec_form();
void parse_fmt_spec_width();
void parse_fmt_spec_precision();
2025-03-04 22:02:51 -05:00
void parse_fill();
2025-03-04 16:17:16 -05:00
void parse_align();
2025-03-03 15:44:42 -05:00
void parse_sign();
2025-03-04 01:26:33 -05:00
void parse_form();
2025-03-03 15:44:42 -05:00
void parse_width();
void parse_precision();
void parse_type();
size_t m_pos = 0;
std::vector<fmt_token_t> m_tokens;
fmt_tokenizer_t m_tokenizer;
fmt_spec_t m_spec;
2025-03-04 22:02:51 -05:00
std::vector<std::function<void(std::ostream&, const fmt_spec_t&)>>& m_handlers;
2025-03-03 15:44:42 -05:00
};
2025-03-03 01:52:20 -05:00
}
#endif //BLT_LOGGING_FMT_TOKENIZER_H