Commit 3eef45a1 authored by Michael Schwarz's avatar Michael Schwarz

Restructering for Reduce/Reduce

parent 69166194
......@@ -87,8 +87,8 @@ public class CupConflictsView extends FailableView
private HashMap<terminal, Integer> terminalsAffectConflicts;
private HashMap<lr_item_core, Integer> ordersAffectConflicts;
PrecedenceToInsert currentPrecs;
ResolutionStrategy currentResolution;
public int ignoreRRAfterLine;
class ShiftReduceDetails {
......@@ -261,7 +261,7 @@ public class CupConflictsView extends FailableView
panel.update(crm, conflict);
}
if (currentPrecs != null && currentPrecs.isActive()) {
if (currentResolution != null && currentResolution.isActive()) {
markAllAffected();
}
......@@ -401,7 +401,7 @@ public class CupConflictsView extends FailableView
@Override
public void modelChanged(Model model) {
if (currentPrecs != null && currentPrecs.isActive()) {
if (currentResolution != null && currentResolution.isActive()) {
// Connected resolution does no longer make sense
abortConnectedResolution(false);
}
......@@ -437,7 +437,7 @@ public class CupConflictsView extends FailableView
void markAllAffected() {
int affected = 0;
for (ConflictPanel c : conflictPanels) {
if (c.markIfAffected(currentPrecs)) {
if (c.markIfAffected(currentResolution)) {
affected++;
}
}
......@@ -446,25 +446,29 @@ public class CupConflictsView extends FailableView
+ affected + " conflicts involved).");
}
private PrecedenceToInsert getPti(){
return (PrecedenceToInsert) currentResolution;
}
void addFirstPrecedence(terminal t, terminal other) {
currentPrecs.add(t, other);
getPti().add(t, other);
markAllAffected();
}
void addFurtherPrecedence(terminal higher, terminal lower) throws PrecedenceCyclicException {
currentPrecs.addPrecedence(higher, lower);
getPti().addPrecedence(higher, lower);
markAllAffected();
}
void setTerminalAssoc(terminal t, Precedence.Type p) {
currentPrecs.setAssoc(t, p);
getPti().setAssoc(t, p);
markAllAffected();
}
/**
* Set currentPrecs Doesn't mark affected conflicts
*/
void beginConnectedResolution() {
void beginConnectedResolutionSR() {
Shell shell = new Shell(Display.getCurrent());
MessageBox box = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
box.setText("Information");
......@@ -472,7 +476,7 @@ public class CupConflictsView extends FailableView
+ "The effects if no further precedences are inserted are simulated right now.");
box.open();
currentPrecs = new PrecedenceToInsert();
currentResolution = new PrecedenceToInsert();
// Text of label already set in the markAllAffected()
this.connectedResolutionLabel.setVisible(true);
this.connectedResolutionAbort.setVisible(true);
......@@ -480,15 +484,31 @@ public class CupConflictsView extends FailableView
refreshScrolledLayout();
}
void beginConnectedResolutionRR(){
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.\n"
+ "The effects if no further orderings are inserted are simulated right now.");
box.open();
currentResolution = new ReordersToDo();
// Text of label already set in the markAllAffected()
this.connectedResolutionLabel.setVisible(true);
this.connectedResolutionAbort.setVisible(true);
this.connectedResolutionApply.setVisible(true);
refreshScrolledLayout();
}
/**
* Abort connected resolution and reset all panels
* Abort connected resolution (regardless of SR or RR) and reset all panels
*
* @param refresh
* Refresh UI to make changes visible (true unless panels will
* change later anyway)
*/
private void abortConnectedResolution(boolean refresh) {
currentPrecs = new PrecedenceToInsert();
currentResolution = null;
this.connectedResolutionLabel.setVisible(false);
this.connectedResolutionAbort.setVisible(false);
this.connectedResolutionApply.setVisible(false);
......@@ -521,12 +541,25 @@ public class CupConflictsView extends FailableView
return;
}
}
if(currentResolution instanceof PrecedenceToInsert){
applyConnectedResolutionSR();
}
else{
applyConnectedResolutionRR();
}
}
private void applyConnectedResolutionRR(){
}
private void applyConnectedResolutionSR(){
try {
IDocument document = editor.getDocument();
ParserResult r = Model.getInstanceForDocument(document).getAstModel();
String toInsert = currentPrecs.precsToInsert();
String toInsert = getPti().precsToInsert();
int positionForPrec = 0;
if (r.precedences.size() == 0) {
......
......@@ -8,14 +8,16 @@ import java.util.Set;
import java.util.Stack;
import java.util.Map.Entry;
import de.in.tum.www2.cup.Conflict;
import de.in.tum.www2.cup.internal.terminal;
import de.tum.in.www2.cupplugin.views.CupConflictsView.ShiftReduceDetails;
public class GraphHelper<X> {
public abstract class GraphHelper<X> {
// Edges are stored in reverse order (i.e. there is a edge from a to b iff b has a explicit
// higher precedence / order in general than a
protected HashMap<X,Set<X>> edges;
public class TarjanNodeInfo{
protected class TarjanNodeInfo{
public int index;
public boolean onStack;
public int lowlink;
......@@ -124,5 +126,4 @@ public class GraphHelper<X> {
return false;
}
}
\ No newline at end of file
......@@ -9,13 +9,16 @@ import java.util.Set;
import java.util.Stack;
import java.util.Map.Entry;
import de.in.tum.www2.cup.Conflict;
import de.in.tum.www2.cup.ReduceReduceConflict;
import de.in.tum.www2.cup.ShiftReduceConflict;
import de.in.tum.www2.cup.ast.Precedence;
import de.in.tum.www2.cup.internal.terminal;
import de.tum.in.www2.cupplugin.Pair;
import de.tum.in.www2.cupplugin.views.CupConflictsView.ShiftReduceDetails;
import de.tum.in.www2.cupplugin.views.GraphHelper.TarjanNodeInfo;
class PrecedenceToInsert extends GraphHelper<terminal>{
class PrecedenceToInsert extends GraphHelper<terminal> implements ResolutionStrategy{
private HashMap<terminal,Precedence.Type> precs = new HashMap<>();
// These need to be inserted whenever an edge that contains the key is to be inserted
......@@ -294,11 +297,18 @@ class PrecedenceToInsert extends GraphHelper<terminal>{
}
}
/**
* Check if active (i.e. more than 0 precedences)
* @return
*/
@Override
public boolean isActive(){
return precs.size() != 0;
}
@Override
public boolean isAffected(Conflict c,ShiftReduceDetails details) {
if(c instanceof ShiftReduceConflict && isAffected(details)){
return true;
}
else{
return false;
}
}
}
\ No newline at end of file
......@@ -7,6 +7,9 @@ import java.util.List;
import java.util.Stack;
import java.util.Map.Entry;
import de.in.tum.www2.cup.Conflict;
import de.in.tum.www2.cup.ReduceReduceConflict;
import de.in.tum.www2.cup.ShiftReduceConflict;
import de.in.tum.www2.cup.ast.Precedence;
import de.in.tum.www2.cup.internal.internal_error;
import de.in.tum.www2.cup.internal.lalr_item;
......@@ -14,9 +17,10 @@ import de.in.tum.www2.cup.internal.lr_item_core;
import de.in.tum.www2.cup.internal.terminal;
import de.tum.in.www2.cupplugin.Pair;
import de.tum.in.www2.cupplugin.views.GraphHelper.TarjanNodeInfo;
import de.tum.in.www2.cupplugin.views.CupConflictsView.ShiftReduceDetails;
import de.tum.in.www2.cupplugin.views.PrecedenceToInsert.PrecedenceCyclicException;
public class ReordersToDo extends GraphHelper<lr_item_core>{
public class ReordersToDo extends GraphHelper<lr_item_core> implements ResolutionStrategy{
private LinkedList<lr_item_core> result;
public ReordersToDo(){
......@@ -190,6 +194,21 @@ public class ReordersToDo extends GraphHelper<lr_item_core>{
return result;
}
@Override
public boolean isAffected(Conflict c,ShiftReduceDetails details) {
if(c instanceof ReduceReduceConflict){
ReduceReduceConflict rrc = (ReduceReduceConflict) c;
return isAffected(rrc.getConflictItem1(),rrc.getConflictItem2());
}
else{
return false;
}
}
@Override
public boolean isActive(){
return edges.keySet().size() != 0;
}
}
package de.tum.in.www2.cupplugin.views;
import de.in.tum.www2.cup.Conflict;
import de.tum.in.www2.cupplugin.views.CupConflictsView.ShiftReduceDetails;
public interface ResolutionStrategy {
/**
* return true iff the conflict is affected
* @param c Conflict
* @param details details if conflict is Shiftreduce, null otherwise
* @return true iff conflict is affected
*/
public abstract boolean isAffected(Conflict c,ShiftReduceDetails details);
/**
* Check if active (i.e. more than 0 item)
* @return
*/
public abstract boolean isActive();
}
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