]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/show_span.rs
rollup merge of #20518: nagisa/weighted-bool
[rust.git] / src / libsyntax / show_span.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Span debugger
12 //!
13 //! This module shows spans for all expressions in the crate
14 //! to help with compiler debugging.
15
16 use std::str::FromStr;
17
18 use ast;
19 use diagnostic;
20 use visit;
21 use visit::Visitor;
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
41 struct ShowSpanVisitor<'a> {
42     span_diagnostic: &'a diagnostic::SpanHandler,
43     mode: Mode,
44 }
45
46 impl<'a, 'v> Visitor<'v> for ShowSpanVisitor<'a> {
47     fn visit_expr(&mut self, e: &ast::Expr) {
48         if let Mode::Expression = self.mode {
49             self.span_diagnostic.span_note(e.span, "expression");
50         }
51         visit::walk_expr(self, e);
52     }
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
68     fn visit_mac(&mut self, macro: &ast::Mac) {
69         visit::walk_mac(self, macro);
70     }
71 }
72
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     };
84     visit::walk_crate(&mut v, krate);
85 }