1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
*/
#include <synext_macro.hpp>
#include "../parse/common.hpp"
#include "../parse/parseerror.hpp"
#include "../parse/tokentree.hpp"
#include "../parse/lex.hpp"
#include <ast/crate.hpp> // for m_load_std
namespace {
struct FmtArgs
{
enum class Align {
Unspec,
Left,
Center,
Right,
};
enum class Sign {
Unspec,
Plus,
Minus,
};
Align align = Align::Unspec;
char align_char = ' ';
Sign sign = Sign::Unspec;
bool alternate = false;
bool zero_pad = false;
bool width_is_arg = false;
unsigned int width = 0;
bool prec_is_arg = false;
unsigned int prec = 0;
};
struct FmtFrag
{
::std::string leading_text;
unsigned int arg_index;
const char* trait_name;
// TODO: Support case where this hasn't been edited (telling the formatter that it has nothing to apply)
FmtArgs args;
};
::std::tuple< ::std::vector<FmtFrag>, ::std::string> parse_format_string(const Span& sp, const ::std::string& format_string, const ::std::map< ::std::string,unsigned int>& named, unsigned int n_free)
{
unsigned int n_named = named.size();
unsigned int next_free = 0;
::std::vector<FmtFrag> frags;
::std::string cur_literal;
const char* s = format_string.c_str();
for( ; *s; s ++)
{
if( *s != '{' )
{
if( *s == '}' ) {
s ++;
if( *s != '}' )
ERROR(sp, E0000, "'}' must be escaped as '}}' in format strings");
// - fall with *s == '}'
}
cur_literal += *s;
}
else
{
s ++;
if( *s == '{' ) {
cur_literal += '{';
continue ;
}
unsigned int index = ~0u;
const char* trait_name;
FmtArgs args;
// Formatting parameter
if( *s != ':' && *s != '}' ) {
// Parse either an integer or an identifer
TODO(sp, "Parse named/positional formatting fragment at \"" << s);
}
else {
// Leave (for now)
// - If index is ~0u at the end of this block, it's set to the next arg
// - This allows {:.*} to format correctly (taking <prec> then <arg>)
}
// If next character is ':', parse extra information
if( *s == ':' ) {
s ++; // eat ':'
// Alignment
if( s[0] != '\0' && (s[1] == '<' || s[1] == '^' || s[1] == '>') ) {
args.align_char = s[0];
s ++;
}
if( *s == '<' ) {
args.align = FmtArgs::Align::Left;
s ++;
}
else if( *s == '^' ) {
args.align = FmtArgs::Align::Center;
s ++;
}
else if( *s == '>' ) {
args.align = FmtArgs::Align::Right;
s ++;
}
else {
//args.align = FmtArgs::Align::Unspec;
}
// Sign
if( *s == '+' ) {
args.sign = FmtArgs::Sign::Plus;
s ++;
}
else if( *s == '-' ) {
args.sign = FmtArgs::Sign::Minus;
s ++;
}
else {
args.sign = FmtArgs::Sign::Unspec;
}
if( *s == '#' ) {
args.alternate = true;
s ++;
}
else {
//args.alternate = false;
}
if( *s == '0' ) {
args.zero_pad = true;
s ++;
}
else {
//args.zero_pad = false;
}
// Padded width
if( ::std::isdigit(*s) /*|| *s == '*'*/ ) {
unsigned int val = 0;
while( ::std::isdigit(*s) )
{
val *= 10;
val += *s - '0';
s ++;
}
args.width = val;
if( *s == '$' ) {
args.width_is_arg = true;
s ++;
}
else {
//args.width_is_arg = false;
}
}
// Precision
if( *s == '.' ) {
s ++;
// '*' - Use next argument
if( *s == '*' ) {
args.prec_is_arg = true;
if( next_free == n_free ) {
ERROR(sp, E0000, "Not enough arguments passed, expected at least " << n_free+1);
}
args.prec = next_free + n_named;
next_free ++;
}
else {
unsigned int val = 0;
while( ::std::isdigit(*s) )
{
val *= 10;
val += *s - '0';
s ++;
}
args.prec = val;
if( *s == '$' ) {
args.prec_is_arg = true;
s ++;
}
else {
//args.prec_is_arg = false;
}
}
}
// Parse ident?
// - Lazy way is to just handle a single char and ensure that it is just a single char
if( s[0] != '}' && s[0] != '\0' && s[1] != '}' ) {
TODO(sp, "Parse formatting fragment at \"" << s << "\" (long type)");
}
switch(s[0])
{
case '\0':
ERROR(sp, E0000, "Unexpected end of formatting string");
default:
ERROR(sp, E0000, "Unknown formatting type specifier '" << *s << "'");
case '}': trait_name = "Display"; break;
case '?': s++; trait_name = "Debug" ; break;
case 'b': s++; trait_name = "Binary"; break;
case 'o': s++; trait_name = "Octal" ; break;
case 'x': s++; trait_name = "LowerHex"; break;
case 'X': s++; trait_name = "UpperHex"; break;
case 'p': s++; trait_name = "Pointer" ; break;
case 'e': s++; trait_name = "LowerExp"; break;
case 'E': s++; trait_name = "UpperExp"; break;
}
assert(*s == '}');
}
else {
if( *s != '}' )
ERROR(sp, E0000, "Malformed formatting fragment, unexpected " << *s);
// Otherwise, it's just a trivial Display call
trait_name = "Display";
}
// Set index if unspecified
if( index == ~0u )
{
if( next_free == n_free ) {
ERROR(sp, E0000, "Not enough arguments passed, expected at least " << n_free+1);
}
index = next_free + n_named;
next_free ++;
}
frags.push_back( FmtFrag {
mv$(cur_literal),
index, trait_name,
mv$(args)
});
}
}
return ::std::make_tuple( mv$(frags), mv$(cur_literal) );
}
}
class CFormatArgsExpander:
public ExpandProcMacro
{
bool expand_early() const override { return true; }
::std::unique_ptr<TokenStream> expand(const Span& sp, const ::AST::Crate& crate, const ::std::string& ident, const TokenTree& tt, AST::Module& mod) override
{
Token tok;
auto lex = TTStream(tt);
if( ident != "" )
ERROR(sp, E0000, "format_args! doesn't take an ident");
auto n = Parse_ExprVal(lex);
auto format_string = dynamic_cast<AST::ExprNode_String&>(*n).m_value;
::std::map< ::std::string, unsigned int> named_args_index;
::std::vector<TokenTree> named_args;
::std::vector<TokenTree> free_args;
// - Parse the arguments
while( GET_TOK(tok, lex) == TOK_COMMA )
{
// - Named parameters
if( lex.lookahead(0) == TOK_IDENT && lex.lookahead(1) == TOK_EQUAL )
{
GET_CHECK_TOK(tok, lex, TOK_IDENT);
auto name = mv$(tok.str());
GET_CHECK_TOK(tok, lex, TOK_EQUAL);
auto expr_tt = Parse_TT_Expr(lex);
auto ins_rv = named_args_index.insert( ::std::make_pair(mv$(name), named_args.size()) );
if( ins_rv.second == false ) {
ERROR(sp, E0000, "Duplicate definition of named argument `" << ins_rv.first->first << "`");
}
named_args.push_back( mv$(expr_tt) );
}
// - Free parameters
else
{
auto expr_tt = Parse_TT_Expr(lex);
free_args.push_back( mv$(expr_tt) );
}
}
// - Parse the format string
::std::vector< FmtFrag> fragments;
::std::string tail;
::std::tie( fragments, tail ) = parse_format_string(sp, format_string, named_args_index, free_args.size());
// TODO: Properly expand format_args! (requires mangling to fit ::core::fmt::rt::v1)
// - For now, just emits the text with no corresponding format fragments
::std::vector<TokenTree> toks;
{
// TODO: Figure out what path to use (::fmt, ::core::fmt, or ::std::fmt)
switch(crate.m_load_std)
{
case ::AST::Crate::LOAD_NONE:
break;
case ::AST::Crate::LOAD_CORE:
toks.push_back( TokenTree(TOK_DOUBLE_COLON) );
toks.push_back( Token(TOK_STRING, "core") );
break;
case ::AST::Crate::LOAD_STD:
toks.push_back( TokenTree(TOK_DOUBLE_COLON) );
toks.push_back( Token(TOK_IDENT, "std") );
break;
}
toks.push_back( TokenTree(TOK_DOUBLE_COLON) );
toks.push_back( Token(TOK_IDENT, "fmt") );
toks.push_back( TokenTree(TOK_DOUBLE_COLON) );
toks.push_back( Token(TOK_IDENT, "Arguments") );
toks.push_back( TokenTree(TOK_DOUBLE_COLON) );
toks.push_back( Token(TOK_IDENT, "new_v1") );
toks.push_back( TokenTree(TOK_PAREN_OPEN) );
{
toks.push_back( TokenTree(TOK_AMP) );
toks.push_back( TokenTree(TOK_SQUARE_OPEN) );
for(const auto& frag : fragments ) {
toks.push_back( Token(TOK_STRING, frag.leading_text) );
toks.push_back( TokenTree(TOK_COMMA) );
}
toks.push_back( TokenTree(TOK_SQUARE_CLOSE) );
toks.push_back( TokenTree(TOK_COMMA) );
toks.push_back( TokenTree(TOK_AMP) );
toks.push_back( TokenTree(TOK_SQUARE_OPEN) );
toks.push_back( TokenTree(TOK_SQUARE_CLOSE) );
}
toks.push_back( TokenTree(TOK_PAREN_CLOSE) );
}
return box$( TTStreamO(TokenTree(mv$(toks))) );
}
};
STATIC_MACRO("format_args", CFormatArgsExpander);
|