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

io.immutables.grammar.processor.TermDispatch Maven / Gradle / Ivy

There is a newer version: 0.0.5
Show newest version
package io.immutables.grammar.processor;

import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.Sets;
import io.immutables.collect.Vect;
import io.immutables.grammar.processor.CodepointMatch.When;
import io.immutables.grammar.processor.Grammars.Identifier;
import io.immutables.grammar.processor.Grammars.Literal;
import java.util.Map;
import java.util.Set;
import org.immutables.value.Value.Derived;
import org.immutables.value.Value.Enclosing;
import org.immutables.value.Value.Immutable;
import org.immutables.value.Value.Parameter;

@Enclosing
@Immutable
interface TermDispatch {
	Vect terms();
	Vect simple();
	Vect simpleGroups();
	Group complexGroup();

	default @Derived Map termsById() {
		return Maps.uniqueIndex(terms(), TermExpansion::id);
	}

	default @Derived Multimap termsByKind() {
		return Multimaps.index(terms(), TermExpansion::kind);
	}

	@Immutable
	interface Selector {
		@Parameter
		Codepoint codepoint();
		@Parameter
		Group group();
	}

	@Immutable
	interface Group {
		@Parameter
		int index();
		@Parameter
		Set terms();
	}

	class Builder extends ImmutableTermDispatch.Builder {}

	static TermDispatch computeFrom(Vect terms) {
		TermDispatch.Builder builder =
				new TermDispatch.Builder()
						.addAllTerms(terms);

		Vect maybeSimple = terms
				.filter(d -> d.firstMatch().whenSimple() != When.NEVER);

		Map, TermDispatch.Group> groups = Maps.newLinkedHashMap();

		for (Codepoint c : Codepoint.SIMPLE_SET) {
			Set matching = Sets.newLinkedHashSet();

			for (TermExpansion d : maybeSimple) {
				if (d.firstMatch().firstMatches(c)) {
					matching.add(d);
				}
			}

			TermDispatch.Group g = groups.computeIfAbsent(matching,
					m -> ImmutableTermDispatch.Group.of(groups.size(), m));

			builder.addSimple(ImmutableTermDispatch.Selector.of(c, g));
		}

		builder.addAllSimpleGroups(groups.values());
		builder.complexGroup(ImmutableTermDispatch.Group.of(
				groups.size(), // just to give some index
				terms.filter(d -> d.firstMatch().whenSimple() != When.ALWAYS)));

		return builder.build();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy