<commit_before>//////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016, <NAME> - All Rights Reserved                /
//                                                                                       /
// Licensed under the GNU General Public License v3;                                     /
// you may not use this file except in compliance with the License.                      /
//                                                                                       /
// You may obtain a copy of the License at                                               /
//     http://www.gnu.org/licenses/gpl.html                                              /
//                                                                                       /
// Unless required by applicable law or agreed to in writing, software                   /
// distributed under the License is distributed on an "AS IS" BASIS,                     /
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.              /
// See the License for the specific language governing permissions and                   /
// limitations under the License.                                                        /
//                                                                                       /
// Written by <NAME> <<EMAIL>>, March 2016           /
// https://www.github.com/BloodShura                                                     /
//////////////////////////////////////////////////////////////////////////////////////////

package br.shura.venus.compiler;

import br.shura.venus.exception.compile.UnexpectedTokenException;
import br.shura.venus.operator.BinaryOperator;
import br.shura.venus.operator.Operator;
import br.shura.venus.operator.UnaryOperator;
import br.shura.venus.resultor.BinaryOperation;
import br.shura.venus.resultor.Resultor;
import br.shura.venus.resultor.UnaryOperation;
import br.shura.x.collection.store.impl.Stack;

/**
 * BuildingResultor.java
 *
 * @author <a href="https://www.github.com/BloodShura">BloodShura</a> (<NAME>)
 * @contact <EMAIL>
 * @date 10/05/16 - 23:01
 * @since GAMMA - 0x3
 */
public class BuildingResultor {
  private Operator operator;
  private Resultor resultor;
  private final Stack<UnaryOperator> unaryWhenAlready;

  public BuildingResultor() {
    this.unaryWhenAlready = new Stack<>();
  }

  public void addOperator(VenusParser parser, Token owner, Operator op) throws UnexpectedTokenException {
    if (hasOperator()) {
      if (op instanceof UnaryOperator) {
        unaryWhenAlready.push((UnaryOperator) op);

        return;
      }
      else {
        parser.bye(owner, "already have an operator \"" + operator + "\"");
      }
    }

    if (op instanceof BinaryOperator && !hasResultor()) {
      parser.bye(owner, "no left operation value");
    }

    if (op instanceof UnaryOperator && hasResultor()) {
      parser.bye(owner, "cannot apply unary operator to left-sided value");
    }

    this.operator = op;
  }

  public void addResultor(VenusParser parser, Token owner, Resultor rslt) throws UnexpectedTokenException {
    if (!hasResultor()) {
      if (operator instanceof UnaryOperator) {
        while (!unaryWhenAlready.isEmpty()) {
          rslt = new UnaryOperation(unaryWhenAlready.pop(), rslt);
        }

        this.resultor = new UnaryOperation((UnaryOperator) operator, rslt);
      }
      else {
        this.resultor = rslt;
      }
    }
    else if (hasOperator()) {
      if (operator instanceof BinaryOperator) {
        while (!unaryWhenAlready.isEmpty()) {
          rslt = new UnaryOperation(unaryWhenAlready.pop(), rslt);
        }

        this.resultor = new BinaryOperation((BinaryOperator) operator, resultor, rslt);
      }
      else {
        parser.bye("Excepted a binary or unary operator, received " + operator.getClass().getSimpleName());
      }

      this.operator = null;
    }
    else {
      parser.bye(owner, "expected operator, found another resultor");
    }
  }

  public Resultor build() {
    return resultor;
  }

  public boolean hasOperator() {
    return operator != null;
  }

  public boolean hasResultor() {
    return resultor != null;
  }
}<commit_msg>Added 'inContext' attribute to BuildingResultor, which now is able to build InContext resultors (part of #23)<commit_after>//////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016, <NAME> - All Rights Reserved                /
//                                                                                       /
// Licensed under the GNU General Public License v3;                                     /
// you may not use this file except in compliance with the License.                      /
//                                                                                       /
// You may obtain a copy of the License at                                               /
//     http://www.gnu.org/licenses/gpl.html                                              /
//                                                                                       /
// Unless required by applicable law or agreed to in writing, software                   /
// distributed under the License is distributed on an "AS IS" BASIS,                     /
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.              /
// See the License for the specific language governing permissions and                   /
// limitations under the License.                                                        /
//                                                                                       /
// Written by <NAME> <<EMAIL>>, March 2016           /
// https://www.github.com/BloodShura                                                     /
//////////////////////////////////////////////////////////////////////////////////////////

package br.shura.venus.compiler;

import br.shura.venus.exception.compile.UnexpectedTokenException;
import br.shura.venus.operator.BinaryOperator;
import br.shura.venus.operator.Operator;
import br.shura.venus.operator.UnaryOperator;
import br.shura.venus.resultor.BinaryOperation;
import br.shura.venus.resultor.InContext;
import br.shura.venus.resultor.Resultor;
import br.shura.venus.resultor.UnaryOperation;
import br.shura.x.collection.store.impl.Stack;

/**
 * BuildingResultor.java
 *
 * @author <a href="https://www.github.com/BloodShura">BloodShura</a> (<NAME>)
 * @contact <EMAIL>
 * @date 10/05/16 - 23:01
 * @since GAMMA - 0x3
 */
public class BuildingResultor {
  private String inContext;
  private Operator operator;
  private Resultor resultor;
  private final Stack<UnaryOperator> unaryWhenAlready;

  public BuildingResultor() {
    this.unaryWhenAlready = new Stack<>();
  }

  public void addInContext(VenusParser parser, Token owner, String context) throws UnexpectedTokenException {
    if (inContext != null) {
      parser.bye(owner, "already in object access \"" + inContext + "\"");
    }

    this.inContext = context;
  }

  public void addOperator(VenusParser parser, Token owner, Operator op) throws UnexpectedTokenException {
    if (hasOperator()) {
      if (op instanceof UnaryOperator) {
        unaryWhenAlready.push((UnaryOperator) op);

        return;
      }
      else {
        parser.bye(owner, "already have an operator \"" + operator + "\"");
      }
    }

    if (op instanceof BinaryOperator && !hasResultor()) {
      parser.bye(owner, "no left operation value");
    }

    if (op instanceof UnaryOperator && hasResultor()) {
      parser.bye(owner, "cannot apply unary operator to left-sided value");
    }

    this.operator = op;
  }

  public void addResultor(VenusParser parser, Token owner, Resultor rslt) throws UnexpectedTokenException {
    if (!hasResultor()) {
      if (operator instanceof UnaryOperator) {
        while (!unaryWhenAlready.isEmpty()) {
          rslt = new UnaryOperation(unaryWhenAlready.pop(), rslt);
        }

        setResultor(new UnaryOperation((UnaryOperator) operator, rslt));
      }
      else {
        setResultor(rslt);
      }
    }
    else if (hasOperator()) {
      if (operator instanceof BinaryOperator) {
        while (!unaryWhenAlready.isEmpty()) {
          rslt = new UnaryOperation(unaryWhenAlready.pop(), rslt);
        }

        setResultor(new BinaryOperation((BinaryOperator) operator, resultor, rslt));
      }
      else {
        parser.bye("Excepted a binary or unary operator, received " + operator.getClass().getSimpleName());
      }

      this.operator = null;
    }
    else {
      parser.bye(owner, "expected operator, found another resultor");
    }
  }

  public Resultor build() {
    return resultor;
  }

  public boolean hasOperator() {
    return operator != null;
  }

  public boolean hasResultor() {
    return resultor != null;
  }

  private void setResultor(Resultor resultor) {
    if (inContext != null) {
      this.resultor = new InContext(inContext, resultor);

      this.inContext = null;
    }
    else {
      this.resultor = resultor;
    }
  }
}
{'fraction_non_alphanumeric': 0.13327507100720995, 'fraction_numerical': 0.004806641905178064, 'mean_word_length': 1.5572625698324023, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1556026', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2184, 'n_words': 608}