Skip to content

Commit 4bc9052

Browse files
brsongraydon
authored andcommitted
Add debug logging for #fmt conv. Implement peek_num fn
1 parent 77a0dc0 commit 4bc9052

File tree

1 file changed

+123
-21
lines changed

1 file changed

+123
-21
lines changed

src/comp/front/extfmt.rs

Lines changed: 123 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,13 @@ tag piece {
7171
piece_conv(conv);
7272
}
7373

74-
fn bad_fmt_call() {
75-
log "malformed #fmt call";
76-
fail;
77-
}
78-
7974
// TODO: Need to thread parser through here to handle errors correctly
8075
fn expand_syntax_ext(vec[@ast.expr] args,
8176
option.t[@ast.expr] body) -> @ast.expr {
8277

8378
if (_vec.len[@ast.expr](args) == 0u) {
84-
bad_fmt_call();
79+
log "malformed #fmt call";
80+
fail;
8581
}
8682

8783
auto fmt = expr_to_str(args.(0));
@@ -101,7 +97,7 @@ fn expr_to_str(@ast.expr expr) -> str {
10197
}
10298
}
10399
}
104-
bad_fmt_call();
100+
log "malformed #fmt call";
105101
fail;
106102
}
107103

@@ -146,19 +142,29 @@ fn parse_fmt_string(str s) -> vec[piece] {
146142
ret pieces;
147143
}
148144

149-
fn peek_num(str s, uint i, uint lim) -> option.t[tup(int, int)] {
145+
fn peek_num(str s, uint i, uint lim) -> option.t[tup(uint, uint)] {
150146
if (i >= lim) {
151-
ret none[tup(int, int)];
152-
} else {
153-
ret none[tup(int, int)];
154-
/*if ('0' <= c && c <= '9') {
155-
log c;
156-
fail;
157-
} else {
158-
ret option.none[tup(int, int)];
147+
ret none[tup(uint, uint)];
148+
}
149+
150+
// FIXME: Presumably s.(i) will return char eventually
151+
auto c = s.(i);
152+
if (!('0' as u8 <= c && c <= '9' as u8)) {
153+
ret option.none[tup(uint, uint)];
154+
}
155+
156+
auto n = (c - ('0' as u8)) as uint;
157+
alt (peek_num(s, i + 1u, lim)) {
158+
case (none[tup(uint, uint)]) {
159+
ret some[tup(uint, uint)](tup(n, i + 1u));
160+
}
161+
case (some[tup(uint, uint)](?next)) {
162+
auto m = next._0;
163+
auto j = next._1;
164+
ret some[tup(uint, uint)](tup(n * 10u + m, j));
159165
}
160-
*/
161166
}
167+
162168
}
163169

164170
fn parse_conversion(str s, uint i, uint lim) -> tup(piece, uint) {
@@ -182,10 +188,10 @@ fn parse_parameter(str s, uint i, uint lim) -> tup(option.t[int], uint) {
182188

183189
auto num = peek_num(s, i, lim);
184190
alt (num) {
185-
case (none[tup(int, int)]) {
191+
case (none[tup(uint, uint)]) {
186192
ret tup(none[int], i);
187193
}
188-
case (some[tup(int, int)](?t)) {
194+
case (some[tup(uint, uint)](?t)) {
189195
fail;
190196
}
191197
}
@@ -342,6 +348,98 @@ fn pieces_to_expr(vec[piece] pieces, vec[@ast.expr] args) -> @ast.expr {
342348
}
343349
}
344350

351+
fn log_conv(conv c) {
352+
alt (c.param) {
353+
case (some[int](?p)) {
354+
log "param: " + std._int.to_str(p, 10u);
355+
}
356+
case (_) {
357+
log "param: none";
358+
}
359+
}
360+
for (flag f in c.flags) {
361+
alt (f) {
362+
case (flag_left_justify) {
363+
log "flag: left justify";
364+
}
365+
case (flag_left_zero_pad) {
366+
log "flag: left zero pad";
367+
}
368+
case (flag_left_space_pad) {
369+
log "flag: left space pad";
370+
}
371+
case (flag_plus_if_positive) {
372+
log "flag: plus if positive";
373+
}
374+
case (flag_alternate) {
375+
log "flag: alternate";
376+
}
377+
}
378+
}
379+
alt (c.width) {
380+
case (count_is(?i)) {
381+
log "width: count is " + std._int.to_str(i, 10u);
382+
}
383+
case (count_is_param(?i)) {
384+
log "width: count is param " + std._int.to_str(i, 10u);
385+
}
386+
case (count_is_next_param) {
387+
log "width: count is next param";
388+
}
389+
case (count_implied) {
390+
log "width: count is implied";
391+
}
392+
}
393+
alt (c.precision) {
394+
case (count_is(?i)) {
395+
log "prec: count is " + std._int.to_str(i, 10u);
396+
}
397+
case (count_is_param(?i)) {
398+
log "prec: count is param " + std._int.to_str(i, 10u);
399+
}
400+
case (count_is_next_param) {
401+
log "prec: count is next param";
402+
}
403+
case (count_implied) {
404+
log "prec: count is implied";
405+
}
406+
}
407+
alt (c.ty) {
408+
case (ty_bool) {
409+
log "type: bool";
410+
}
411+
case (ty_str) {
412+
log "type: str";
413+
}
414+
case (ty_char) {
415+
log "type: char";
416+
}
417+
case (ty_int(?s)) {
418+
alt (s) {
419+
case (signed) {
420+
log "type: signed";
421+
}
422+
case (unsigned) {
423+
log "type: unsigned";
424+
}
425+
}
426+
}
427+
case (ty_bits) {
428+
log "type: bits";
429+
}
430+
case (ty_hex(?cs)) {
431+
alt (cs) {
432+
case (case_upper) {
433+
log "type: uhex";
434+
}
435+
case (case_lower) {
436+
log "type: lhex";
437+
}
438+
}
439+
}
440+
}
441+
}
442+
345443
auto sp = args.(0).span;
346444
auto n = 0u;
347445
auto tmp_expr = make_new_str(sp, "");
@@ -358,6 +456,10 @@ fn pieces_to_expr(vec[piece] pieces, vec[@ast.expr] args) -> @ast.expr {
358456
fail;
359457
}
360458

459+
// TODO: Remove debug logging
460+
log "Building conversion:";
461+
log_conv(conv);
462+
361463
n += 1u;
362464
auto arg_expr = args.(n);
363465
auto c_expr = make_new_conv(conv, arg_expr);
@@ -366,10 +468,10 @@ fn pieces_to_expr(vec[piece] pieces, vec[@ast.expr] args) -> @ast.expr {
366468
}
367469
}
368470

369-
// TODO: Remove this print and return the real expanded AST
471+
// TODO: Remove this debug logging
370472
log "dumping expanded ast:";
371473
log pretty.print_expr(tmp_expr);
372-
ret make_new_str(sp, "TODO");
474+
ret tmp_expr;
373475
}
374476

375477
//

0 commit comments

Comments
 (0)