using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
using CanonSharp.Benchmark.Canon.Core.LexicalParser;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
///
/// 使用数值产生式事件的事件参数
///
public class NumberConstValueEventArgs : EventArgs
{
///
/// 是否含有负号
///
public bool IsNegative { get; init; }
///
/// 数值记号
///
public required NumberSemanticToken Token { get; init; }
}
///
/// 使用字符产生式事件的事件参数
///
public class CharacterConstValueEventArgs : EventArgs
{
///
/// 字符记号
///
public required CharacterSemanticToken Token { get; init; }
}
public class ConstValue : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ConstValue;
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
RaiseGeneratorEvent();
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
RaiseGeneratorEvent();
}
public string ValueString { get; set; } = string.Empty;
///
/// 使用数值产生式的事件
///
public event EventHandler? OnNumberGenerator;
///
/// 使用字符产生式的事件
///
public event EventHandler? OnCharacterGenerator;
private void RaiseGeneratorEvent()
{
if (Children.Count == 2)
{
OperatorSemanticToken operatorSemanticToken = Children[0].Convert().Token
.Convert();
NumberSemanticToken numberSemanticToken = Children[1].Convert().Token
.Convert();
OnNumberGenerator?.Invoke(this, new NumberConstValueEventArgs
{
Token = numberSemanticToken,
IsNegative = operatorSemanticToken.OperatorType == OperatorType.Minus
});
return;
}
SemanticToken token = Children[0].Convert().Token;
if (token.TokenType == SemanticTokenType.Number)
{
OnNumberGenerator?.Invoke(this,
new NumberConstValueEventArgs
{
Token = token.Convert()
});
}
else
{
OnCharacterGenerator?.Invoke(this, new CharacterConstValueEventArgs
{
Token = token.Convert()
});
}
OnNumberGenerator = null;
OnCharacterGenerator = null;
}
public static ConstValue Create(List children)
{
return new ConstValue { Children = children };
}
}