libxr
Embedded C++ framework for bare-metal, RTOS and Linux
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{
59
64enum class Align : uint8_t
65{
66 None,
67 Left,
68 Right,
69 Center,
70};
71
77{
78 size_t arg_index = 0;
80 char fill = ' ';
81 bool force_sign = false;
82 bool space_sign = false;
83 bool alternate = false;
84 bool zero_pad = false;
85 uint8_t width = 0;
87 false;
88 uint8_t precision = 0;
90 0;
91};
92
97enum class ArgumentKind : uint8_t
98{
100 Bool,
101 Character,
102 Signed,
103 Unsigned,
105 Pointer,
106 Float32,
107 Float64,
108 LongDouble,
109};
110
116{
120 false;
121};
132
133// These implementation headers form an ordered dependency chain.
134// clang-format off
135#include "format_frontend_source.hpp"
136
137#include "format_frontend_binding_base.hpp"
138#include "format_frontend_binding_integer.hpp"
139#include "format_frontend_binding_float.hpp"
140// clang-format on
141
150template <Text Source>
151[[nodiscard]] consteval auto Analyze()
152{
153 return SourceSyntax::Analyze<Source>();
154}
155
168template <Text Source, typename... Args>
169[[nodiscard]] consteval Error WalkSourceAsFormatFields(auto& visitor)
170{
171 struct ResolvingVisitor
172 {
173 decltype(visitor)& inner;
174
175 [[nodiscard]] consteval Error Text(size_t offset, size_t text_size)
176 {
177 return inner.Text(offset, text_size);
178 }
179
180 [[nodiscard]] consteval Error Field(const ParsedField& parsed)
181 {
182 auto resolved = ArgumentResolution::ResolveField<Args...>(parsed);
183 if (resolved.error != Error::None)
184 {
185 return resolved.error;
186 }
187 return inner.Field(resolved.field);
188 }
189 };
190
191 ResolvingVisitor resolving{visitor};
192 return SourceSyntax::WalkSource(std::string_view(Source.Data(), Source.Size()),
193 resolving);
194}
195
203template <Text Source, typename... Args>
205{
206 public:
207 using ErrorType = Error;
208
214 [[nodiscard]] static constexpr const char* SourceData() { return Source.Data(); }
220 [[nodiscard]] static constexpr size_t SourceSize() { return Source.Size(); }
221
230 [[nodiscard]] static consteval ErrorType Walk(auto& visitor)
231 {
232 return WalkSourceAsFormatFields<Source, Args...>(visitor);
233 }
234};
235} // 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
不支持的 C++ 参数类型 / unsupported C++ argument type
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
终止字节之前出现嵌入式 NUL / embedded NUL before the terminator
@ UnmatchedBrace
花括号未正确配对 / unmatched { or }
@ InvalidSpecifier
format-spec 语法非法 / malformed format-spec grammar
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
整数是否必须走 64 位存储 / whether integer storage must be 64-bit
ArgumentKind kind
前端侧参数类别 / frontend-side argument category
在绑定到具体 C++ 参数类型之前的 brace 字段解析结果 / Parsed brace field before binding it to one concrete C++ argument t...
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
展示类型字符;缺省时为 0 / parsed presentation character, or 0 for default
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 prot...
Error error
字段解析结果 / field-resolution result
FormatField field
解析后的共享字段 / resolved shared field
一条已经决定完毕、运行期 writer 知道如何打印的值字段 / One fully-decided value field that the runtime writer knows how to p...
作为 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