Commit 13b5659f authored by Michael Schwarz's avatar Michael Schwarz

Highlight other affected conflicts graphically

parent edc8d859
......@@ -31,5 +31,8 @@ public class Colors {
public static final RGB conflictFgBlue = new RGB(17, 62, 91);
public static final RGB conflictBgYellow = new RGB(247, 247, 231);
public static final RGB conflictFgYellow = new RGB(91, 71, 17);
public static final RGB conflictBgRed = new RGB(255, 198, 198);
public static final RGB conflictFgRed = new RGB(255, 0, 0);
}
......@@ -73,22 +73,45 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
private CupTextEditor editor;
private boolean isVisible;
private List<ConflictPanel> conflictPanels;
private Label countConflictsLabel;
private Label countConflictsLabel, connectedResolutionLabel;
private ScrolledComposite scrolled;
private Composite list;
private PrecedenceToInsert currentPrecs;
class ConflictPanel extends Composite {
class ShiftReduceDetails {
terminal shift,reduce;
boolean shiftAffectsOthers,reduceAffectsOthers;
class PrecedenceToInsert {
private List<terminal> terminals = new ArrayList<>();
public boolean isResolvedInFavorOfShift(ShiftReduceDetails srd){
return terminals.contains(srd.shift);
}
public boolean isResolvedInFavorOfReduce(ShiftReduceDetails srd) {
return terminals.contains(srd.reduce);
}
public boolean isAffected(ShiftReduceDetails srd){
return terminals.contains(srd.shift) || terminals.contains(srd.reduce);
}
public void add(terminal t){
terminals.add(t);
}
}
class ShiftReduceDetails {
terminal shift,reduce;
boolean shiftAffectsOthers,reduceAffectsOthers;
}
class ConflictPanel extends Composite {
private static final int BASE_HEIGHT = 85;
private static final int COLUMNS = 3;
private static final int HEADING_FONT_SIZE = 14;
private Conflict conflict;
// srdetails may be null if conflict is not a shift-reduce conflict
private ShiftReduceDetails srdetails;
private Label conflictTypeLabel;
......@@ -103,21 +126,23 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
private Label resolutionHint;
private List<Range> links = new ArrayList<Range>();
private Color backgroundColorReduce;
private Color foregroundColorReduce;
private Color backgroundColorShift;
private Color foregroundColorShift;
private Color backgroundColorReduceReduce = new Color(Display.getCurrent(), Colors.conflictBgBlue);
private Color foregroundColorReduceReduce = new Color(Display.getCurrent(), Colors.conflictFgBlue);
private Color backgroundColorShiftReduce = new Color(Display.getCurrent(), Colors.conflictBgYellow);
private Color foregroundColorShiftReduce = new Color(Display.getCurrent(), Colors.conflictFgYellow);
private Color backgroundColorShiftReduceAffected = new Color(Display.getCurrent(), Colors.conflictBgRed);
private Color foregroundColorShiftReduceAffected = new Color(Display.getCurrent(), Colors.conflictFgRed);
private Color backgroundColorShiftReduceUnrelated = new Color(Display.getCurrent(), Colors.conflictBgYellow);
private Color foregroundColorShiftReduceUnrelated = new Color(Display.getCurrent(), Colors.conflictFgYellow);
GridData gridData;
public ConflictPanel(Composite parent, int style) {
super(parent, style);
backgroundColorReduce = new Color(Display.getCurrent(), Colors.conflictBgBlue);
foregroundColorReduce = new Color(Display.getCurrent(), Colors.conflictFgBlue);
backgroundColorShift = new Color(Display.getCurrent(), Colors.conflictBgYellow);
foregroundColorShift = new Color(Display.getCurrent(), Colors.conflictFgYellow);
gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
gridData.grabExcessHorizontalSpace = true;
gridData.widthHint = 100; // Do not remove. This is a bugfix. :-)
......@@ -258,7 +283,7 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
srdetails = null;
ReduceReduceConflict rrc = (ReduceReduceConflict) conflict;
changeColors(backgroundColorReduce, foregroundColorReduce);
changeColors(backgroundColorReduceReduce, foregroundColorReduceReduce);
conflictTypeLabel.setText("Reduce-Reduce conflict");
ProductionRight conflict1Ast = rrc.getConflictItem1()
......@@ -316,7 +341,7 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
srdetails = new ShiftReduceDetails();
ShiftReduceConflict src = (ShiftReduceConflict) conflict;
changeColors(backgroundColorShift, foregroundColorShift);
changeColors(backgroundColorShiftReduce, foregroundColorShiftReduce);
conflictTypeLabel.setText("Shift-Reduce conflict");
symbolLabel.setText("Under symbol: " + src.getTerminal().name());
......@@ -447,6 +472,30 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
}
public void markIfAffected(PrecedenceToInsert pti){
if(conflict instanceof ShiftReduceConflict && pti.isAffected(srdetails)){
changeColors(backgroundColorShiftReduceAffected,foregroundColorShiftReduceAffected);
if(pti.isResolvedInFavorOfReduce(srdetails)){
resolutionOptions.select(2);
}
if(pti.isResolvedInFavorOfShift(srdetails)){
resolutionOptions.select(1);
}
resolutionOptions.setEnabled(true);
resolutionButton.setEnabled(false);
}
else {
resolutionButton.setEnabled(false);
resolutionOptions.setEnabled(false);
if(conflict instanceof ShiftReduceConflict){
changeColors(backgroundColorShiftReduceUnrelated,foregroundColorShiftReduceUnrelated);
}
}
}
// replace (*) generated by CUP with BULLET U+2022 •
private String prettyString(lalr_item item) throws internal_error {
return item.toString().replace("(*)", "\u2022");
......@@ -465,14 +514,20 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
return;
}
ShiftReduceConflict src = (ShiftReduceConflict) conflict;
if(srdetails.reduceAffectsOthers && srdetails.shiftAffectsOthers){
Shell shell = new Shell(getDisplay());
MessageBox box = new MessageBox(shell,SWT.ICON_WARNING | SWT.OK);
box.setText("Nope");
box.setMessage("Both affect others.");
box.open();
// The user wants to reduce, but reduce affects other conflicts
if(srdetails.reduceAffectsOthers && resolutionOptions.getSelectionIndex() == 2){
}
// The user wants to shift, but shifting affects other conflicts
if(srdetails.shiftAffectsOthers && resolutionOptions.getSelectionIndex() == 1){
beginConnectedResolution();
currentPrecs.add(srdetails.shift);
for(ConflictPanel c : conflictPanels){
c.markIfAffected(currentPrecs);
}
return;
}
......@@ -530,6 +585,9 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
this.countConflictsLabel = new Label(list, SWT.NONE);
this.countConflictsLabel.setBackground(new Color(Display.getCurrent(), Colors.white));
this.connectedResolutionLabel = new Label(list,SWT.NONE);
this.connectedResolutionLabel.setBackground(new Color(Display.getCurrent(), Colors.white));
refreshScrolledLayout();
}
......@@ -635,6 +693,19 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
}
}
private void beginConnectedResolution(){
Shell shell = new Shell(Display.getCurrent());
MessageBox box = new MessageBox(shell,SWT.ICON_WARNING | SWT.OK);
box.setText("Information");
box.setMessage("This affects some other conflicts. These are marked now. Please resolve all those as well."
+ "The effects if no further precedences are inserted are simulated right now.");
box.open();
currentPrecs = new PrecedenceToInsert();
this.connectedResolutionLabel.setText("Currently resolving a set of conflicts that are connected.");
refreshScrolledLayout();
}
private void insertPrecedence(terminal terminal,Precedence.Type assoc){
try {
String toInsert = "precedence " + assoc.toString().toLowerCase() + " " + terminal.name()+ "; // Inserted by Eclipse plugin";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment