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

import java.io.IOException;

import org.spoofax.interpreter.ConcreteInterpreter;
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.library.eclipse.EFILibrary;
import org.spoofax.interpreter.library.java.JFFLibrary;
import org.spoofax.interpreter.ui.SpoofaxConsole.ConsoleIOAgent;
import org.spoofax.jsglr.client.ParseException;
import org.spoofax.jsglr.shared.BadTokenException;
import org.spoofax.jsglr.shared.SGLRException;
import org.spoofax.jsglr.shared.TokenExpectedException;

public class SpoofaxInterpreter {

	private final ConcreteInterpreter interpreter;
	private final ConsoleIOAgent ioAgent;

	public SpoofaxInterpreter(ConsoleIOAgent ioAgent) {
		this.ioAgent = ioAgent;
		interpreter = new ConcreteInterpreter(); //new ECJFactory());
		try {
			JFFLibrary.attach(interpreter);
			EFILibrary.attach(interpreter);
			interpreter.setIOAgent(ioAgent);
		} catch(IOException e) {
			throw new RuntimeException(e);
		} catch(InterpreterException e) {
			throw new RuntimeException(e);
		}

	}

	public boolean eval(String line) {
		try {
			if(!interpreter.parseAndInvoke(line)) {
				ioAgent.err.println("command failed");
			} else {
				ioAgent.out.println(interpreter.current().toString());
			}
		} catch (TokenExpectedException e) {
			ioAgent.err.println(e.getMessage());
		} catch (InterpreterErrorExit e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadTokenException e) {
			ioAgent.err.println(e.getMessage());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterpreterExit e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UndefinedStrategyException e) {
			ioAgent.err.println(e.getMessage());
		} catch (SGLRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterpreterException e) {
			if(e.getCause() != null)
				ioAgent.err.println(e.getCause().getMessage());
			else
				ioAgent.err.println(e.getMessage());
		} finally {
			ioAgent.out.flush();
			ioAgent.err.flush();
		}
		return true;
	}


}<commit_msg>Report all exceptions in the error log; some of these should probably rather be reported to the user in the console, after pretty-printing.<commit_after>/*
 * Copyright (c) 2011, <NAME> <karltk at strategoxt dot org>
 *
 * Licensed under the GNU Lesser General Public License, v2.1
 */
package org.spoofax.interpreter.ui;

import java.io.IOException;

import org.spoofax.interpreter.ConcreteInterpreter;
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.library.eclipse.EFILibrary;
import org.spoofax.interpreter.library.java.JFFLibrary;
import org.spoofax.interpreter.ui.SpoofaxConsole.ConsoleIOAgent;
import org.spoofax.jsglr.client.ParseException;
import org.spoofax.jsglr.shared.BadTokenException;
import org.spoofax.jsglr.shared.SGLRException;
import org.spoofax.jsglr.shared.TokenExpectedException;

public class SpoofaxInterpreter {

	private final ConcreteInterpreter interpreter;
	private final ConsoleIOAgent ioAgent;

	public SpoofaxInterpreter(ConsoleIOAgent ioAgent) {
		this.ioAgent = ioAgent;
		interpreter = new ConcreteInterpreter(); //new ECJFactory());
		try {
			JFFLibrary.attach(interpreter);
			EFILibrary.attach(interpreter);
			interpreter.setIOAgent(ioAgent);
		} catch(IOException e) {
			throw new RuntimeException(e);
		} catch(InterpreterException e) {
			throw new RuntimeException(e);
		}

	}

	public boolean eval(String line) {
		try {
			if(!interpreter.parseAndInvoke(line)) {
				ioAgent.err.println("command failed");
			} else {
				ioAgent.out.println(interpreter.current().toString());
			}
		} catch (TokenExpectedException e) {
			ioAgent.err.println(e.getMessage());
		} catch (InterpreterErrorExit e) {
			logError(e); // FIXME might want to report this sensibly
		} catch (BadTokenException e) {
			ioAgent.err.println(e.getMessage());
		} catch (ParseException e) {
			logError(e); // FIXME might want to report this sensibly
		} catch (InterpreterExit e) {
			logError(e); // FIXME might want to report this sensibly
		} catch (UndefinedStrategyException e) {
			ioAgent.err.println(e.getMessage());
		} catch (SGLRException e) {
			logError(e); // FIXME might want to report this sensibly
		} catch (InterpreterException e) {
			if(e.getCause() != null)
				ioAgent.err.println(e.getCause().getMessage());
			else
				ioAgent.err.println(e.getMessage());
		} finally {
			ioAgent.out.flush();
			ioAgent.err.flush();
		}
		return true;
	}

	private void logError(Exception e) {
		InterpreterPlugin.logError("Interpreter failure", e);
	}


}
{'fraction_non_alphanumeric': 0.11464354527938343, 'fraction_numerical': 0.0023121387283236996, 'mean_word_length': 4.654684095860566, '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': '6277104', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1691, 'n_words': 339}