package org.strategoxt.imp.testing.strategies;

import static org.spoofax.interpreter.core.Tools.isTermString;

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

import org.eclipse.imp.language.Language;
import org.eclipse.imp.language.LanguageRegistry;
import org.eclipse.imp.parser.IParseController;
import org.spoofax.interpreter.terms.IStrategoString;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.jsglr.shared.BadTokenException;
import org.spoofax.jsglr.shared.SGLRException;
import org.spoofax.jsglr.shared.TokenExpectedException;
import org.strategoxt.imp.runtime.Environment;
import org.strategoxt.imp.runtime.dynamicloading.BadDescriptorException;
import org.strategoxt.imp.runtime.dynamicloading.Descriptor;
import org.strategoxt.imp.runtime.dynamicloading.DynamicParseController;
import org.strategoxt.imp.runtime.parser.JSGLRI;
import org.strategoxt.imp.runtime.parser.SGLRParseController;
import org.strategoxt.lang.Context;
import org.strategoxt.lang.Strategy;

/**
 * parse-spt-string strategy to get AST of Spoofax-Testing testsuite, 
 * where the input fragments have been annotated with the AST of the input.
 * 
 * The current term is the string to parse and the sole term argument is an
 * absolute path to the file this string is coming from.
 */
public class parse_spt_file_0_0 extends Strategy {

	public static parse_spt_file_0_0 instance = new parse_spt_file_0_0();
	
	@Override
	public IStrategoTerm invoke(Context context, IStrategoTerm current) {
		if (!isTermString(current)) return null;
		String filename = ((IStrategoString)current ).stringValue();
		File file = new File(filename);

		Language l = LanguageRegistry.findLanguage("Spoofax-Testing");
		Descriptor d = Environment.getDescriptor(l);
		IStrategoTerm result = null;
		try {
			IParseController ip = d.createParseController();
			if (ip instanceof DynamicParseController)
				ip = ((DynamicParseController) ip).getWrapped();
			if (ip instanceof SGLRParseController) {
				SGLRParseController sglrController = (SGLRParseController) ip;
				JSGLRI parser = sglrController.getParser(); 
				parser.setUseRecovery(false);
				result = parser.parse(new FileInputStream(file), file.getAbsolutePath());
			}
		} catch (BadDescriptorException e) {
			Environment.logException("Could not parse testing string", e);
		} catch (TokenExpectedException e) {
			Environment.logException("Could not parse testing string", e);
		} catch (BadTokenException e) {
			Environment.logException("Could not parse testing string", e);
		} catch (SGLRException e) {
			Environment.logException("Could not parse testing string", e);
		} catch (IOException e) {
			Environment.logException("Could not parse testing string", e);
		}
		
		return result;
	}

}

@@ -2078,16 +2078,171 @@
 er) ip;%0A
+%09%09%09%09// Must lock the parse lock of this controller%0A%09%09%09%09// or hit the assertion in AbstractSGLRI.parse%0A%09%09%09%09sglrController.getParseLock().lock();%0A%09%09%09%09try %7B%0A%09
 %09%09%09%09JSGL
@@ -2282,16 +2282,17 @@
 ser(); %0A
+%09
 %09%09%09%09pars
@@ -2321,16 +2321,17 @@
 e);%0A%09%09%09%09
+%09
 result =
@@ -2396,16 +2396,83 @@
 ath());%0A
+%09%09%09%09%7D finally %7B%0A%09%09%09%09%09sglrController.getParseLock().unlock();%0A%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Fix failed parseLock assertion in AbstractSGLRI