Commit a8f4125f authored by Michael Schwarz's avatar Michael Schwarz 🤔
Browse files

insert precedence for conflicts that are isolated

parent 1444575f
...@@ -7,7 +7,6 @@ public class Colors { ...@@ -7,7 +7,6 @@ public class Colors {
public static final RGB black = new RGB(0, 0, 0); public static final RGB black = new RGB(0, 0, 0);
public static final RGB white = new RGB(255, 255, 255); public static final RGB white = new RGB(255, 255, 255);
public static final RGB darkGray = new RGB(20, 20, 20); public static final RGB darkGray = new RGB(20, 20, 20);
// public static final RGB darkGray = new RGB (30, 30, 30);
public static final RGB gray = new RGB(150, 150, 150); public static final RGB gray = new RGB(150, 150, 150);
public static final RGB lightGray = new RGB(240, 240, 240); public static final RGB lightGray = new RGB(240, 240, 240);
public static final RGB blue = new RGB(53, 87, 197); public static final RGB blue = new RGB(53, 87, 197);
...@@ -20,14 +19,7 @@ public class Colors { ...@@ -20,14 +19,7 @@ public class Colors {
public static final RGB pink = new RGB(255, 0, 255); public static final RGB pink = new RGB(255, 0, 255);
public static final RGB brown = new RGB(132, 90, 49); public static final RGB brown = new RGB(132, 90, 49);
/*
* public static final RGB stateGreen = new RGB (155, 218, 158); public
* static final RGB stateBlue = new RGB (155, 208, 128); public static final
* RGB stateRed = new RGB (218, 155, 173); public static final RGB
* stateYellow = new RGB (218, 214, 155);
*/
// public static final RGB stateGreen = new RGB (196, 232, 197);
public static final RGB stateGreen = new RGB(146, 203, 135); public static final RGB stateGreen = new RGB(146, 203, 135);
public static final RGB stateBlue = new RGB(196, 232, 230); public static final RGB stateBlue = new RGB(196, 232, 230);
......
...@@ -108,7 +108,7 @@ public class JavaTokenScanner implements ITokenScanner { ...@@ -108,7 +108,7 @@ public class JavaTokenScanner implements ITokenScanner {
if (previousCodeBlockRange != null if (previousCodeBlockRange != null
&& previousCodeBlockRange.contains(pos)) { && previousCodeBlockRange.contains(pos)) {
// the old AST is till valid and we are within the same code // the old AST is still valid and we are within the same code
// block. // block.
// -> just keep the existing labels. // -> just keep the existing labels.
return; return;
......
...@@ -4,12 +4,15 @@ import java.util.ArrayList; ...@@ -4,12 +4,15 @@ import java.util.ArrayList;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite; import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseWheelListener; import org.eclipse.swt.events.MouseWheelListener;
import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.graphics.FontData;
...@@ -21,20 +24,28 @@ import org.eclipse.swt.widgets.Button; ...@@ -21,20 +24,28 @@ import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link; import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.widgets.TableWrapLayout; import org.eclipse.ui.forms.widgets.TableWrapLayout;
import de.in.tum.www2.cup.Conflict; import de.in.tum.www2.cup.Conflict;
import de.in.tum.www2.cup.CupConflictState; import de.in.tum.www2.cup.CupConflictState;
import de.in.tum.www2.cup.ConflictResolutionManager; import de.in.tum.www2.cup.ConflictResolutionManager;
import de.in.tum.www2.cup.LALRResult; import de.in.tum.www2.cup.LALRResult;
import de.in.tum.www2.cup.Position;
import de.in.tum.www2.cup.Range; import de.in.tum.www2.cup.Range;
import de.in.tum.www2.cup.ReduceReduceConflict; import de.in.tum.www2.cup.ReduceReduceConflict;
import de.in.tum.www2.cup.ShiftReduceConflict; import de.in.tum.www2.cup.ShiftReduceConflict;
import de.in.tum.www2.cup.ast.IProductionRightPart; import de.in.tum.www2.cup.ast.IProductionRightPart;
import de.in.tum.www2.cup.ast.ParserResult;
import de.in.tum.www2.cup.ast.Precedence;
import de.in.tum.www2.cup.ast.ProductionRight; import de.in.tum.www2.cup.ast.ProductionRight;
import de.in.tum.www2.cup.ast.ProductionSymbolRef; import de.in.tum.www2.cup.ast.ProductionSymbolRef;
import de.in.tum.www2.cup.ast.SymbolDeclaration;
import de.in.tum.www2.cup.ast.Terminal; import de.in.tum.www2.cup.ast.Terminal;
import de.in.tum.www2.cup.internal.internal_error; import de.in.tum.www2.cup.internal.internal_error;
import de.in.tum.www2.cup.internal.lalr_item; import de.in.tum.www2.cup.internal.lalr_item;
...@@ -68,12 +79,18 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi ...@@ -68,12 +79,18 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
class ConflictPanel extends Composite { class ConflictPanel extends Composite {
class ShiftReduceDetails {
terminal shift,reduce;
boolean shiftAffectsOthers,reduceAffectsOthers;
}
private static final int BASE_HEIGHT = 85; private static final int BASE_HEIGHT = 85;
private static final int COLUMNS = 3; private static final int COLUMNS = 3;
private static final int HEADING_FONT_SIZE = 14; private static final int HEADING_FONT_SIZE = 14;
private Conflict conflict; private Conflict conflict;
private ShiftReduceDetails srdetails;
private Label conflictTypeLabel; private Label conflictTypeLabel;
private Link stateLabel; private Link stateLabel;
private Label symbolLabel; private Label symbolLabel;
...@@ -164,9 +181,10 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi ...@@ -164,9 +181,10 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
resolutionButtonData.grabExcessHorizontalSpace = true; resolutionButtonData.grabExcessHorizontalSpace = true;
resolutionButtonData.horizontalSpan = 1; resolutionButtonData.horizontalSpan = 1;
resolutionButton.setLayoutData(resolutionButtonData); resolutionButton.setLayoutData(resolutionButtonData);
resolutionButton.addSelectionListener(resolutionButtonClick);
this.resolutionButton.setText("Apply"); this.resolutionButton.setText("Apply");
} }
private Composite makeDetailWrapper(int columns) { private Composite makeDetailWrapper(int columns) {
Composite detailWrapper = new Composite(this, SWT.WRAP); Composite detailWrapper = new Composite(this, SWT.WRAP);
...@@ -237,6 +255,8 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi ...@@ -237,6 +255,8 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
stateLabel.setText("<a href=\"" + cindex + "\">State: " + cindex + "</a>"); stateLabel.setText("<a href=\"" + cindex + "\">State: " + cindex + "</a>");
if (conflict instanceof ReduceReduceConflict) { if (conflict instanceof ReduceReduceConflict) {
srdetails = null;
ReduceReduceConflict rrc = (ReduceReduceConflict) conflict; ReduceReduceConflict rrc = (ReduceReduceConflict) conflict;
changeColors(backgroundColorReduce, foregroundColorReduce); changeColors(backgroundColorReduce, foregroundColorReduce);
conflictTypeLabel.setText("Reduce-Reduce conflict"); conflictTypeLabel.setText("Reduce-Reduce conflict");
...@@ -290,11 +310,11 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi ...@@ -290,11 +310,11 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
symbolLabel.setText(symbolsBuilder.toString()); symbolLabel.setText(symbolsBuilder.toString());
resolutionOptions.removeAll(); resolutionOptions.removeAll();
resolutionOptions.add("Use first");
resolutionOptions.add("Use second");
} else if (conflict instanceof ShiftReduceConflict) { } else if (conflict instanceof ShiftReduceConflict) {
srdetails = new ShiftReduceDetails();
ShiftReduceConflict src = (ShiftReduceConflict) conflict; ShiftReduceConflict src = (ShiftReduceConflict) conflict;
changeColors(backgroundColorShift, foregroundColorShift); changeColors(backgroundColorShift, foregroundColorShift);
conflictTypeLabel.setText("Shift-Reduce conflict"); conflictTypeLabel.setText("Shift-Reduce conflict");
...@@ -371,6 +391,13 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi ...@@ -371,6 +391,13 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
} }
} }
srdetails.shift = sourceOfShiftPrec;
srdetails.shiftAffectsOthers = insertingPrecAffectsOtherConflicts(crm,src,sourceOfShiftPrec);
srdetails.reduce = sourceOfReducePrec;
srdetails.reduceAffectsOthers = insertingPrecAffectsOtherConflicts(crm,src,sourceOfReducePrec);
// RHS is only nonterminal and no %prec directive => sourceOfReducePrec == null // RHS is only nonterminal and no %prec directive => sourceOfReducePrec == null
int reducePred = src.getConflictItem1().the_production().precedence_num(); int reducePred = src.getConflictItem1().the_production().precedence_num();
...@@ -384,7 +411,7 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi ...@@ -384,7 +411,7 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
// If the precedence of the terminal and the production come from the same terminal // If the precedence of the terminal and the production come from the same terminal
// the thing we need to consider are assocs // the thing we need to consider are assocs
if(!insertingPrecAffectsOtherConflicts(crm,src,sourceOfShiftPrec)){ if(!srdetails.shiftAffectsOthers){
resolutionOptions.add("Shift (make " + sourceOfShiftPrec.name() +" right associative)"); resolutionOptions.add("Shift (make " + sourceOfShiftPrec.name() +" right associative)");
resolutionOptions.add("Reduce (make " + sourceOfShiftPrec.name() +" left associative)"); resolutionOptions.add("Reduce (make " + sourceOfShiftPrec.name() +" left associative)");
} }
...@@ -398,7 +425,7 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi ...@@ -398,7 +425,7 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
// If the precedence of the terminal and the production come from different terminals // If the precedence of the terminal and the production come from different terminals
// we need to choose the precedence // we need to choose the precedence
if(!insertingPrecAffectsOtherConflicts(crm,src,sourceOfShiftPrec)) { if(!srdetails.shiftAffectsOthers) {
resolutionOptions.add("Shift (insert precedence for " + sourceOfShiftPrec.name() +")"); resolutionOptions.add("Shift (insert precedence for " + sourceOfShiftPrec.name() +")");
} }
else { else {
...@@ -406,7 +433,7 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi ...@@ -406,7 +433,7 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
} }
if(sourceOfReducePrec != null){ if(sourceOfReducePrec != null){
if(!insertingPrecAffectsOtherConflicts(crm,src,sourceOfReducePrec)) { if(!srdetails.reduceAffectsOthers) {
resolutionOptions.add("Reduce (insert precedence for " + sourceOfReducePrec.name() +")"); resolutionOptions.add("Reduce (insert precedence for " + sourceOfReducePrec.name() +")");
} }
else{ else{
...@@ -424,6 +451,47 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi ...@@ -424,6 +451,47 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
private String prettyString(lalr_item item) throws internal_error { private String prettyString(lalr_item item) throws internal_error {
return item.toString().replace("(*)", "\u2022"); return item.toString().replace("(*)", "\u2022");
} }
private SelectionListener resolutionButtonClick = new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e){ }
public void widgetSelected(SelectionEvent e){
if(!(conflict instanceof ShiftReduceConflict)) {
return;
}
if(resolutionOptions.getSelectionIndex() == 0){
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();
return;
}
if(srdetails.reduce.equals(srdetails.shift)){
// The options are to insert a assoc
if(resolutionOptions.getSelectionIndex() == 1) // Shift
insertPrecedence(srdetails.shift,Precedence.Type.Right);
else if (resolutionOptions.getSelectionIndex() == 2) // Reduce
insertPrecedence(srdetails.shift,Precedence.Type.Left);
}
else {
if(resolutionOptions.getSelectionIndex() == 1) // Shift
insertPrecedence(srdetails.shift,Precedence.Type.Left);
else if (resolutionOptions.getSelectionIndex() == 2) // Reduce
insertPrecedence(srdetails.reduce,Precedence.Type.Left);
}
}
};
} }
public CupConflictsView(Composite realParent, Jumper jumper, public CupConflictsView(Composite realParent, Jumper jumper,
...@@ -567,6 +635,22 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi ...@@ -567,6 +635,22 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
} }
} }
private void insertPrecedence(terminal terminal,Precedence.Type assoc){
try {
IDocument document = editor.getDocument();
ParserResult r = Model.getInstanceForDocument(document).getAstModel();
// Precedences are located immediately after the last symbol decl
int endOfLastDecl = r.symbols.get(r.symbols.size()-1).getEnd().getOffsetFromStart();
document.replace(endOfLastDecl, 0, "\nprecedence " + assoc.toString().toLowerCase() + " " + terminal.name()+"; // Inserted by Eclipse plugin");
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private boolean insertingPrecAffectsOtherConflicts(ConflictResolutionManager crm,ShiftReduceConflict intended,terminal t){ private boolean insertingPrecAffectsOtherConflicts(ConflictResolutionManager crm,ShiftReduceConflict intended,terminal t){
for(Conflict c : crm.getAllConflicts()){ for(Conflict c : crm.getAllConflicts()){
if(c instanceof ShiftReduceConflict){ if(c instanceof ShiftReduceConflict){
......
Supports Markdown
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