Show file name in error messages, to handle errors from modules easier

This commit is contained in:
Deukhoofd 2018-12-07 14:51:02 +01:00
parent edd352e62a
commit b0450d3bf5
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
7 changed files with 31 additions and 23 deletions

View File

@ -45,7 +45,7 @@ namespace Upsilon.Binder
_unboundFunctions.Clear(); _unboundFunctions.Clear();
} }
public BoundScript BindScript(BlockStatementSyntax e) public BoundScript BindScript(string fileName, BlockStatementSyntax e)
{ {
var bound = BindStatement(e); var bound = BindStatement(e);
foreach (var unboundFunctionStatement in _unboundFunctions) foreach (var unboundFunctionStatement in _unboundFunctions)
@ -68,7 +68,7 @@ namespace Upsilon.Binder
variable.ResultType = resultType; variable.ResultType = resultType;
} }
_unboundFunctions = new Dictionary<string, UnboundFunctionExpression>(); _unboundFunctions = new Dictionary<string, UnboundFunctionExpression>();
return new BoundScript((BoundBlockStatement) bound, e.Span, Scope); return new BoundScript((BoundBlockStatement) bound, e.Span, Scope, fileName);
} }
private BoundStatement BindStatement(StatementSyntax s) private BoundStatement BindStatement(StatementSyntax s)

View File

@ -5,12 +5,14 @@ namespace Upsilon.Binder
{ {
public class BoundScript : BoundStatement public class BoundScript : BoundStatement
{ {
public BoundScript(BoundBlockStatement statement, TextSpan span, BoundScope scope) : base(span) public BoundScript(BoundBlockStatement statement, TextSpan span, BoundScope scope, string fileName) : base(span)
{ {
Statement = statement; Statement = statement;
Scope = scope; Scope = scope;
FileName = fileName;
} }
public string FileName { get; set; }
public override BoundKind Kind => BoundKind.BoundScript; public override BoundKind Kind => BoundKind.BoundScript;
public override IEnumerable<BoundNode> GetNodeAtPosition(int characterPosition) public override IEnumerable<BoundNode> GetNodeAtPosition(int characterPosition)
{ {

View File

@ -8,15 +8,17 @@ namespace Upsilon
{ {
public class Diagnostics public class Diagnostics
{ {
public string FileName { get; }
public SourceText ScriptString { get; } public SourceText ScriptString { get; }
public readonly List<DiagnosticsMessage> Errors = new List<DiagnosticsMessage>(); public readonly List<DiagnosticsMessage> Errors = new List<DiagnosticsMessage>();
public readonly List<DiagnosticsMessage> Warnings = new List<DiagnosticsMessage>(); public readonly List<DiagnosticsMessage> Warnings = new List<DiagnosticsMessage>();
public bool ThrowsOnError { get; } public bool ThrowsOnError { get; }
public Diagnostics(SourceText scriptString, bool throwsOnError) public Diagnostics(SourceText scriptString, bool throwsOnError, string fileName)
{ {
ScriptString = scriptString; ScriptString = scriptString;
ThrowsOnError = throwsOnError; ThrowsOnError = throwsOnError;
FileName = fileName;
} }
public void Log(DiagnosticLevel level, string message, TextSpan location) public void Log(DiagnosticLevel level, string message, TextSpan location)
@ -33,7 +35,7 @@ namespace Upsilon
{ {
var linePos = ScriptString.GetLinePosition(location.Start); var linePos = ScriptString.GetLinePosition(location.Start);
var line = ScriptString.GetLine(linePos.Line); var line = ScriptString.GetLine(linePos.Line);
throw new ParseException(message, linePos.Line, linePos.Pos, line); throw new ParseException(FileName, message, linePos.Line, linePos.Pos, line);
} }
Log(DiagnosticLevel.Error, message, location); Log(DiagnosticLevel.Error, message, location);
} }

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using Upsilon.BaseTypes; using Upsilon.BaseTypes;
using Upsilon.Binder; using Upsilon.Binder;
using Upsilon.Parser; using Upsilon.Parser;
@ -12,6 +11,7 @@ namespace Upsilon.Evaluator
{ {
public class Script : IDisposable public class Script : IDisposable
{ {
public string FileName { get; }
public ScriptOptions Options { get; } public ScriptOptions Options { get; }
private readonly string _scriptString; private readonly string _scriptString;
public SourceText ScriptString { get; } public SourceText ScriptString { get; }
@ -22,12 +22,13 @@ namespace Upsilon.Evaluator
private Binder.Binder Binder { get; } private Binder.Binder Binder { get; }
private EvaluationScope Scope { get; } private EvaluationScope Scope { get; }
internal Script(string scriptString, ScriptOptions options ) internal Script(string scriptString, string fileName, ScriptOptions options )
{ {
FileName = fileName;
Options = options; Options = options;
_scriptString = scriptString; _scriptString = scriptString;
ScriptString = new SourceText(scriptString); ScriptString = new SourceText(scriptString);
Diagnostics = new Diagnostics(ScriptString, options.ThrowExceptionOnError); Diagnostics = new Diagnostics(ScriptString, options.ThrowExceptionOnError, fileName);
var staticBoundScope = options.OverrideStaticBoundScope ?? StaticScope.BoundScope; var staticBoundScope = options.OverrideStaticBoundScope ?? StaticScope.BoundScope;
var boundScope = BoundScope.WithReadOnlyScope(staticBoundScope); var boundScope = BoundScope.WithReadOnlyScope(staticBoundScope);
@ -38,12 +39,12 @@ namespace Upsilon.Evaluator
Evaluator = Evaluator.CreateWithSetScope(Diagnostics, Scope, this); Evaluator = Evaluator.CreateWithSetScope(Diagnostics, Scope, this);
} }
private Script(string scriptString, Binder.Binder binder, Evaluator evaluator, ScriptOptions options) private Script(string fileName, string scriptString, Binder.Binder binder, Evaluator evaluator, ScriptOptions options)
{ {
ScriptString = new SourceText(scriptString); ScriptString = new SourceText(scriptString);
Options = options; Options = options;
_scriptString = scriptString; _scriptString = scriptString;
Diagnostics = new Diagnostics(ScriptString, options.ThrowExceptionOnError); Diagnostics = new Diagnostics(ScriptString, options.ThrowExceptionOnError, fileName);
Binder = Upsilon.Binder.Binder.CreateWithSetScope(Diagnostics, binder.Scope, this); Binder = Upsilon.Binder.Binder.CreateWithSetScope(Diagnostics, binder.Scope, this);
Evaluator = Evaluator.CreateWithSetScope(Diagnostics, evaluator.Scope, this); Evaluator = Evaluator.CreateWithSetScope(Diagnostics, evaluator.Scope, this);
@ -54,7 +55,7 @@ namespace Upsilon.Evaluator
{ {
Options = options; Options = options;
_bound = boundScript; _bound = boundScript;
Diagnostics = new Diagnostics(ScriptString, options.ThrowExceptionOnError); Diagnostics = new Diagnostics(ScriptString, options.ThrowExceptionOnError, boundScript.FileName);
Binder = Upsilon.Binder.Binder.CreateWithSetScope(Diagnostics, binder.Scope, this); Binder = Upsilon.Binder.Binder.CreateWithSetScope(Diagnostics, binder.Scope, this);
Evaluator = Evaluator.CreateWithSetScope(Diagnostics, evaluator.Scope, this); Evaluator = Evaluator.CreateWithSetScope(Diagnostics, evaluator.Scope, this);
@ -62,9 +63,9 @@ namespace Upsilon.Evaluator
} }
internal static Script ContinueWith(Script previousScript, string scriptString) internal static Script ContinueWith(Script previousScript, string fileName, string scriptString)
{ {
var s = new Script(scriptString, previousScript.Binder, previousScript.Evaluator, previousScript.Options); var s = new Script(fileName, scriptString, previousScript.Binder, previousScript.Evaluator, previousScript.Options);
return s; return s;
} }
@ -82,7 +83,7 @@ namespace Upsilon.Evaluator
public BoundScript Bind() public BoundScript Bind()
{ {
return _bound ?? (_bound = Binder.BindScript(_parsed)); return _bound ?? (_bound = Binder.BindScript(FileName, _parsed));
} }
public object Evaluate() public object Evaluate()

View File

@ -5,13 +5,15 @@ namespace Upsilon.Exceptions
{ {
public class ParseException : Exception public class ParseException : Exception
{ {
public string FileName { get; }
public string ErrorMessage { get; } public string ErrorMessage { get; }
public int Line { get; } public int Line { get; }
public int Character { get; } public int Character { get; }
public string ErrorLine { get; } public string ErrorLine { get; }
public ParseException(string errorMessage, int line, int character, string errorLine) public ParseException(string fileName, string errorMessage, int line, int character, string errorLine)
{ {
FileName = fileName;
ErrorMessage = errorMessage; ErrorMessage = errorMessage;
Line = line; Line = line;
Character = character; Character = character;
@ -20,7 +22,7 @@ namespace Upsilon.Exceptions
public override string ToString() public override string ToString()
{ {
return $"{ErrorMessage} at ({Line}, {Character})\n{ErrorLine}"; return $"[{FileName}] {ErrorMessage} at ({Line}, {Character})\n{ErrorLine}";
} }
public override string Message => ToString(); public override string Message => ToString();

View File

@ -10,7 +10,7 @@ namespace Upsilon
public static Script ParseInput(string input, ScriptOptions options = null) public static Script ParseInput(string input, ScriptOptions options = null)
{ {
if (options == null) options = DefaultOptions; if (options == null) options = DefaultOptions;
var script = new Script(input, options); var script = new Script(input, "unnamed", options);
script.Parse(); script.Parse();
return script; return script;
} }
@ -19,7 +19,7 @@ namespace Upsilon
{ {
if (options == null) options = DefaultOptions; if (options == null) options = DefaultOptions;
var input = options.ScriptLoader.LoadFile(fileName); var input = options.ScriptLoader.LoadFile(fileName);
var script = new Script(input, options); var script = new Script(input,fileName, options);
script.Parse(); script.Parse();
return script; return script;
} }
@ -27,7 +27,7 @@ namespace Upsilon
public static Script ParseInputAndEvaluate(string input, ScriptOptions options = null) public static Script ParseInputAndEvaluate(string input, ScriptOptions options = null)
{ {
if (options == null) options = DefaultOptions; if (options == null) options = DefaultOptions;
var script = new Script(input, options); var script = new Script(input, "unnamed", options);
script.Parse(); script.Parse();
script.Evaluate(); script.Evaluate();
return script; return script;
@ -37,19 +37,20 @@ namespace Upsilon
{ {
if (options == null) options = DefaultOptions; if (options == null) options = DefaultOptions;
var input = options.ScriptLoader.LoadFile(fileName); var input = options.ScriptLoader.LoadFile(fileName);
var script = new Script(input, options); var script = new Script(input, fileName, options);
script.Parse(); script.Parse();
script.Evaluate(); script.Evaluate();
return script; return script;
} }
public static Script ContinueWith(Script script, string input) public static Script ContinueWith(Script script, string input, string fileName = "unnamed")
{ {
var s = Script.ContinueWith(script, input); var s = Script.ContinueWith(script, fileName, input);
s.Parse(); s.Parse();
return s; return s;
} }
public static Script ContinueWith(Script script, BoundScript bound) public static Script ContinueWith(Script script, BoundScript bound)
{ {
var s = Script.ContinueWith(script, bound); var s = Script.ContinueWith(script, bound);

View File

@ -16,7 +16,7 @@ namespace Upsilon
var moduleScript = script.Options.ScriptLoader.LoadModule(name); var moduleScript = script.Options.ScriptLoader.LoadModule(name);
if (moduleScript == null) if (moduleScript == null)
return null; return null;
var parsed = Executor.ContinueWith(script, moduleScript); var parsed = Executor.ContinueWith(script, moduleScript, name);
module = parsed.Bind(); module = parsed.Bind();
_cachedModules.Add(name, module); _cachedModules.Add(name, module);
return module; return module;