package rapanui.dsl;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.inject.Inject;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;

import rapanui.dsl.moai.RuleSystem;
import rapanui.dsl.moai.Formula;
import rapanui.dsl.moai.Term;

public class Parser {
	@Inject
	private XtextResourceSet resourceSet;

	public Parser() {
		new MoaiStandaloneSetup()
		.createInjectorAndDoEMFRegistration()
		.injectMembers(this);

		resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
	}

	public RuleSystem parseRuleSystem(String input) {
		return (RuleSystem)parse(input);
	}

	public Formula parseFormula(String input) {
		return (Formula)parse(input);
	}

	public Term parseTerm(String input) {
		return (Term)parse(input);
	}

	private EObject parse(String input) {
		InputStream in;
		try {
			in = new ByteArrayInputStream(input.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			throw new IllegalArgumentException(e);
		}

		Resource resource = resourceSet.createResource(URI.createURI("dummy:/inmemory.moai"));
		try {
			resource.load(in, resourceSet.getLoadOptions());
		} catch (IOException e) {
			throw new IllegalStateException(e);
		}
		return resource.getContents().get(0);
	}
}

@@ -34,129 +34,20 @@
 .io.
-ByteArrayInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.UnsupportedEncodingException
+StringReader
 ;%0A%0Ai
@@ -101,23 +101,21 @@
 emf.
+e
 co
-mmon.util.URI
+re.EObject
 ;%0Aim
@@ -131,33 +131,32 @@
 eclipse.
-emf.ecore.EObject
+xtext.ParserRule
 ;%0Aimport
@@ -172,34 +172,28 @@
 pse.
-emf.ecore.resource.Resourc
+xtext.nodemodel.INod
 e;%0Ai
@@ -216,38 +216,35 @@
 e.xtext.
-resource.XtextResource
+parser.IParseResult
 ;%0Aimport
@@ -266,33 +266,22 @@
 ext.
-resource.XtextResourceSet
+parser.IParser
 ;%0A%0Ai
@@ -382,77 +382,164 @@
 rm;%0A
-%0Apublic class Parser %7B%0A%09@Inject%0A%09private XtextResourceSet resourceSet
+import rapanui.dsl.services.MoaiGrammarAccess;%0A%0Apublic class Parser %7B%0A%09@Inject%0A%09private IParser internalParser;%0A%0A%09@Inject%0A%09private MoaiGrammarAccess grammar
 ;%0A%0A%09
@@ -651,86 +651,8 @@
 is);
-%0A%0A%09%09resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
 %0A%09%7D%0A
@@ -727,32 +727,61 @@
 stem)parse(input
+, grammar.getRuleSystemRule()
 );%0A%09%7D%0A%0A%09public F
@@ -837,32 +837,58 @@
 mula)parse(input
+, grammar.getFormulaRule()
 );%0A%09%7D%0A%0A%09public T
@@ -943,16 +943,39 @@
 se(input
+, grammar.getTermRule()
 );%0A%09%7D%0A%0A%09
@@ -1012,446 +1012,333 @@
 nput
-) %7B%0A%09%09InputStream in;%0A%09%09try %7B%0A%09%09%09in = new ByteArrayInputStream(input.getBytes(%22UTF-8%22));%0A%09%09%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09throw new IllegalArgumentException(e);%0A%09%09%7D%0A%0A%09%09Resource resource = resourceSet.createResource(URI.createURI(%22dummy:/inmemory.moai%22));%0A%09%09try %7B%0A%09%09%09resource.load(in, resourceSet.getLoadOptions());%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09throw new IllegalStateException(e);%0A%09%09%7D%0A%09%09return resource.getContents().ge
+, ParserRule rule) %7B%0A%09%09IParseResult result = internalParser.parse(rule, new StringReader(input));%0A%09%09if (result.hasSyntaxErrors()) %7B%0A%09%09%09for (INode node : result.getSyntaxErrors())%0A%09%09%09%09throw new IllegalArgumentException(%22Parsing error: %22%0A%09%09%09%09%09+ node.getSyntaxErrorMessage().getMessage());%0A%09%09%7D%0A%09%09return result.getRootASTElemen
 t(
-0
 );%0A%09

Fix parsing for terms and formulas