Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/MoonSharp.Interpreter/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using MoonSharp.Interpreter.Interop;
using MoonSharp.Interpreter.IO;
using MoonSharp.Interpreter.Platforms;
using MoonSharp.Interpreter.Tree;
using MoonSharp.Interpreter.Tree.Expressions;
using MoonSharp.Interpreter.Tree.Fast_Interface;

Expand All @@ -23,6 +24,32 @@ namespace MoonSharp.Interpreter
/// </summary>
public class Script : IScriptPrivateResource
{
public enum ScriptParserMessageType
{
Error,
Warning,
Info
}

public class ScriptParserMessage
{
public string Msg { get; set; }
private Token Token { get; set; }
public ScriptParserMessageType Type { get; set; }

internal ScriptParserMessage(Token token)
{
Token = token;
Msg = $"unexpected symbol near '{token}'";
}

internal ScriptParserMessage(Token token, string msg)
{
Token = token;
Msg = msg;
}
}

/// <summary>
/// The version of the MoonSharp engine
/// </summary>
Expand All @@ -39,6 +66,7 @@ public class Script : IScriptPrivateResource
Table m_GlobalTable;
IDebugger m_Debugger;
Table[] m_TypeMetatables = new Table[(int)LuaTypeExtensions.MaxMetaTypes];
internal List<ScriptParserMessage> i_ParserMessages { get; set; } = new List<ScriptParserMessage>();

/// <summary>
/// Initializes the <see cref="Script"/> class.
Expand Down Expand Up @@ -838,5 +866,7 @@ Script IScriptPrivateResource.OwnerScript
{
get { return this; }
}

public List<ScriptParserMessage> ParserMessages => i_ParserMessages;
}
}
12 changes: 12 additions & 0 deletions src/MoonSharp.Interpreter/ScriptOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ internal ScriptOptions(ScriptOptions defaults)

this.CheckThreadAccess = defaults.CheckThreadAccess;
}

public enum ParserErrorModes
{
Throw,
Report
}

/// <summary>
/// Gets or sets the current script-loader.
Expand Down Expand Up @@ -127,5 +133,11 @@ internal ScriptOptions(ScriptOptions defaults)
/// These directions will store the RHS as a string annotation on the chunk.
/// </summary>
public HashSet<string> Directives { get; set; } = new HashSet<string>();

/// <summary>
/// Specifies how parser reacts to errors while parsing.
/// Options are: Throw (paring is aborted after first error), Report (errors are stashed and available in Script.ParserMessages)
/// </summary>
public ParserErrorModes ParserErrorMode { get; set; } = ParserErrorModes.Throw;
}
}
84 changes: 70 additions & 14 deletions src/MoonSharp.Interpreter/Tree/Statements/CompositeStatement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

using MoonSharp.Interpreter.Execution;

Expand All @@ -14,30 +15,85 @@ class CompositeStatement : Statement
{
List<Statement> m_Statements = new List<Statement>();

public Token EndToken;

public CompositeStatement(ScriptLoadingContext lcontext, BlockEndType endType)
: base(lcontext)
{
while (true)
{
ParseAnnotations(lcontext);
Token t = lcontext.Lexer.Current;
EndToken = lcontext.Lexer.Current;
if (t.IsEndOfBlock()) break;
if (endType == BlockEndType.CloseCurly && t.Type == TokenType.Brk_Close_Curly) break;
bool forceLast;

Statement s = Statement.CreateStatement(lcontext, out forceLast);
m_Statements.Add(s);
EndToken = lcontext.Lexer.Current;
if (forceLast) break;
try
{
ParseAnnotations(lcontext);
Token t = lcontext.Lexer.Current;
if (t.IsEndOfBlock()) break;
if (endType == BlockEndType.CloseCurly && t.Type == TokenType.Brk_Close_Curly) break;

Statement s = CreateStatement(lcontext, out bool forceLast);
m_Statements.Add(s);
if (forceLast) break;
}
catch (InterpreterException e)
{
if (lcontext.Script.Options.ParserErrorMode == ScriptOptions.ParserErrorModes.Report)
{
Token token = null;
if (e is SyntaxErrorException se)
{
token = se.Token;
}

lcontext.Script.i_ParserMessages.Add(new Script.ScriptParserMessage(token, e.Message));
Synchronize(lcontext);

if (lcontext.Lexer.PeekNext().Type == TokenType.Eof)
{
lcontext.Lexer.Next();
break;
}
}
else
{
throw;
}
}
}

// eat away all superfluos ';'s
while (lcontext.Lexer.Current.Type == TokenType.SemiColon)
lcontext.Lexer.Next();
}

private void Synchronize(ScriptLoadingContext lcontext)
{
while (lcontext.Lexer.PeekNext().Type != TokenType.Eof)
{
lcontext.Lexer.Next();
Token tkn = lcontext.Lexer.Current;

switch (tkn.Type)
{
case TokenType.ChunkAnnotation:
case TokenType.Local:
case TokenType.Until:
case TokenType.Break:
case TokenType.Continue:
case TokenType.While:
case TokenType.For:
case TokenType.ElseIf:
case TokenType.Else:
case TokenType.Function:
case TokenType.Goto:
case TokenType.Directive:
case TokenType.Do:
case TokenType.If:
{
goto endSynchronize;
}
}
}

endSynchronize: ;
}


public override void ResolveScope(ScriptLoadingContext lcontext)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if (

x = 0
x1 = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if (

x = 0
x1 = []

local x1 = = =
24 changes: 21 additions & 3 deletions src/MoonSharp.Tests/EndToEnd/CLikeTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ static string[] GetTestCases()

return files;
}

[Test, TestCaseSource(nameof(GetTestCases))]
public async Task RunThrowErros(string path)
{
await RunCore(path);
}

[Test, TestCaseSource(nameof(GetTestCases))]
public async Task Run(string path)
public async Task RunReportErrors(string path)
{
await RunCore(path, true);
}

public async Task RunCore(string path, bool reportErrors = false)
{
string outputPath = path.Replace(".lua", ".txt");

Expand All @@ -36,14 +47,21 @@ public async Task Run(string path)
script.Options.DebugPrint = s => stdOut.AppendLine(s);
script.Options.IndexTablesFrom = 0;

if (path.Contains("flaky"))
{
Assert.Inconclusive($"Test {path} marked as flaky");
return;
}

if (path.Contains("SyntaxCLike"))
{
script.Options.Syntax = ScriptSyntax.CLike;
}

if (path.Contains("flaky"))
if (reportErrors)
{
Assert.Inconclusive($"Test {path} marked as flaky");
script.Options.ParserErrorMode = ScriptOptions.ParserErrorModes.Report;
await script.DoStringAsync(code);
return;
}

Expand Down