<commit_before>package org.metaborg.meta.nabl2.spoofax.primitives;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.metaborg.meta.nabl2.spoofax.analysis.IScopeGraphContext;
import org.metaborg.meta.nabl2.spoofax.analysis.IScopeGraphUnit;
import org.metaborg.meta.nabl2.spoofax.analysis.StrategoAnalysis;
import org.metaborg.meta.nabl2.stratego.ConstraintTerms;
import org.metaborg.meta.nabl2.stratego.StrategoTerms;
import org.metaborg.meta.nabl2.terms.ITerm;
import org.spoofax.interpreter.core.InterpreterException;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.interpreter.terms.ITermFactory;
import org.spoofax.terms.util.NotImplementedException;

public abstract class AnalysisPrimitive extends ScopeGraphContextPrimitive {

    public AnalysisPrimitive(String name) {
        this(name, 0);
    }

    public AnalysisPrimitive(String name, int tvars) {
        super(name, 0, tvars + 1);
    }

    @Override public Optional<? extends IStrategoTerm> call(IScopeGraphContext<?> context, IStrategoTerm sterm,
            List<IStrategoTerm> sterms, ITermFactory factory) throws InterpreterException {
        if(sterms.size() < 1) {
            throw new IllegalArgumentException("Expected as first term argument: analysis");
        }
        if(!(sterms.get(0) instanceof StrategoAnalysis)) {
            throw new IllegalArgumentException("Not a valid analysis term.");
        }
        final StrategoAnalysis analysis = (StrategoAnalysis) sterms.get(0);
        return call(analysis, sterm, sterms, factory);
    }

    public Optional<? extends IStrategoTerm> call(IScopeGraphUnit unit, IStrategoTerm sterm, List<IStrategoTerm> sterms,
            ITermFactory factory) throws InterpreterException {
        final StrategoTerms strategoTerms = new StrategoTerms(factory);
        final List<ITerm> terms = sterms.stream().skip(1).map(strategoTerms::fromStratego)
                .map(ConstraintTerms::specialize).collect(Collectors.toList());<commit_msg>Add support for previous analysis token for bootstrapping.<commit_after>package org.metaborg.meta.nabl2.spoofax.primitives;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.metaborg.meta.nabl2.spoofax.analysis.IScopeGraphContext;
import org.metaborg.meta.nabl2.spoofax.analysis.IScopeGraphUnit;
import org.metaborg.meta.nabl2.spoofax.analysis.StrategoAnalysis;
import org.metaborg.meta.nabl2.stratego.ConstraintTerms;
import org.metaborg.meta.nabl2.stratego.StrategoTermIndices;
import org.metaborg.meta.nabl2.stratego.StrategoTerms;
import org.metaborg.meta.nabl2.terms.ITerm;
import org.spoofax.interpreter.core.InterpreterException;
import org.spoofax.interpreter.core.Tools;
import org.spoofax.interpreter.terms.IStrategoAppl;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.interpreter.terms.ITermFactory;
import org.spoofax.terms.util.NotImplementedException;

public abstract class AnalysisPrimitive extends ScopeGraphContextPrimitive {

    public AnalysisPrimitive(String name) {
        this(name, 0);
    }

    public AnalysisPrimitive(String name, int tvars) {
        super(name, 0, tvars + 1);
    }

    @Override public Optional<? extends IStrategoTerm> call(IScopeGraphContext<?> context, IStrategoTerm sterm,
            List<IStrategoTerm> sterms, ITermFactory factory) throws InterpreterException {
        if(sterms.size() < 1) {
            throw new IllegalArgumentException("Expected as first term argument: analysis");
        }
        final IStrategoTerm analysisTerm = sterms.get(0);
        final StrategoAnalysis analysis;
        if((analysisTerm instanceof StrategoAnalysis)) {
            analysis = (StrategoAnalysis) analysisTerm;
        } else if(Tools.isTermAppl(analysisTerm)
                && Tools.hasConstructor((IStrategoAppl) analysisTerm, "AnalysisToken", 0)) {
            // TODO Remove legacy case after bootstrapping
            analysis = StrategoTermIndices.get(analysisTerm)
                    .map(idx -> new StrategoAnalysis(context.unit(idx.getResource())))
                    .orElseThrow(() -> new IllegalArgumentException("Not a valid analysis term."));
        } else {
            throw new IllegalArgumentException("Not a valid analysis term.");
        }
        return call(analysis, sterm, sterms, factory);
    }

    public Optional<? extends IStrategoTerm> call(IScopeGraphUnit unit, IStrategoTerm sterm, List<IStrategoTerm> sterms,
            ITermFactory factory) throws InterpreterException {
        final StrategoTerms strategoTerms = new StrategoTerms(factory);
        final List<ITerm> terms = sterms.stream().skip(1).map(strategoTerms::fromStratego)
                .map(ConstraintTerms::specialize).collect(Collectors.toList());
{'fraction_non_alphanumeric': 0.09622563251762754, 'fraction_numerical': 0.0060141020323517215, 'mean_word_length': 4.0187304890738815, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '6004829', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1465, 'n_words': 276}