Commit 18eeec17 authored by Michael Schwarz's avatar Michael Schwarz 🤔
Browse files

ReduceReduce where one/both items are from expr w/ multiple rhs where

item to be moved is not the last one and moving does not affect further
conflicts
parent 212bd004
...@@ -624,11 +624,11 @@ class ConflictPanel extends Composite { ...@@ -624,11 +624,11 @@ class ConflictPanel extends Composite {
if(!cupConflictsView.settingOrderAffetsOtherConflicts(item1, item2)){ if(!cupConflictsView.settingOrderAffetsOtherConflicts(item1, item2)){
ReduceReduceReorder rrr = new ReduceReduceReorder(cupConflictsView.getDocument(),cupConflictsView.isThresholdPresent()); ReduceReduceReorder rrr = new ReduceReduceReorder(cupConflictsView.getDocument(),cupConflictsView.isThresholdPresent());
if(resolutionOptions.getSelectionIndex() ==1){ if(resolutionOptions.getSelectionIndex() == 1){
rrr.moveItemToVeryEnd(rrc.getConflictItem1()); rrr.moveItemToVeryEnd(rrc.getConflictItem1());
rrr.moveItemToVeryEnd(rrc.getConflictItem2()); rrr.moveItemToVeryEnd(rrc.getConflictItem2());
} }
else if(resolutionOptions.getSelectionIndex() ==2){ else if(resolutionOptions.getSelectionIndex() == 2){
rrr.moveItemToVeryEnd(rrc.getConflictItem2()); rrr.moveItemToVeryEnd(rrc.getConflictItem2());
rrr.moveItemToVeryEnd(rrc.getConflictItem1()); rrr.moveItemToVeryEnd(rrc.getConflictItem1());
} }
......
...@@ -558,7 +558,7 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi ...@@ -558,7 +558,7 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
return true; return true;
} }
affected = ordersAffectConflicts.get(item2); affected = ordersAffectConflicts.get(item2);
if(affected==null){ if(affected==null){
return false; return false;
} }
...@@ -598,41 +598,4 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi ...@@ -598,41 +598,4 @@ public class CupConflictsView extends FailableView implements ICupEditorPageVisi
public IDocument getDocument(){ public IDocument getDocument(){
return editor.getDocument(); return editor.getDocument();
} }
public void moveItemToVeryEnd(lalr_item item){
Production p =(Production)item.the_production().getAstNode().getParent();
IDocument document = editor.getDocument();
if(p.getRightHandSides().size() == 1){
// Base case
try {
String pString = document.get(p.getBegin().getOffsetFromStart(),
p.getEnd().getOffsetFromStart() - p.getBegin().getOffsetFromStart());
ParserResult r = Model.getInstanceForDocument(document).getAstModel();
int positionForProd = 0;
// Grammar can be assumed to have at least two productions -> safe
positionForProd = r.productions.get(r.productions.size()-1).getEnd().getOffsetFromStart();
String toInsert = "\n\n//Moved by CUP Eclipse plugin\n" + pString;
if(ignoreRRAfterLine == -1){
toInsert = "\n\n// " + TRESHOLD_STRING + "\n // After this the order of productions matters" + toInsert;
ignoreRRAfterLine = 0;
}
document.replace(positionForProd, 0, toInsert);
// We have a guarantee that we are moving stuff backwards, so this works
document.replace(p.getBegin().getOffsetFromStart(), p.getEnd().getOffsetFromStart() - p.getBegin().getOffsetFromStart(), "");
return;
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return;
}
} }
...@@ -9,6 +9,7 @@ import org.eclipse.jface.text.IDocument; ...@@ -9,6 +9,7 @@ import org.eclipse.jface.text.IDocument;
import de.in.tum.www2.cup.ast.ParserResult; import de.in.tum.www2.cup.ast.ParserResult;
import de.in.tum.www2.cup.ast.Production; import de.in.tum.www2.cup.ast.Production;
import de.in.tum.www2.cup.ast.ProductionRight;
import de.in.tum.www2.cup.internal.lalr_item; import de.in.tum.www2.cup.internal.lalr_item;
import de.tum.in.www2.cupplugin.model.Model; import de.tum.in.www2.cupplugin.model.Model;
...@@ -40,34 +41,70 @@ public class ReduceReduceReorder { ...@@ -40,34 +41,70 @@ public class ReduceReduceReorder {
rangesToRemove = new ArrayList<>(); rangesToRemove = new ArrayList<>();
} }
/**
* Mark item to be moved to the very end, change to document is only done once
* the apply function is called
* @param item lalr_item which is to be moved
*/
public void moveItemToVeryEnd(lalr_item item){ public void moveItemToVeryEnd(lalr_item item){
Production p =(Production)item.the_production().getAstNode().getParent(); Production p =(Production)item.the_production().getAstNode().getParent();
String toInsert = "";
if(p.getRightHandSides().size() == 1){ if(p.getRightHandSides().size() == 1){
// Base case
try { try {
String pString = document.get(p.getBegin().getOffsetFromStart(), String pString = document.get(p.getBegin().getOffsetFromStart(),
p.getEnd().getOffsetFromStart() - p.getBegin().getOffsetFromStart()); p.getEnd().getOffsetFromStart() - p.getBegin().getOffsetFromStart());
toInsert = "\n\n//Moved by CUP Eclipse plugin\n" + pString;
rangesToRemove.add(new RangeToRemove(p.getBegin().getOffsetFromStart(), p.getEnd().getOffsetFromStart() - p.getBegin().getOffsetFromStart()));
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
try {
ProductionRight pr = item.the_production().getAstNode();
List<ProductionRight> rhs = p.getRightHandSides();
int index = 1;
int start = pr.getRange().getBegin().getOffsetFromStart();
int end = 0;
for(int i=0;i < rhs.size();i++){
if(rhs.get(i).equals(pr)){
index = i;
break;
}
}
if(index == rhs.size()-1){
//TODO: take care of the SEMI
}
else{
end = rhs.get(index+1).getRange().getBegin().getOffsetFromStart();
}
String toInsert = "\n\n//Moved by CUP Eclipse plugin\n" + pString; // +1 here because we don't need the |
String pString = p.getName().name + "::=" + document.get(start+1,end-(start+1)) + ";";
toInsert = "\n\n//Separated and moved by CUP Eclipse plugin\n" + pString;
if(!flagPresent){ // But we want to remove the entire thing
toInsert = "\n\n// " + CupConflictsView.TRESHOLD_STRING + "\n // After this the order of productions matters" + toInsert; rangesToRemove.add(new RangeToRemove(start,end-start));
flagPresent = true;
}
textToAppend += toInsert;
rangesToRemove.add(new RangeToRemove(p.getBegin().getOffsetFromStart(), p.getEnd().getOffsetFromStart() - p.getBegin().getOffsetFromStart()));
return;
} catch (BadLocationException e) { } catch (BadLocationException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
return; if(!flagPresent){
toInsert = "\n\n// " + CupConflictsView.TRESHOLD_STRING + "\n // After this the order of productions matters" + toInsert;
flagPresent = true;
}
textToAppend += toInsert;
} }
public void apply() throws BadLocationException{ public void apply() throws BadLocationException{
......
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