添加了对部分类型的测试样例,修改添加了NumberSemanticToken

This commit is contained in:
duqoo
2024-03-13 16:19:07 +08:00
parent 35aec34a8e
commit 7ea97ea97e
7 changed files with 226 additions and 0 deletions

View File

@@ -4,6 +4,8 @@ namespace Canon.Core.LexicalParser;
using Enums;
using System.Text;
/// <summary>
/// 词法记号基类
/// </summary>
@@ -198,13 +200,59 @@ public class OperatorSemanticToken : SemanticToken
/// <summary>
/// 数值类型记号
/// </summary>
/// TODO进制表示只有$1的十六进制表示
public class NumberSemanticToken : SemanticToken
{
public override SemanticTokenType TokenType => SemanticTokenType.Number;
public required NumberType NumberType { get; init; }
public double Value { get; private init; }
public static bool TryParse(uint linePos, uint characterPos, LinkedListNode<char> now,
out NumberSemanticToken? token)
{
StringBuilder buffer = new();
bool hasDecimalPoint = false;
bool hasExponent = false;
while (now != null && (char.IsDigit(now.Value) || now.Value == '.' || now.Value == 'e' || now.Value == 'E'))
{
if (now.Value == '.')
{
if (hasDecimalPoint)
{
break;
}
hasDecimalPoint = true;
}
if (now.Value == 'e' || now.Value == 'E')
{
if (hasExponent)
{
break;
}
hasExponent = true;
}
buffer.Append(now.Value);
now = now.Next;
}
if (double.TryParse(buffer.ToString(), out double value))
{
token = new NumberSemanticToken
{
LinePos = linePos,
CharacterPos = characterPos,
LiteralValue = buffer.ToString(),
Value = value,
NumberType = hasDecimalPoint || hasExponent ? NumberType.Real : NumberType.Integer
};
return true;
}
token = null;
return false;
}