Exception throwing when required, and fixes for unit tests
This commit is contained in:
@@ -201,12 +201,6 @@ namespace Upsilon.Binder
|
||||
{
|
||||
var variableExpression =(BoundVariableExpression) expression;
|
||||
var function = (FunctionVariableSymbol)variableExpression.Variable.VariableSymbol;
|
||||
if (function.Parameters.Length != parameters.Count)
|
||||
{
|
||||
_diagnostics.LogError($"Invalid number of parameters for function '{function.Name}'. " +
|
||||
$"Expected '{function.Parameters.Length}', got '{parameters.Count}'", e.Span);
|
||||
return new BoundLiteralExpression(new ScriptNull(), e.Span);
|
||||
}
|
||||
if (!function.IsBound)
|
||||
{
|
||||
Scope = new BoundScope(Scope);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using Upsilon.BaseTypes;
|
||||
using Upsilon.Exceptions;
|
||||
using Upsilon.Parser;
|
||||
using Upsilon.Text;
|
||||
|
||||
@@ -9,10 +10,12 @@ namespace Upsilon
|
||||
{
|
||||
public SourceText ScriptString { get; }
|
||||
public readonly List<DiagnosticsMessage> Messages = new List<DiagnosticsMessage>();
|
||||
public bool ThrowsOnError { get; }
|
||||
|
||||
public Diagnostics(SourceText scriptString)
|
||||
public Diagnostics(SourceText scriptString, bool throwsOnError)
|
||||
{
|
||||
ScriptString = scriptString;
|
||||
ThrowsOnError = throwsOnError;
|
||||
}
|
||||
|
||||
public void Log(DiagnosticLevel level, string message, TextSpan location)
|
||||
@@ -22,6 +25,12 @@ namespace Upsilon
|
||||
|
||||
public void LogError(string message, TextSpan location)
|
||||
{
|
||||
if (ThrowsOnError)
|
||||
{
|
||||
var linePos = ScriptString.GetLinePosition(location.Start);
|
||||
var line = ScriptString.GetLine(linePos.Line);
|
||||
throw new ParseException(message, linePos.Line, linePos.Pos, line);
|
||||
}
|
||||
Log(DiagnosticLevel.Error, message, location);
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Upsilon.Evaluator
|
||||
Options = options;
|
||||
_scriptString = scriptString;
|
||||
ScriptString = new SourceText(scriptString);
|
||||
Diagnostics = new Diagnostics(ScriptString);
|
||||
Diagnostics = new Diagnostics(ScriptString, options.ThrowExceptionOnError);
|
||||
|
||||
var staticBoundScope = options.OverrideStaticBoundScope ?? StaticScope.BoundScope;
|
||||
var boundScope = BoundScope.WithReadOnlyScope(staticBoundScope);
|
||||
@@ -38,10 +38,10 @@ namespace Upsilon.Evaluator
|
||||
Evaluator = Evaluator.CreateWithSetScope(Diagnostics, Scope);
|
||||
}
|
||||
|
||||
private Script(string scriptString, Binder.Binder binder, Evaluator evaluator)
|
||||
private Script(string scriptString, Binder.Binder binder, Evaluator evaluator, ScriptOptions options)
|
||||
{
|
||||
ScriptString = new SourceText(scriptString);
|
||||
Diagnostics = new Diagnostics(ScriptString);
|
||||
Diagnostics = new Diagnostics(ScriptString, Options.ThrowExceptionOnError);
|
||||
Binder = new Binder.Binder(Diagnostics, binder.Scope.Variables);
|
||||
Evaluator = new Evaluator( Diagnostics, evaluator.Scope.Variables);
|
||||
Scope = Evaluator.Scope;
|
||||
@@ -50,7 +50,7 @@ namespace Upsilon.Evaluator
|
||||
|
||||
internal static Script ContinueWith(Script previousScript, string scriptString)
|
||||
{
|
||||
var s = new Script(scriptString, previousScript.Binder, previousScript.Evaluator);
|
||||
var s = new Script(scriptString, previousScript.Binder, previousScript.Evaluator, previousScript.Options);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
28
Upsilon/Exceptions/ParseException.cs
Normal file
28
Upsilon/Exceptions/ParseException.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using Upsilon.Text;
|
||||
|
||||
namespace Upsilon.Exceptions
|
||||
{
|
||||
public class ParseException : Exception
|
||||
{
|
||||
public string ErrorMessage { get; }
|
||||
public int Line { get; }
|
||||
public int Character { get; }
|
||||
public string ErrorLine { get; }
|
||||
|
||||
public ParseException(string errorMessage, int line, int character, string errorLine)
|
||||
{
|
||||
ErrorMessage = errorMessage;
|
||||
Line = line;
|
||||
Character = character;
|
||||
ErrorLine = errorLine;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{ErrorMessage} at ({Line}, {Character})\n{ErrorLine}";
|
||||
}
|
||||
|
||||
public override string Message => ToString();
|
||||
}
|
||||
}
|
||||
@@ -44,13 +44,13 @@ namespace Upsilon
|
||||
|
||||
public static T EvaluateFunction<T>(string input, string function, object[] parameters = null, ScriptOptions options = null)
|
||||
{
|
||||
var script = ParseInput(input, options);
|
||||
var script = ParseInputAndEvaluate(input, options);
|
||||
return script.EvaluateFunction<T>(function, parameters);
|
||||
}
|
||||
|
||||
public static object EvaluateFunction(string input, string function, object[] parameters = null, ScriptOptions options = null)
|
||||
{
|
||||
var script = ParseInput(input, options);
|
||||
var script = ParseInputAndEvaluate(input, options);
|
||||
return script.EvaluateFunction(function, parameters);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,9 @@ namespace Upsilon.Parser
|
||||
ThenToken = thenToken;
|
||||
Block = block;
|
||||
EndToken = endToken;
|
||||
Span = new TextSpan(IfToken.Span.Start, EndToken.Span.End - IfToken.Span.Start);
|
||||
var end = Block.Span.End;
|
||||
if (EndToken != null) end = EndToken.Span.End;
|
||||
Span = new TextSpan(IfToken.Span.Start, end - IfToken.Span.Start);
|
||||
}
|
||||
|
||||
public IfStatementSyntax(SyntaxToken ifToken, ExpressionStatementSyntax condition, SyntaxToken thenToken,
|
||||
@@ -27,7 +29,7 @@ namespace Upsilon.Parser
|
||||
ThenToken = thenToken;
|
||||
Block = block;
|
||||
ElseStatement = elseStatement;
|
||||
Span = new TextSpan(IfToken.Span.Start, EndToken.Span.End - IfToken.Span.Start);
|
||||
Span = new TextSpan(IfToken.Span.Start, ElseStatement.Span.End - IfToken.Span.Start);
|
||||
}
|
||||
|
||||
public IfStatementSyntax(SyntaxToken ifToken, ExpressionStatementSyntax condition, SyntaxToken thenToken,
|
||||
|
||||
@@ -85,6 +85,12 @@ namespace Upsilon.Text
|
||||
return _text.Length;
|
||||
}
|
||||
|
||||
public string GetLine(int linePosition)
|
||||
{
|
||||
var line = _lines[linePosition];
|
||||
return GetSpan(line.Start, line.LineLength);
|
||||
}
|
||||
|
||||
public SourceTextLine GetLineInfo(int lineIndex)
|
||||
{
|
||||
return _lines[lineIndex];
|
||||
|
||||
Reference in New Issue
Block a user