From b0450d3bf5105aeaf8b30581b8c71f063534c1fb Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 7 Dec 2018 14:51:02 +0100 Subject: [PATCH] Show file name in error messages, to handle errors from modules easier --- Upsilon/Binder/Binder.cs | 4 ++-- Upsilon/Binder/BoundStatements/BoundScript.cs | 4 +++- Upsilon/Diagnostics.cs | 6 ++++-- Upsilon/Evaluator/Script.cs | 19 ++++++++++--------- Upsilon/Exceptions/ParseException.cs | 6 ++++-- Upsilon/Executor.cs | 13 +++++++------ Upsilon/ModuleHandler.cs | 2 +- 7 files changed, 31 insertions(+), 23 deletions(-) diff --git a/Upsilon/Binder/Binder.cs b/Upsilon/Binder/Binder.cs index cd1064b..043600d 100644 --- a/Upsilon/Binder/Binder.cs +++ b/Upsilon/Binder/Binder.cs @@ -45,7 +45,7 @@ namespace Upsilon.Binder _unboundFunctions.Clear(); } - public BoundScript BindScript(BlockStatementSyntax e) + public BoundScript BindScript(string fileName, BlockStatementSyntax e) { var bound = BindStatement(e); foreach (var unboundFunctionStatement in _unboundFunctions) @@ -68,7 +68,7 @@ namespace Upsilon.Binder variable.ResultType = resultType; } _unboundFunctions = new Dictionary(); - return new BoundScript((BoundBlockStatement) bound, e.Span, Scope); + return new BoundScript((BoundBlockStatement) bound, e.Span, Scope, fileName); } private BoundStatement BindStatement(StatementSyntax s) diff --git a/Upsilon/Binder/BoundStatements/BoundScript.cs b/Upsilon/Binder/BoundStatements/BoundScript.cs index 1f22a2d..e13d5ce 100644 --- a/Upsilon/Binder/BoundStatements/BoundScript.cs +++ b/Upsilon/Binder/BoundStatements/BoundScript.cs @@ -5,12 +5,14 @@ namespace Upsilon.Binder { 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; Scope = scope; + FileName = fileName; } + public string FileName { get; set; } public override BoundKind Kind => BoundKind.BoundScript; public override IEnumerable GetNodeAtPosition(int characterPosition) { diff --git a/Upsilon/Diagnostics.cs b/Upsilon/Diagnostics.cs index ab7a0c3..4a693e8 100644 --- a/Upsilon/Diagnostics.cs +++ b/Upsilon/Diagnostics.cs @@ -8,15 +8,17 @@ namespace Upsilon { public class Diagnostics { + public string FileName { get; } public SourceText ScriptString { get; } public readonly List Errors = new List(); public readonly List Warnings = new List(); public bool ThrowsOnError { get; } - public Diagnostics(SourceText scriptString, bool throwsOnError) + public Diagnostics(SourceText scriptString, bool throwsOnError, string fileName) { ScriptString = scriptString; ThrowsOnError = throwsOnError; + FileName = fileName; } public void Log(DiagnosticLevel level, string message, TextSpan location) @@ -33,7 +35,7 @@ namespace Upsilon { var linePos = ScriptString.GetLinePosition(location.Start); 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); } diff --git a/Upsilon/Evaluator/Script.cs b/Upsilon/Evaluator/Script.cs index 78c7593..f949ed8 100644 --- a/Upsilon/Evaluator/Script.cs +++ b/Upsilon/Evaluator/Script.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Collections.Immutable; using Upsilon.BaseTypes; using Upsilon.Binder; using Upsilon.Parser; @@ -12,6 +11,7 @@ namespace Upsilon.Evaluator { public class Script : IDisposable { + public string FileName { get; } public ScriptOptions Options { get; } private readonly string _scriptString; public SourceText ScriptString { get; } @@ -22,12 +22,13 @@ namespace Upsilon.Evaluator private Binder.Binder Binder { get; } private EvaluationScope Scope { get; } - internal Script(string scriptString, ScriptOptions options ) + internal Script(string scriptString, string fileName, ScriptOptions options ) { + FileName = fileName; Options = options; _scriptString = 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 boundScope = BoundScope.WithReadOnlyScope(staticBoundScope); @@ -38,12 +39,12 @@ namespace Upsilon.Evaluator 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); Options = options; _scriptString = scriptString; - Diagnostics = new Diagnostics(ScriptString, options.ThrowExceptionOnError); + Diagnostics = new Diagnostics(ScriptString, options.ThrowExceptionOnError, fileName); Binder = Upsilon.Binder.Binder.CreateWithSetScope(Diagnostics, binder.Scope, this); Evaluator = Evaluator.CreateWithSetScope(Diagnostics, evaluator.Scope, this); @@ -54,7 +55,7 @@ namespace Upsilon.Evaluator { Options = options; _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); 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; } @@ -82,7 +83,7 @@ namespace Upsilon.Evaluator public BoundScript Bind() { - return _bound ?? (_bound = Binder.BindScript(_parsed)); + return _bound ?? (_bound = Binder.BindScript(FileName, _parsed)); } public object Evaluate() diff --git a/Upsilon/Exceptions/ParseException.cs b/Upsilon/Exceptions/ParseException.cs index c01abdc..4246206 100644 --- a/Upsilon/Exceptions/ParseException.cs +++ b/Upsilon/Exceptions/ParseException.cs @@ -5,13 +5,15 @@ namespace Upsilon.Exceptions { public class ParseException : Exception { + public string FileName { get; } 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) + public ParseException(string fileName, string errorMessage, int line, int character, string errorLine) { + FileName = fileName; ErrorMessage = errorMessage; Line = line; Character = character; @@ -20,7 +22,7 @@ namespace Upsilon.Exceptions public override string ToString() { - return $"{ErrorMessage} at ({Line}, {Character})\n{ErrorLine}"; + return $"[{FileName}] {ErrorMessage} at ({Line}, {Character})\n{ErrorLine}"; } public override string Message => ToString(); diff --git a/Upsilon/Executor.cs b/Upsilon/Executor.cs index f778d76..ad35cff 100644 --- a/Upsilon/Executor.cs +++ b/Upsilon/Executor.cs @@ -10,7 +10,7 @@ namespace Upsilon public static Script ParseInput(string input, ScriptOptions options = null) { if (options == null) options = DefaultOptions; - var script = new Script(input, options); + var script = new Script(input, "unnamed", options); script.Parse(); return script; } @@ -19,7 +19,7 @@ namespace Upsilon { if (options == null) options = DefaultOptions; var input = options.ScriptLoader.LoadFile(fileName); - var script = new Script(input, options); + var script = new Script(input,fileName, options); script.Parse(); return script; } @@ -27,7 +27,7 @@ namespace Upsilon public static Script ParseInputAndEvaluate(string input, ScriptOptions options = null) { if (options == null) options = DefaultOptions; - var script = new Script(input, options); + var script = new Script(input, "unnamed", options); script.Parse(); script.Evaluate(); return script; @@ -37,19 +37,20 @@ namespace Upsilon { if (options == null) options = DefaultOptions; var input = options.ScriptLoader.LoadFile(fileName); - var script = new Script(input, options); + var script = new Script(input, fileName, options); script.Parse(); script.Evaluate(); 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(); return s; } + public static Script ContinueWith(Script script, BoundScript bound) { var s = Script.ContinueWith(script, bound); diff --git a/Upsilon/ModuleHandler.cs b/Upsilon/ModuleHandler.cs index 7c61ec5..b18a10f 100644 --- a/Upsilon/ModuleHandler.cs +++ b/Upsilon/ModuleHandler.cs @@ -16,7 +16,7 @@ namespace Upsilon var moduleScript = script.Options.ScriptLoader.LoadModule(name); if (moduleScript == null) return null; - var parsed = Executor.ContinueWith(script, moduleScript); + var parsed = Executor.ContinueWith(script, moduleScript, name); module = parsed.Bind(); _cachedModules.Add(name, module); return module;