<commit_before>package org.metaborg.meta.lang.dynsem.interpreter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.metaborg.meta.lang.dynsem.interpreter.nodes.rules.InlinedRuleAdapter;
import org.metaborg.meta.lang.dynsem.interpreter.nodes.rules.OverloadedRule;
import org.metaborg.meta.lang.dynsem.interpreter.nodes.rules.Rule;
import org.metaborg.meta.lang.dynsem.interpreter.nodes.rules.RuleRoot;
import org.spoofax.interpreter.core.Tools;
import org.spoofax.interpreter.terms.IStrategoAppl;
import org.spoofax.interpreter.terms.IStrategoList;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.terms.TermFactory;
import org.spoofax.terms.io.TAFTermReader;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;

public abstract class RuleRegistry {

	private final Map<String, RuleRoot> rules = new HashMap<>();

	@TruffleBoundary
	public RuleRoot lookupRule(String name, String constr, int arity) {
		String k = makeKey(name, constr, arity);
		RuleRoot rr = rules.get(k);
		if (rr != null) {
			return rr;
		}
		throw new InterpreterException("No rule found for: " + k);
	}

	public void registerRule(RuleRoot rr) {
		CompilerAsserts.neverPartOfCompilation();
		Rule r = rr.getRule();
		String k = makeKey(r.getName(), r.getConstructor(), r.getArity());

		RuleRoot prevRR = rules.put(k, rr);
		if (prevRR != null) {
			Rule prevRule = prevRR.getRule();
			OverloadedRule ruleChain = null;
			if (prevRule instanceof OverloadedRule) {
				ruleChain = (OverloadedRule) prevRule;
			} else {
				ruleChain = new OverloadedRule(new InlinedRuleAdapter(prevRule, prevRR.getFrameDescriptor()));
			}
			ruleChain.addNext(new InlinedRuleAdapter(r, rr.getFrameDescriptor()));
			rules.put(k, new RuleRoot(ruleChain, rr.getFrameDescriptor()));
		}
	}

	@TruffleBoundary
	public int ruleCount() {
		return rules.size();
	}

	private static String makeKey(String name, String constr, int arity) {
		return name + "/" + constr + "/" + arity;
	}

	public static void populate(RuleRegistry reg, File specificationFile) {
		TAFTermReader reader = new TAFTermReader(new TermFactory());

		IStrategoTerm topSpecTerm;
		try {
			topSpecTerm = reader.parseFromStream(new FileInputStream(specificationFile));
		} catch (IOException ioex) {
			throw new RuntimeException("Could not load specification ATerm", ioex);
		}

		IStrategoList rulesTerm = ruleListTerm(topSpecTerm);
		for (IStrategoTerm ruleTerm : rulesTerm) {
			reg.registerRule(RuleRoot.create(ruleTerm));
		}
	}

	private static IStrategoList ruleListTerm(IStrategoTerm topSpecTerm) {
		IStrategoList sections = Tools.listAt(topSpecTerm, 1);
		for (IStrategoTerm section : sections) {
			if (Tools.isTermAppl(section) && Tools.hasConstructor((IStrategoAppl) section, "Rules", 1)) {
				return Tools.listAt(section, 0);
			}
		}
		throw new InterpreterException("Malformed specification: could not find Rules section");
	}

}<commit_msg>Create a VM Source instance for the specification instead of explicitly handing the file<commit_after>package org.metaborg.meta.lang.dynsem.interpreter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.metaborg.meta.lang.dynsem.interpreter.nodes.rules.InlinedRuleAdapter;
import org.metaborg.meta.lang.dynsem.interpreter.nodes.rules.OverloadedRule;
import org.metaborg.meta.lang.dynsem.interpreter.nodes.rules.Rule;
import org.metaborg.meta.lang.dynsem.interpreter.nodes.rules.RuleRoot;
import org.spoofax.interpreter.core.Tools;
import org.spoofax.interpreter.terms.IStrategoAppl;
import org.spoofax.interpreter.terms.IStrategoList;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.terms.TermFactory;
import org.spoofax.terms.io.TAFTermReader;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.source.SourceSection;

public abstract class RuleRegistry {

	private final Map<String, RuleRoot> rules = new HashMap<>();

	@TruffleBoundary
	public RuleRoot lookupRule(String name, String constr, int arity) {
		String k = makeKey(name, constr, arity);
		RuleRoot rr = rules.get(k);
		if (rr != null) {
			return rr;
		}
		throw new InterpreterException("No rule found for: " + k);
	}

	public void registerRule(RuleRoot rr) {
		CompilerAsserts.neverPartOfCompilation();
		Rule r = rr.getRule();
		String k = makeKey(r.getName(), r.getConstructor(), r.getArity());

		RuleRoot prevRR = rules.put(k, rr);
		if (prevRR != null) {
			Rule prevRule = prevRR.getRule();
			OverloadedRule ruleChain = null;
			if (prevRule instanceof OverloadedRule) {
				ruleChain = (OverloadedRule) prevRule;
			} else {
				ruleChain = new OverloadedRule(new InlinedRuleAdapter(prevRule, prevRR.getFrameDescriptor()));
			}
			ruleChain.addNext(new InlinedRuleAdapter(r, rr.getFrameDescriptor()));
			rules.put(k, new RuleRoot(ruleChain, rr.getFrameDescriptor()));
		}
	}

	@TruffleBoundary
	public int ruleCount() {
		return rules.size();
	}

	private static String makeKey(String name, String constr, int arity) {
		return name + "/" + constr + "/" + arity;
	}

	public static void populate(RuleRegistry reg, File specificationFile) {
		try {
			Source source = Source.fromFileName(specificationFile.getAbsolutePath().toString());
			TAFTermReader reader = new TAFTermReader(new TermFactory());

			IStrategoTerm topSpecTerm;
			topSpecTerm = reader.parseFromStream(source.getInputStream());

			IStrategoList rulesTerm = ruleListTerm(topSpecTerm);
			for (IStrategoTerm ruleTerm : rulesTerm) {
				reg.registerRule(RuleRoot.create(ruleTerm));
			}
		} catch (IOException ioex) {
			throw new RuntimeException("Could not load specification ATerm", ioex);
		}
	}

	private static IStrategoList ruleListTerm(IStrategoTerm topSpecTerm) {
		IStrategoList sections = Tools.listAt(topSpecTerm, 1);
		for (IStrategoTerm section : sections) {
			if (Tools.isTermAppl(section) && Tools.hasConstructor((IStrategoAppl) section, "Rules", 1)) {
				return Tools.listAt(section, 0);
			}
		}
		throw new InterpreterException("Malformed specification: could not find Rules section");
	}

}
{'fraction_non_alphanumeric': 0.11295064211666409, 'fraction_numerical': 0.0009283614420547734, 'mean_word_length': 5.121212121212121, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6967163', 'n_tokens_mistral': 2284, 'n_tokens_neox': 2120, 'n_words': 441}