libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
format_frontend_detail.hpp
1#pragma once
2
8#include <array>
9#include <cstddef>
10#include <cstdint>
11#include <limits>
12#include <string_view>
13#include <type_traits>
14
15#include "../format_argument.hpp"
16#include "../format_compile.hpp"
17#include "../printf.hpp"
18
20{
56
61enum class Align : uint8_t
62{
63 None,
64 Left,
65 Right,
66 Center,
67};
68
74{
75 size_t arg_index = 0;
77 char fill = ' ';
78 bool force_sign = false;
79 bool space_sign = false;
80 bool alternate = false;
81 bool zero_pad = false;
82 uint8_t width = 0;
84 false;
85 uint8_t precision = 0;
87 0;
88};
89
94enum class ArgumentKind : uint8_t
95{
97 Bool,
98 Character,
99 Signed,
100 Unsigned,
101 String,
102 Pointer,
103 Float32,
105 LongDouble,
106};
107
113{
117 false;
119
129
130// These implementation headers form an ordered dependency chain.
131// clang-format off
132#include "format_frontend_source.hpp"
133
134#include "format_frontend_binding_base.hpp"
135#include "format_frontend_binding_integer.hpp"
136#include "format_frontend_binding_float.hpp"
137// clang-format on
147template <Text Source>
148[[nodiscard]] consteval auto Analyze()
149{
150 return SourceSyntax::Analyze<Source>();
151}
152
165template <Text Source, typename... Args>
166[[nodiscard]] consteval Error WalkSourceAsFormatFields(auto& visitor)
167{
168 struct ResolvingVisitor
169 {
170 decltype(visitor)& inner;
172 [[nodiscard]] consteval Error Text(size_t offset, size_t text_size)
173 {
174 return inner.Text(offset, text_size);
175 }
176
177 [[nodiscard]] consteval Error Field(const ParsedField& parsed)
178 {
179 auto resolved = ArgumentResolution::ResolveField<Args...>(parsed);
180 if (resolved.error != Error::None)
181 {
182 return resolved.error;
183 }
184 return inner.Field(resolved.field);
185 }
186 };
188 ResolvingVisitor resolving{visitor};
189 return SourceSyntax::WalkSource(std::string_view(Source.Data(), Source.Size()),
190 resolving);
191}
192
200template <Text Source, typename... Args>
202{
203 public:
204 using ErrorType = Error;
205
211 [[nodiscard]] static constexpr const char* SourceData() { return Source.Data(); }
217 [[nodiscard]] static constexpr size_t SourceSize() { return Source.Size(); }
218
227 [[nodiscard]] static consteval ErrorType Walk(auto& visitor)
228 {
229 return WalkSourceAsFormatFields<Source, Args...>(visitor);
230 }
231};
232} // namespace LibXR::Print::Detail::FormatFrontend
将一条 brace 风格源字面量绑定到一组具体 C++ 参数类型上的前端适配器 / Frontend adapter that binds one brace-style source literal ...
static constexpr size_t SourceSize()
返回不含结尾零字节的源字符串长度 / Return the source string length without a terminating zero byte
static consteval ErrorType Walk(auto &visitor)
遍历当前已绑定前端,并产出文本片段和最终 FormatField 记录 / Walk this bound frontend and emit text spans plus final FormatF...
static constexpr const char * SourceData()
返回不含结尾零字节的源字符串字节序列 / Return the source string bytes without a terminating zero byte
consteval ResolvedField ResolveField(const ParsedField &parsed)
先判断一个已解析 brace 字段指向哪类参数,再选择匹配的字段构造逻辑 / Check which argument family a parsed brace field points to,...
brace 前端的编译期分析与降级入口层 / Brace frontend compile-time analysis and lowering entry surface
ArgumentKind
前端侧的参数类别,用来选择字段该走哪条解析路径。 / Frontend-side argument categories used to choose one field-resolution path...
@ Signed
signed integer / 有符号整数
@ Unsigned
unsigned integer / 无符号整数
@ Unsupported
unsupported C++ argument type / 不支持的 C++ 参数类型
Error
brace 风格 format 前端的编译期失败类别。 / Compile-time failure categories for the brace-style format frontend.
@ UnexpectedEnd
source ended in the middle of one field / 字段尚未结束时源串已结束
@ MixedIndexing
automatic and manual indexing were mixed / 混用了自动索引与手动索引
@ EmbeddedNul
embedded NUL before the terminator / 终止字节之前出现嵌入式 NUL
@ UnmatchedBrace
unmatched { or } / 花括号未正确配对
@ InvalidSpecifier
malformed format-spec grammar / format-spec 语法非法
consteval Error WalkSourceAsFormatFields(auto &visitor)
遍历一条 brace 风格字面量,并针对具体 C++ 参数列表产出共享 FormatField 记录 / Walk one brace-style literal and emit shared For...
Align
降为 FormatFlag 位之前的已解析对齐方式。 / Parsed alignment directive before lowering into FormatFlag bits.
@ None
default alignment / 默认对齐
consteval auto Analyze()
对单条 brace 风格字面量执行仅源串分析 / Run source-only analysis for one brace-style literal
单个 C++ 参数在前端里的类别摘要,以及对应的宽度策略。 / Frontend summary of one C++ argument category plus width policy.
bool uses_64bit_storage
whether integer storage must be 64-bit / 整数是否必须走 64 位存储
ArgumentKind kind
frontend-side argument category / 前端侧参数类别
在绑定到具体 C++ 参数类型之前的 brace 字段解析结果。 / Parsed brace field before binding it to one concrete C++ argument ...
uint8_t precision
parsed precision when present / 显式给出时的精度值
size_t arg_index
selected source argument index / 选中的源参数索引
bool force_sign
parsed plus-sign option / 已解析正号选项
char fill
parsed fill character / 已解析填充字符
Align align
parsed alignment / 已解析对齐方式
bool space_sign
parsed space-sign option / 已解析空格符号选项
uint8_t width
parsed constant width / 已解析常量宽度
bool zero_pad
parsed zero-pad option / 已解析零填充选项
char presentation
parsed presentation character, or 0 for default / 展示类型字符;缺省时为 0
bool has_precision
whether precision was explicitly present / 是否显式给出了精度
bool alternate
parsed alternate-form option / 已解析备用格式选项
将单个已解析 brace 字段解析成共享格式协议后的结果。 / Result of resolving one parsed brace field into the shared format pro...
Error error
field-resolution result / 字段解析结果
FormatField field
resolved shared field / 解析后的共享字段
一条已经决定完毕、运行期 writer 知道如何打印的值字段。 / One fully-decided value field that the runtime writer knows how to ...
作为 printf 格式源的结构化字符串字面量包装 / Structural literal wrapper used as the NTTP source for printf formats
Definition printf.hpp:17
constexpr Text(const char(&text)[N])
将字符串字面量复制到结构化 NTTP 对象中 / Copy the string literal into the structural NTTP object
Definition printf.hpp:29
constexpr const char * Data() const
返回含结尾零字节的字面量指针 / Return the literal bytes including the terminating zero byte
Definition printf.hpp:50
constexpr size_t Size() const
返回不含结尾零字节的格式串长度 / Return the format length without the terminating zero byte
Definition printf.hpp:43