Skip to content

Commit

Permalink
Merge pull request #230 from Vbif/json_error_format_2
Browse files Browse the repository at this point in the history
Json error format
  • Loading branch information
Boddlnagg authored Sep 27, 2016
2 parents a10ad0a + 1338f4b commit 17d0eac
Show file tree
Hide file tree
Showing 12 changed files with 501 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
using System;

namespace VisualRust.Build
namespace VisualRust.Build.Message.Human
{
enum RustcParsedMessageType
{
Error,
Warning,
Note,
Help
}

class RustcParsedMessage
class RustcMessageHuman
{
public RustcParsedMessageType Type;
public RustcMessageType Type;
public string Message;
public string ErrorCode;
public string File;
Expand All @@ -22,7 +12,7 @@ class RustcParsedMessage
public int EndColumnNumber;
public bool CanExplain; // TODO: currently we don't do anything with this

public RustcParsedMessage(RustcParsedMessageType type, string message, string errorCode, string file,
public RustcMessageHuman(RustcMessageType type, string message, string errorCode, string file,
int lineNumber, int columnNumber, int endLineNumber, int endColumnNumber)
{
Type = type;
Expand All @@ -36,13 +26,13 @@ public RustcParsedMessage(RustcParsedMessageType type, string message, string er
CanExplain = false;
}

public bool TryMergeWithFollowing(RustcParsedMessage other)
public bool TryMergeWithFollowing(RustcMessageHuman other)
{
if ((other.Type == RustcParsedMessageType.Note || other.Type == RustcParsedMessageType.Help)
if ((other.Type == RustcMessageType.Note || other.Type == RustcMessageType.Help)
&& other.File == this.File && other.LineNumber == this.LineNumber && other.ColumnNumber == this.ColumnNumber &&
other.EndLineNumber == this.EndLineNumber && other.EndColumnNumber == this.EndColumnNumber)
{
var prefix = other.Type == RustcParsedMessageType.Note ? "\nnote: " : "\nhelp: ";
var prefix = other.Type == RustcMessageType.Note ? "\nnote: " : "\nhelp: ";
this.Message += prefix + other.Message;
return true;
}
Expand Down
77 changes: 77 additions & 0 deletions VisualRust.Build/Message/Human/RustcMessageHumanParser.cs
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;
}
}
}
131 changes: 131 additions & 0 deletions VisualRust.Build/Message/Json/RustcMessageJson.cs
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;
}
}
}
20 changes: 20 additions & 0 deletions VisualRust.Build/Message/Json/RustcMessageJsonParser.cs
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);
}
}
}
10 changes: 10 additions & 0 deletions VisualRust.Build/Message/RustcMessageType.cs
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
}
}
Loading

0 comments on commit 17d0eac

Please sign in to comment.