2024-04-26 10:18:49 +08:00
|
|
|
|
using Canon.Core.Abstractions;
|
2024-04-21 22:24:35 +08:00
|
|
|
|
using Canon.Core.Enums;
|
|
|
|
|
using Canon.Core.LexicalParser;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
using Canon.Core.SemanticParser;
|
2024-04-07 16:47:28 +08:00
|
|
|
|
|
|
|
|
|
namespace Canon.Core.SyntaxNodes;
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class NumberGeneratorEventArgs : EventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
public required NumberSemanticToken Token { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class VariableGeneratorEventArgs : EventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
public required Variable Variable { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class ParethnesisGeneratorEventArgs : EventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
public required Expression Expression { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class ProcedureCallGeneratorEventArgs : EventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
public required IdentifierSemanticToken ProcedureName { get; init; }
|
|
|
|
|
|
2024-05-04 11:56:06 +08:00
|
|
|
|
public List<Expression> Parameters { get; } = [];
|
2024-04-26 10:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class NotGeneratorEventArgs : EventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
public required Factor Factor { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class UminusGeneratorEventArgs : EventArgs
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
public required Factor Factor { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public class PlusGeneratorEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
public required Factor Factor { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class BooleanGeneratorEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
public required bool Value { get; init; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 16:47:28 +08:00
|
|
|
|
public class Factor : NonTerminatedSyntaxNode
|
|
|
|
|
{
|
|
|
|
|
public override NonTerminatorType Type => NonTerminatorType.Factor;
|
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
public override void PreVisit(SyntaxNodeVisitor visitor)
|
|
|
|
|
{
|
|
|
|
|
visitor.PreVisit(this);
|
|
|
|
|
RaiseEvent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void PostVisit(SyntaxNodeVisitor visitor)
|
|
|
|
|
{
|
|
|
|
|
visitor.PostVisit(this);
|
|
|
|
|
RaiseEvent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 使用数值产生式的事件
|
|
|
|
|
/// </summary>
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public event EventHandler<NumberGeneratorEventArgs>? OnNumberGenerator;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 使用括号产生式的事件
|
|
|
|
|
/// </summary>
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public event EventHandler<ParethnesisGeneratorEventArgs>? OnParethnesisGenerator;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 使用变量产生式的事件
|
|
|
|
|
/// </summary>
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public event EventHandler<VariableGeneratorEventArgs>? OnVariableGenerator;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-05-01 21:06:27 +08:00
|
|
|
|
/// 使用否定产生式的事件
|
2024-04-26 10:18:49 +08:00
|
|
|
|
/// </summary>
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public event EventHandler<NotGeneratorEventArgs>? OnNotGenerator;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-05-01 21:06:27 +08:00
|
|
|
|
/// 使用负号产生式的事件
|
2024-04-26 10:18:49 +08:00
|
|
|
|
/// </summary>
|
2024-05-01 21:06:27 +08:00
|
|
|
|
public event EventHandler<UminusGeneratorEventArgs>? OnUminusGenerator;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-05-01 21:06:27 +08:00
|
|
|
|
/// 使用加号产生式的事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<PlusGeneratorEventArgs>? OnPlusGenerator;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 使用布尔值产生式的事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<BooleanGeneratorEventArgs>? OnBooleanGenerator;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 过程调用产生式的事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<ProcedureCallGeneratorEventArgs>? OnProcedureCallGenerator;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
|
|
|
|
private PascalType? _factorType;
|
|
|
|
|
|
|
|
|
|
public PascalType FactorType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_factorType is null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _factorType;
|
|
|
|
|
}
|
2024-05-01 21:06:27 +08:00
|
|
|
|
set { _factorType = value; }
|
2024-04-26 10:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 16:47:28 +08:00
|
|
|
|
public static Factor Create(List<SyntaxNodeBase> children)
|
|
|
|
|
{
|
|
|
|
|
return new Factor { Children = children };
|
|
|
|
|
}
|
2024-04-21 22:24:35 +08:00
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
private void RaiseEvent()
|
|
|
|
|
{
|
|
|
|
|
if (Children.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
if (Children[0].IsTerminated)
|
|
|
|
|
{
|
|
|
|
|
SemanticToken token = Children[0].Convert<TerminatedSyntaxNode>().Token;
|
2024-05-01 21:06:27 +08:00
|
|
|
|
|
|
|
|
|
switch (token.TokenType)
|
|
|
|
|
{
|
|
|
|
|
// factor -> num
|
|
|
|
|
case SemanticTokenType.Number:
|
|
|
|
|
OnNumberGenerator?.Invoke(this,
|
|
|
|
|
new NumberGeneratorEventArgs { Token = token.Convert<NumberSemanticToken>() });
|
|
|
|
|
break;
|
|
|
|
|
// factor -> true | false
|
|
|
|
|
case SemanticTokenType.Keyword:
|
|
|
|
|
KeywordSemanticToken keywordSemanticToken = token.Convert<KeywordSemanticToken>();
|
|
|
|
|
|
|
|
|
|
switch (keywordSemanticToken.KeywordType)
|
|
|
|
|
{
|
|
|
|
|
case KeywordType.True:
|
|
|
|
|
OnBooleanGenerator?.Invoke(this, new BooleanGeneratorEventArgs { Value = true });
|
|
|
|
|
break;
|
|
|
|
|
case KeywordType.False:
|
|
|
|
|
OnBooleanGenerator?.Invoke(this, new BooleanGeneratorEventArgs { Value = false });
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-04-26 10:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
OnVariableGenerator?.Invoke(this,
|
2024-05-01 21:06:27 +08:00
|
|
|
|
new VariableGeneratorEventArgs { Variable = Children[0].Convert<Variable>() });
|
2024-04-26 10:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (Children.Count == 3)
|
|
|
|
|
{
|
2024-05-01 21:06:27 +08:00
|
|
|
|
TerminatedSyntaxNode terminatedSyntaxNode = Children[0].Convert<TerminatedSyntaxNode>();
|
|
|
|
|
|
|
|
|
|
// factor -> ( expression )
|
|
|
|
|
if (terminatedSyntaxNode.Token.TokenType == SemanticTokenType.Delimiter)
|
|
|
|
|
{
|
|
|
|
|
OnParethnesisGenerator?.Invoke(this,
|
|
|
|
|
new ParethnesisGeneratorEventArgs { Expression = Children[1].Convert<Expression>() });
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// factor -> id ( )
|
2024-05-04 11:56:06 +08:00
|
|
|
|
OnProcedureCallGenerator?.Invoke(this, new ProcedureCallGeneratorEventArgs
|
2024-05-01 21:06:27 +08:00
|
|
|
|
{
|
|
|
|
|
ProcedureName = terminatedSyntaxNode.Token.Convert<IdentifierSemanticToken>()
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-04-26 10:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
else if (Children.Count == 4)
|
|
|
|
|
{
|
2024-05-01 21:06:27 +08:00
|
|
|
|
// factor -> id ( expressionList)
|
2024-05-04 11:56:06 +08:00
|
|
|
|
ProcedureCallGeneratorEventArgs eventArgs = new()
|
2024-05-01 21:06:27 +08:00
|
|
|
|
{
|
2024-05-04 11:56:06 +08:00
|
|
|
|
ProcedureName =
|
|
|
|
|
Children[0].Convert<TerminatedSyntaxNode>().Token.Convert<IdentifierSemanticToken>(),
|
|
|
|
|
};
|
|
|
|
|
eventArgs.Parameters.AddRange(Children[2].Convert<ExpressionList>().Expressions);
|
|
|
|
|
OnProcedureCallGenerator?.Invoke(this, eventArgs);
|
2024-04-26 10:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-05-01 21:06:27 +08:00
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
SemanticToken token = Children[0].Convert<TerminatedSyntaxNode>().Token;
|
|
|
|
|
Factor factor = Children[1].Convert<Factor>();
|
|
|
|
|
|
|
|
|
|
if (token.TokenType == SemanticTokenType.Keyword)
|
|
|
|
|
{
|
|
|
|
|
// factor -> not factor
|
2024-05-01 21:06:27 +08:00
|
|
|
|
OnNotGenerator?.Invoke(this, new NotGeneratorEventArgs { Factor = factor });
|
2024-04-26 10:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-05-01 21:06:27 +08:00
|
|
|
|
OperatorSemanticToken operatorSemanticToken = token.Convert<OperatorSemanticToken>();
|
|
|
|
|
|
|
|
|
|
switch (operatorSemanticToken.OperatorType)
|
|
|
|
|
{
|
|
|
|
|
// factor -> + factor
|
|
|
|
|
case OperatorType.Plus:
|
|
|
|
|
OnPlusGenerator?.Invoke(this, new PlusGeneratorEventArgs { Factor = factor });
|
|
|
|
|
break;
|
|
|
|
|
// factor -> - factor
|
|
|
|
|
case OperatorType.Minus:
|
|
|
|
|
OnUminusGenerator?.Invoke(this, new UminusGeneratorEventArgs { Factor = factor });
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-04-26 10:18:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OnNumberGenerator = null;
|
|
|
|
|
OnVariableGenerator = null;
|
|
|
|
|
OnParethnesisGenerator = null;
|
|
|
|
|
OnProcedureCallGenerator = null;
|
|
|
|
|
OnNotGenerator = null;
|
|
|
|
|
OnUminusGenerator = null;
|
|
|
|
|
}
|
2024-04-07 16:47:28 +08:00
|
|
|
|
}
|