Skip to content

Commit 6421147

Browse files
committed
---
yaml --- r: 149108 b: refs/heads/try2 c: ca65c00 h: refs/heads/master v: v3
1 parent 52c6cfe commit 6421147

File tree

2 files changed

+128
-117
lines changed

2 files changed

+128
-117
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: b2b4c79b179413d4a5e1d706844b11dfca8226c8
8+
refs/heads/try2: ca65c00ef2e1fb7373c97085315703887ee4d53c
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libsyntax/ext/format.rs

Lines changed: 127 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rsparse = parse;
2121
use std::fmt::parse;
2222
use std::hashmap::{HashMap, HashSet};
2323
use std::vec;
24+
use std::cell::RefCell;
2425

2526
#[deriving(Eq)]
2627
enum ArgumentType {
@@ -367,169 +368,179 @@ impl<'a> Context<'a> {
367368
return ~[unnamed, allow_dead_code];
368369
}
369370

370-
/// Translate a `parse::Piece` to a static `rt::Piece`
371-
fn trans_piece(&mut self, piece: &parse::Piece) -> @ast::Expr {
372-
let sp = self.fmtsp;
373-
let parsepath = |s: &str| {
374-
~[self.ecx.ident_of("std"), self.ecx.ident_of("fmt"),
375-
self.ecx.ident_of("parse"), self.ecx.ident_of(s)]
376-
};
377-
let rtpath = |s: &str| {
378-
~[self.ecx.ident_of("std"), self.ecx.ident_of("fmt"),
379-
self.ecx.ident_of("rt"), self.ecx.ident_of(s)]
380-
};
381-
let ctpath = |s: &str| {
382-
~[self.ecx.ident_of("std"), self.ecx.ident_of("fmt"),
383-
self.ecx.ident_of("parse"), self.ecx.ident_of(s)]
384-
};
385-
let none = self.ecx.path_global(sp, ~[
371+
fn parsepath(&self, s: &str) -> ~[ast::Ident] {
372+
~[self.ecx.ident_of("std"), self.ecx.ident_of("fmt"),
373+
self.ecx.ident_of("parse"), self.ecx.ident_of(s)]
374+
}
375+
376+
fn rtpath(&self, s: &str) -> ~[ast::Ident] {
377+
~[self.ecx.ident_of("std"), self.ecx.ident_of("fmt"),
378+
self.ecx.ident_of("rt"), self.ecx.ident_of(s)]
379+
}
380+
381+
fn ctpath(&self, s: &str) -> ~[ast::Ident] {
382+
~[self.ecx.ident_of("std"), self.ecx.ident_of("fmt"),
383+
self.ecx.ident_of("parse"), self.ecx.ident_of(s)]
384+
}
385+
386+
fn none(&self) -> @ast::Expr {
387+
let none = self.ecx.path_global(self.fmtsp, ~[
386388
self.ecx.ident_of("std"),
387389
self.ecx.ident_of("option"),
388390
self.ecx.ident_of("None")]);
389-
let none = self.ecx.expr_path(none);
390-
let some = |e: @ast::Expr| {
391-
let p = self.ecx.path_global(sp, ~[
391+
self.ecx.expr_path(none)
392+
}
393+
394+
fn some(&self, e: @ast::Expr) -> @ast::Expr {
395+
let p = self.ecx.path_global(self.fmtsp, ~[
392396
self.ecx.ident_of("std"),
393397
self.ecx.ident_of("option"),
394398
self.ecx.ident_of("Some")]);
395-
let p = self.ecx.expr_path(p);
396-
self.ecx.expr_call(sp, p, ~[e])
397-
};
398-
let trans_count = |c: parse::Count| {
399-
match c {
400-
parse::CountIs(i) => {
401-
self.ecx.expr_call_global(sp, rtpath("CountIs"),
402-
~[self.ecx.expr_uint(sp, i)])
403-
}
404-
parse::CountIsParam(i) => {
405-
self.ecx.expr_call_global(sp, rtpath("CountIsParam"),
406-
~[self.ecx.expr_uint(sp, i)])
407-
}
408-
parse::CountImplied => {
409-
let path = self.ecx.path_global(sp, rtpath("CountImplied"));
410-
self.ecx.expr_path(path)
411-
}
412-
parse::CountIsNextParam => {
413-
let path = self.ecx.path_global(sp, rtpath("CountIsNextParam"));
414-
self.ecx.expr_path(path)
415-
}
416-
parse::CountIsName(n) => {
417-
let i = match self.name_positions.find_equiv(&n) {
418-
Some(&i) => i,
419-
None => 0, // error already emitted elsewhere
420-
};
421-
let i = i + self.args.len();
422-
self.ecx.expr_call_global(sp, rtpath("CountIsParam"),
423-
~[self.ecx.expr_uint(sp, i)])
424-
}
399+
let p = self.ecx.expr_path(p);
400+
self.ecx.expr_call(self.fmtsp, p, ~[e])
401+
}
402+
403+
fn trans_count(&self, c: parse::Count) -> @ast::Expr {
404+
let sp = self.fmtsp;
405+
match c {
406+
parse::CountIs(i) => {
407+
self.ecx.expr_call_global(sp, self.rtpath("CountIs"),
408+
~[self.ecx.expr_uint(sp, i)])
425409
}
426-
};
427-
let trans_method = |method: &parse::Method| {
428-
let method = match *method {
429-
parse::Select(ref arms, ref default) => {
430-
let arms = arms.iter().map(|arm| {
431-
let p = self.ecx.path_global(sp, rtpath("SelectArm"));
410+
parse::CountIsParam(i) => {
411+
self.ecx.expr_call_global(sp, self.rtpath("CountIsParam"),
412+
~[self.ecx.expr_uint(sp, i)])
413+
}
414+
parse::CountImplied => {
415+
let path = self.ecx.path_global(sp, self.rtpath("CountImplied"));
416+
self.ecx.expr_path(path)
417+
}
418+
parse::CountIsNextParam => {
419+
let path = self.ecx.path_global(sp, self.rtpath("CountIsNextParam"));
420+
self.ecx.expr_path(path)
421+
}
422+
parse::CountIsName(n) => {
423+
let i = match self.name_positions.find_equiv(&n) {
424+
Some(&i) => i,
425+
None => 0, // error already emitted elsewhere
426+
};
427+
let i = i + self.args.len();
428+
self.ecx.expr_call_global(sp, self.rtpath("CountIsParam"),
429+
~[self.ecx.expr_uint(sp, i)])
430+
}
431+
}
432+
}
433+
434+
fn trans_method(&mut self, method: &parse::Method) -> @ast::Expr {
435+
let sp = self.fmtsp;
436+
let method = match *method {
437+
parse::Select(ref arms, ref default) => {
438+
let arms = arms.iter().map(|arm| {
439+
let p = self.ecx.path_global(sp, self.rtpath("SelectArm"));
432440
let result = arm.result.iter().map(|p| {
433441
self.trans_piece(p)
434442
}).collect();
435443
let s = token::intern_and_get_ident(arm.selector);
436444
let selector = self.ecx.expr_str(sp, s);
437445
self.ecx.expr_struct(sp, p, ~[
438-
self.ecx.field_imm(sp,
439-
self.ecx.ident_of("selector"),
440-
selector),
441-
self.ecx.field_imm(sp, self.ecx.ident_of("result"),
442-
self.ecx.expr_vec_slice(sp, result)),
443-
])
446+
self.ecx.field_imm(sp,
447+
self.ecx.ident_of("selector"),
448+
selector),
449+
self.ecx.field_imm(sp, self.ecx.ident_of("result"),
450+
self.ecx.expr_vec_slice(sp, result)),
451+
])
444452
}).collect();
445-
let default = default.iter().map(|p| {
453+
let default = default.iter().map(|p| {
446454
self.trans_piece(p)
447455
}).collect();
448-
self.ecx.expr_call_global(sp, rtpath("Select"), ~[
456+
self.ecx.expr_call_global(sp, self.rtpath("Select"), ~[
449457
self.ecx.expr_vec_slice(sp, arms),
450458
self.ecx.expr_vec_slice(sp, default),
451-
])
452-
}
453-
parse::Plural(offset, ref arms, ref default) => {
454-
let offset = match offset {
455-
Some(i) => { some(self.ecx.expr_uint(sp, i)) }
456-
None => { none.clone() }
457-
};
458-
let arms = arms.iter().map(|arm| {
459-
let p = self.ecx.path_global(sp, rtpath("PluralArm"));
459+
])
460+
}
461+
parse::Plural(offset, ref arms, ref default) => {
462+
let offset = match offset {
463+
Some(i) => { self.some(self.ecx.expr_uint(sp, i)) }
464+
None => { self.none() }
465+
};
466+
let arms = arms.iter().map(|arm| {
467+
let p = self.ecx.path_global(sp, self.rtpath("PluralArm"));
460468
let result = arm.result.iter().map(|p| {
461-
self.trans_piece(p)
462-
}).collect();
469+
self.trans_piece(p)
470+
}).collect();
463471
let (lr, selarg) = match arm.selector {
464472
parse::Keyword(t) => {
465-
let p = ctpath(format!("{:?}", t));
473+
let p = self.ctpath(format!("{:?}", t));
466474
let p = self.ecx.path_global(sp, p);
467-
(rtpath("Keyword"), self.ecx.expr_path(p))
475+
(self.rtpath("Keyword"), self.ecx.expr_path(p))
468476
}
469477
parse::Literal(i) => {
470-
(rtpath("Literal"), self.ecx.expr_uint(sp, i))
478+
(self.rtpath("Literal"), self.ecx.expr_uint(sp, i))
471479
}
472480
};
473481
let selector = self.ecx.expr_call_global(sp,
474-
lr, ~[selarg]);
482+
lr, ~[selarg]);
475483
self.ecx.expr_struct(sp, p, ~[
476-
self.ecx.field_imm(sp,
477-
self.ecx.ident_of("selector"),
478-
selector),
479-
self.ecx.field_imm(sp, self.ecx.ident_of("result"),
480-
self.ecx.expr_vec_slice(sp, result)),
481-
])
484+
self.ecx.field_imm(sp,
485+
self.ecx.ident_of("selector"),
486+
selector),
487+
self.ecx.field_imm(sp, self.ecx.ident_of("result"),
488+
self.ecx.expr_vec_slice(sp, result)),
489+
])
482490
}).collect();
483-
let default = default.iter().map(|p| {
491+
let default = default.iter().map(|p| {
484492
self.trans_piece(p)
485493
}).collect();
486-
self.ecx.expr_call_global(sp, rtpath("Plural"), ~[
494+
self.ecx.expr_call_global(sp, self.rtpath("Plural"), ~[
487495
offset,
488496
self.ecx.expr_vec_slice(sp, arms),
489497
self.ecx.expr_vec_slice(sp, default),
490-
])
491-
}
492-
};
493-
let life = self.ecx.lifetime(sp, self.ecx.ident_of("static"));
494-
let ty = self.ecx.ty_path(self.ecx.path_all(
498+
])
499+
}
500+
};
501+
let life = self.ecx.lifetime(sp, self.ecx.ident_of("static"));
502+
let ty = self.ecx.ty_path(self.ecx.path_all(
495503
sp,
496504
true,
497-
rtpath("Method"),
505+
self.rtpath("Method"),
498506
opt_vec::with(life),
499507
~[]
500-
), None);
501-
let st = ast::ItemStatic(ty, ast::MutImmutable, method);
502-
let static_name = self.ecx.ident_of(format!("__STATIC_METHOD_{}",
503-
self.method_statics.len()));
504-
let item = self.ecx.item(sp, static_name, self.static_attrs(), st);
505-
self.method_statics.push(item);
506-
self.ecx.expr_ident(sp, static_name)
507-
};
508+
), None);
509+
let st = ast::ItemStatic(ty, ast::MutImmutable, method);
510+
let static_name = self.ecx.ident_of(format!("__STATIC_METHOD_{}",
511+
self.method_statics.len()));
512+
let item = self.ecx.item(sp, static_name, self.static_attrs(), st);
513+
self.method_statics.push(item);
514+
self.ecx.expr_ident(sp, static_name)
515+
}
508516

517+
/// Translate a `parse::Piece` to a static `rt::Piece`
518+
fn trans_piece(&mut self, piece: &parse::Piece) -> @ast::Expr {
519+
let sp = self.fmtsp;
509520
match *piece {
510521
parse::String(s) => {
511522
let s = token::intern_and_get_ident(s);
512523
self.ecx.expr_call_global(sp,
513-
rtpath("String"),
524+
self.rtpath("String"),
514525
~[
515526
self.ecx.expr_str(sp, s)
516527
])
517528
}
518529
parse::CurrentArgument => {
519530
let nil = self.ecx.expr_lit(sp, ast::LitNil);
520-
self.ecx.expr_call_global(sp, rtpath("CurrentArgument"), ~[nil])
531+
self.ecx.expr_call_global(sp, self.rtpath("CurrentArgument"), ~[nil])
521532
}
522533
parse::Argument(ref arg) => {
523534
// Translate the position
524535
let pos = match arg.position {
525536
// These two have a direct mapping
526537
parse::ArgumentNext => {
527538
let path = self.ecx.path_global(sp,
528-
rtpath("ArgumentNext"));
539+
self.rtpath("ArgumentNext"));
529540
self.ecx.expr_path(path)
530541
}
531542
parse::ArgumentIs(i) => {
532-
self.ecx.expr_call_global(sp, rtpath("ArgumentIs"),
543+
self.ecx.expr_call_global(sp, self.rtpath("ArgumentIs"),
533544
~[self.ecx.expr_uint(sp, i)])
534545
}
535546
// Named arguments are converted to positional arguments at
@@ -540,7 +551,7 @@ impl<'a> Context<'a> {
540551
None => 0, // error already emitted elsewhere
541552
};
542553
let i = i + self.args.len();
543-
self.ecx.expr_call_global(sp, rtpath("ArgumentIs"),
554+
self.ecx.expr_call_global(sp, self.rtpath("ArgumentIs"),
544555
~[self.ecx.expr_uint(sp, i)])
545556
}
546557
};
@@ -550,20 +561,20 @@ impl<'a> Context<'a> {
550561
let fill = self.ecx.expr_lit(sp, ast::LitChar(fill as u32));
551562
let align = match arg.format.align {
552563
parse::AlignLeft => {
553-
self.ecx.path_global(sp, parsepath("AlignLeft"))
564+
self.ecx.path_global(sp, self.parsepath("AlignLeft"))
554565
}
555566
parse::AlignRight => {
556-
self.ecx.path_global(sp, parsepath("AlignRight"))
567+
self.ecx.path_global(sp, self.parsepath("AlignRight"))
557568
}
558569
parse::AlignUnknown => {
559-
self.ecx.path_global(sp, parsepath("AlignUnknown"))
570+
self.ecx.path_global(sp, self.parsepath("AlignUnknown"))
560571
}
561572
};
562573
let align = self.ecx.expr_path(align);
563574
let flags = self.ecx.expr_uint(sp, arg.format.flags);
564-
let prec = trans_count(arg.format.precision);
565-
let width = trans_count(arg.format.width);
566-
let path = self.ecx.path_global(sp, rtpath("FormatSpec"));
575+
let prec = self.trans_count(arg.format.precision);
576+
let width = self.trans_count(arg.format.width);
577+
let path = self.ecx.path_global(sp, self.rtpath("FormatSpec"));
567578
let fmt = self.ecx.expr_struct(sp, path, ~[
568579
self.ecx.field_imm(sp, self.ecx.ident_of("fill"), fill),
569580
self.ecx.field_imm(sp, self.ecx.ident_of("align"), align),
@@ -574,19 +585,19 @@ impl<'a> Context<'a> {
574585

575586
// Translate the method (if any)
576587
let method = match arg.method {
577-
None => { none.clone() }
588+
None => { self.none() }
578589
Some(ref m) => {
579-
let m = trans_method(*m);
580-
some(self.ecx.expr_addr_of(sp, m))
590+
let m = self.trans_method(*m);
591+
self.some(self.ecx.expr_addr_of(sp, m))
581592
}
582593
};
583-
let path = self.ecx.path_global(sp, rtpath("Argument"));
594+
let path = self.ecx.path_global(sp, self.rtpath("Argument"));
584595
let s = self.ecx.expr_struct(sp, path, ~[
585596
self.ecx.field_imm(sp, self.ecx.ident_of("position"), pos),
586597
self.ecx.field_imm(sp, self.ecx.ident_of("format"), fmt),
587598
self.ecx.field_imm(sp, self.ecx.ident_of("method"), method),
588599
]);
589-
self.ecx.expr_call_global(sp, rtpath("Argument"), ~[s])
600+
self.ecx.expr_call_global(sp, self.rtpath("Argument"), ~[s])
590601
}
591602
}
592603
}

0 commit comments

Comments
 (0)