2024-03-09 23:57:43 +08:00
|
|
|
|
using Canon.Core.Enums;
|
2024-03-10 12:06:27 +08:00
|
|
|
|
using Canon.Core.LexicalParser;
|
2024-03-09 23:57:43 +08:00
|
|
|
|
|
|
|
|
|
namespace Canon.Core.GrammarParser;
|
|
|
|
|
|
|
|
|
|
public abstract class TerminatorBase
|
|
|
|
|
{
|
|
|
|
|
public abstract bool IsTerminated { get; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-03-10 11:14:52 +08:00
|
|
|
|
/// 语法中的一个终结符
|
|
|
|
|
/// 终结符标识词法分析中得到的一个记号
|
2024-03-09 23:57:43 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public class Terminator : TerminatorBase, IEquatable<Terminator>
|
|
|
|
|
{
|
|
|
|
|
public override bool IsTerminated => true;
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
private readonly SemanticTokenType _terminatorType;
|
2024-03-09 23:57:43 +08:00
|
|
|
|
|
|
|
|
|
private readonly KeywordType _keywordType;
|
|
|
|
|
private readonly DelimiterType _delimiterType;
|
2024-03-10 11:14:52 +08:00
|
|
|
|
private readonly OperatorType _operatorType;
|
2024-03-09 23:57:43 +08:00
|
|
|
|
|
|
|
|
|
public Terminator(KeywordType keywordType)
|
|
|
|
|
{
|
2024-03-10 11:14:52 +08:00
|
|
|
|
_terminatorType = SemanticTokenType.Keyword;
|
2024-03-09 23:57:43 +08:00
|
|
|
|
_keywordType = keywordType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Terminator(DelimiterType delimiterType)
|
|
|
|
|
{
|
2024-03-10 11:14:52 +08:00
|
|
|
|
_terminatorType = SemanticTokenType.Delimiter;
|
2024-03-09 23:57:43 +08:00
|
|
|
|
_delimiterType = delimiterType;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
public Terminator(OperatorType operatorType)
|
|
|
|
|
{
|
|
|
|
|
_terminatorType = SemanticTokenType.Operator;
|
|
|
|
|
_operatorType = operatorType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Terminator(SemanticTokenType type)
|
|
|
|
|
{
|
|
|
|
|
_terminatorType = type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 标识符终结符单例
|
|
|
|
|
/// 鉴于在语法中不关心标识符具体内容,因此可以使用单例对象
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static Terminator IdentifierTerminator => new(SemanticTokenType.Identifier);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 字符终结符单例
|
|
|
|
|
/// 鉴于在语法中不关心具体字符,因此可以使用单例对象
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static Terminator CharacterTerminator => new(SemanticTokenType.Character);
|
|
|
|
|
|
2024-03-10 12:06:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 数值终结符单例
|
|
|
|
|
/// 鉴于在语法中不关心具体数值,因此可以使用单例对象
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static Terminator NumberTerminator => new(SemanticTokenType.Number);
|
|
|
|
|
|
2024-03-10 15:37:18 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 栈底的终结符
|
|
|
|
|
/// </summary>
|
2024-03-11 19:33:02 +08:00
|
|
|
|
public static Terminator EndTerminator => new(SemanticTokenType.End);
|
2024-03-10 15:37:18 +08:00
|
|
|
|
|
2024-03-12 14:52:42 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 空字符串的终结符
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static Terminator EmptyTerminator => new(SemanticTokenType.Empty);
|
|
|
|
|
|
2024-03-09 23:57:43 +08:00
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
2024-03-10 11:14:52 +08:00
|
|
|
|
int hash = _terminatorType.GetHashCode();
|
|
|
|
|
|
|
|
|
|
switch (_terminatorType)
|
2024-03-09 23:57:43 +08:00
|
|
|
|
{
|
2024-03-10 11:14:52 +08:00
|
|
|
|
case SemanticTokenType.Keyword:
|
|
|
|
|
return hash ^ _keywordType.GetHashCode();
|
|
|
|
|
case SemanticTokenType.Delimiter:
|
|
|
|
|
return hash ^ _delimiterType.GetHashCode();
|
|
|
|
|
case SemanticTokenType.Operator:
|
|
|
|
|
return hash ^ _operatorType.GetHashCode();
|
|
|
|
|
default:
|
|
|
|
|
return hash;
|
2024-03-09 23:57:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-11 19:33:02 +08:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
switch (_terminatorType)
|
|
|
|
|
{
|
|
|
|
|
case SemanticTokenType.Keyword:
|
|
|
|
|
return _keywordType.ToString();
|
|
|
|
|
case SemanticTokenType.Operator:
|
|
|
|
|
return _operatorType.ToString();
|
|
|
|
|
case SemanticTokenType.Delimiter:
|
|
|
|
|
return _delimiterType.ToString();
|
|
|
|
|
default:
|
2024-03-11 21:57:47 +08:00
|
|
|
|
return _terminatorType.ToString();
|
2024-03-11 19:33:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 23:57:43 +08:00
|
|
|
|
public bool Equals(Terminator? other)
|
|
|
|
|
{
|
|
|
|
|
if (other is null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
if (_terminatorType != other._terminatorType)
|
2024-03-09 23:57:43 +08:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
switch (_terminatorType)
|
2024-03-09 23:57:43 +08:00
|
|
|
|
{
|
2024-03-10 11:14:52 +08:00
|
|
|
|
case SemanticTokenType.Keyword:
|
|
|
|
|
return _keywordType == other._keywordType;
|
|
|
|
|
case SemanticTokenType.Delimiter:
|
|
|
|
|
return _delimiterType == other._delimiterType;
|
|
|
|
|
case SemanticTokenType.Operator:
|
|
|
|
|
return _operatorType == other._operatorType;
|
|
|
|
|
default:
|
|
|
|
|
return true;
|
2024-03-09 23:57:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object? obj)
|
|
|
|
|
{
|
|
|
|
|
if (obj is not Terminator other)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Equals(other);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator ==(Terminator a, Terminator b)
|
|
|
|
|
{
|
|
|
|
|
return a.Equals(b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator !=(Terminator a, Terminator b)
|
|
|
|
|
{
|
|
|
|
|
return !a.Equals(b);
|
|
|
|
|
}
|
2024-03-10 12:06:27 +08:00
|
|
|
|
|
|
|
|
|
public static bool operator ==(Terminator a, SemanticToken b)
|
|
|
|
|
{
|
|
|
|
|
return a.EqualSemanticToken(b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator !=(Terminator a, SemanticToken b)
|
|
|
|
|
{
|
|
|
|
|
return !a.EqualSemanticToken(b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator ==(SemanticToken a, Terminator b)
|
|
|
|
|
{
|
|
|
|
|
return b.EqualSemanticToken(a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator !=(SemanticToken a, Terminator b)
|
|
|
|
|
{
|
|
|
|
|
return !b.EqualSemanticToken(a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool EqualSemanticToken(SemanticToken token)
|
|
|
|
|
{
|
|
|
|
|
if (token.TokenType != _terminatorType)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (_terminatorType)
|
|
|
|
|
{
|
|
|
|
|
case SemanticTokenType.Delimiter:
|
|
|
|
|
return (token as DelimiterSemanticToken)?.DelimiterType == _delimiterType;
|
|
|
|
|
case SemanticTokenType.Keyword:
|
|
|
|
|
return (token as KeywordSemanticToken)?.KeywordType == _keywordType;
|
|
|
|
|
case SemanticTokenType.Operator:
|
|
|
|
|
return (token as OperatorSemanticToken)?.OperatorType == _operatorType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-03-09 23:57:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-03-10 11:14:52 +08:00
|
|
|
|
/// 语法中的非终结符
|
2024-03-09 23:57:43 +08:00
|
|
|
|
/// </summary>
|
2024-03-11 21:57:47 +08:00
|
|
|
|
public class NonTerminator(NonTerminatorType type) : TerminatorBase, IEquatable<NonTerminator>
|
2024-03-09 23:57:43 +08:00
|
|
|
|
{
|
|
|
|
|
public override bool IsTerminated => false;
|
|
|
|
|
|
2024-03-11 21:57:47 +08:00
|
|
|
|
public NonTerminatorType Type { get; } = type;
|
2024-03-09 23:57:43 +08:00
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
2024-03-11 21:57:47 +08:00
|
|
|
|
return Type.GetHashCode();
|
2024-03-09 23:57:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-11 21:57:47 +08:00
|
|
|
|
public override string ToString() => Type.ToString();
|
2024-03-11 19:33:02 +08:00
|
|
|
|
|
2024-03-09 23:57:43 +08:00
|
|
|
|
public bool Equals(NonTerminator? other)
|
|
|
|
|
{
|
|
|
|
|
if (other is null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-11 21:57:47 +08:00
|
|
|
|
return Type == other.Type;
|
2024-03-09 23:57:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object? obj)
|
|
|
|
|
{
|
|
|
|
|
if (obj is not NonTerminator other)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Equals(other);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator ==(NonTerminator a, NonTerminator b)
|
|
|
|
|
{
|
|
|
|
|
return a.Equals(b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator !=(NonTerminator a, NonTerminator b)
|
|
|
|
|
{
|
|
|
|
|
return !a.Equals(b);
|
|
|
|
|
}
|
|
|
|
|
}
|