|
13 | 13 | //! This module shows spans for all expressions in the crate
|
14 | 14 | //! to help with compiler debugging.
|
15 | 15 |
|
| 16 | +use std::str::FromStr; |
| 17 | + |
16 | 18 | use ast;
|
17 | 19 | use diagnostic;
|
18 | 20 | use visit;
|
19 | 21 | use visit::Visitor;
|
20 | 22 |
|
| 23 | +enum Mode { |
| 24 | + Expression, |
| 25 | + Pattern, |
| 26 | + Type, |
| 27 | +} |
| 28 | + |
| 29 | +impl FromStr for Mode { |
| 30 | + fn from_str(s: &str) -> Option<Mode> { |
| 31 | + let mode = match s { |
| 32 | + "expr" => Mode::Expression, |
| 33 | + "pat" => Mode::Pattern, |
| 34 | + "ty" => Mode::Type, |
| 35 | + _ => return None |
| 36 | + }; |
| 37 | + Some(mode) |
| 38 | + } |
| 39 | +} |
| 40 | + |
21 | 41 | struct ShowSpanVisitor<'a> {
|
22 | 42 | span_diagnostic: &'a diagnostic::SpanHandler,
|
| 43 | + mode: Mode, |
23 | 44 | }
|
24 | 45 |
|
25 | 46 | impl<'a, 'v> Visitor<'v> for ShowSpanVisitor<'a> {
|
26 | 47 | fn visit_expr(&mut self, e: &ast::Expr) {
|
27 |
| - self.span_diagnostic.span_note(e.span, "expression"); |
| 48 | + if let Mode::Expression = self.mode { |
| 49 | + self.span_diagnostic.span_note(e.span, "expression"); |
| 50 | + } |
28 | 51 | visit::walk_expr(self, e);
|
29 | 52 | }
|
30 | 53 |
|
| 54 | + fn visit_pat(&mut self, p: &ast::Pat) { |
| 55 | + if let Mode::Pattern = self.mode { |
| 56 | + self.span_diagnostic.span_note(p.span, "pattern"); |
| 57 | + } |
| 58 | + visit::walk_pat(self, p); |
| 59 | + } |
| 60 | + |
| 61 | + fn visit_ty(&mut self, t: &ast::Ty) { |
| 62 | + if let Mode::Type = self.mode { |
| 63 | + self.span_diagnostic.span_note(t.span, "type"); |
| 64 | + } |
| 65 | + visit::walk_ty(self, t); |
| 66 | + } |
| 67 | + |
31 | 68 | fn visit_mac(&mut self, macro: &ast::Mac) {
|
32 | 69 | visit::walk_mac(self, macro);
|
33 | 70 | }
|
34 | 71 | }
|
35 | 72 |
|
36 |
| -pub fn run(span_diagnostic: &diagnostic::SpanHandler, krate: &ast::Crate) { |
37 |
| - let mut v = ShowSpanVisitor { span_diagnostic: span_diagnostic }; |
| 73 | +pub fn run(span_diagnostic: &diagnostic::SpanHandler, |
| 74 | + mode: &str, |
| 75 | + krate: &ast::Crate) { |
| 76 | + let mode = match mode.parse() { |
| 77 | + Some(mode) => mode, |
| 78 | + None => return |
| 79 | + }; |
| 80 | + let mut v = ShowSpanVisitor { |
| 81 | + span_diagnostic: span_diagnostic, |
| 82 | + mode: mode, |
| 83 | + }; |
38 | 84 | visit::walk_crate(&mut v, krate);
|
39 | 85 | }
|
0 commit comments