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

Refactoring to reduce complexity

parent 69f5e1f7
...@@ -7,11 +7,16 @@ import java.util.List; ...@@ -7,11 +7,16 @@ import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Stack; import java.util.Stack;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import de.in.tum.www2.cup.Conflict; import de.in.tum.www2.cup.Conflict;
import de.in.tum.www2.cup.ShiftReduceConflict; import de.in.tum.www2.cup.ShiftReduceConflict;
import de.in.tum.www2.cup.ast.ParserResult;
import de.in.tum.www2.cup.ast.Precedence; import de.in.tum.www2.cup.ast.Precedence;
import de.in.tum.www2.cup.internal.terminal; import de.in.tum.www2.cup.internal.terminal;
import de.tum.in.www2.cupplugin.Pair; import de.tum.in.www2.cupplugin.Pair;
import de.tum.in.www2.cupplugin.model.Model;
import de.tum.in.www2.cupplugin.views.CupConflictsView.ShiftReduceDetails; import de.tum.in.www2.cupplugin.views.CupConflictsView.ShiftReduceDetails;
public class PrecedenceToInsert extends GraphHelper<terminal> implements ResolutionStrategy{ public class PrecedenceToInsert extends GraphHelper<terminal> implements ResolutionStrategy{
...@@ -307,4 +312,31 @@ public class PrecedenceToInsert extends GraphHelper<terminal> implements Resolut ...@@ -307,4 +312,31 @@ public class PrecedenceToInsert extends GraphHelper<terminal> implements Resolut
return false; return false;
} }
} }
@Override
public void apply(IDocument doc) {
try {
ParserResult r = Model.getInstanceForDocument(doc).getAstModel();
String toInsert = precsToInsert();
int positionForPrec = 0;
if (r.precedences.size() == 0) {
// Precedences are located immediately after the last symbol
// decl
positionForPrec = r.symbols.get(r.symbols.size() - 1).getEnd().getOffsetFromStart();
toInsert = "\n\n//Precedences added by Eclipse plugin\n" + toInsert;
} else {
positionForPrec = r.precedences.get(0).getRange().getBegin().getOffsetFromStart();
toInsert = toInsert + "\n";
}
doc.replace(positionForPrec, 0, toInsert);
} catch (BadLocationException e) {
e.printStackTrace();
} catch (de.tum.in.www2.cupplugin.conflictresolution.PrecedenceToInsert.PrecedenceCyclicException e) {
e.printStackTrace();
}
}
} }
\ No newline at end of file
...@@ -37,9 +37,9 @@ public class ReduceReduceReorder { ...@@ -37,9 +37,9 @@ public class ReduceReduceReorder {
} }
} }
public ReduceReduceReorder(IDocument document,boolean flagPresent){ public ReduceReduceReorder(IDocument document){
this.document = document; this.document = document;
this.flagPresent = flagPresent; this.flagPresent = (document.get().indexOf(CupConflictsView.TRESHOLD_STRING) != -1);
textToAppend=""; textToAppend="";
rangesToRemove = new ArrayList<>(); rangesToRemove = new ArrayList<>();
} }
......
...@@ -7,11 +7,15 @@ import java.util.List; ...@@ -7,11 +7,15 @@ import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Stack; import java.util.Stack;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import de.in.tum.www2.cup.Conflict; import de.in.tum.www2.cup.Conflict;
import de.in.tum.www2.cup.ReduceReduceConflict; import de.in.tum.www2.cup.ReduceReduceConflict;
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;
import de.in.tum.www2.cup.internal.lr_item_core; import de.in.tum.www2.cup.internal.lr_item_core;
import de.tum.in.www2.cupplugin.conflictresolution.ReordersToDo.OrderCyclicException;
import de.tum.in.www2.cupplugin.views.CupConflictsView.ShiftReduceDetails; import de.tum.in.www2.cupplugin.views.CupConflictsView.ShiftReduceDetails;
public class ReordersToDo extends GraphHelper<lr_item_core> implements ResolutionStrategy{ public class ReordersToDo extends GraphHelper<lr_item_core> implements ResolutionStrategy{
...@@ -204,5 +208,26 @@ public class ReordersToDo extends GraphHelper<lr_item_core> implements Resolutio ...@@ -204,5 +208,26 @@ public class ReordersToDo extends GraphHelper<lr_item_core> implements Resolutio
public boolean isActive(){ public boolean isActive(){
return edges.keySet().size() != 0; return edges.keySet().size() != 0;
} }
@Override
public void apply(IDocument doc) {
try {
LinkedList<lr_item_core> order = getItemsOrdered();
java.util.Collections.reverse(order);
ReduceReduceReorder rrr = new ReduceReduceReorder(doc);
for(lr_item_core item : order){
rrr.moveItemToVeryEnd(item);
}
rrr.apply();
} catch (OrderCyclicException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
package de.tum.in.www2.cupplugin.conflictresolution; package de.tum.in.www2.cupplugin.conflictresolution;
import org.eclipse.jface.text.IDocument;
import de.in.tum.www2.cup.Conflict; import de.in.tum.www2.cup.Conflict;
import de.tum.in.www2.cupplugin.views.CupConflictsView.ShiftReduceDetails; import de.tum.in.www2.cupplugin.views.CupConflictsView.ShiftReduceDetails;
...@@ -17,4 +19,10 @@ public interface ResolutionStrategy { ...@@ -17,4 +19,10 @@ public interface ResolutionStrategy {
* @return * @return
*/ */
public abstract boolean isActive(); public abstract boolean isActive();
/**
* Apply the strategy to the file
* @param doc file to which the strategy is to be applied
*/
public abstract void apply(IDocument doc);
} }
...@@ -533,7 +533,6 @@ public class CupConflictsView extends FailableView ...@@ -533,7 +533,6 @@ public class CupConflictsView extends FailableView
* insert to appropriate position and end connected resolution * insert to appropriate position and end connected resolution
*/ */
private void applyConnectedResolution() { private void applyConnectedResolution() {
// Conflicts are part of current connected conflicts that are not // Conflicts are part of current connected conflicts that are not
// resolved yet. // resolved yet.
for (ConflictPanel p : conflictPanels) { for (ConflictPanel p : conflictPanels) {
...@@ -548,66 +547,8 @@ public class CupConflictsView extends FailableView ...@@ -548,66 +547,8 @@ public class CupConflictsView extends FailableView
} }
} }
if(currentResolution instanceof PrecedenceToInsert){ currentResolution.apply(editor.getDocument());
applyConnectedResolutionSR(); abortConnectedResolution(true);
}
else{
applyConnectedResolutionRR();
}
}
private void applyConnectedResolutionRR() {
try {
ReordersToDo rtd = (ReordersToDo) currentResolution;
LinkedList<lr_item_core> order = rtd.getItemsOrdered();
java.util.Collections.reverse(order);
ReduceReduceReorder rrr = new ReduceReduceReorder(editor.getDocument(),isThresholdPresent());
for(lr_item_core item : order){
rrr.moveItemToVeryEnd(item);
}
rrr.apply();
} catch (OrderCyclicException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
abortConnectedResolution(true);
}
}
private void applyConnectedResolutionSR(){
try {
IDocument document = editor.getDocument();
ParserResult r = Model.getInstanceForDocument(document).getAstModel();
String toInsert = getPti().precsToInsert();
int positionForPrec = 0;
if (r.precedences.size() == 0) {
// Precedences are located immediately after the last symbol
// decl
positionForPrec = r.symbols.get(r.symbols.size() - 1).getEnd().getOffsetFromStart();
toInsert = "\n\n//Precedences added by Eclipse plugin\n" + toInsert;
} else {
positionForPrec = r.precedences.get(0).getRange().getBegin().getOffsetFromStart();
toInsert = toInsert + "\n";
}
document.replace(positionForPrec, 0, toInsert);
} catch (BadLocationException e) {
e.printStackTrace();
} catch (de.tum.in.www2.cupplugin.conflictresolution.PrecedenceToInsert.PrecedenceCyclicException e) {
e.printStackTrace();
} finally {
abortConnectedResolution(true);
}
} }
/** /**
......
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