Skip to content

Commit

Permalink
mark trees from above for diff. much better. also show diffs in chose…
Browse files Browse the repository at this point in the history
…n tree. wasn't undoing input editor highlighting.
  • Loading branch information
parrt committed Jul 3, 2015
1 parent 11435da commit e9a7d51
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<properties/>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
Expand All @@ -24,7 +24,7 @@
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand All @@ -39,6 +39,20 @@
</component>
</children>
</grid>
<component id="99a33" class="javax.swing.JLabel" binding="ambigPhraseLabel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font style="1"/>
<text value="ambiguity"/>
</properties>
</component>
<hspacer id="3a696">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
<scrollpane id="cf514" binding="treeScrollPane">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.tree.Tree;
import org.antlr.v4.runtime.tree.gui.TreeViewer;
import org.antlr.v4.tool.GrammarInterpreterRuleContext;
import org.antlr.v4.tool.GrammarParserInterpreter;

import javax.swing.*;
Expand All @@ -45,6 +44,7 @@ public class ShowAmbigTreesDialog extends JDialog {
private JButton buttonOK;
protected JScrollPane treeScrollPane;
protected JSlider treeSizeSlider;
protected JLabel ambigPhraseLabel;
public List<? extends RuleContext> ambiguousParseTrees;
public TreeViewer[] treeViewers;
public PreviewState previewState;
Expand Down Expand Up @@ -124,6 +124,7 @@ public static void popupAmbigTreesDialog(PreviewState previewState, AmbiguityInf
" Interpretations of Ambiguous Input Phrase: " +
phrase;
int predictedAlt = ambigInfo.ambigAlts.nextSetBit(0);
dialog.ambigPhraseLabel.setText(title);
dialog.setTrees(previewState, ambiguousParseTrees, title, predictedAlt - 1,
ambigInfo.startIndex, ambigInfo.stopIndex, true);
}
Expand Down Expand Up @@ -181,6 +182,7 @@ public static void popupLookaheadTreesDialog(PreviewState previewState, Lookahea
String title = lookaheadParseTrees.size() +
" Interpretations of Lookahead Phrase: " +
phrase;
dialog.ambigPhraseLabel.setText(title);
dialog.setTrees(previewState, lookaheadParseTrees, title, lookaheadInfo.predictedAlt - 1,
lookaheadInfo.startIndex, lookaheadInfo.stopIndex, false);
}
Expand Down Expand Up @@ -210,15 +212,15 @@ public void setTrees(PreviewState previewState,
setTitle(title);
treeViewers = new TreeViewer[ambiguousParseTrees.size()];
JBPanel panelOfTrees = new JBPanel();
GrammarInterpreterRuleContext chosenTree = (GrammarInterpreterRuleContext)
ambiguousParseTrees.get(highlightTreeIndex);
PreviewInterpreterRuleContext chosenTree =
(PreviewInterpreterRuleContext) ambiguousParseTrees.get(highlightTreeIndex);
panelOfTrees.setLayout(new BoxLayout(panelOfTrees, BoxLayout.X_AXIS));
for (int i = 0; i < numTrees; i++) {
if (i > 0) {
panelOfTrees.add(new JSeparator(JSeparator.VERTICAL));
}
GrammarInterpreterRuleContext ctx = (GrammarInterpreterRuleContext) ambiguousParseTrees.get(i);
treeViewers[i] = new TrackpadZoomingTreeView(null, null, highlightDiffs && ctx != chosenTree);
PreviewInterpreterRuleContext ctx = (PreviewInterpreterRuleContext) ambiguousParseTrees.get(i);
treeViewers[i] = new TrackpadZoomingTreeView(null, null, highlightDiffs); // && ctx != chosenTree);
AltLabelTextProvider treeText =
new AltLabelTextProvider(previewState.parsingResult.parser, previewState.g);
treeViewers[i].setTreeTextProvider(treeText);
Expand All @@ -232,12 +234,12 @@ public void setTrees(PreviewState previewState,
// ;
// TODO: display a message?
}
if (ctx != chosenTree) {
mark(chosenTree, ctx, startIndex, stopIndex);
}
treeViewers[i].addHighlightedNodes(new ArrayList<Tree>() {{
add(root);
}});
if (ctx != chosenTree) {
mark(chosenTree, ctx, startIndex, stopIndex);
}
JBPanel wrapper = new JBPanel(new BorderLayout());
if (i == highlightTreeIndex) {
wrapper.setBackground(JBColor.white);
Expand All @@ -257,11 +259,14 @@ public void setTrees(PreviewState previewState,
* means u might contain fewer in-range leaves. t's leaves should be
* start..stop indexes.
*/
public static void mark(final GrammarInterpreterRuleContext t,
final GrammarInterpreterRuleContext u,
public static void mark(final PreviewInterpreterRuleContext t,
final PreviewInterpreterRuleContext u,
final int startIndex,
final int stopIndex) {
// Get leaves and the root so we can do a difference between the trees starting at the bottom and top
// First mark from roots down
markFromRoots(t, u);

// Get leaves so we can do a difference between the trees starting at the bottom and top
List<Tree> tleaves = ParsingUtils.getAllLeaves(t);
List<Tree> uleaves = ParsingUtils.getAllLeaves(u);

Expand Down Expand Up @@ -300,14 +305,38 @@ public boolean apply(Tree t) {
tancestor = (PreviewInterpreterRuleContext) tancestors.get(a);
uancestor = (PreviewInterpreterRuleContext) uancestors.get(a);
if (!tancestor.equals(uancestor)) break;
tancestor.reached = true;
uancestor.reached = true;
if (tancestor == t || uancestor == u)
break; // stop if we hit incoming root nodes
// if (tancestor == t || uancestor == u)
// break; // stop if we hit incoming root nodes
a++;
}
}
}

public static void markFromRoots(final PreviewInterpreterRuleContext t, final PreviewInterpreterRuleContext u) {
if (t == null || u == null) return;
if (!t.equals(u)) return;
t.reached = true;
u.reached = true;
int n = Math.min(t.getChildCount(), u.getChildCount());
for (int i = 0; i < n; i++) { // for each leaf of t and u
Tree tchild = t.getChild(i);
Tree uchild = u.getChild(i);
if (!tchild.equals(uchild)) {
return; // don't consider other kids if ith doesn't match
}
if (tchild instanceof PreviewInterpreterRuleContext &&
uchild instanceof PreviewInterpreterRuleContext) {
markFromRoots((PreviewInterpreterRuleContext) tchild,
(PreviewInterpreterRuleContext) uchild);
}
else {
return; // mismatched kids. should be caught above but...
}
}
}

private void onOK() {
// add your code here
dispose();
Expand All @@ -324,16 +353,22 @@ private void onOK() {
contentPane = new JPanel();
contentPane.setLayout(new GridLayoutManager(3, 1, new Insets(10, 10, 10, 10), -1, -1));
final JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
panel1.setLayout(new GridLayoutManager(1, 4, new Insets(0, 0, 0, 0), -1, -1));
contentPane.add(panel1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
final Spacer spacer1 = new Spacer();
panel1.add(spacer1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
final JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
panel1.add(panel2, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
panel1.add(panel2, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
buttonOK = new JButton();
buttonOK.setText("OK");
panel2.add(buttonOK, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
ambigPhraseLabel = new JLabel();
ambigPhraseLabel.setFont(new Font(ambigPhraseLabel.getFont().getName(), Font.BOLD, ambigPhraseLabel.getFont().getSize()));
ambigPhraseLabel.setText("ambiguity");
panel1.add(ambigPhraseLabel, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final Spacer spacer2 = new Spacer();
panel1.add(spacer2, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
treeScrollPane = new JScrollPane();
contentPane.add(treeScrollPane, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
treeSizeSlider = new JSlider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public void highlightPhrases(PreviewState previewState, int decision) {
CaretModel caretModel = editor.getCaretModel();

MarkupModel markupModel = editor.getMarkupModel();
markupModel.removeAllHighlighters();

DecisionInfo decisionInfo = parseInfo.getDecisionInfo()[decision];

Expand Down

0 comments on commit e9a7d51

Please sign in to comment.