All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.unlaxer.context.ParseContext Maven / Gradle / Ivy

package org.unlaxer.context;

import java.io.Closeable;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.unlaxer.CodePointIndex;
import org.unlaxer.CodePointLength;
import org.unlaxer.Name;
import org.unlaxer.ParserCursor;
import org.unlaxer.Source;
import org.unlaxer.Source.SourceKind;
import org.unlaxer.TransactionElement;
import org.unlaxer.listener.ParserListener;
import org.unlaxer.listener.ParserListenerContainer;
import org.unlaxer.listener.TransactionListener;
import org.unlaxer.parser.GlobalScopeTree;
import org.unlaxer.parser.Parser;
import org.unlaxer.parser.Parsers;
import org.unlaxer.parser.combinator.ChoiceInterface;
import org.unlaxer.parser.combinator.NonOrdered;

public class ParseContext implements 
	Closeable, Transaction,
	ParserListenerContainer,
	GlobalScopeTree , ParserContextScopeTree{

  // TODO store successfully token's  map
	boolean doMemoize;

	public final Source source;

	boolean createMetaToken = true;
	
	Map parserListenerByName = new LinkedHashMap<>();
	
	Map listenerByName = new LinkedHashMap<>();

	final Deque tokenStack = new ArrayDeque();

	//FIXME change store to ScopeTree
	public Map chosenParserByChoice = new HashMap<>();
	
	//FIXME change store to ScopeTree
	public Map orderedParsersByNonOrdered = new HashMap<>();
	
	Map> scopeTreeMapByParser = new HashMap<>();
	
	Map globalScopeTreeMap = new HashMap<>();
	
	Collection actions;

	public ParseContext(Source source, ParseContextEffector... parseContextEffectors) {
	  parseContextByThread.set(this);
	  if(source.sourceKind() != SourceKind.root) {
	    throw new IllegalArgumentException();
	  }
		this.source = source;
		actions = new ArrayList<>();
		tokenStack.add(new TransactionElement(new ParserCursor()));

		for (ParseContextEffector parseContextEffector : parseContextEffectors) {
			parseContextEffector.effect(this);
		}
		onOpen(this);
	}
	
	@Override
	public Deque getTokenStack(){
		return tokenStack;
	}

	@Override
	public void close() {
	  parseContextByThread.set(null);
		if (tokenStack.size() != 1) {
			throw new IllegalStateException("transaction nest is illegal. check source code.");
		}
		onClose(this);
	}

	@Override
	public Map getTransactionListenerByName() {
		return listenerByName;
	}

	@Override
	public Map> getParserContextScopeTreeMap() {
		return scopeTreeMapByParser;
	}

	@Override
	public Map getGlobalScopeTreeMap() {
		return globalScopeTreeMap;
	}

	@Override
	public Map getParserListenerByName() {
		return parserListenerByName;
	}

	@Override
	public ParseContext get() {
		return this;
	}

	@Override
	public boolean doCreateMetaToken() {
		return createMetaToken;
	}

	@Override
	public Map getChosenParserByChoice() {
		return chosenParserByChoice;
	}

	@Override
	public Map getOrderedParsersByNonOrdered() {
		return orderedParsersByNonOrdered;
	}

	@Override
	public Source getSource() {
		return source;
	}

  @Override
  public Collection getActions() {
    return actions;
  }

  @Override
  public void addActions(List additionalCommitActions) {
    actions.addAll(additionalCommitActions);
  }
  
  public Source peekLast(CodePointIndex endIndexInclusive, CodePointLength length) {
    return getSource().peekLast(endIndexInclusive, length);
  }
  
  public Source peek(CodePointIndex startIndexInclusive, CodePointLength length) {
    return getSource().peek(startIndexInclusive, length);
  }
  
  public  Source peek(CodePointIndex startIndexInclusive, CodePointIndex endIndexExclusive) {
    return peek(startIndexInclusive, endIndexExclusive);
  }
  
  static ThreadLocal parseContextByThread = new ThreadLocal<>();
  
  public static ParseContext getParseContextWithCurrentThread() {
    return parseContextByThread.get();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy