// Generated from ./src/domain/grammar/StepGrammar.g4 by ANTLR 4.7.1
// jshint ignore: start
var antlr4 = require('antlr4/index');
import StepAST from "../../domain/StepAST";
import { STEP_OPERATOR } from "../../domain/StepOperator";
import Action from "../../domain/Action";
import InteractionFactory from "../../domain/InteractionFactory";

// This class defines a complete generic visitor for a parse tree produced by StepGrammarParser.

function StepGrammarVisitor() {
	antlr4.tree.ParseTreeVisitor.call(this);
	return this;
}

let builder;

exports.setBuilder = function setBuilder(newBuilder) {
  builder = newBuilder;
}

StepGrammarVisitor.prototype = Object.create(antlr4.tree.ParseTreeVisitor.prototype);
StepGrammarVisitor.prototype.constructor = StepGrammarVisitor;

// Visit a parse tree produced by StepGrammarParser#action.
StepGrammarVisitor.prototype.visitAction = function(ctx) {
  return new StepAST(InteractionFactory.parseInteraction(ctx.getText()));
};

// Visit a parse tree produced by StepGrammarParser#string.
StepGrammarVisitor.prototype.visitString = function(ctx) {
   return ctx.getText().replace(/'/g,'');

};


// Visit a parse tree produced by StepGrammarParser#check.
StepGrammarVisitor.prototype.visitCheck = function(ctx) {
  const stepCtx = ctx.step();
  const string = this.visitString(ctx.string());
  if (stepCtx.length ===1) {
    return new StepAST(STEP_OPERATOR.if, this.visitStep(stepCtx[0]), undefined, string);
  } else {
    return new StepAST(STEP_OPERATOR.if_else, this.visitStep(stepCtx[0]), this.visitStep(stepCtx[1]), string);
  }
};


// Visit a parse tree produced by StepGrammarParser#par_step.
StepGrammarVisitor.prototype.visitPar_step = function(ctx) {
  return this.visitStep(ctx.step());
};


// Visit a parse tree produced by StepGrammarParser#and_step.
StepGrammarVisitor.prototype.visitAnd_step = function(ctx, left) {
  return new StepAST(STEP_OPERATOR.and, left, this.visitStep(ctx.step()));
};


// Visit a parse tree produced by StepGrammarParser#or_step.
StepGrammarVisitor.prototype.visitOr_step = function(ctx, left) {
  return new StepAST(STEP_OPERATOR.or, left, this.visitStep(ctx.step()));
};


// Visit a parse tree produced by StepGrammarParser#arrow_step.
StepGrammarVisitor.prototype.visitArrow_step = function(ctx, left) {
  return new StepAST(STEP_OPERATOR.arrow, left, this.visitStep(ctx.step()));
};


// Visit a parse tree produced by StepGrammarParser#sequence_step.
StepGrammarVisitor.prototype.visitSequence_step = function(ctx, left) {
  return new StepAST(STEP_OPERATOR.seq, left, this.visitStep(ctx.step()));
};

// Visit a parse tree produced by StepGrammarParser#step.
StepGrammarVisitor.prototype.visitStep = function(ctx) {
  if (ctx.action()) {
    return this.visitAction(ctx.action());
  } else if (ctx.sequence_step()) {
    return this.visitSequence_step(ctx.sequence_step(), this.visitStep(ctx.step()))
  } else if  (ctx.and_step()) {
    return this.visitAnd_step(ctx.and_step(), this.visitStep(ctx.step()));
  } else if (ctx.or_step()) {
    return this.visitOr_step(ctx.or_step(), this.visitStep(ctx.step()));
  } else if (ctx.arrow_step()) {
    return this.visitArrow_step(ctx.arrow_step(), this.visitStep(ctx.step()));
  } else if (ctx.par_step()) {
    return this.visitPar_step(ctx.par_step());
  } else if (ctx.check()) {
    return this.visitCheck(ctx.check());
  }
};


// Visit a parse tree produced by StepGrammarParser#main.
StepGrammarVisitor.prototype.visitMain = function(ctx) {
  return this.visitStep(ctx.step())
};



exports.StepGrammarVisitor = StepGrammarVisitor;