libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
SourceSyntax Namespace Reference

printf 降级阶段共享的描述表与策略辅助函数 / Shared descriptor-table and policy helpers for printf lowering More...

Data Structures

struct  IndexingState
 brace 字段自动索引与手动索引的源级状态 / Source-level indexing mode for automatic versus manual brace fields More...
 
struct  ResolvedArgumentVisitor
 将已解析转换解析成最终源顺序参数元信息摘要的 visitor / Visitor that resolves parsed conversions into the final source-ordered argument metadata summary More...
 
struct  SourceAnalysis
 单条 brace 风格格式字面量的仅源串分析数据 / Source-only analysis data for one brace-style format literal More...
 
struct  SourceAnalysisScratch
 仅源串分析阶段使用的保守临时累加器 / Conservative temporary accumulator used during source-only analysis More...
 

Functions

constexpr bool IsDigit (char ch)
 brace 风格语法的源串解析辅助函数 / Source parser helpers for the brace-style grammar
 
constexpr bool IsAlign (char ch)
 判断一个源字符是否为合法的 brace 格式对齐标记 / Return whether one source character is a valid brace-format align token
 
constexpr Align ParseAlign (char ch)
 将一个 brace 格式对齐字符转换为前端枚举值 / Convert one brace-format align token to the frontend enum value
 
constexpr bool HasEmbeddedNul (std::string_view source)
 判断源字符串在终止前是否包含嵌入式 NUL / Return whether the source contains an embedded NUL before the terminator
 
constexpr bool IsSupportedPresentation (char ch)
 判断一个展示字符是否被 brace 前端支持 / Return whether one presentation token is supported by the brace frontend
 
template<typename UInt >
consteval Error ParseUnsigned (std::string_view source, size_t &pos, UInt limit, UInt &value)
 解析一个十进制整数字段片段,并检查是否溢出 / Parse one decimal integer fragment with overflow checking
 
consteval Error ParseFieldHead (std::string_view source, size_t &pos, IndexingState &indexing, ParsedField &field)
 解析 :} 之前的字段头,包括自动或手动参数索引选择 / Parse the field head before : or }, including automatic or manual argument index selection
 
consteval Error ParseFormatSpec (std::string_view source, size_t &pos, ParsedField &field)
 解析一个 brace 字段中 : 之后的可选 format-spec 部分 / Parse the optional format-spec portion after : inside one brace field
 
consteval Error ParseField (std::string_view source, size_t &pos, IndexingState &indexing, ParsedField &field)
 解析一个完整 brace 字段,从 { 一直到匹配的 } / Parse one complete brace field from { through the matching }
 
consteval Error WalkSource (std::string_view source, auto &visitor)
 遍历一个 brace 源字符串,并发射字面文本片段与已解析字段 / Walk one brace source string and emit literal-text spans plus parsed fields
 
template<Text Source>
consteval auto Analyze ()
 对一个 brace 字面量执行仅源串分析,并返回按顺序整理的参数索引摘要 / Run source-only analysis for one brace literal and return the ordered argument-index summary
 
consteval Error ParseArgumentIndex (std::string_view source, size_t &pos, IndexingState &indexing, Conversion &conversion)
 解析可选的前导 n$ 位置参数选择器 / Parse the optional leading n$ positional argument selector
 
consteval Error ParseByte (std::string_view source, size_t &pos, uint8_t limit, uint8_t &value)
 解析一个目标为字节宽度的十进制整数字段片段,并检查是否溢出 / Parse one decimal byte-sized integer fragment with overflow checking
 
consteval Error ParseFlags (std::string_view source, size_t &pos, Conversion &conversion)
 单个 printf 转换体的源级解析器 / Source parser for one printf conversion body
 
consteval Error ParseWidth (std::string_view source, size_t &pos, Conversion &conversion)
 解析一个可选的常量宽度字段 / Parse one optional constant width field
 
consteval Error ParsePrecision (std::string_view source, size_t &pos, Conversion &conversion)
 解析一个可选的常量精度字段 / Parse one optional constant precision field
 
consteval void ParseLength (std::string_view source, size_t &pos, Conversion &conversion)
 解析一个可选的长度修饰符序列 / Parse one optional length modifier sequence
 
consteval Error ParseSpecifier (std::string_view source, size_t &pos, Conversion &conversion)
 解析并校验最终的转换说明符字符 / Parse and validate the final conversion specifier token
 
consteval Error Parse (std::string_view source, size_t &pos, IndexingState &indexing, Conversion &conversion)
 在前导 % 之后解析一个完整 printf 转换项 / Parse one complete printf conversion after the leading %
 

Detailed Description

printf 降级阶段共享的描述表与策略辅助函数 / Shared descriptor-table and policy helpers for printf lowering

为一个已解析 printf 转换选择运行期语义类型 / Choose the runtime semantic type for one parsed printf conversion

Function Documentation

◆ Analyze()

template<Text Source>
auto SourceSyntax::Analyze ( )
nodiscardconsteval

对一个 brace 字面量执行仅源串分析,并返回按顺序整理的参数索引摘要 / Run source-only analysis for one brace literal and return the ordered argument-index summary

对一个 printf 字面量执行仅源串分析,并返回按顺序整理的参数引用摘要 / Run source-only analysis for one printf literal and return the ordered argument-reference summary

Template Parameters
Sourcebrace 风格格式串字面量 / Brace-style format literal
Returns
源串分析结果,包含字段顺序、所需参数个数与首个源级错误 / Returns the source analysis result, including field order, required argument count, and the first source-level error
Template Parameters
Sourceprintf 风格格式串字面量 / Printf-style format literal
Returns
源串分析结果,包含字段顺序、参数引用摘要与首个源级错误 / Returns the source analysis result, including field order, argument reference summary, and the first source-level error

Definition at line 155 of file format_frontend_source.hpp.

156{
157 constexpr auto scratch = []() consteval
158 {
159 SourceAnalysisScratch<Source.Size()> visitor{};
160 visitor.error = WalkSource(std::string_view(Source.Data(), Source.Size()), visitor);
161 return visitor;
162 }();
163
164 SourceAnalysis<scratch.field_count> result{};
165 result.required_argument_count = scratch.required_argument_count;
166 result.error = scratch.error;
167 for (size_t i = 0; i < scratch.field_count; ++i)
168 {
169 result.argument_order[i] = scratch.order[i];
170 }
171 return result;
172}
仅源串分析阶段使用的保守临时累加器 / Conservative temporary accumulator used during source-only analysis
Error error
first parse error / 首个解析错误

◆ HasEmbeddedNul()

bool SourceSyntax::HasEmbeddedNul ( std::string_view source)
nodiscardconstexpr

判断源字符串在终止前是否包含嵌入式 NUL / Return whether the source contains an embedded NUL before the terminator

判断源字符串在结尾终止字节之前是否包含嵌入式 NUL / Return whether the source contains an embedded NUL byte before the terminator

Parameters
source待检查的源字符串 / Source string to inspect
Returns
若终止前包含 \\0 则返回 true,否则返回 false / Returns true when \\0 appears before the terminator, otherwise false
Parameters
source待检查的源字符串 / Source string to inspect
Returns
若终止前包含嵌入式 NUL 则返回 true,否则返回 false / Returns true when an embedded NUL exists before the terminator, otherwise false

Definition at line 58 of file format_frontend_source.hpp.

63{
64 if (HasEmbeddedNul(source))
65 {
66 return Error::EmbeddedNul;
67 }
68

◆ IsAlign()

bool SourceSyntax::IsAlign ( char ch)
nodiscardconstexpr

判断一个源字符是否为合法的 brace 格式对齐标记 / Return whether one source character is a valid brace-format align token

Parameters
ch待检查的源字符 / Source character to inspect
Returns
合法对齐字符返回 true,否则返回 false / Returns true for a valid align token, otherwise false

Definition at line 24 of file format_frontend_source.hpp.

◆ IsDigit()

bool SourceSyntax::IsDigit ( char ch)
nodiscardconstexpr

brace 风格语法的源串解析辅助函数 / Source parser helpers for the brace-style grammar

判断一个源字节是否为 ASCII 十进制数字 / Return whether one source byte is an ASCII decimal digit

判断一个源字节是否为 ASCII 十进制数字 / Return whether one source byte is an ASCII decimal digit

Parameters
ch待检查的源字节 / Source byte to inspect
Returns
若是 ASCII 十进制数字则返回 true,否则返回 false / Returns true for an ASCII decimal digit, otherwise false
Parameters
ch待检查的源字节 / Source byte to test
Returns
若是 ASCII 十进制数字则返回 true,否则返回 false / Returns true for an ASCII decimal digit, otherwise false

Definition at line 15 of file format_frontend_source.hpp.

◆ IsSupportedPresentation()

bool SourceSyntax::IsSupportedPresentation ( char ch)
nodiscardconstexpr

判断一个展示字符是否被 brace 前端支持 / Return whether one presentation token is supported by the brace frontend

Parameters
ch展示字符 / Presentation token
Returns
支持返回 true,否则返回 false / Returns true when the token is supported, otherwise false

Definition at line 77 of file format_frontend_source.hpp.

77 {')
78 {
79 auto error = visitor.Text(text_begin, pos - text_begin);
80 if (error != Error::None)
81 {
82 return error;
83 }
84 error = visitor.Text(pos, 1);
85 if (error != Error::None)
86 {
87 return error;
88 }
89 pos += 2;
90 text_begin = pos;
91 continue;
92 }
93
94 auto error = visitor.Text(text_begin, pos - text_begin);
95 if (error != Error::None)
96 {
97 return error;
98 }
99
100 ParsedField field{};

◆ Parse()

Error SourceSyntax::Parse ( std::string_view source,
size_t & pos,
IndexingState & indexing,
Conversion & conversion )
nodiscardconsteval

在前导 % 之后解析一个完整 printf 转换项 / Parse one complete printf conversion after the leading %

Parameters
source完整 printf 源字符串 / Full printf source string
pos当前解析位置,进入时应指向 %,成功时推进到该转换项之后 / Current parse position; must point at % on entry and lands after the conversion on success
indexing源级索引模式跟踪状态 / Source-level indexing mode tracker
conversion输出转换项结果 / Conversion result output
Returns
成功返回 Error::None;解析或校验失败时返回对应错误 / Returns Error::None on success, or the first parse or validation failure

Definition at line 218 of file printf_frontend_source.hpp.

◆ ParseAlign()

Align SourceSyntax::ParseAlign ( char ch)
nodiscardconstexpr

将一个 brace 格式对齐字符转换为前端枚举值 / Convert one brace-format align token to the frontend enum value

Parameters
ch对齐字符 / Align token
Returns
对应的 Align 枚举;未知字符返回 Align::None / Returns the corresponding Align enum; unknown tokens map to Align::None

Definition at line 36 of file format_frontend_source.hpp.

37 { return Error::None; }
38
39 [[nodiscard]] consteval Error Field(const ParsedField& field)
40 {
41 order[field_count++] = field.arg_index;
42 size_t used_argument_count = field.arg_index + 1;
43 if (used_argument_count > required_argument_count)
44 {
45 required_argument_count = used_argument_count;
46 }
47 return Error::None;
48 }
49};

◆ ParseArgumentIndex()

Error SourceSyntax::ParseArgumentIndex ( std::string_view source,
size_t & pos,
IndexingState & indexing,
Conversion & conversion )
nodiscardconsteval

解析可选的前导 n$ 位置参数选择器 / Parse the optional leading n$ positional argument selector

This probe only consumes digits when they are immediately followed by '$'. Plain width digits such as %05d stay untouched for the later width parser. 只有当数字后面紧跟 '$' 时,这个探测才会真正消费它们;像 %05d 这样的普通宽度 数字会完整保留给后续宽度解析阶段。

Parameters
source完整 printf 源字符串 / Full printf source string
pos当前解析位置,成功时推进到 n$ 之后 / Current parse position; advanced past n$ on success
indexing源级索引模式跟踪状态 / Source-level indexing mode tracker
conversion当前正在填充的转换项 / Conversion being filled
Returns
解析成功或本段没有位置参数选择器时返回 Error::None;出错时返回首个解析错误 / Returns the first parse error, or Error::None when no positional selector is present or parsing succeeds

Definition at line 62 of file printf_frontend_source.hpp.

62{
64
65 [[nodiscard]] consteval Error Text(size_t, size_t) const { return Error::None; }
66
67 [[nodiscard]] consteval Error Field(const Conversion& conversion)
68 {
69 auto field = FieldSelection::BuildFormatField(conversion);
70 auto info = FormatArgumentInfo{
71 .pack = field.pack,
72 .rule = field.rule,
73 };
74 auto& slot = analysis.args[conversion.arg_index];
75 if (slot.rule != FormatArgumentRule::None &&
76 (slot.rule != info.rule || slot.pack != info.pack))
77 {
78 return Error::ConflictingArgument;
79 }
80
81 slot = info;
82 return Error::None;
83 }
84};
85
86#include "printf_frontend_parser.hpp"
87
97[[nodiscard]] consteval Error WalkSource(std::string_view source, auto& visitor)
98{
99 if (HasEmbeddedNul(source))
100 {
101 return Error::EmbeddedNul;
102 }
103
104 size_t pos = 0;
105 size_t text_begin = 0;
106 IndexingState indexing{};
107
108 while (pos < source.size())
109 {
Error
brace 风格 format 前端的编译期失败类别。 / Compile-time failure categories for the brace-style format frontend.
单个 printf 转换在降为共享格式前的解析结果 / One parsed printf conversion before lowering into the shared format
size_t arg_index
source argument index consumed by this field / 当前字段消耗的源参数索引
brace 字段自动索引与手动索引的源级状态 / Source-level indexing mode for automatic versus manual brace fields
单条 brace 风格格式字面量的仅源串分析数据 / Source-only analysis data for one brace-style format literal
std::array< FormatArgumentInfo, ArgCount > args
source-ordered argument metadata / 按源参数顺序排列的参数元信息

◆ ParseByte()

Error SourceSyntax::ParseByte ( std::string_view source,
size_t & pos,
uint8_t limit,
uint8_t & value )
nodiscardconsteval

解析一个目标为字节宽度的十进制整数字段片段,并检查是否溢出 / Parse one decimal byte-sized integer fragment with overflow checking

Parameters
source含该十进制片段的源字符串 / Source string holding the decimal fragment
pos当前解析位置,成功时推进到片段之后 / Current parse position; advanced past the fragment on success
limit该字段允许的最大值 / Inclusive upper bound accepted by this field
value输出解析结果 / Parsed result output
Returns
成功返回 Error::None;溢出或语法非法时返回对应错误 / Returns Error::None on success, or the first overflow or syntax failure

Definition at line 122 of file printf_frontend_source.hpp.

123 {
124 error = visitor.Text(pos, 1);
125 if (error != Error::None)
126 {
127 return error;
128 }
129
130 pos += 2;
131 text_begin = pos;
132 continue;
133 }
134
135 auto parse_pos = pos;
136 Conversion conversion{};
137 error = Parse(source, parse_pos, indexing, conversion);
138 if (error != Error::None)
139 {
140 return error;
141 }
142
143 error = visitor.Field(conversion);
144 if (error != Error::None)
consteval Error Parse(std::string_view source, size_t &pos, IndexingState &indexing, Conversion &conversion)
在前导 % 之后解析一个完整 printf 转换项 / Parse one complete printf conversion after the leading %

◆ ParseField()

Error SourceSyntax::ParseField ( std::string_view source,
size_t & pos,
IndexingState & indexing,
ParsedField & field )
nodiscardconsteval

解析一个完整 brace 字段,从 { 一直到匹配的 } / Parse one complete brace field from { through the matching }

Parameters
source源字符串 / Source string
pos当前解析位置,进入时应指向 {,成功时推进到 } 之后 / Current parse position; must point at { on entry and lands after } on success
indexing自动或手动索引状态 / Auto or manual indexing state
field输出字段结果 / Parsed field output
Returns
成功返回 Error::None;字段语法不合法时返回对应错误 / Returns Error::None on success, or the first field-syntax error

Definition at line 341 of file format_frontend_source.hpp.

344{
350template <size_t FieldCount>
351struct SourceAnalysis
352{
353 std::array<size_t, FieldCount>
354 argument_order{};
356 size_t required_argument_count =
357 0;
358 Error error = Error::None;
359};
360
367template <size_t MaxFieldCount>
368struct SourceAnalysisScratch
369{
370 std::array<size_t, MaxFieldCount> order{};
372 size_t field_count = 0;
373 size_t required_argument_count =
374 0;
375 Error error = Error::None;
376
377 [[nodiscard]] consteval Error Text(size_t, size_t) const { return Error::None; }
378
379 [[nodiscard]] consteval Error Field(const ParsedField& field)
380 {
381 order[field_count++] = field.arg_index;
382 size_t used_argument_count = field.arg_index + 1;
383 if (used_argument_count > required_argument_count)
384 {
385 required_argument_count = used_argument_count;
386 }
387 return Error::None;
388 }
389};
390
391#include "format_frontend_parser.hpp"
392
402[[nodiscard]] consteval Error WalkSource(std::string_view source, auto& visitor)
403{
404 if (HasEmbeddedNul(source))
405 {
406 return Error::EmbeddedNul;
407 }
408
409 size_t pos = 0;
410 size_t text_begin = 0;
411 IndexingState indexing{};
412
413 while (pos < source.size())
414 {
415 if (source[pos] == '{')
416 {
417 if (pos + 1 < source.size() && source[pos + 1] == '{')
418 {
419 auto error = visitor.Text(text_begin, pos - text_begin);
420 if (error != Error::None)
421 {
422 return error;
423 }
424 error = visitor.Text(pos, 1);
425 if (error != Error::None)
426 {
427 return error;
428 }
429 pos += 2;
430 text_begin = pos;
431 continue;
432 }
433
434 auto error = visitor.Text(text_begin, pos - text_begin);
435 if (error != Error::None)
436 {
437 return error;
438 }
439
440 ParsedField field{};
441 error = ParseField(source, pos, indexing, field);
442 if (error != Error::None)
443 {
444 return error;
445 }
446
447 error = visitor.Field(field);
448 if (error != Error::None)
449 {
450 return error;
451 }
452
453 text_begin = pos;
454 continue;
455 }
456
457 if (source[pos] == '}')
458 {
459 if (pos + 1 < source.size() && source[pos + 1] == '}')
460 {
461 auto error = visitor.Text(text_begin, pos - text_begin);
462 if (error != Error::None)
463 {
464 return error;
465 }
466 error = visitor.Text(pos, 1);
467 if (error != Error::None)
468 {
469 return error;
470 }
471 pos += 2;
472 text_begin = pos;
473 continue;
474 }
475
476 return Error::UnmatchedBrace;
477 }
478
479 ++pos;
480 }
481
482 return visitor.Text(text_begin, source.size() - text_begin);
483}
484
494template <Text Source>
495[[nodiscard]] consteval auto Analyze()
496{
497 constexpr auto scratch = []() consteval
498 {
499 SourceAnalysisScratch<Source.Size()> visitor{};
500 visitor.error = WalkSource(std::string_view(Source.Data(), Source.Size()), visitor);
501 return visitor;
502 }();
503
504 SourceAnalysis<scratch.field_count> result{};
505 result.required_argument_count = scratch.required_argument_count;
506 result.error = scratch.error;
507 for (size_t i = 0; i < scratch.field_count; ++i)
508 {
509 result.argument_order[i] = scratch.order[i];
510 }
511 return result;
512}
513} // namespace SourceSyntax

◆ ParseFieldHead()

Error SourceSyntax::ParseFieldHead ( std::string_view source,
size_t & pos,
IndexingState & indexing,
ParsedField & field )
nodiscardconsteval

解析 :} 之前的字段头,包括自动或手动参数索引选择 / Parse the field head before : or }, including automatic or manual argument index selection

Parameters
source源字符串 / Source string
pos当前解析位置,成功时推进到字段头之后 / Current parse position; advanced past the field head on success
indexing自动或手动索引状态 / Auto or manual indexing state
field输出字段头结果 / Parsed field-head output
Returns
成功返回 Error::None;语法或索引模式不合法时返回对应错误 / Returns Error::None on success, or the first syntax or indexing error

Definition at line 162 of file format_frontend_source.hpp.

164 {};
165 result.required_argument_count = scratch.required_argument_count;
166 result.error = scratch.error;
167 for (size_t i = 0; i < scratch.field_count; ++i)
168 {
169 result.argument_order[i] = scratch.order[i];
170 }
171 return result;
172}
173} // namespace SourceSyntax

◆ ParseFlags()

Error SourceSyntax::ParseFlags ( std::string_view source,
size_t & pos,
Conversion & conversion )
nodiscardconsteval

单个 printf 转换体的源级解析器 / Source parser for one printf conversion body

解析单个转换项前导的标志位簇 / Parse the leading flag cluster of one conversion

Parameters
source完整 printf 源字符串 / Full printf source string
pos当前解析位置,成功时推进到已解析标志之后 / Current parse position; advanced past parsed flags
conversion当前正在填充的转换项 / Conversion being filled
Returns
成功返回 Error::None;遇到非法标志时返回对应错误 / Returns Error::None on success, or the first invalid flag error

Definition at line 18 of file printf_frontend_source.hpp.

18 {};
19 size_t field_count = 0;
20 size_t argument_count =
21 0;
22 Error error = Error::None;
23
24 [[nodiscard]] consteval Error Text(size_t, size_t) const { return Error::None; }
25
26 [[nodiscard]] consteval Error Field(const Conversion& conversion)
27 {
28 order[field_count++] = conversion.arg_index;
29 size_t used_argument_count = conversion.arg_index + 1;
30 if (used_argument_count > argument_count)
31 {
32 argument_count = used_argument_count;
33 }
34 return Error::None;
35 }
36};
37
44template <size_t FieldCount, size_t ArgCount>
45struct SourceAnalysis
46{
47 std::array<size_t, FieldCount>
48 order{};
49 std::array<FormatArgumentInfo, ArgCount>
50 args{};
51 Error error = Error::None;

◆ ParseFormatSpec()

Error SourceSyntax::ParseFormatSpec ( std::string_view source,
size_t & pos,
ParsedField & field )
nodiscardconsteval

解析一个 brace 字段中 : 之后的可选 format-spec 部分 / Parse the optional format-spec portion after : inside one brace field

Parameters
source源字符串 / Source string
pos当前解析位置,成功时推进到 format-spec 之后 / Current parse position; advanced past the format-spec on success
field待填充的字段结果 / Parsed field output being filled
Returns
成功返回 Error::None;format-spec 非法时返回对应错误 / Returns Error::None on success, or the first format-spec error

Definition at line 219 of file format_frontend_source.hpp.

222{
228template <size_t FieldCount>
229struct SourceAnalysis
230{
231 std::array<size_t, FieldCount>
232 argument_order{};
234 size_t required_argument_count =
235 0;
236 Error error = Error::None;
237};
238
245template <size_t MaxFieldCount>
246struct SourceAnalysisScratch
247{
248 std::array<size_t, MaxFieldCount> order{};
250 size_t field_count = 0;
251 size_t required_argument_count =
252 0;
253 Error error = Error::None;
254
255 [[nodiscard]] consteval Error Text(size_t, size_t) const { return Error::None; }
256
257 [[nodiscard]] consteval Error Field(const ParsedField& field)
258 {
259 order[field_count++] = field.arg_index;
260 size_t used_argument_count = field.arg_index + 1;
261 if (used_argument_count > required_argument_count)
262 {
263 required_argument_count = used_argument_count;
264 }
265 return Error::None;
266 }
267};
268
269#include "format_frontend_parser.hpp"
270
280[[nodiscard]] consteval Error WalkSource(std::string_view source, auto& visitor)
281{
282 if (HasEmbeddedNul(source))
283 {
284 return Error::EmbeddedNul;
285 }
286
287 size_t pos = 0;
288 size_t text_begin = 0;
289 IndexingState indexing{};
290
291 while (pos < source.size())
292 {
293 if (source[pos] == '{')
294 {
295 if (pos + 1 < source.size() && source[pos + 1] == '{')
296 {
297 auto error = visitor.Text(text_begin, pos - text_begin);
298 if (error != Error::None)
299 {
300 return error;
301 }
302 error = visitor.Text(pos, 1);
303 if (error != Error::None)
304 {
305 return error;
306 }
307 pos += 2;
308 text_begin = pos;
309 continue;
310 }
311
312 auto error = visitor.Text(text_begin, pos - text_begin);
313 if (error != Error::None)
314 {
315 return error;
316 }
317
318 ParsedField field{};
319 error = ParseField(source, pos, indexing, field);
320 if (error != Error::None)
321 {
322 return error;
323 }
324
325 error = visitor.Field(field);
326 if (error != Error::None)
327 {
328 return error;
329 }
330
331 text_begin = pos;
332 continue;
333 }
334
335 if (source[pos] == '}')
336 {
337 if (pos + 1 < source.size() && source[pos + 1] == '}')
338 {
339 auto error = visitor.Text(text_begin, pos - text_begin);
340 if (error != Error::None)
341 {
342 return error;
343 }
344 error = visitor.Text(pos, 1);
345 if (error != Error::None)
346 {
347 return error;
348 }
349 pos += 2;
350 text_begin = pos;
351 continue;
352 }
353
354 return Error::UnmatchedBrace;
355 }
356
357 ++pos;
358 }
359
360 return visitor.Text(text_begin, source.size() - text_begin);
361}
362
372template <Text Source>
373[[nodiscard]] consteval auto Analyze()
374{
375 constexpr auto scratch = []() consteval
376 {
377 SourceAnalysisScratch<Source.Size()> visitor{};
378 visitor.error = WalkSource(std::string_view(Source.Data(), Source.Size()), visitor);
379 return visitor;
380 }();
381
382 SourceAnalysis<scratch.field_count> result{};
383 result.required_argument_count = scratch.required_argument_count;
384 result.error = scratch.error;
385 for (size_t i = 0; i < scratch.field_count; ++i)
386 {
387 result.argument_order[i] = scratch.order[i];
388 }
389 return result;
390}
391} // namespace SourceSyntax

◆ ParseLength()

void SourceSyntax::ParseLength ( std::string_view source,
size_t & pos,
Conversion & conversion )
consteval

解析一个可选的长度修饰符序列 / Parse one optional length modifier sequence

Parameters
source完整 printf 源字符串 / Full printf source string
pos当前解析位置;若存在长度修饰符则推进到其后 / Current parse position; advanced past the length modifier when present
conversion当前正在填充的转换项 / Conversion being filled

Definition at line 126 of file printf_frontend_source.hpp.

126 {
127 return error;
128 }
129
130 pos += 2;
131 text_begin = pos;
132 continue;
133 }
134
135 auto parse_pos = pos;
136 Conversion conversion{};
137 error = Parse(source, parse_pos, indexing, conversion);
138 if (error != Error::None)
139 {
140 return error;
141 }
142
143 error = visitor.Field(conversion);
144 if (error != Error::None)
145 {
146 return error;
147 }
148
149 pos = parse_pos;
150 text_begin = pos;
151 }
152
153 return visitor.Text(text_begin, source.size() - text_begin);
154}
155
165template <Text Source>
166[[nodiscard]] consteval auto Analyze()
167{

◆ ParsePrecision()

Error SourceSyntax::ParsePrecision ( std::string_view source,
size_t & pos,
Conversion & conversion )
nodiscardconsteval

解析一个可选的常量精度字段 / Parse one optional constant precision field

Parameters
source完整 printf 源字符串 / Full printf source string
pos当前解析位置,成功时推进到精度片段之后 / Current parse position; advanced past the precision fragment on success
conversion当前正在填充的转换项 / Conversion being filled
Returns
成功返回 Error::None;精度相关错误时返回对应错误 / Returns Error::None on success, or the first precision-related error

Definition at line 87 of file printf_frontend_source.hpp.

98{
99 if (HasEmbeddedNul(source))
100 {
101 return Error::EmbeddedNul;
102 }
103
104 size_t pos = 0;
105 size_t text_begin = 0;
106 IndexingState indexing{};
107
108 while (pos < source.size())
109 {
110 if (source[pos] != '%')
111 {
112 ++pos;
113 continue;
114 }
115
116 auto error = visitor.Text(text_begin, pos - text_begin);
117 if (error != Error::None)

◆ ParseSpecifier()

Error SourceSyntax::ParseSpecifier ( std::string_view source,
size_t & pos,
Conversion & conversion )
nodiscardconsteval

解析并校验最终的转换说明符字符 / Parse and validate the final conversion specifier token

Parameters
source完整 printf 源字符串 / Full printf source string
pos当前解析位置,成功时推进到说明符之后 / Current parse position; advanced past the specifier on success
conversion当前正在填充的转换项 / Conversion being filled
Returns
成功返回 Error::None;说明符、长度或功能开关不合法时返回对应错误 / Returns Error::None on success, or the first specifier, length, or gate error

Definition at line 179 of file printf_frontend_source.hpp.

183 {
184 return result;
185 }
186 else
187 {
188 ResolvedArgumentVisitor<scratch.field_count, scratch.argument_count> visitor{result};
189 result.error = WalkSource(std::string_view(Source.Data(), Source.Size()), visitor);
190 return result;
191 }
192}
193} // namespace SourceSyntax

◆ ParseUnsigned()

template<typename UInt >
Error SourceSyntax::ParseUnsigned ( std::string_view source,
size_t & pos,
UInt limit,
UInt & value )
nodiscardconsteval

解析一个十进制整数字段片段,并检查是否溢出 / Parse one decimal integer fragment with overflow checking

Template Parameters
UInt目标无符号整数类型 / Target unsigned integer type
Parameters
source源字符串 / Source string
pos当前解析位置,成功时推进到片段之后 / Current parse position; advanced past the fragment on success
limit允许的最大值 / Inclusive upper bound accepted by this field
value输出解析结果 / Parsed result output
Returns
成功返回 Error::None;溢出或语法不合法时返回对应错误 / Returns Error::None on success, or the first overflow or syntax error

Definition at line 115 of file format_frontend_source.hpp.

118 {
119 if (pos + 1 < source.size() && source[pos + 1] == '}')
120 {
121 auto error = visitor.Text(text_begin, pos - text_begin);
122 if (error != Error::None)
123 {
124 return error;
125 }
126 error = visitor.Text(pos, 1);
127 if (error != Error::None)
128 {
129 return error;
130 }
131 pos += 2;
132 text_begin = pos;
133 continue;
134 }
135
136 return Error::UnmatchedBrace;
137 }
138

◆ ParseWidth()

Error SourceSyntax::ParseWidth ( std::string_view source,
size_t & pos,
Conversion & conversion )
nodiscardconsteval

解析一个可选的常量宽度字段 / Parse one optional constant width field

Parameters
source完整 printf 源字符串 / Full printf source string
pos当前解析位置,成功时推进到宽度片段之后 / Current parse position; advanced past the width fragment on success
conversion当前正在填充的转换项 / Conversion being filled
Returns
成功返回 Error::None;动态宽度或非法宽度时返回对应错误 / Returns Error::None on success, or the first dynamic-field or invalid-width error

Definition at line 62 of file printf_frontend_source.hpp.

62{
64
65 [[nodiscard]] consteval Error Text(size_t, size_t) const { return Error::None; }
66
67 [[nodiscard]] consteval Error Field(const Conversion& conversion)
68 {
69 auto field = FieldSelection::BuildFormatField(conversion);
70 auto info = FormatArgumentInfo{
71 .pack = field.pack,
72 .rule = field.rule,
73 };
74 auto& slot = analysis.args[conversion.arg_index];
75 if (slot.rule != FormatArgumentRule::None &&
76 (slot.rule != info.rule || slot.pack != info.pack))

◆ WalkSource()

Error SourceSyntax::WalkSource ( std::string_view source,
auto & visitor )
nodiscardconsteval

遍历一个 brace 源字符串,并发射字面文本片段与已解析字段 / Walk one brace source string and emit literal-text spans plus parsed fields

遍历一个 printf 源字符串,并发射字面文本片段与已解析转换项 / Walk one printf source string and emit literal-text spans plus parsed conversions

Parameters
source当前待解析的 brace 源字符串 / Brace source string to walk
visitor接收文本片段与已解析字段的 visitor / Visitor receiving text spans and parsed fields
Returns
首个源级解析错误;成功时返回 Error::None / Returns the first source-level parse error, or Error::None on success
Parameters
source当前待解析的 printf 源字符串 / Printf source string to walk
visitor接收文本片段与已解析转换项的 visitor / Visitor receiving text spans and parsed conversions
Returns
首个源级解析错误;成功时返回 Error::None / Returns the first source-level parse error, or Error::None on success

Definition at line 62 of file format_frontend_source.hpp.

63{
64 if (HasEmbeddedNul(source))
65 {
66 return Error::EmbeddedNul;
67 }
68
69 size_t pos = 0;
70 size_t text_begin = 0;
71 IndexingState indexing{};
72
73 while (pos < source.size())
74 {
75 if (source[pos] == '{')
76 {
77 if (pos + 1 < source.size() && source[pos + 1] == '{')
78 {
79 auto error = visitor.Text(text_begin, pos - text_begin);
80 if (error != Error::None)
81 {
82 return error;
83 }
84 error = visitor.Text(pos, 1);
85 if (error != Error::None)
86 {
87 return error;
88 }
89 pos += 2;
90 text_begin = pos;
91 continue;
92 }
93
94 auto error = visitor.Text(text_begin, pos - text_begin);
95 if (error != Error::None)
96 {
97 return error;
98 }
99
100 ParsedField field{};
101 error = ParseField(source, pos, indexing, field);
102 if (error != Error::None)
103 {
104 return error;
105 }
106
107 error = visitor.Field(field);
108 if (error != Error::None)
109 {
110 return error;
111 }
112
113 text_begin = pos;
114 continue;
115 }
116
117 if (source[pos] == '}')
118 {
119 if (pos + 1 < source.size() && source[pos + 1] == '}')
120 {
121 auto error = visitor.Text(text_begin, pos - text_begin);
122 if (error != Error::None)
123 {
124 return error;
125 }
126 error = visitor.Text(pos, 1);
127 if (error != Error::None)
128 {
129 return error;
130 }
131 pos += 2;
132 text_begin = pos;
133 continue;
134 }
135
136 return Error::UnmatchedBrace;
137 }
138
139 ++pos;
140 }
141
142 return visitor.Text(text_begin, source.size() - text_begin);
143}