Skip to content

Commit

Permalink
Print proper error messages to console again;
Browse files Browse the repository at this point in the history
Fix stack overflow when parsing an expression;
  • Loading branch information
onepiecefreak3 committed Nov 26, 2023
1 parent 0e1f264 commit 668c6f7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -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 };

Expand All @@ -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}");
}
}
}
Expand Down Expand Up @@ -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 };

Expand All @@ -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}");
}
}
}
Expand Down Expand Up @@ -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 };

Expand All @@ -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}");
}
}
}
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -431,7 +432,11 @@ private ExpressionSyntax ParseExpression(IBuffer<Level5SyntaxToken> 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);
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 668c6f7

Please sign in to comment.