Improved diagnostic handling

This commit is contained in:
Deukhoofd 2019-06-19 15:50:11 +02:00
parent 203ddf149f
commit 0ff2815694
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
6 changed files with 75 additions and 16 deletions

View File

@ -1,9 +1,12 @@
using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace PorygonSharp.DiagnosticHandling namespace PorygonSharp.DiagnosticHandling
{ {
public class DiagnosticItem
{
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct DiagnosticItem public struct InternalRepresentation
{ {
private readonly int _severity; private readonly int _severity;
private readonly DiagnosticCode _code; private readonly DiagnosticCode _code;
@ -15,4 +18,38 @@ namespace PorygonSharp.DiagnosticHandling
return _code; return _code;
} }
} }
private InternalRepresentation _internalRepresentation;
private readonly IntPtr _handle;
private bool _initialed;
private InternalRepresentation Internal
{
get
{
if (!_initialed)
{
_internalRepresentation = Marshal.PtrToStructure<InternalRepresentation>(_handle);
}
_initialed = true;
return _internalRepresentation;
}
}
internal DiagnosticItem(IntPtr handle)
{
_handle = handle;
}
internal IntPtr GetHandle()
{
return _handle;
}
public DiagnosticCode GetCode()
{
return Internal.GetCode();
}
}
} }

View File

@ -47,14 +47,23 @@ namespace PorygonSharp.DiagnosticHandling
for (var i = 0; i < count; i++) for (var i = 0; i < count; i++)
{ {
var ptr = GetDiagnosticAt(_handle, i); var ptr = GetDiagnosticAt(_handle, i);
yield return Marshal.PtrToStructure<DiagnosticItem>(ptr); yield return new DiagnosticItem(ptr);
} }
} }
[DllImport("libPorygonLang", EntryPoint = "GetDiagnosticsCount")] public string GetFullDiagnosticMessage(DiagnosticItem item)
{
var ptr = GetFullDiagnostic(_handle, item.GetHandle());
return Marshal.PtrToStringUTF8(ptr);
}
[DllImport("libPorygonLang", EntryPoint = "GetDiagnosticsCount", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetDiagnosticsCount(IntPtr ptr); private static extern int GetDiagnosticsCount(IntPtr ptr);
[DllImport("libPorygonLang", EntryPoint = "GetDiagnosticAt")] [DllImport("libPorygonLang", EntryPoint = "GetDiagnosticAt", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr GetDiagnosticAt(IntPtr ptr, int position); private static extern IntPtr GetDiagnosticAt(IntPtr ptr, int position);
[DllImport("libPorygonLang", EntryPoint = "GetFullDiagnostic", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr GetFullDiagnostic(IntPtr diagnosticsHolder, IntPtr diagnostic);
} }
} }

View File

@ -86,7 +86,7 @@ namespace PorygonSharp
public string EvaluateString() public string EvaluateString()
{ {
var ptr = EvaluateString(_handle); var ptr = EvaluateString(_handle);
return Marshal.PtrToStringUTF8(ptr); return Marshal.PtrToStringUni(ptr);
} }
public object GetObjectValue() public object GetObjectValue()

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using NUnit.Framework; using NUnit.Framework;
using PorygonSharp; using PorygonSharp;
using PorygonSharp.UserData; using PorygonSharp.UserData;
@ -16,7 +17,7 @@ namespace PorygonSharpTests
var diags = script.Diagnostics.GetDiagnostics(); var diags = script.Diagnostics.GetDiagnostics();
foreach (var diag in diags) foreach (var diag in diags)
{ {
throw new Exception(diag.GetCode().ToString()); throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
} }
Assert.False(script.Diagnostics.HasErrors()); Assert.False(script.Diagnostics.HasErrors());
} }
@ -109,6 +110,18 @@ namespace PorygonSharpTests
} }
} }
[Test]
public void TestDiagnostic()
{
using (var script = new Script("\n'\\x'"))
{
var diagnostics = script.Diagnostics.GetDiagnostics().ToArray();
Assert.IsNotEmpty(diagnostics);
Assert.AreEqual("[Error] (1, 3) '\\x' is not a valid control character.", script.Diagnostics.GetFullDiagnosticMessage(diagnostics[0]));
}
}
[Test] [Test]
public void TestHash() public void TestHash()
{ {

View File

@ -30,7 +30,7 @@ end
var diags = script.Diagnostics.GetDiagnostics(); var diags = script.Diagnostics.GetDiagnostics();
foreach (var diag in diags) foreach (var diag in diags)
{ {
throw new Exception(diag.GetCode().ToString()); throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
} }
script.Evaluate(); script.Evaluate();
@ -54,7 +54,7 @@ end
var diags = script.Diagnostics.GetDiagnostics(); var diags = script.Diagnostics.GetDiagnostics();
foreach (var diag in diags) foreach (var diag in diags)
{ {
throw new Exception(diag.GetCode().ToString()); throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
} }
script.Evaluate(); script.Evaluate();
@ -78,7 +78,7 @@ end
var diags = script.Diagnostics.GetDiagnostics(); var diags = script.Diagnostics.GetDiagnostics();
foreach (var diag in diags) foreach (var diag in diags)
{ {
throw new Exception(diag.GetCode().ToString()); throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
} }
script.Evaluate(); script.Evaluate();
@ -119,7 +119,7 @@ end
var diags = script.Diagnostics.GetDiagnostics(); var diags = script.Diagnostics.GetDiagnostics();
foreach (var diag in diags) foreach (var diag in diags)
{ {
throw new Exception(diag.GetCode().ToString()); throw new Exception(script.Diagnostics.GetFullDiagnosticMessage(diag));
} }
script.Evaluate(); script.Evaluate();

Binary file not shown.