diff --git a/XtractQuery/Logic.Business.Level5ScriptManagement/Logic.Business.Level5ScriptManagement/Level5ScriptManagementWorkflow.cs b/XtractQuery/Logic.Business.Level5ScriptManagement/Logic.Business.Level5ScriptManagement/Level5ScriptManagementWorkflow.cs index fec0369..5fd03d2 100644 --- a/XtractQuery/Logic.Business.Level5ScriptManagement/Logic.Business.Level5ScriptManagement/Level5ScriptManagementWorkflow.cs +++ b/XtractQuery/Logic.Business.Level5ScriptManagement/Logic.Business.Level5ScriptManagement/Level5ScriptManagementWorkflow.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -82,7 +83,8 @@ private void ExtractScripts() PopulateStringHashCache(); // Collect files to extract - string[] files = Directory.Exists(_config.FilePath) ? + bool isDirectory = Directory.Exists(_config.FilePath); + string[] files = isDirectory ? Directory.GetFiles(_config.FilePath, "*.xq", SearchOption.AllDirectories) : new[] { _config.FilePath }; @@ -97,7 +99,10 @@ private void ExtractScripts() } catch (Exception e) { - Console.WriteLine($"Error ({e.Message})"); + string relativePath = isDirectory ? + Path.GetRelativePath(_config.FilePath, file) : + Path.GetFileName(file); + Console.WriteLine($"Could not extract {relativePath}: {GetInnermostException(e).Message}"); } } } @@ -128,7 +133,8 @@ private void ExtractScript(string filePath) private void CreateScripts() { // Collect files to extract - string[] files = Directory.Exists(_config.FilePath) ? + bool isDirectory = Directory.Exists(_config.FilePath); + string[] files = isDirectory ? Directory.GetFiles(_config.FilePath, "*.txt", SearchOption.AllDirectories) : new[] { _config.FilePath }; @@ -143,7 +149,10 @@ private void CreateScripts() } catch (Exception e) { - Console.WriteLine($"Error ({e.Message})"); + string relativePath = isDirectory ? + Path.GetRelativePath(_config.FilePath, file) : + Path.GetFileName(file); + Console.WriteLine($"Could not compile {relativePath}: {GetInnermostException(e).Message}"); } } } @@ -173,7 +182,8 @@ private void CreateScript(string filePath) private void DecompressScripts() { // Collect files to extract - string[] files = Directory.Exists(_config.FilePath) ? + bool isDirectory = Directory.Exists(_config.FilePath); + string[] files = isDirectory ? Directory.GetFiles(_config.FilePath, "*.xq", SearchOption.AllDirectories) : new[] { _config.FilePath }; @@ -188,7 +198,10 @@ private void DecompressScripts() } catch (Exception e) { - Console.WriteLine($"Error ({e.Message})"); + string relativePath = isDirectory ? + Path.GetRelativePath(_config.FilePath, file) : + Path.GetFileName(file); + Console.WriteLine($"Could not decompress {relativePath}: {GetInnermostException(e).Message}"); } } } @@ -266,5 +279,13 @@ private void PrintHelp() Console.WriteLine($"\tExtract any script to human readable text: {Environment.ProcessPath} -o e -f Path/To/File.xq"); Console.WriteLine($"\tCreate a xq32 script from human readable text: {Environment.ProcessPath} -o c -t xq32 -f Path/To/File.txt"); } + + private Exception GetInnermostException(Exception e) + { + while (e.InnerException != null) + e = e.InnerException; + + return e; + } } } diff --git a/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis.Contract/Exceptions/LexerException.cs b/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis.Contract/Exceptions/LexerException.cs index 6431da8..21f8fae 100644 --- a/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis.Contract/Exceptions/LexerException.cs +++ b/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis.Contract/Exceptions/LexerException.cs @@ -5,6 +5,9 @@ namespace Logic.Domain.CodeAnalysis.Contract.Exceptions [Serializable] public class LexerException : Exception { + public int Line { get; } + public int Column { get; } + public LexerException() { } @@ -17,6 +20,12 @@ public LexerException(string message, Exception inner) : base(message, inner) { } + public LexerException(string message, int line, int column) : base(message) + { + Line = line; + Column = column; + } + protected LexerException( SerializationInfo info, StreamingContext context) : base(info, context) diff --git a/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis.Contract/Level5/Exceptions/Level5ScriptParserException.cs b/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis.Contract/Level5/Exceptions/Level5ScriptParserException.cs index e59ec16..8ee29a8 100644 --- a/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis.Contract/Level5/Exceptions/Level5ScriptParserException.cs +++ b/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis.Contract/Level5/Exceptions/Level5ScriptParserException.cs @@ -9,6 +9,9 @@ namespace Logic.Domain.CodeAnalysis.Contract.Level5.Exceptions { public class Level5ScriptParserException:Exception { + public int Line { get; } + public int Column { get; } + public Level5ScriptParserException() { } @@ -21,6 +24,12 @@ public Level5ScriptParserException(string message, Exception inner) : base(messa { } + public Level5ScriptParserException(string message, int line, int column) : base(message) + { + Line = line; + Column = column; + } + protected Level5ScriptParserException( SerializationInfo info, StreamingContext context) : base(info, context) diff --git a/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis/Level5/Level5ScriptLexer.cs b/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis/Level5/Level5ScriptLexer.cs index ae34ea9..84bdf7f 100644 --- a/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis/Level5/Level5ScriptLexer.cs +++ b/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis/Level5/Level5ScriptLexer.cs @@ -4,6 +4,7 @@ using System.Text; using System.Threading.Tasks; using Logic.Domain.CodeAnalysis.Contract; +using Logic.Domain.CodeAnalysis.Contract.Exceptions; using Logic.Domain.CodeAnalysis.Contract.Level5.DataClasses; using Logic.Domain.CodeAnalysis.Level5.InternalContract.DataClasses; @@ -634,7 +635,7 @@ private Exception CreateException(string message, string? expected = null) if (!string.IsNullOrEmpty(expected)) message = $"{message} (Expected \"{expected}\")"; - throw new InvalidOperationException(message); + throw new LexerException(message, Line, Column); } } } diff --git a/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis/Level5/Level5ScriptParser.cs b/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis/Level5/Level5ScriptParser.cs index faba3a9..25f1be7 100644 --- a/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis/Level5/Level5ScriptParser.cs +++ b/XtractQuery/Logic.Domain.CodeAnalysis/Logic.Domain.CodeAnalysis/Level5/Level5ScriptParser.cs @@ -7,6 +7,7 @@ using Logic.Domain.CodeAnalysis.Contract.DataClasses; using Logic.Domain.CodeAnalysis.Contract.Level5; using Logic.Domain.CodeAnalysis.Contract.Level5.DataClasses; +using Logic.Domain.CodeAnalysis.Contract.Level5.Exceptions; using Logic.Domain.CodeAnalysis.Level5.InternalContract.DataClasses; namespace Logic.Domain.CodeAnalysis.Level5 @@ -431,7 +432,11 @@ private ExpressionSyntax ParseExpression(IBuffer buffer) left = ParseArrayIndexExpression(buffer, value); } else - left = ParseExpression(buffer); + throw CreateException(buffer, "Invalid expression.", SyntaxTokenKind.Variable, + SyntaxTokenKind.StringLiteral, + SyntaxTokenKind.NumericLiteral, SyntaxTokenKind.FloatingNumericLiteral, + SyntaxTokenKind.HashStringLiteral, + SyntaxTokenKind.HashNumericLiteral); if (HasTokenKind(buffer, SyntaxTokenKind.SwitchKeyword)) return ParseSwitchExpression(buffer, left); @@ -1226,7 +1231,7 @@ private Exception CreateException(string message, int line, int column, params S $"{message} (Expected any of {string.Join(", ", expected)})"; } - throw new InvalidOperationException(message); + throw new Level5ScriptParserException(message, line, column); } } }