feat: CanonSharp Benchmark. (#4)
Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/4 Co-authored-by: jackfiled <xcrenchangjun@outlook.com> Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
This commit is contained in:
19
CanonSharp.Benchmark/Canon.Core/Exceptions/CanonException.cs
Normal file
19
CanonSharp.Benchmark/Canon.Core/Exceptions/CanonException.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace CanonSharp.Benchmark.Canon.Core.Exceptions;
|
||||
|
||||
/// <summary>
|
||||
/// 编译器中的统一异常基类
|
||||
/// </summary>
|
||||
public class CanonException : Exception
|
||||
{
|
||||
public CanonException()
|
||||
{
|
||||
}
|
||||
|
||||
public CanonException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public CanonException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
@@ -0,0 +1,71 @@
|
||||
using System.Text;
|
||||
using CanonSharp.Benchmark.Canon.Core.Abstractions;
|
||||
using CanonSharp.Benchmark.Canon.Core.GrammarParser;
|
||||
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
|
||||
|
||||
namespace CanonSharp.Benchmark.Canon.Core.Exceptions;
|
||||
|
||||
/// <summary>
|
||||
/// 语法分析中引发的异常
|
||||
/// </summary>
|
||||
public class GrammarException : CanonException
|
||||
{
|
||||
public override string Message { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 语法分析错误时的分析状态
|
||||
/// </summary>
|
||||
public ITransformer CurrentState { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 语法分析错误时的输入符号
|
||||
/// </summary>
|
||||
public SemanticToken CurrentToken { get; }
|
||||
|
||||
public GrammarException(ITransformer currentState)
|
||||
{
|
||||
CurrentState = currentState;
|
||||
CurrentToken = SemanticToken.End;
|
||||
|
||||
StringBuilder builder = new();
|
||||
builder.Append("Except ");
|
||||
|
||||
foreach (TerminatorBase terminatorBase in ListNextTerminators(CurrentState))
|
||||
{
|
||||
builder.Append('\'').Append(terminatorBase).Append("' ");
|
||||
}
|
||||
|
||||
Message = builder.ToString();
|
||||
}
|
||||
|
||||
public GrammarException(ITransformer currentState, SemanticToken currentToken)
|
||||
{
|
||||
CurrentState = currentState;
|
||||
CurrentToken = currentToken;
|
||||
|
||||
StringBuilder builder = new();
|
||||
builder.Append("Expect ");
|
||||
|
||||
foreach (TerminatorBase terminatorBase in ListNextTerminators(CurrentState))
|
||||
{
|
||||
builder.Append('\'').Append(terminatorBase).Append("',");
|
||||
}
|
||||
|
||||
if (!CurrentToken.Equals(SemanticToken.End))
|
||||
{
|
||||
builder.Append("but '").Append(CurrentToken.LiteralValue).Append("' found.");
|
||||
}
|
||||
|
||||
Message = builder.ToString();
|
||||
}
|
||||
|
||||
private static List<TerminatorBase> ListNextTerminators(ITransformer state)
|
||||
{
|
||||
List<TerminatorBase> result = [];
|
||||
|
||||
result.AddRange(state.ShiftTable.Keys);
|
||||
result.AddRange(state.ReduceTable.Keys);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
namespace CanonSharp.Benchmark.Canon.Core.Exceptions;
|
||||
using Enums;
|
||||
/// <summary>
|
||||
/// 词法分析中引发的异常
|
||||
/// </summary>
|
||||
public class LexemeException : CanonException
|
||||
{
|
||||
public LexemeErrorType ErrorType { get; }
|
||||
|
||||
public uint Line { get; }
|
||||
|
||||
public uint CharPosition { get; }
|
||||
|
||||
private readonly string _message;
|
||||
|
||||
/// <param name="errorType">错误类型</param>
|
||||
/// <param name="line">单词的行号</param>
|
||||
/// <param name="charPosition">单词的列号</param>
|
||||
/// <param name="message">错误信息</param>
|
||||
public LexemeException(LexemeErrorType errorType, uint line, uint charPosition, string message)
|
||||
{
|
||||
ErrorType = errorType;
|
||||
Line = line;
|
||||
CharPosition = charPosition;
|
||||
_message = message;
|
||||
}
|
||||
|
||||
public override string Message => ToString();
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"LexemeException: ErrorType={ErrorType}, Line={Line}, CharPosition={CharPosition}, Message={_message}\n";
|
||||
}
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
namespace CanonSharp.Benchmark.Canon.Core.Exceptions;
|
||||
|
||||
public class ReduceAndShiftConflictException : Exception
|
||||
{
|
||||
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
using CanonSharp.Benchmark.Canon.Core.GrammarParser;
|
||||
|
||||
namespace CanonSharp.Benchmark.Canon.Core.Exceptions;
|
||||
|
||||
public class ReduceConflictException(LrState originState, Terminator lookAhead, NonTerminator left1, NonTerminator left2)
|
||||
: Exception
|
||||
{
|
||||
public LrState OriginState { get; } = originState;
|
||||
|
||||
public Terminator LookAhead { get; } = lookAhead;
|
||||
|
||||
public NonTerminator Left1 { get; } = left1;
|
||||
|
||||
public NonTerminator Left2 { get; } = left2;
|
||||
|
||||
public override string Message => "Reduce Conflict!";
|
||||
}
|
Reference in New Issue
Block a user