Repository URL to install this package:
|
Version:
2023.12.1 ▾
|
using System;
using System.Linq;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using Gs2.Unity.Gs2StateMachine.Local.Model;
using Gs2.Unity.Gs2StateMachine.Local.Parser;
namespace Gs2.Unity.Gs2StateMachine.Local
{
public class Listener : IStateMachineParserListener
{
private StateMachine[] _stateMachines = Array.Empty<StateMachine>();
private StateMachine _currentStateMachine;
private ScriptTask _currentScriptTask;
private SubStateMachineTask _currentSubStateMachineTask;
private WaitTask _currentWaitTask;
public StateMachine[] StateMachines => this._stateMachines;
public void VisitTerminal(ITerminalNode node) {
}
public void VisitErrorNode(IErrorNode node) {
}
public void EnterEveryRule(ParserRuleContext ctx) {
}
public void ExitEveryRule(ParserRuleContext ctx) {
}
public void EnterStateMachines(StateMachineParser.StateMachinesContext context) {
}
public void ExitStateMachines(StateMachineParser.StateMachinesContext context) {
}
public void EnterStateMachineDeclaration(StateMachineParser.StateMachineDeclarationContext context) {
this._currentStateMachine = new StateMachine {
Name = context.IDENTIFIER()
.GetText(),
Variables = Array.Empty<Variable>(),
Tasks = Array.Empty<ITask>(),
Transitions = Array.Empty<Transition>(),
};
}
public void ExitStateMachineDeclaration(StateMachineParser.StateMachineDeclarationContext context) {
this._stateMachines = this._stateMachines.Concat(new []{
this._currentStateMachine
}).ToArray();
}
public void EnterStateMachineBody(StateMachineParser.StateMachineBodyContext context) {
}
public void ExitStateMachineBody(StateMachineParser.StateMachineBodyContext context) {
}
public void EnterParametersSection(StateMachineParser.ParametersSectionContext context) {
}
public void ExitParametersSection(StateMachineParser.ParametersSectionContext context) {
}
public void EnterParameterDeclaration(StateMachineParser.ParameterDeclarationContext context) {
}
public void ExitParameterDeclaration(StateMachineParser.ParameterDeclarationContext context) {
}
public void EnterVariablesSection(StateMachineParser.VariablesSectionContext context) {
}
public void ExitVariablesSection(StateMachineParser.VariablesSectionContext context) {
}
public void EnterVariableDeclaration(StateMachineParser.VariableDeclarationContext context) {
}
public void ExitVariableDeclaration(StateMachineParser.VariableDeclarationContext context) {
var dataType = context.dataType().GetText();
foreach (var id in context.IDENTIFIER()) {
this._currentStateMachine.Variables = this._currentStateMachine.Variables.Concat(
new[] {
new Variable {
Type = VariableTypeExt.FromString(dataType),
Name = id.GetText(),
}
}
).ToArray();
}
}
public void EnterEntryPointSection(StateMachineParser.EntryPointSectionContext context) {
}
public void ExitEntryPointSection(StateMachineParser.EntryPointSectionContext context) {
this._currentStateMachine.EntryPoint = context.IDENTIFIER().GetText();
}
public void EnterTasksSection(StateMachineParser.TasksSectionContext context) {
}
public void ExitTasksSection(StateMachineParser.TasksSectionContext context) {
}
public void EnterTransitionsSection(StateMachineParser.TransitionsSectionContext context) {
}
public void ExitTransitionsSection(StateMachineParser.TransitionsSectionContext context) {
}
public void EnterDataType(StateMachineParser.DataTypeContext context) {
}
public void ExitDataType(StateMachineParser.DataTypeContext context) {
}
public void EnterParameterList(StateMachineParser.ParameterListContext context) {
}
public void ExitParameterList(StateMachineParser.ParameterListContext context) {
}
public void EnterParameter(StateMachineParser.ParameterContext context) {
}
public void ExitParameter(StateMachineParser.ParameterContext context) {
}
public void EnterTaskDeclaration(StateMachineParser.TaskDeclarationContext context) {
this._currentScriptTask = new ScriptTask(context.IDENTIFIER().GetText());
if (context.parameterList() != null) {
foreach (var parameter in context.parameterList().parameter()) {
this._currentScriptTask.Parameters = this._currentScriptTask.Parameters.Concat(
new[] {
new Parameter {
Type = VariableTypeExt.FromString(parameter.dataType().GetText()),
Name = parameter.IDENTIFIER().GetText(),
}
}
).ToArray();
}
}
}
public void ExitTaskDeclaration(StateMachineParser.TaskDeclarationContext context) {
this._currentStateMachine.Tasks = this._currentStateMachine.Tasks.Concat(
new[] {
this._currentScriptTask,
}
).ToArray();
this._currentScriptTask = null;
}
public void EnterSubStateMachineTaskDeclaration(StateMachineParser.SubStateMachineTaskDeclarationContext context) {
this._currentSubStateMachineTask = new SubStateMachineTask(context.IDENTIFIER().GetText());
}
public void ExitSubStateMachineTaskDeclaration(StateMachineParser.SubStateMachineTaskDeclarationContext context) {
this._currentStateMachine.Tasks = this._currentStateMachine.Tasks.Concat(
new[] {
this._currentSubStateMachineTask,
}
).ToArray();
this._currentSubStateMachineTask = null;
}
public void EnterPassTaskDeclaration(StateMachineParser.PassTaskDeclarationContext context) {
}
public void ExitPassTaskDeclaration(StateMachineParser.PassTaskDeclarationContext context) {
this._currentStateMachine.Tasks = this._currentStateMachine.Tasks.Concat(
new[] {
new PassTask(context.IDENTIFIER().GetText()),
}
).ToArray();
}
public void EnterErrorTaskDeclaration(StateMachineParser.ErrorTaskDeclarationContext context) {
}
public void ExitErrorTaskDeclaration(StateMachineParser.ErrorTaskDeclarationContext context) {
var errorTask = new ErrorTask(context.IDENTIFIER().GetText());
if (context.parameterList() != null) {
errorTask.Parameters = context.parameterList().parameter().Select(v => new Parameter {
Type = VariableTypeExt.FromString(v.dataType().GetText()),
Name = v.IDENTIFIER().GetText()
}).ToArray();
}
this._currentStateMachine.Tasks = this._currentStateMachine.Tasks.Concat(
new[] {
errorTask,
}
).ToArray();
}
public void EnterWaitTaskDeclaration(StateMachineParser.WaitTaskDeclarationContext context) {
this._currentWaitTask = new WaitTask(context.IDENTIFIER().GetText());
}
public void ExitWaitTaskDeclaration(StateMachineParser.WaitTaskDeclarationContext context) {
this._currentStateMachine.Tasks = this._currentStateMachine.Tasks.Concat(
new[] {
this._currentWaitTask,
}
).ToArray();
this._currentWaitTask = null;
}
public void EnterSubStateMachineTaskBody(StateMachineParser.SubStateMachineTaskBodyContext context) {
}
public void ExitSubStateMachineTaskBody(StateMachineParser.SubStateMachineTaskBodyContext context) {
}
public void EnterTaskBody(StateMachineParser.TaskBodyContext context) {
}
public void ExitTaskBody(StateMachineParser.TaskBodyContext context) {
}
public void EnterWaitTaskBody(StateMachineParser.WaitTaskBodyContext context) {
}
public void ExitWaitTaskBody(StateMachineParser.WaitTaskBodyContext context) {
}
public void EnterEventDeclarationSection(StateMachineParser.EventDeclarationSectionContext context) {
}
public void ExitEventDeclarationSection(StateMachineParser.EventDeclarationSectionContext context) {
var eve = new Event {
Name = context.IDENTIFIER().GetText(),
Parameters = Array.Empty<Parameter>(),
};
if (context.parameterList() != null) {
eve.Parameters = context.parameterList().parameter().Select(v => new Parameter {
Type = VariableTypeExt.FromString(v.dataType().GetText()),
Name = v.IDENTIFIER().GetText(),
}).ToArray();
}
if (this._currentScriptTask != null) {
this._currentScriptTask.Events = this._currentScriptTask.Events.Concat(
new[] {
eve
}
).ToArray();
}
if (this._currentWaitTask != null) {
this._currentWaitTask.Events = this._currentWaitTask.Events.Concat(
new[] {
eve
}
).ToArray();
}
}
public void EnterInParametersSection(StateMachineParser.InParametersSectionContext context) {
foreach (var parameterMapping in context.inParameterMapping()) {
this._currentSubStateMachineTask.InParameters = this._currentSubStateMachineTask.InParameters.Concat(
new[] {
new ParameterMapping {
From = parameterMapping.GetChild(2).GetText(),
To = parameterMapping.GetChild(0).GetText(),
}
}
).ToArray();
}
}
public void ExitInParametersSection(StateMachineParser.InParametersSectionContext context) {
}
public void EnterInParameterMapping(StateMachineParser.InParameterMappingContext context) {
}
public void ExitInParameterMapping(StateMachineParser.InParameterMappingContext context) {
}
public void EnterOutParametersSection(StateMachineParser.OutParametersSectionContext context) {
foreach (var parameterMapping in context.outParameterMapping()) {
this._currentSubStateMachineTask.OutParameters = this._currentSubStateMachineTask.OutParameters.Concat(
new[] {
new ParameterMapping {
From = parameterMapping.GetChild(0).GetText(),
To = parameterMapping.GetChild(2).GetText(),
}
}
).ToArray();
}
}
public void ExitOutParametersSection(StateMachineParser.OutParametersSectionContext context) {
}
public void EnterOutParameterMapping(StateMachineParser.OutParameterMappingContext context) {
}
public void ExitOutParameterMapping(StateMachineParser.OutParameterMappingContext context) {
}
public void EnterUsingStateMachineSection(StateMachineParser.UsingStateMachineSectionContext context) {
}
public void ExitUsingStateMachineSection(StateMachineParser.UsingStateMachineSectionContext context) {
this._currentSubStateMachineTask.Using = context.IDENTIFIER().GetText();
}
public void EnterScriptSection(StateMachineParser.ScriptSectionContext context) {
throw new NotSupportedException("Local execution is only supported for scripts defined in Task for GSLs that directly describe the contents of the script in Payload.");
}
public void ExitScriptSection(StateMachineParser.ScriptSectionContext context) {
throw new NotSupportedException("Local execution is only supported for scripts defined in Task for GSLs that directly describe the contents of the script in Payload.");
}
public void EnterRawScriptSection(StateMachineParser.RawScriptSectionContext context) {
}
public void ExitRawScriptSection(StateMachineParser.RawScriptSectionContext context) {
var script = context.GetText();
script = script.Substring(context.KW_PAYLOAD_LBRACE().GetText().Length);
script = script.Substring(0, script.Length - context.RAW_SCRIPT_RBRACE().GetText().Length);
if (this._currentScriptTask != null) {
this._currentScriptTask.Payload = script;
}
}
public void EnterRawScriptBody(StateMachineParser.RawScriptBodyContext context) {
}
public void ExitRawScriptBody(StateMachineParser.RawScriptBodyContext context) {
}
public void EnterTransitionDeclaration(StateMachineParser.TransitionDeclarationContext context) {
}
public void ExitTransitionDeclaration(StateMachineParser.TransitionDeclarationContext context) {
this._currentStateMachine.Transitions = this._currentStateMachine.Transitions.Concat(
new[] {
new Transition {
Name = context.IDENTIFIER()[0].GetText(),
Handling = context.IDENTIFIER()[1].GetText(),
Destination = context.IDENTIFIER()[2].GetText()
},
}
).ToArray();
}
}
}