Commit 64a908b8 authored by Michael Schwarz's avatar Michael Schwarz

If user chooses assoc for terminal in current connected conflict

reflected everywhere + disable other dropdowns
parent 3c807939
......@@ -2,6 +2,8 @@ package de.tum.in.www2.cupplugin.views;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.eclipse.jface.text.BadLocationException;
......@@ -81,22 +83,49 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
private PrecedenceToInsert currentPrecs;
class PrecedenceToInsert {
private List<terminal> terminals = new ArrayList<>();
//TODO: Can I do this? Does the terminal class properly implement all the required things?
private HashMap<terminal,Precedence.Type> precs = new HashMap<>();
public boolean isResolvedInFavorOfShift(ShiftReduceDetails srd){
return terminals.contains(srd.shift);
if(!srd.reduce.equals(srd.shift))
return precs.containsKey(srd.shift);
else
return precs.get(srd.shift) == Precedence.Type.Right;
}
public boolean isResolvedInFavorOfReduce(ShiftReduceDetails srd) {
return terminals.contains(srd.reduce);
if(!srd.reduce.equals(srd.shift))
return precs.containsKey(srd.reduce);
else
return precs.get(srd.shift) == Precedence.Type.Left;
}
public boolean isResolvedAsError(ShiftReduceDetails srd) {
if(!srd.reduce.equals(srd.shift))
return false;
else
return precs.get(srd.shift) == Precedence.Type.NonAssoc;
}
public boolean isAffected(ShiftReduceDetails srd){
return terminals.contains(srd.shift) || terminals.contains(srd.reduce);
return precs.containsKey(srd.shift) || precs.containsKey(srd.reduce);
}
// Add with non-set precedence
public void add(terminal t){
terminals.add(t);
// We use NoPrec as a placeholder
precs.put(t, Precedence.Type.NoPrec);
for(ConflictPanel c : conflictPanels){
c.markIfAffected(this);
}
}
public void setAssoc(terminal t,Precedence.Type p){
if(precs.get(t) != Precedence.Type.NoPrec){
throw new IllegalStateException();
}
precs.put(t, p);
for(ConflictPanel c : conflictPanels){
c.markIfAffected(this);
......@@ -119,6 +148,9 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
// srdetails may be null if conflict is not a shift-reduce conflict
private ShiftReduceDetails srdetails;
private boolean partOfCurrentConnected = false;
private boolean forceEnableDropdown = false;
private Label conflictTypeLabel;
private Link stateLabel;
private Label symbolLabel;
......@@ -203,8 +235,11 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
this.resolutionOptions = new Combo(this,SWT.DROP_DOWN);
GridData resolutionOptionsData = new GridData();
resolutionOptionsData.grabExcessHorizontalSpace = false;
resolutionOptionsData.horizontalSpan = 1;
resolutionOptionsData.horizontalSpan = 2;
resolutionOptionsData.widthHint = 600;
resolutionOptions.setLayoutData(resolutionOptionsData);
resolutionOptions.addSelectionListener(resolutionBoxSelected);
this.resolutionButton = new Button(this,SWT.PUSH);
GridData resolutionButtonData = new GridData();
......@@ -486,19 +521,40 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
public void markIfAffected(PrecedenceToInsert pti){
if(conflict instanceof ShiftReduceConflict && pti.isAffected(srdetails)){
partOfCurrentConnected = true;
changeColors(backgroundColorShiftReduceAffected,foregroundColorShiftReduceAffected);
if(pti.isResolvedInFavorOfReduce(srdetails)){
resolutionOptions.select(2);
resolutionOptions.setEnabled(true);
// Used assoc to sort out. Can't be changed anymore (unless this is the start)
if(srdetails.reduce.equals(srdetails.shift)){
resolutionOptions.setEnabled(forceEnableDropdown);
}
}
if(pti.isResolvedInFavorOfShift(srdetails)){
resolutionOptions.select(1);
resolutionOptions.setEnabled(true);
// Used assoc to sort out. Can't be changed anymore
if(srdetails.reduce.equals(srdetails.shift)){
resolutionOptions.setEnabled(forceEnableDropdown);
}
}
if(pti.isResolvedAsError(srdetails)){
//TODO
throw new IllegalStateException("Nope");
}
resolutionOptions.setEnabled(true);
resolutionButton.setEnabled(false);
}
else {
partOfCurrentConnected = false;
forceEnableDropdown = false;
if(conflict instanceof ShiftReduceConflict){
resolutionButton.setEnabled(false);
resolutionOptions.setEnabled(false);
......@@ -535,34 +591,74 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
return;
}
// 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);
return;
}
if(srdetails.reduce.equals(srdetails.shift)){
// The options are to insert a assoc
if(!srdetails.reduce.equals(srdetails.shift)){
// The user wants to shift, but shifting affects other conflicts
if(srdetails.shiftAffectsOthers && resolutionOptions.getSelectionIndex() == 1){
beginConnectedResolution();
currentPrecs.add(srdetails.shift);
return;
}
if(resolutionOptions.getSelectionIndex() == 1) // Shift
insertPrecedence(srdetails.shift,Precedence.Type.Right);
else if (resolutionOptions.getSelectionIndex() == 2) // Reduce
insertPrecedence(srdetails.shift,Precedence.Type.Left);
}
else {
// The user wants to reduce, but reduce affects other conflicts
if(srdetails.reduceAffectsOthers && resolutionOptions.getSelectionIndex() == 2){
beginConnectedResolution();
currentPrecs.add(srdetails.reduce);
return;
}
// These are the cases where nothing is affected
if(resolutionOptions.getSelectionIndex() == 1) // Shift
insertPrecedence(srdetails.shift,Precedence.Type.Left);
else if (resolutionOptions.getSelectionIndex() == 2) // Reduce
insertPrecedence(srdetails.reduce,Precedence.Type.Left);
}
else {
// Checking this is sufficient since the terminals are identical
if(srdetails.shiftAffectsOthers){
if(resolutionOptions.getSelectionIndex() == 1) { //Shift
forceEnableDropdown = true;
currentPrecs.setAssoc(srdetails.shift,Precedence.Type.Right);
}
else if (resolutionOptions.getSelectionIndex() == 2) { // Reduce
forceEnableDropdown = true;
currentPrecs.setAssoc(srdetails.shift,Precedence.Type.Left);
}
}
else {
if(resolutionOptions.getSelectionIndex() == 1) // Shift
insertPrecedence(srdetails.shift,Precedence.Type.Right);
else if (resolutionOptions.getSelectionIndex() == 2) // Reduce
insertPrecedence(srdetails.shift,Precedence.Type.Left);
}
}
}
};
private SelectionListener resolutionBoxSelected = new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
if(partOfCurrentConnected && srdetails.reduce.equals(srdetails.shift)){
if(resolutionOptions.getSelectionIndex() == 1) { //Shift
forceEnableDropdown = true;
currentPrecs.setAssoc(srdetails.shift,Precedence.Type.Right);
}
else if (resolutionOptions.getSelectionIndex() == 2) { // Reduce
forceEnableDropdown = true;
currentPrecs.setAssoc(srdetails.shift,Precedence.Type.Left);
}
}
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
};
}
public CupConflictsView(Composite realParent, Jumper jumper,
......
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