Commit 55068302 authored by Michael Schwarz's avatar Michael Schwarz

Refactoring to reduce complexity

parent 69f5e1f7
......@@ -7,11 +7,16 @@ import java.util.List;
import java.util.Map.Entry;
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.ShiftReduceConflict;
import de.in.tum.www2.cup.ast.ParserResult;
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.model.Model;
import de.tum.in.www2.cupplugin.views.CupConflictsView.ShiftReduceDetails;
public class PrecedenceToInsert extends GraphHelper<terminal> implements ResolutionStrategy{
......@@ -307,4 +312,31 @@ public class PrecedenceToInsert extends GraphHelper<terminal> implements Resolut
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 {
}
}
public ReduceReduceReorder(IDocument document,boolean flagPresent){
public ReduceReduceReorder(IDocument document){
this.document = document;
this.flagPresent = flagPresent;
this.flagPresent = (document.get().indexOf(CupConflictsView.TRESHOLD_STRING) != -1);
textToAppend="";
rangesToRemove = new ArrayList<>();
}
......
......@@ -7,11 +7,15 @@ import java.util.List;
import java.util.Map.Entry;
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.ReduceReduceConflict;
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.lr_item_core;
import de.tum.in.www2.cupplugin.conflictresolution.ReordersToDo.OrderCyclicException;
import de.tum.in.www2.cupplugin.views.CupConflictsView.ShiftReduceDetails;
public class ReordersToDo extends GraphHelper<lr_item_core> implements ResolutionStrategy{
......@@ -204,5 +208,26 @@ public class ReordersToDo extends GraphHelper<lr_item_core> implements Resolutio
public boolean isActive(){
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;
import org.eclipse.jface.text.IDocument;
import de.in.tum.www2.cup.Conflict;
import de.tum.in.www2.cupplugin.views.CupConflictsView.ShiftReduceDetails;
......@@ -17,4 +19,10 @@ public interface ResolutionStrategy {
* @return
*/
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
* insert to appropriate position and end connected resolution
*/
private void applyConnectedResolution() {
// Conflicts are part of current connected conflicts that are not
// resolved yet.
for (ConflictPanel p : conflictPanels) {
......@@ -548,66 +547,8 @@ public class CupConflictsView extends FailableView
}
}
if(currentResolution instanceof PrecedenceToInsert){
applyConnectedResolutionSR();
}
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);
}
currentResolution.apply(editor.getDocument());
abortConnectedResolution(true);
}
/**
......
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