2024-03-11 19:33:02 +08:00
|
|
|
|
using Canon.Core.GrammarParser;
|
|
|
|
|
|
|
|
|
|
namespace Canon.Core.LexicalParser;
|
2024-03-09 21:16:44 +08:00
|
|
|
|
|
|
|
|
|
using Enums;
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 词法记号基类
|
|
|
|
|
/// </summary>
|
2024-04-04 21:25:11 +08:00
|
|
|
|
public abstract class SemanticToken : IEquatable<SemanticToken>
|
2024-03-09 21:16:44 +08:00
|
|
|
|
{
|
|
|
|
|
public abstract SemanticTokenType TokenType { get; }
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 记号出现的行号
|
|
|
|
|
/// </summary>
|
2024-03-09 21:16:44 +08:00
|
|
|
|
public required uint LinePos { get; init; }
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 记号出现的列号
|
|
|
|
|
/// </summary>
|
2024-03-09 21:16:44 +08:00
|
|
|
|
public required uint CharacterPos { get; init; }
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 记号的字面值
|
|
|
|
|
/// </summary>
|
2024-03-09 21:16:44 +08:00
|
|
|
|
public required string LiteralValue { get; init; }
|
2024-03-11 19:33:02 +08:00
|
|
|
|
|
|
|
|
|
public static implicit operator Terminator(SemanticToken token)
|
|
|
|
|
{
|
|
|
|
|
switch (token.TokenType)
|
|
|
|
|
{
|
|
|
|
|
case SemanticTokenType.Character:
|
|
|
|
|
return Terminator.CharacterTerminator;
|
|
|
|
|
case SemanticTokenType.Identifier:
|
|
|
|
|
return Terminator.IdentifierTerminator;
|
|
|
|
|
case SemanticTokenType.Number:
|
|
|
|
|
return Terminator.NumberTerminator;
|
|
|
|
|
case SemanticTokenType.End:
|
|
|
|
|
return Terminator.EndTerminator;
|
|
|
|
|
case SemanticTokenType.Delimiter:
|
|
|
|
|
return new Terminator(((DelimiterSemanticToken)token).DelimiterType);
|
|
|
|
|
case SemanticTokenType.Keyword:
|
|
|
|
|
return new Terminator(((KeywordSemanticToken)token).KeywordType);
|
|
|
|
|
case SemanticTokenType.Operator:
|
|
|
|
|
return new Terminator(((OperatorSemanticToken)token).OperatorType);
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentException("Unknown token type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-21 22:24:35 +08:00
|
|
|
|
public T Convert<T>() where T : SemanticToken
|
|
|
|
|
{
|
|
|
|
|
if (this is T result)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new InvalidOperationException("Can not convert target type0");
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-11 19:33:02 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 栈底符号单例对象
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static EndSemanticToken End => new()
|
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
LinePos = uint.MaxValue, CharacterPos = uint.MaxValue, LiteralValue = string.Empty
|
2024-03-11 19:33:02 +08:00
|
|
|
|
};
|
|
|
|
|
|
2024-04-04 21:25:11 +08:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
return
|
|
|
|
|
$"LinePos: {LinePos}, CharacterPos: {CharacterPos}, LiteralValue: {LiteralValue}, TokenType: {TokenType}";
|
2024-04-04 21:25:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Equals(SemanticToken? other)
|
|
|
|
|
{
|
|
|
|
|
if (other == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return LinePos == other.LinePos &&
|
|
|
|
|
CharacterPos == other.CharacterPos &&
|
|
|
|
|
LiteralValue == other.LiteralValue &&
|
|
|
|
|
TokenType == other.TokenType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object? obj)
|
|
|
|
|
{
|
|
|
|
|
return obj is SemanticToken semanticTokenObj && Equals(semanticTokenObj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return LinePos.GetHashCode() ^
|
|
|
|
|
CharacterPos.GetHashCode() ^
|
|
|
|
|
LiteralValue.GetHashCode() ^
|
|
|
|
|
TokenType.GetHashCode();
|
|
|
|
|
}
|
2024-03-09 21:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 字符类型记号
|
|
|
|
|
/// </summary>
|
2024-03-09 21:16:44 +08:00
|
|
|
|
public class CharacterSemanticToken : SemanticToken
|
|
|
|
|
{
|
|
|
|
|
public override SemanticTokenType TokenType => SemanticTokenType.Character;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 分隔符类型记号
|
|
|
|
|
/// </summary>
|
2024-03-09 21:16:44 +08:00
|
|
|
|
public class DelimiterSemanticToken : SemanticToken
|
|
|
|
|
{
|
|
|
|
|
public override SemanticTokenType TokenType => SemanticTokenType.Delimiter;
|
|
|
|
|
|
|
|
|
|
public required DelimiterType DelimiterType { get; init; }
|
|
|
|
|
|
2024-04-04 21:25:11 +08:00
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
return base.GetHashCode() ^ DelimiterType.GetHashCode();
|
2024-04-04 21:25:11 +08:00
|
|
|
|
}
|
2024-03-09 21:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 关键字类型记号
|
|
|
|
|
/// </summary>
|
2024-03-09 21:16:44 +08:00
|
|
|
|
public class KeywordSemanticToken : SemanticToken
|
|
|
|
|
{
|
|
|
|
|
public override SemanticTokenType TokenType => SemanticTokenType.Keyword;
|
|
|
|
|
|
|
|
|
|
public required KeywordType KeywordType { get; init; }
|
|
|
|
|
|
2024-04-04 21:25:11 +08:00
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
2024-04-21 17:42:08 +08:00
|
|
|
|
return base.GetHashCode() ^ KeywordType.GetHashCode();
|
2024-04-04 21:25:11 +08:00
|
|
|
|
}
|
2024-03-09 21:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 操作数类型记号
|
|
|
|
|
/// </summary>
|
2024-03-09 21:16:44 +08:00
|
|
|
|
public class OperatorSemanticToken : SemanticToken
|
|
|
|
|
{
|
|
|
|
|
public override SemanticTokenType TokenType => SemanticTokenType.Operator;
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
public required OperatorType OperatorType { get; init; }
|
|
|
|
|
|
2024-04-04 21:25:11 +08:00
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
return base.GetHashCode() ^ OperatorType.GetHashCode();
|
2024-04-04 21:25:11 +08:00
|
|
|
|
}
|
2024-03-09 21:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 数值类型记号
|
|
|
|
|
/// </summary>
|
2024-03-09 21:16:44 +08:00
|
|
|
|
public class NumberSemanticToken : SemanticToken
|
|
|
|
|
{
|
|
|
|
|
public override SemanticTokenType TokenType => SemanticTokenType.Number;
|
|
|
|
|
|
2024-03-13 16:19:07 +08:00
|
|
|
|
public required NumberType NumberType { get; init; }
|
|
|
|
|
|
2024-04-04 21:25:11 +08:00
|
|
|
|
public override int GetHashCode()
|
2024-03-09 21:16:44 +08:00
|
|
|
|
{
|
2024-04-20 11:48:05 +08:00
|
|
|
|
return base.GetHashCode() ^ NumberType.GetHashCode();
|
2024-03-09 21:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 11:14:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 标识符类型记号
|
|
|
|
|
/// </summary>
|
2024-03-09 21:16:44 +08:00
|
|
|
|
public class IdentifierSemanticToken : SemanticToken
|
|
|
|
|
{
|
|
|
|
|
public override SemanticTokenType TokenType => SemanticTokenType.Identifier;
|
|
|
|
|
|
2024-04-20 11:48:05 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 标识符名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string IdentifierName => LiteralValue.ToLower();
|
2024-03-09 21:16:44 +08:00
|
|
|
|
}
|
2024-03-11 19:33:02 +08:00
|
|
|
|
|
2024-04-21 17:42:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 终结符记号
|
|
|
|
|
/// </summary>
|
2024-03-11 19:33:02 +08:00
|
|
|
|
public class EndSemanticToken : SemanticToken
|
|
|
|
|
{
|
|
|
|
|
public override SemanticTokenType TokenType => SemanticTokenType.End;
|
|
|
|
|
}
|