<commit_before>/*
 * Copyright (c) 2005-2011, <NAME> <karltk at strategoxt dot org>
 *
 * Licensed under the GNU Lesser General Public License, v2.1
 */
package org.spoofax.interpreter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.spoofax.interpreter.core.Interpreter;
import org.spoofax.interpreter.core.InterpreterErrorExit;
import org.spoofax.interpreter.core.InterpreterException;
import org.spoofax.interpreter.core.InterpreterExit;
import org.spoofax.interpreter.core.UndefinedStrategyException;
import org.spoofax.interpreter.stratego.SDefT;
import org.spoofax.interpreter.terms.IStrategoAppl;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.interpreter.terms.ITermFactory;
import org.spoofax.jsglr.client.InvalidParseTableException;
import org.spoofax.jsglr.client.ParseException;
import org.spoofax.jsglr.client.ParseTable;
import org.spoofax.jsglr.client.SGLR;
import org.spoofax.jsglr.client.imploder.TreeBuilder;
import org.spoofax.jsglr.io.ParseTableManager;
import org.spoofax.jsglr.shared.BadTokenException;
import org.spoofax.jsglr.shared.SGLRException;
import org.spoofax.jsglr.shared.TokenExpectedException;
import org.spoofax.terms.ParseError;
import org.spoofax.terms.TermFactory;

public class ConcreteInterpreter extends Interpreter {

	private final ParseTable sugarTable;
	private final SGLR sugarParser;

	public ConcreteInterpreter() {
		this(new TermFactory());
	}

	public ConcreteInterpreter(ITermFactory termFactory) {
		super(termFactory, new TermFactory());
		try {
			load(findLibrary("stratego-lib/libstratego-lib.ctree"));
			load(findLibrary("libstrc.ctree"));
			load(findLocalLibrary("share/frontend.ctree"));
			// load(findLibrary("libstratego-aterm.ctree"));
			// load(findLibrary("libstratego-gpp.ctree"));
			// load(findLibrary("libstratego-rtg.ctree"));
			// load(findLibrary("libstratego-sdf.ctree"));
			// load(findLibrary("libstratego-sglr.ctree"));
			// load(findLibrary("libstratego-tool-doc.ctree"));

			ParseTableManager ptm = new ParseTableManager();
			sugarTable = ptm
					.loadFromStream(findLocalLibrary("/share/Stratego-Shell.tbl"));
			sugarParser = new SGLR(new TreeBuilder(), sugarTable);
			sugarParser.setUseStructureRecovery(false);
			setCurrent(getFactory().makeList());
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (InterpreterException e) {
			throw new RuntimeException(e);
		} catch (ParseError e) {
			throw new RuntimeException(e);
		} catch (InvalidParseTableException e) {
			throw new RuntimeException(e);
		}

	}

	private InputStream findLocalLibrary(String path) throws IOException {
		InputStream ins = ConcreteInterpreter.class.getClassLoader().getResourceAsStream(path);
		if(ins == null)
			throw new IOException("Failed to load internal library " + path);
		return ins;
	}

	protected InputStream findLibrary(String libraryPath) throws IOException {
		String shareDir = System.getProperty("user.home")
				+ "/.nix-profile/share/";
		File file = new File(shareDir + "/" + libraryPath);
		if (file.exists()) {
			try {
				return new FileInputStream(file);
			} catch (FileNotFoundException e) {
				throw new RuntimeException(e);
			}
		}
		throw new IOException("Failed to find Stratego library " + file.getAbsolutePath());
	}

	public void loadConcrete(String file, String[] path, boolean lib)
			throws IOException, InterpreterException {
	}

	private IStrategoAppl parseAndCompile(String codeAsString, String frontendStrategy, String startSymbol) throws TokenExpectedException, BadTokenException, ParseException, SGLRException, InterpreterErrorExit, InterpreterExit, UndefinedStrategyException, InterpreterException {
		IStrategoTerm tree = (IStrategoTerm) sugarParser.parse(codeAsString, "stdin", startSymbol);
		IStrategoTerm old = current();
		setCurrent(tree);<commit_msg>Renamed findLocalLibrary to findLocalResource because that's what we use it for. Fixed path to Stratego-Shell.tbl<commit_after>/*
 * Copyright (c) 2005-2011, <NAME> <karltk at strategoxt dot org>
 *
 * Licensed under the GNU Lesser General Public License, v2.1
 */
package org.spoofax.interpreter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.spoofax.interpreter.core.Interpreter;
import org.spoofax.interpreter.core.InterpreterErrorExit;
import org.spoofax.interpreter.core.InterpreterException;
import org.spoofax.interpreter.core.InterpreterExit;
import org.spoofax.interpreter.core.UndefinedStrategyException;
import org.spoofax.interpreter.stratego.SDefT;
import org.spoofax.interpreter.terms.IStrategoAppl;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.interpreter.terms.ITermFactory;
import org.spoofax.jsglr.client.InvalidParseTableException;
import org.spoofax.jsglr.client.ParseException;
import org.spoofax.jsglr.client.ParseTable;
import org.spoofax.jsglr.client.SGLR;
import org.spoofax.jsglr.client.imploder.TreeBuilder;
import org.spoofax.jsglr.io.ParseTableManager;
import org.spoofax.jsglr.shared.BadTokenException;
import org.spoofax.jsglr.shared.SGLRException;
import org.spoofax.jsglr.shared.TokenExpectedException;
import org.spoofax.terms.ParseError;
import org.spoofax.terms.TermFactory;

public class ConcreteInterpreter extends Interpreter {

	private final ParseTable sugarTable;
	private final SGLR sugarParser;

	public ConcreteInterpreter() {
		this(new TermFactory());
	}

	public ConcreteInterpreter(ITermFactory termFactory) {
		super(termFactory, new TermFactory());
		try {
			load(findLibrary("stratego-lib/libstratego-lib.ctree"));
			load(findLibrary("libstrc.ctree"));
			load(findLocalResource("share/frontend.ctree"));
			// load(findLibrary("libstratego-aterm.ctree"));
			// load(findLibrary("libstratego-gpp.ctree"));
			// load(findLibrary("libstratego-rtg.ctree"));
			// load(findLibrary("libstratego-sdf.ctree"));
			// load(findLibrary("libstratego-sglr.ctree"));
			// load(findLibrary("libstratego-tool-doc.ctree"));

			ParseTableManager ptm = new ParseTableManager();
			sugarTable = ptm
					.loadFromStream(findLocalResource("share/Stratego-Shell.tbl"));
			sugarParser = new SGLR(new TreeBuilder(), sugarTable);
			sugarParser.setUseStructureRecovery(false);
			setCurrent(getFactory().makeList());
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (InterpreterException e) {
			throw new RuntimeException(e);
		} catch (ParseError e) {
			throw new RuntimeException(e);
		} catch (InvalidParseTableException e) {
			throw new RuntimeException(e);
		}

	}

	private InputStream findLocalResource(String path) throws IOException {
		InputStream ins = ConcreteInterpreter.class.getClassLoader().getResourceAsStream(path);
		if(ins == null)
			throw new IOException("Failed to load internal resource " + path);
		return ins;
	}

	protected InputStream findLibrary(String libraryPath) throws IOException {
		String shareDir = System.getProperty("user.home")
				+ "/.nix-profile/share/";
		File file = new File(shareDir + "/" + libraryPath);
		if (file.exists()) {
			try {
				return new FileInputStream(file);
			} catch (FileNotFoundException e) {
				throw new RuntimeException(e);
			}
		}
		throw new IOException("Failed to find Stratego library " + file.getAbsolutePath());
	}

	public void loadConcrete(String file, String[] path, boolean lib)
			throws IOException, InterpreterException {
	}

	private IStrategoAppl parseAndCompile(String codeAsString, String frontendStrategy, String startSymbol) throws TokenExpectedException, BadTokenException, ParseException, SGLRException, InterpreterErrorExit, InterpreterExit, UndefinedStrategyException, InterpreterException {
		IStrategoTerm tree = (IStrategoTerm) sugarParser.parse(codeAsString, "stdin", startSymbol);
		IStrategoTerm old = current();
		setCurrent(tree);
{'fraction_non_alphanumeric': 0.1107765650483001, 'fraction_numerical': 0.002509095471082675, 'mean_word_length': 5.648874061718098, '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': '538039', 'n_tokens_mistral': 2717, 'n_tokens_neox': 2512, 'n_words': 485}