<commit_before>package com.hubspot.jinjava.lib.tag.eager;

import com.google.common.collect.Sets;
import com.hubspot.jinjava.interpret.Context.TemporaryValueClosable;
import com.hubspot.jinjava.interpret.DeferredMacroValueImpl;
import com.hubspot.jinjava.interpret.DeferredValue;
import com.hubspot.jinjava.interpret.DeferredValueException;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.OutputTooBigException;
import com.hubspot.jinjava.interpret.TemplateError;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.lib.tag.ForTag;
import com.hubspot.jinjava.objects.serialization.PyishObjectMapper;
import com.hubspot.jinjava.tree.TagNode;
import com.hubspot.jinjava.tree.parse.TagToken;
import com.hubspot.jinjava.util.EagerExpressionResolver;
import com.hubspot.jinjava.util.EagerExpressionResolver.EagerExpressionResult;
import com.hubspot.jinjava.util.EagerExpressionResolver.EagerExpressionResult.ResolutionState;
import com.hubspot.jinjava.util.EagerReconstructionUtils;
import com.hubspot.jinjava.util.EagerReconstructionUtils.EagerChildContextConfig;
import com.hubspot.jinjava.util.LengthLimitingStringBuilder;
import com.hubspot.jinjava.util.LengthLimitingStringJoiner;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.Pair;

public class EagerForTag extends EagerTagDecorator<ForTag> {

  public EagerForTag() {
    super(new ForTag());
  }

  public EagerForTag(ForTag forTag) {
    super(forTag);
  }

  @Override
  public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
    try {
      Set<DeferredToken> addedTokens = new HashSet<>();
      EagerExecutionResult result = EagerReconstructionUtils.executeInChildContext(
        eagerInterpreter -> {
          EagerExpressionResult expressionResult = EagerExpressionResult.fromSupplier(
            () -> getTag().interpretUnchecked(tagNode, eagerInterpreter),
            eagerInterpreter
          );
          addedTokens.addAll(eagerInterpreter.getContext().getDeferredTokens());
          return expressionResult;
        },
        interpreter,
        EagerChildContextConfig.newBuilder().withCheckForContextChanges(true).build()
      );
      if (
        result.getResult().getResolutionState() == ResolutionState.NONE ||
        (
          !result.getResult().isFullyResolved() &&
          !result.getSpeculativeBindings().isEmpty()
        )
      ) {
        EagerIfTag.resetBindingsForNextBranch(interpreter, result);
        interpreter.getContext().removeDeferredTokens(addedTokens);
        throw new DeferredValueException(
          result.getResult().getResolutionState() == ResolutionState.NONE
            ? result.getResult().toString()
            : "Modification inside partially evaluated for loop"
        );
      }
      if (result.getResult().isFullyResolved()) {
        return result.getResult().toString(true);
      } else {<commit_msg>Don't swallow exceptions thrown from executeInChildContext<commit_after>package com.hubspot.jinjava.lib.tag.eager;

import com.google.common.collect.Sets;
import com.hubspot.jinjava.interpret.Context.TemporaryValueClosable;
import com.hubspot.jinjava.interpret.DeferredMacroValueImpl;
import com.hubspot.jinjava.interpret.DeferredValue;
import com.hubspot.jinjava.interpret.DeferredValueException;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.OutputTooBigException;
import com.hubspot.jinjava.interpret.TemplateError;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.lib.tag.ForTag;
import com.hubspot.jinjava.objects.serialization.PyishObjectMapper;
import com.hubspot.jinjava.tree.TagNode;
import com.hubspot.jinjava.tree.parse.TagToken;
import com.hubspot.jinjava.util.EagerExpressionResolver;
import com.hubspot.jinjava.util.EagerExpressionResolver.EagerExpressionResult;
import com.hubspot.jinjava.util.EagerExpressionResolver.EagerExpressionResult.ResolutionState;
import com.hubspot.jinjava.util.EagerReconstructionUtils;
import com.hubspot.jinjava.util.EagerReconstructionUtils.EagerChildContextConfig;
import com.hubspot.jinjava.util.LengthLimitingStringBuilder;
import com.hubspot.jinjava.util.LengthLimitingStringJoiner;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.Pair;

public class EagerForTag extends EagerTagDecorator<ForTag> {

  public EagerForTag() {
    super(new ForTag());
  }

  public EagerForTag(ForTag forTag) {
    super(forTag);
  }

  @Override
  public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
    Set<DeferredToken> addedTokens = new HashSet<>();
    EagerExecutionResult result = EagerReconstructionUtils.executeInChildContext(
      eagerInterpreter -> {
        EagerExpressionResult expressionResult = EagerExpressionResult.fromSupplier(
          () -> getTag().interpretUnchecked(tagNode, eagerInterpreter),
          eagerInterpreter
        );
        addedTokens.addAll(eagerInterpreter.getContext().getDeferredTokens());
        return expressionResult;
      },
      interpreter,
      EagerChildContextConfig.newBuilder().withCheckForContextChanges(true).build()
    );
    try {
      if (
        result.getResult().getResolutionState() == ResolutionState.NONE ||
        (
          !result.getResult().isFullyResolved() &&
          !result.getSpeculativeBindings().isEmpty()
        )
      ) {
        EagerIfTag.resetBindingsForNextBranch(interpreter, result);
        interpreter.getContext().removeDeferredTokens(addedTokens);
        throw new DeferredValueException(
          result.getResult().getResolutionState() == ResolutionState.NONE
            ? result.getResult().toString()
            : "Modification inside partially evaluated for loop"
        );
      }
      if (result.getResult().isFullyResolved()) {
        return result.getResult().toString(true);
      } else {
{'fraction_non_alphanumeric': 0.09918543363679923, 'fraction_numerical': 0.00031943778949049674, 'mean_word_length': 4.416955017301038, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4669580', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1809, 'n_words': 247}