package dyvil.tools.repl;

import dyvil.tools.compiler.ast.operator.Operator;
import dyvil.tools.compiler.ast.type.Types;
import dyvil.tools.compiler.parser.Parser;
import dyvil.tools.compiler.parser.ParserManager;
import dyvil.tools.parsing.Name;
import dyvil.tools.parsing.TokenIterator;
import dyvil.tools.parsing.marker.MarkerList;
import dyvil.tools.parsing.token.IToken;

public class REPLParser extends ParserManager
{
	private REPLContext context;
	private boolean     syntaxErrors;
	
	public REPLParser(REPLContext context)
	{
		this.context = context;
	}
	
	@Override
	public void report(IToken token, String message)
	{
		this.syntaxErrors = true;
		if (this.markers != null)
		{
			super.report(token, message);
		}
	}
	
	public boolean parse(MarkerList markers, TokenIterator tokens, Parser parser)
	{
		this.tokens = tokens;
		this.parser = parser;
		this.skip = 0;
		this.reparse = false;
		this.markers = markers;
		this.syntaxErrors = false;
		
		IToken token = null;
		
		tokens.reset();
		
		while (true)
		{
			if (this.reparse)
			{
				this.reparse = false;
			}
			else
			{
				if (!this.tokens.hasNext())
				{
					break;
				}
				
				token = tokens.next();
			}
			
			if (this.skip > 0)
			{
				this.skip--;
				continue;
			}
			
			if (this.parser == null)
			{
				if (!token.isInferred())
				{
					this.report(token, "Unexpected Token: " + token);
				}
				continue;
			}
			
			try
			{
				this.parser.parse(this, token);
			}
			catch (Exception ex)
			{
				ex.printStackTrace();
				return false;
			}
			
			if (this.syntaxErrors && this.markers == null)
			{
				return false;
			}
		}
		
		this.parseRemaining(token);
		
		return !this.syntaxErrors;
	}
	
	@Override
	public Operator getOperator(Name name)
	{
		Operator op = this.context.getOperator(name);
		if (op != null)
		{
			return op;
		}
		return Types.LANG_HEADER.getOperator(name);
	}
	
	protected boolean isRoot()
	{
		return this.parser == Parser.rootParser;
	}
}

@@ -275,32 +275,74 @@
 .TokenIterator;%0A
+import dyvil.tools.parsing.marker.Marker;%0A
 import dyvil.too
@@ -596,26 +596,25 @@
 context;%0A%09%7D%0A
-%09
 %0A
+
 %09@Override%0A%09
@@ -636,36 +636,20 @@
 ort(
-IToken token, String message
+Marker error
 )%0A%09%7B
@@ -729,22 +729,13 @@
 ort(
-token, message
+error
 );%0A%09
@@ -740,17 +740,16 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
 %0A%09public

Fix REPL Parser