]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/errors/json.rs
Include source text in JSON errors
[rust.git] / src / libsyntax / errors / json.rs
1 // Copyright 2015 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 //! A JSON emitter for errors.
12 //!
13 //! This works by converting errors to a simplified structural format (see the
14 //! structs at the start of the file) and then serialising them. These should
15 //! contain as much information about the error as possible.
16 //!
17 //! The format of the JSON output should be considered *unstable*. For now the
18 //! structs at the end of this file (Diagnostic*) specify the error format.
19
20 // FIXME spec the JSON output properly.
21
22
23 use codemap::{Span, MultiSpan, CodeMap};
24 use diagnostics::registry::Registry;
25 use errors::{Level, DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion};
26 use errors::emitter::Emitter;
27
28 use std::rc::Rc;
29 use std::io::{self, Write};
30
31 use rustc_serialize::json::as_json;
32
33 pub struct JsonEmitter {
34     dst: Box<Write + Send>,
35     registry: Option<Registry>,
36     cm: Rc<CodeMap>,
37 }
38
39 impl JsonEmitter {
40     pub fn basic() -> JsonEmitter {
41         JsonEmitter::stderr(None, Rc::new(CodeMap::new()))
42     }
43
44     pub fn stderr(registry: Option<Registry>,
45                   code_map: Rc<CodeMap>) -> JsonEmitter {
46         JsonEmitter {
47             dst: Box::new(io::stderr()),
48             registry: registry,
49             cm: code_map,
50         }
51     }
52 }
53
54 impl Emitter for JsonEmitter {
55     fn emit(&mut self, span: Option<&MultiSpan>, msg: &str, code: Option<&str>, level: Level) {
56         let data = Diagnostic::new(span, msg, code, level, self);
57         if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) {
58             panic!("failed to print diagnostics: {:?}", e);
59         }
60     }
61
62     fn custom_emit(&mut self, sp: &RenderSpan, msg: &str, level: Level) {
63         let data = Diagnostic::from_render_span(sp, msg, level, self);
64         if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) {
65             panic!("failed to print diagnostics: {:?}", e);
66         }
67     }
68
69     fn emit_struct(&mut self, db: &DiagnosticBuilder) {
70         let data = Diagnostic::from_diagnostic_builder(db, self);
71         if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) {
72             panic!("failed to print diagnostics: {:?}", e);
73         }
74     }
75 }
76
77 // The following data types are provided just for serialisation.
78
79 #[derive(RustcEncodable)]
80 struct Diagnostic<'a> {
81     /// The primary error message.
82     message: &'a str,
83     code: Option<DiagnosticCode>,
84     /// "error: internal compiler error", "error", "warning", "note", "help".
85     level: &'static str,
86     spans: Vec<DiagnosticSpan>,
87     /// Assocaited diagnostic messages.
88     children: Vec<Diagnostic<'a>>,
89 }
90
91 #[derive(RustcEncodable)]
92 struct DiagnosticSpan {
93     file_name: String,
94     byte_start: u32,
95     byte_end: u32,
96     /// 1-based.
97     line_start: usize,
98     line_end: usize,
99     /// 1-based, character offset.
100     column_start: usize,
101     column_end: usize,
102     /// Source text from the start of line_start to the end of line_end.
103     text: Vec<DiagnosticSpanLine>,
104 }
105
106 #[derive(RustcEncodable)]
107 struct DiagnosticSpanLine {
108     text: String,
109     /// 1-based, character offset in self.text.
110     highlight_start: usize,
111     highlight_end: usize,
112 }
113
114 #[derive(RustcEncodable)]
115 struct DiagnosticCode {
116     /// The code itself.
117     code: String,
118     /// An explanation for the code.
119     explanation: Option<&'static str>,
120 }
121
122 impl<'a> Diagnostic<'a> {
123     fn new(msp: Option<&MultiSpan>,
124            msg: &'a str,
125            code: Option<&str>,
126            level: Level,
127            je: &JsonEmitter)
128            -> Diagnostic<'a> {
129         Diagnostic {
130             message: msg,
131             code: DiagnosticCode::map_opt_string(code.map(|c| c.to_owned()), je),
132             level: level.to_str(),
133             spans: msp.map_or(vec![], |msp| DiagnosticSpan::from_multispan(msp, je)),
134             children: vec![],
135         }
136     }
137
138     fn from_render_span(span: &RenderSpan,
139                         msg: &'a str,
140                         level: Level,
141                         je: &JsonEmitter)
142                         -> Diagnostic<'a> {
143         Diagnostic {
144             message: msg,
145             code: None,
146             level: level.to_str(),
147             spans: DiagnosticSpan::from_render_span(span, je),
148             children: vec![],
149         }
150     }
151
152     fn from_diagnostic_builder<'c>(db: &'c DiagnosticBuilder,
153                                    je: &JsonEmitter)
154                                    -> Diagnostic<'c> {
155         Diagnostic {
156             message: &db.message,
157             code: DiagnosticCode::map_opt_string(db.code.clone(), je),
158             level: db.level.to_str(),
159             spans: db.span.as_ref().map_or(vec![], |sp| DiagnosticSpan::from_multispan(sp, je)),
160             children: db.children.iter().map(|c| {
161                 Diagnostic::from_sub_diagnostic(c, je)
162             }).collect(),
163         }
164     }
165
166     fn from_sub_diagnostic<'c>(db: &'c SubDiagnostic, je: &JsonEmitter) -> Diagnostic<'c> {
167         Diagnostic {
168             message: &db.message,
169             code: None,
170             level: db.level.to_str(),
171             spans: db.render_span.as_ref()
172                      .map(|sp| DiagnosticSpan::from_render_span(sp, je))
173                      .or_else(|| db.span.as_ref().map(|s| DiagnosticSpan::from_multispan(s, je)))
174                      .unwrap_or(vec![]),
175             children: vec![],
176         }
177     }
178 }
179
180 impl DiagnosticSpan {
181     fn from_multispan(msp: &MultiSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
182         msp.spans.iter().map(|span| {
183             let start = je.cm.lookup_char_pos(span.lo);
184             let end = je.cm.lookup_char_pos(span.hi);
185             DiagnosticSpan {
186                 file_name: start.file.name.clone(),
187                 byte_start: span.lo.0,
188                 byte_end: span.hi.0,
189                 line_start: start.line,
190                 line_end: end.line,
191                 column_start: start.col.0 + 1,
192                 column_end: end.col.0 + 1,
193                 text: DiagnosticSpanLine::from_span(span, je),
194             }
195         }).collect()
196     }
197
198     fn from_render_span(rsp: &RenderSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
199         match *rsp {
200             // FIXME(#30701) handle Suggestion properly
201             RenderSpan::FullSpan(ref msp) |
202             RenderSpan::Suggestion(CodeSuggestion { ref msp, .. }) => {
203                 DiagnosticSpan::from_multispan(msp, je)
204             }
205             RenderSpan::EndSpan(ref msp) => {
206                 msp.spans.iter().map(|span| {
207                     let end = je.cm.lookup_char_pos(span.hi);
208                     DiagnosticSpan {
209                         file_name: end.file.name.clone(),
210                         byte_start: span.lo.0,
211                         byte_end: span.hi.0,
212                         line_start: 0,
213                         line_end: end.line,
214                         column_start: 0,
215                         column_end: end.col.0 + 1,
216                         text: DiagnosticSpanLine::from_span(span, je),
217                     }
218                 }).collect()
219             }
220             RenderSpan::FileLine(ref msp) => {
221                 msp.spans.iter().map(|span| {
222                     let start = je.cm.lookup_char_pos(span.lo);
223                     let end = je.cm.lookup_char_pos(span.hi);
224                     DiagnosticSpan {
225                         file_name: start.file.name.clone(),
226                         byte_start: span.lo.0,
227                         byte_end: span.hi.0,
228                         line_start: start.line,
229                         line_end: end.line,
230                         column_start: 0,
231                         column_end: 0,
232                         text: DiagnosticSpanLine::from_span(span, je),
233                     }
234                 }).collect()
235             }
236         }
237     }
238 }
239
240 impl DiagnosticSpanLine {
241     fn from_span(span: &Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
242         let lines = match je.cm.span_to_lines(*span) {
243             Ok(lines) => lines,
244             Err(_) => {
245                 debug!("unprintable span");
246                 return Vec::new();
247             }
248         };
249
250         let mut result = Vec::new();
251         let fm = &*lines.file;
252
253         for line in &lines.lines {
254             result.push(DiagnosticSpanLine {
255                 text: fm.get_line(line.line_index).unwrap().to_owned(),
256                 highlight_start: line.start_col.0 + 1,
257                 highlight_end: line.end_col.0 + 1,
258             });
259         }
260
261         result
262     }
263 }
264
265 impl DiagnosticCode {
266     fn map_opt_string(s: Option<String>, je: &JsonEmitter) -> Option<DiagnosticCode> {
267         s.map(|s| {
268
269             let explanation = je.registry
270                                 .as_ref()
271                                 .and_then(|registry| registry.find_description(&s));
272
273             DiagnosticCode {
274                 code: s,
275                 explanation: explanation,
276             }
277         })
278     }
279 }