[serializer] improve debug output and comments MyLangSyntacticSequencer

Change-Id: I6bfcdc0e6b6beef0c4427828c4f6280e6d3fe0a6
This commit is contained in:
Moritz Eysholdt 2015-02-03 19:46:36 +01:00
parent 54c6e2f1bf
commit ef1dd4949b
4 changed files with 29 additions and 40 deletions

View file

@ -27,6 +27,8 @@ public class ProductionFormatter<ELEMENT, TOKEN> implements Function<Production<
protected int autoWrapChars = 140;
protected int autoWrapChildren = 5;
protected String indent = " ";
protected Function<TOKEN, String> tokenToString = new ObjToStrFunction<TOKEN>();
@ -54,18 +56,18 @@ public class ProductionFormatter<ELEMENT, TOKEN> implements Function<Production<
}
Iterable<ELEMENT> alternative = adapter.getAlternativeChildren(grammarElement);
if (alternative != null)
return format(adapter, grammarElement, alternative, " | ", false, needParenthesis, 5);
return format(adapter, grammarElement, alternative, " | ", false, needParenthesis);
Iterable<ELEMENT> group = adapter.getSequentialChildren(grammarElement);
if (group != null)
return format(adapter, grammarElement, group, " ", false, needParenthesis, 5);
return format(adapter, grammarElement, group, " ", false, needParenthesis);
Iterable<ELEMENT> ungroup = adapter.getUnorderedChildren(grammarElement);
if (ungroup != null)
return format(adapter, grammarElement, ungroup, " & ", false, needParenthesis, 5);
return format(adapter, grammarElement, ungroup, " & ", false, needParenthesis);
return "<unknown>";
}
protected String format(Production<ELEMENT, TOKEN> adapter, ELEMENT element, Iterable<ELEMENT> children,
String separator, boolean needWrap, boolean needParenthesis, int maxChildren) {
String separator, boolean needWrap, boolean needParenthesis) {
List<String> childStrs2 = Lists.newArrayList();
int width2 = 0;
for (ELEMENT child : children) {
@ -75,7 +77,7 @@ public class ProductionFormatter<ELEMENT, TOKEN> implements Function<Production<
if (childStr.contains("\n"))
needWrap = true;
}
if (childStrs2.size() > maxChildren)
if (childStrs2.size() > autoWrapChildren)
needWrap = true;
if (width2 > autoWrapChars)
needWrap = true;
@ -104,6 +106,11 @@ public class ProductionFormatter<ELEMENT, TOKEN> implements Function<Production<
return this;
}
public ProductionFormatter<ELEMENT, TOKEN> setAutoWrapChildren(int autoWrapChildren) {
this.autoWrapChildren = autoWrapChildren;
return this;
}
public ProductionFormatter<ELEMENT, TOKEN> setIndent(String indent) {
this.indent = indent;
return this;

View file

@ -18,6 +18,7 @@ import org.eclipse.xtext.util.formallang.ProductionFactory;
import org.eclipse.xtext.util.formallang.ProductionFormatter;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
@ -232,7 +233,7 @@ public class GrammarAlias {
if (obj == null || obj.getClass() != getClass())
return false;
TokenAlias other = (TokenAlias) obj;
return many == other.many && optional == other.optional && token.equals(other.token);
return many == other.many && optional == other.optional && Objects.equal(token, other.token);
}
public AbstractElement getToken() {

View file

@ -89,6 +89,8 @@ public interface ISyntacticSequencerPDAProvider {
public interface ISynTransition extends ISynNavigable {
Nfa<ISynState> getAmbiguousNfa();
AbstractElementAlias getAmbiguousSyntax();
List<AbstractElementAlias> getAmbiguousSyntaxes();

View file

@ -30,7 +30,6 @@ import org.eclipse.xtext.serializer.analysis.GrammarAlias.AbstractElementAlias;
import org.eclipse.xtext.serializer.analysis.GrammarAlias.AlternativeAlias;
import org.eclipse.xtext.serializer.analysis.GrammarAlias.GrammarAliasFactory;
import org.eclipse.xtext.serializer.analysis.GrammarAlias.GroupAlias;
import org.eclipse.xtext.serializer.analysis.GrammarAlias.TokenAlias;
import org.eclipse.xtext.serializer.analysis.ISerState.SerStateType;
import org.eclipse.xtext.serializer.sequencer.RuleCallStack;
import org.eclipse.xtext.util.Pair;
@ -449,11 +448,9 @@ public class SyntacticSequencerPDAProvider implements ISyntacticSequencerPDAProv
}
}
protected static final AbstractElementAlias UNINITIALIZED = new TokenAlias(false, false, null);
protected AbstractElementAlias ambiguousSyntax = null;
protected AbstractElementAlias ambiguousSyntax = UNINITIALIZED;
protected List<AbstractElementAlias> ambiguousSyntaxes;
protected List<AbstractElementAlias> ambiguousSyntaxes = null;
protected ISynAbsorberState source;
@ -462,29 +459,19 @@ public class SyntacticSequencerPDAProvider implements ISyntacticSequencerPDAProv
this.source = source;
}
@Override
public Nfa<ISynState> getAmbiguousNfa() {
Nfa<ISynState> nfa = new PdaUtil().filterUnambiguousPaths(getPathToTarget());
return new NfaUtil().filter(nfa, new Filter());
}
@Override
public AbstractElementAlias getAmbiguousSyntax() {
if (ambiguousSyntax != UNINITIALIZED)
if (ambiguousSyntax != null)
return ambiguousSyntax;
ambiguousSyntax = getShortSyntax();
if (ambiguousSyntax instanceof GroupAlias) {
GroupAlias group = (GroupAlias) ambiguousSyntax;
List<AbstractElementAlias> children = group.getChildren();
int start = 0;
while (start < children.size() && children.get(start) instanceof TokenAlias
&& !children.get(start).isMany() && !children.get(start).isOptional())
start++;
int end = children.size() - 1;
while (end >= 0 && children.get(end) instanceof TokenAlias && !children.get(end).isMany()
&& !children.get(end).isOptional())
end--;
if (start <= end) {
ambiguousSyntax = group = new GroupAlias(false, false, children.subList(start, end + 1));
if (group.children.size() == 1)
ambiguousSyntax = group.children.get(0);
} else
ambiguousSyntax = null;
}
Nfa<ISynState> nfa = getAmbiguousNfa();
NfaToProduction prod = new NfaToProduction();
ambiguousSyntax = prod.nfaToGrammar(nfa, new GetGrammarElement(), new GrammarAliasFactory());
return ambiguousSyntax;
}
@ -495,10 +482,7 @@ public class SyntacticSequencerPDAProvider implements ISyntacticSequencerPDAProv
if (!isSyntacticallyAmbiguous())
return ambiguousSyntaxes = Collections.emptyList();
ambiguousSyntaxes = Lists.newArrayList();
Nfa<ISynState> nfa = new PdaUtil().filterUnambiguousPaths(getPathToTarget());
nfa = new NfaUtil().filter(nfa, new Filter());
AbstractElementAlias syntax = new NfaToProduction().nfaToGrammar(nfa, new GetGrammarElement(),
new GrammarAliasFactory());
AbstractElementAlias syntax = getAmbiguousSyntax();
if (syntax instanceof GroupAlias) {
GroupAlias group = (GroupAlias) syntax;
for (AbstractElementAlias child : group.getChildren())
@ -509,11 +493,6 @@ public class SyntacticSequencerPDAProvider implements ISyntacticSequencerPDAProv
return ambiguousSyntaxes;
}
public AbstractElementAlias getShortSyntax() {
Nfa<ISynState> path = new NfaUtil().filter(getPathToTarget(), new Filter());
return new NfaToProduction().nfaToGrammar(path, new GetGrammarElement(), new GrammarAliasFactory());
}
@Override
public ISynAbsorberState getSource() {
return source;