Commit 9b418e3c authored by Michael Schwarz's avatar Michael Schwarz

fix problems coming from moving first production by inserting explicit

start symbol
parent cd6346e4
...@@ -7,6 +7,7 @@ import java.util.List; ...@@ -7,6 +7,7 @@ import java.util.List;
import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocument;
import de.in.tum.www2.cup.ast.NonTerminal;
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.ast.ProductionRight;
...@@ -18,6 +19,8 @@ public class ReduceReduceReorder { ...@@ -18,6 +19,8 @@ public class ReduceReduceReorder {
String textToAppend; String textToAppend;
boolean flagPresent; boolean flagPresent;
private List<RangeToRemove> rangesToRemove; private List<RangeToRemove> rangesToRemove;
private boolean movedFirstProd = false;
private String firstNonTermWas = "";
private class RangeToRemove implements Comparable<RangeToRemove>{ private class RangeToRemove implements Comparable<RangeToRemove>{
int start; int start;
...@@ -52,6 +55,12 @@ public class ReduceReduceReorder { ...@@ -52,6 +55,12 @@ public class ReduceReduceReorder {
if(p.getRightHandSides().size() == 1){ if(p.getRightHandSides().size() == 1){
try { try {
if(Model.getInstanceForDocument(document).getAstModel().productions.indexOf(p) == 0){
movedFirstProd = true;
firstNonTermWas = p.getName().name;
}
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());
...@@ -131,6 +140,7 @@ public class ReduceReduceReorder { ...@@ -131,6 +140,7 @@ public class ReduceReduceReorder {
public void apply() throws BadLocationException{ public void apply() throws BadLocationException{
insert(); insert();
removeAllOldItems(); removeAllOldItems();
fixStart();
} }
private void insert() throws BadLocationException{ private void insert() throws BadLocationException{
...@@ -142,6 +152,23 @@ public class ReduceReduceReorder { ...@@ -142,6 +152,23 @@ public class ReduceReduceReorder {
document.replace(positionForProd, 0, textToAppend); document.replace(positionForProd, 0, textToAppend);
} }
/**
* Add the start with statement if the first production was moved and no start with statement was given
* Otherwise the start symbol defaults to the first production that is usually different
* @throws BadLocationException
*/
private void fixStart() throws BadLocationException{
if(movedFirstProd){
ParserResult r = Model.getInstanceForDocument(document).getAstModel();
if(r.startWith == null){
String toInsert = "// Inserted by CUP Eclipse plugin after moving rules\nstart with " + firstNonTermWas + "; \n\n\n";
int pos = r.productions.get(0).getRange().getBegin().getOffsetFromStart();
document.replace(pos, 0, toInsert);
}
}
}
private void removeAllOldItems() throws BadLocationException{ private void removeAllOldItems() throws BadLocationException{
Collections.sort(rangesToRemove); Collections.sort(rangesToRemove);
Collections.reverse(rangesToRemove); Collections.reverse(rangesToRemove);
......
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