-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from Vbif/json_error_format_2
Json error format
- Loading branch information
Showing
12 changed files
with
501 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace VisualRust.Build.Message.Human | ||
{ | ||
static class RustcMessageHumanParser | ||
{ | ||
private static readonly Regex defectRegex = new Regex(@"^([^\n:]+):(\d+):(\d+):\s+(\d+):(\d+)\s+(.*)$", RegexOptions.Multiline | RegexOptions.CultureInvariant); | ||
|
||
private static readonly Regex errorCodeRegex = new Regex(@"\[([A-Z]\d\d\d\d)\]$", RegexOptions.CultureInvariant); | ||
|
||
public static IEnumerable<RustcMessageHuman> Parse(string output) | ||
{ | ||
MatchCollection errorMatches = defectRegex.Matches(output); | ||
|
||
RustcMessageHuman previous = null; | ||
foreach (Match match in errorMatches) | ||
{ | ||
string remainingMsg = match.Groups[6].Value.Trim(); | ||
Match errorMatch = errorCodeRegex.Match(remainingMsg); | ||
string errorCode = errorMatch.Success ? errorMatch.Groups[1].Value : null; | ||
int line = Int32.Parse(match.Groups[2].Value, NumberStyles.None); | ||
int col = Int32.Parse(match.Groups[3].Value, NumberStyles.None); | ||
int endLine = Int32.Parse(match.Groups[4].Value, NumberStyles.None); | ||
int endCol = Int32.Parse(match.Groups[5].Value, NumberStyles.None); | ||
|
||
if (remainingMsg.StartsWith("warning: ")) | ||
{ | ||
string msg = match.Groups[6].Value.Substring(9, match.Groups[6].Value.Length - 9 - (errorCode != null ? 8 : 0)); | ||
if (previous != null) yield return previous; | ||
previous = new RustcMessageHuman(RustcMessageType.Warning, msg, errorCode, match.Groups[1].Value, | ||
line, col, endLine, endCol); | ||
} | ||
else if (remainingMsg.StartsWith("note: ") || remainingMsg.StartsWith("help: ")) | ||
{ | ||
if (remainingMsg.StartsWith("help: pass `--explain ") && previous != null) | ||
{ | ||
previous.CanExplain = true; | ||
continue; | ||
} | ||
|
||
// NOTE: "note: " and "help: " are both 6 characters long (though hardcoding this is probably still not a very good idea) | ||
string msg = remainingMsg.Substring(6, remainingMsg.Length - 6 - (errorCode != null ? 8 : 0)); | ||
var type = remainingMsg.StartsWith("note: ") ? RustcMessageType.Note : RustcMessageType.Help; | ||
RustcMessageHuman note = new RustcMessageHuman(type, msg, errorCode, match.Groups[1].Value, | ||
line, col, endLine, endCol); | ||
|
||
if (previous != null) | ||
{ | ||
// try to merge notes and help messages with a previous message (warning or error where it belongs to), if the span is the same | ||
if (previous.TryMergeWithFollowing(note)) | ||
{ | ||
continue; // skip setting new previous, because we successfully merged the new note into the previous message | ||
} | ||
else | ||
{ | ||
yield return previous; | ||
} | ||
} | ||
previous = note; | ||
} | ||
else | ||
{ | ||
bool startsWithError = remainingMsg.StartsWith("error: "); | ||
string msg = remainingMsg.Substring((startsWithError ? 7 : 0), remainingMsg.Length - (startsWithError ? 7 : 0) - (errorCode != null ? 8 : 0)); | ||
if (previous != null) yield return previous; | ||
previous = new RustcMessageHuman(RustcMessageType.Error, msg, errorCode, match.Groups[1].Value, | ||
line, col, endLine, endCol); | ||
} | ||
} | ||
|
||
if (previous != null) yield return previous; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace VisualRust.Build.Message.Json | ||
{ | ||
public class RustcMessageJson | ||
{ | ||
public class DiagnosticCode | ||
{ | ||
/// <summary> | ||
/// The code itself. | ||
/// </summary> | ||
public string code { get; set; } | ||
/// <summary> | ||
/// An explanation for the code. | ||
/// </summary> | ||
public string explanation { get; set; } | ||
} | ||
|
||
public class DiagnosticSpanLine | ||
{ | ||
public string text { get; set; } | ||
public int highlight_start { get; set; } | ||
public int highlight_end { get; set; } | ||
} | ||
|
||
public class DiagnosticSpanMacroExpansion | ||
{ | ||
/// <summary> | ||
/// span where macro was applied to generate this code; note that | ||
/// this may itself derive from a macro (if | ||
/// `span.expansion.is_some()`) | ||
/// </summary> | ||
public DiagnosticSpan span { get; set; } | ||
/// <summary> | ||
/// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]") | ||
/// </summary> | ||
public String macro_decl_name { get; set; } | ||
/// <summary> | ||
/// span where macro was defined (if known) | ||
/// </summary> | ||
public DiagnosticSpan def_site_span { get; set; } | ||
} | ||
|
||
public class DiagnosticSpan | ||
{ | ||
public string file_name { get; set; } | ||
public int byte_start { get; set; } | ||
public int byte_end { get; set; } | ||
public int line_start { get; set; } | ||
public int line_end { get; set; } | ||
public int column_start { get; set; } | ||
public int column_end { get; set; } | ||
/// <summary> | ||
/// Is this a "primary" span -- meaning the point, or one of the points, | ||
/// where the error occurred? | ||
/// </summary> | ||
public bool is_primary { get; set; } | ||
/// <summary> | ||
/// Source text from the start of line_start to the end of line_end. | ||
/// </summary> | ||
public List<DiagnosticSpanLine> text { get; set; } | ||
/// <summary> | ||
/// Label that should be placed at this location (if any) | ||
/// </summary> | ||
public object label { get; set; } | ||
/// <summary> | ||
/// If we are suggesting a replacement, this will contain text | ||
/// that should be sliced in atop this span. You may prefer to | ||
/// load the fully rendered version from the parent `Diagnostic`, | ||
/// however. | ||
/// </summary> | ||
public String suggested_replacement { get; set; } | ||
/// <summary> | ||
/// Macro invocations that created the code at this span, if any. | ||
/// </summary> | ||
public DiagnosticSpanMacroExpansion expansion { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// The primary error message. | ||
/// </summary> | ||
public string message { get; set; } | ||
public DiagnosticCode code { get; set; } | ||
/// <summary> | ||
/// "error: internal compiler error", "error", "warning", "note", "help" | ||
/// </summary> | ||
public string level { private get; set; } | ||
public List<DiagnosticSpan> spans { get; set; } | ||
/// <summary> | ||
/// Associated diagnostic messages. | ||
/// </summary> | ||
public List<object> children { get; set; } | ||
/// <summary> | ||
/// The message as rustc would render it. Currently this is only | ||
/// `Some` for "suggestions", but eventually it will include all | ||
/// snippets. | ||
/// </summary> | ||
public object rendered { get; set; } | ||
|
||
public RustcMessageType GetLevelAsEnum() | ||
{ | ||
if (level == "error: internal compiler error" || level == "error") | ||
return RustcMessageType.Error; | ||
if (level == "warning") | ||
return RustcMessageType.Warning; | ||
if (level == "note") | ||
return RustcMessageType.Note; | ||
if (level == "help") | ||
return RustcMessageType.Help; | ||
|
||
return RustcMessageType.Error; | ||
} | ||
|
||
public DiagnosticSpan GetPrimarySpan() | ||
{ | ||
if (spans == null || spans.Count == 0) | ||
return null; | ||
|
||
return spans.FirstOrDefault(a => a.is_primary); | ||
} | ||
|
||
public String GetErrorCodeAsString() | ||
{ | ||
if (code == null) | ||
return string.Empty; | ||
return code.code; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Newtonsoft.Json; | ||
|
||
namespace VisualRust.Build.Message.Json | ||
{ | ||
public static class RustcMessageJsonParser | ||
{ | ||
private static readonly JsonSerializer serializer = new JsonSerializer(); | ||
|
||
public static IEnumerable<RustcMessageJson> Parse(String output) | ||
{ | ||
var reader = new JsonTextReader(new StringReader(output)) {SupportMultipleContent = true}; | ||
|
||
while (reader.Read()) | ||
yield return serializer.Deserialize<RustcMessageJson>(reader); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace VisualRust.Build.Message | ||
{ | ||
public enum RustcMessageType | ||
{ | ||
Error, | ||
Warning, | ||
Note, | ||
Help | ||
} | ||
} |
Oops, something went wrong.