/**
  *
  *                      OOAS Compiler
  *
  *       Copyright 2015, AIT Austrian Institute of Technology.
  * This code is based on the C# Version of the OOAS Compiler, which is
  * copyright 2015 by the Institute of Software Technology, Graz University
  * of Technology with portions copyright by the AIT Austrian Institute of
  * Technology. All rights reserved.
  *
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
  *
  * If you modify the file please update the list of contributors below to in-
  * clude your name. Please also stick to the coding convention of using TABs
  * to do the basic (block-level) indentation and spaces for anything after
  * that. (Enable the display of special chars and it should be pretty obvious
  * what this means.) Also, remove all trailing whitespace.
  *
  * Contributors:
  *               Willibald Krenn (AIT)
  *               Stephan Zimmerer (AIT)
  *               Markus Demetz (AIT)
  *               Christoph Czurda (AIT)
  *
  */


package org.momut.ooas.parser;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Token;
import org.momut.ooas.ast.IScope;
import org.momut.ooas.ast.identifiers.Identifier;
import org.momut.ooas.ast.identifiers.MainModule;
import org.momut.ooas.ast.identifiers.MethodIdentifier;
import org.momut.ooas.ast.types.OoActionSystemType;
import org.momut.ooas.ast.types.OpaqueType;
import org.momut.ooas.utils.exceptions.InternalCompilerException;

public class ParserState
{
	public ooaParser parser;
	public ooaLexer lexer;
	public String filename;
	public String namesTypePrefix = "";
	public boolean parsingAttributes;
	public List<String> mappingInformation = new ArrayList<>();


	public MainModule ooaSystem;

	public IScope currentScope;
	public ArrayList<ParserError> listOfParserErrors;
	public ArrayList<ParserWarning> listOfParserWarnings;
	public ArrayList<ParserMessage> listOfParserMessages;
	public ArrayList<OpaqueType> typesToFixUp;

	public ParserState(String unitName, InputStream toParse, String namedTypePrefix) throws IOException{
		try {
			listOfParserErrors = new ArrayList<ParserError>();
			listOfParserWarnings = new ArrayList<ParserWarning>();
			listOfParserMessages = new ArrayList<ParserMessage>();
			typesToFixUp = new ArrayList<OpaqueType>();
			this.filename = unitName;
			this.namesTypePrefix = namedTypePrefix;

			final ANTLRStringStream input = new ANTLRInputStream(toParse); //, Encoding.ASCII); FIXME: Encoding.ASCII
			lexer = new ooaLexer(input);
			final CommonTokenStream tokens = new CommonTokenStream(lexer);
			parser = new ooaParser(tokens);
			parser.pState = this;
		} finally {
			toParse.close();
		}
	}

	public ParserState(String fileToParse, String namedTypePrefix) throws IOException
	{
		this(fileToParse, new FileInputStream(fileToParse), namedTypePrefix);
	}


	public void AddErrorMessage(ParserError parserError)
	{
		listOfParserErrors.add(parserError);
	}
	public void AddWarningMessage(ParserWarning parserWarning)
	{
		listOfParserWarnings.add(parserWarning);
	}
	public void AddMessage(ParserMessage aMessage)
	{
		listOfParserMessages.add(aMessage);
	}

	public Identifier Lookup(String symbolName, IScope resolveStack)
	{
		final String lkupname = symbolName;
		do
		{
			final Identifier result = resolveStack.ResolveIdentifier(lkupname);
			if (result != null)
				return result;
			resolveStack = resolveStack.GetParentScope();
		} while (resolveStack != null);

		return null;
	}
	public Identifier Lookup(String symbolName)
	{
		return Lookup(symbolName, currentScope);
	}
	public Identifier Lookup(Token symbolName)
	{
		if (symbolName != null)
			return Lookup(symbolName.getText());
		else
			return null;
	}

	public void PushResolveStack(IScope newScope)
	{
		// some sanity check.
		if (newScope instanceof MethodIdentifier && !(currentScope instanceof OoActionSystemType))
			throw new InternalCompilerException("Assertion violated: current scope no action system.");

		newScope.SetParentScope(currentScope);
		currentScope = newScope;
	}
	public void PopResolveStack()
	{
		currentScope = currentScope.GetParentScope();
	}
}
