protected <T> boolean mergeOptionalIntoMany(StateAlias<T> state, Set<StateAlias<T>> visited) { if (!visited.add(state)) return false; boolean merged = false; if (state.getElement().isMany()) { for (StateAlias<T> out : ImmutableList.copyOf(state.getOutgoing())) { if (state.getIncoming().contains(out) && isOptionalIgnoring(out, state)) { mergeOptionalIntoMany(state, out); merged = true; } } for (StateAlias<T> in : ImmutableList.copyOf(state.getIncoming())) { if (state.getOutgoing().contains(in) && isOptionalIgnoring(in, state)) { mergeOptionalIntoMany(in, state); merged = true; } } } for (StateAlias<T> out : ImmutableList.copyOf(state.getOutgoing())) { if (mergeOptionalIntoMany(out, visited)) merged = true; } return merged; }
protected <T> boolean createMany(StateAlias<T> state, Set<StateAlias<T>> visited) { if (!visited.add(state)) return false; boolean created = false; if (state.getOutgoing().contains(state)) { state.getElement().setMany(true); state.getOutgoing().remove(state); state.getIncoming().remove(state); created = true; } for (StateAlias<T> out : state.getOutgoing()) { if (createMany(out, visited)) created = true; } return created; }
protected <T> boolean createOptional(StateAliasNfa<T> states) { List<StateAlias<T>> opt = Lists.newArrayList(); L: for (StateAlias<T> cand : new NfaUtil().collect(states)) { if (cand.getIncoming().isEmpty() || cand.getOutgoing().isEmpty()) continue L; for (StateAlias<T> in : cand.getIncoming()) if (!in.getOutgoing().containsAll(cand.getOutgoing())) continue L; opt.add(cand); } for (StateAlias<T> o : opt) { o.getElement().setOptional(true); for (StateAlias<T> in : Lists.newArrayList(o.getIncoming())) if (in != o) for (StateAlias<T> out : Lists.newArrayList(o.getOutgoing())) if (out != o) { out.getIncoming().remove(in); in.getOutgoing().remove(out); } } return !opt.isEmpty(); }
root.sort(new ElementAliasComparator<TOKEN>(order)); AliasGrammarProvider<TOKEN> production = new AliasGrammarProvider<TOKEN>(root); return new ProductionUtil().clone(production, grammarFactory);
protected <T> void mergeOptionalIntoMany(StateAlias<T> first, StateAlias<T> second) { StateAlias<T> many = first.element.isMany() ? first : second; StateAlias<T> optional = many == first ? second : first; if (optional.getOutgoing().contains(optional)) { optional.element.setMany(true); } many.element.setMany(false); optional.element.setOptional(true); for (StateAlias<T> out : optional.getOutgoing()) { out.getIncoming().remove(optional); } for (StateAlias<T> in : optional.getIncoming()) { in.getOutgoing().remove(optional); } GroupAlias<T> group = new GroupAlias<T>(); group.setMany(true); if (first.element instanceof GroupAlias<?> && !first.element.many && !first.element.optional) { group.children.addAll(((GroupAlias<T>) first.element).children); } else { group.addChild(first.getElement()); } if (second.element instanceof GroupAlias<?> && !second.element.many && !second.element.optional) { group.children.addAll(((GroupAlias<T>) second.element).children); } else { group.addChild(second.element); } many.element = group; }
protected <T> boolean createOptional(StateAliasNfa<T> states) { List<StateAlias<T>> opt = Lists.newArrayList(); L: for (StateAlias<T> cand : new NfaUtil().collect(states)) { if (cand.getIncoming().isEmpty() || cand.getOutgoing().isEmpty()) continue L; for (StateAlias<T> in : cand.getIncoming()) if (!in.getOutgoing().containsAll(cand.getOutgoing())) continue L; opt.add(cand); } for (StateAlias<T> o : opt) { o.getElement().setOptional(true); for (StateAlias<T> in : Lists.newArrayList(o.getIncoming())) if (in != o) for (StateAlias<T> out : Lists.newArrayList(o.getOutgoing())) if (out != o) { out.getIncoming().remove(in); in.getOutgoing().remove(out); } } return !opt.isEmpty(); }
protected <T> boolean mergeAlternativeMultiples(StateAlias<T> state, Set<StateAlias<T>> visited) { if (!visited.add(state)) return false; boolean merged = false; if (state.getElement().isMany()) { for (StateAlias<T> out : ImmutableList.copyOf(state.getOutgoing())) { if (areAlternativeMultiples(state, out)) { mergeAlternativeMultiples(state, out); merged = true; } } } for (StateAlias<T> out : ImmutableList.copyOf(state.getOutgoing())) { if (mergeAlternativeMultiples(out, visited)) merged = true; } return merged; }
@SuppressWarnings({ "unchecked", "rawtypes" }) protected <T> void createGroup(StateAlias<T> first, StateAlias<T> second) { GroupAlias<T> group = new GroupAlias<T>(); if (first.getElement() instanceof GroupAlias && first.getElement().isOne()) group.getChildren().addAll(((GroupAlias) first.getElement()).getChildren()); else group.addChild(first.getElement()); if (second.getElement() instanceof GroupAlias && second.getElement().isOne()) group.getChildren().addAll(((GroupAlias) second.getElement()).getChildren()); else group.addChild(second.getElement()); first.element = group; first.getOutgoing().clear(); first.absorbOutgoing(second); }
protected <T> boolean areAlternativeMultiples(StateAlias<T> first, StateAlias<T> second) { if (!first.getElement().isMany() || !second.getElement().isMany()) return false; if (!first.getOutgoing().contains(second) || !first.getIncoming().contains(second)) return false; for (StateAlias<T> in : first.getIncoming()) { if (!in.equals(second) && !second.getIncoming().contains(in)) return false; } for (StateAlias<T> out : first.getOutgoing()) { if (!out.equals(second) && !second.getOutgoing().contains(out)) return false; } return true; }
@SuppressWarnings({ "unchecked", "rawtypes" }) protected <T> void createGroup(StateAlias<T> first, StateAlias<T> second) { GroupAlias<T> group = new GroupAlias<T>(); if (first.getElement() instanceof GroupAlias && first.getElement().isOne()) group.getChildren().addAll(((GroupAlias) first.getElement()).getChildren()); else group.addChild(first.getElement()); if (second.getElement() instanceof GroupAlias && second.getElement().isOne()) group.getChildren().addAll(((GroupAlias) second.getElement()).getChildren()); else group.addChild(second.getElement()); first.element = group; first.getOutgoing().clear(); first.absorbOutgoing(second); }
protected <T> boolean createMany(StateAlias<T> state, Set<StateAlias<T>> visited) { if (!visited.add(state)) return false; boolean created = false; if (state.getOutgoing().contains(state)) { state.getElement().setMany(true); state.getOutgoing().remove(state); state.getIncoming().remove(state); created = true; } for (StateAlias<T> out : state.getOutgoing()) { if (createMany(out, visited)) created = true; } return created; }
protected Node drawState(Digraph dg, StateAlias<T> state, Map<StateAlias<T>, Node> nodes) { Node n = nodes.get(state); if (n != null) return n; n = new Node(state, state.getElement().toString()); nodes.put(state, n); dg.add(n); for (StateAlias<T> follower : state.getOutgoing()) { drawState(dg, follower, nodes); Edge e = new Edge(state, follower); e.put("arrowhead", "onormal"); dg.add(e); } return n; }
protected Node drawState(Digraph dg, StateAlias<T> state, Map<StateAlias<T>, Node> nodes) { Node n = nodes.get(state); if (n != null) return n; n = new Node(state, state.getElement().toString()); nodes.put(state, n); dg.add(n); for (StateAlias<T> follower : state.getOutgoing()) { drawState(dg, follower, nodes); Edge e = new Edge(state, follower); e.put("arrowhead", "onormal"); dg.add(e); } return n; }
public boolean isMany(AbstractElementAlias<TOKEN> ele) { return ele.isMany(); }
@Override protected T getFirstElement() { return this.children.get(0).getFirstElement(); }
@Override public boolean isMany(AbstractElementAlias<TOKEN> ele) { return ele.isMany(); }
public boolean isOptional(AbstractElementAlias<TOKEN> ele) { return ele.isOptional(); } }
@Override protected T getFirstElement() { return this.children.iterator().next().getFirstElement(); }
@Override protected int getElementCount() { int result = 1; for (AbstractElementAlias<T> child : children) result += child.getElementCount(); return result; }
protected <T> boolean collectMergeableOptions(boolean root, AbstractElementAlias<T> alt, List<AbstractElementAlias<T>> result) { boolean optional = alt.optional; if ((root || !alt.isMany()) && alt instanceof AlternativeAlias<?>) { for (AbstractElementAlias<T> child : ((AlternativeAlias<T>) alt).getChildren()) optional |= collectMergeableOptions(false, child, result); } else { result.add(alt); alt.optional = false; } return optional; }