Commit 683d4c0f authored by Michael Schwarz's avatar Michael Schwarz

fixed bug if separator string is present, but no productions afterwards

show error message in case buildfile was not found
parent 0c1acc13
...@@ -28,46 +28,58 @@ import org.xml.sax.SAXException; ...@@ -28,46 +28,58 @@ import org.xml.sax.SAXException;
public class BuildfileUpdater { public class BuildfileUpdater {
public static void updateExpectedConflicts(IProject project,int number) throws ParserConfigurationException, SAXException, CoreException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError, IOException{ public static class BuildfileNotFoundException extends Exception{
boolean modified = false; public BuildfileNotFoundException(Exception e) {
super(e);
Path path = new Path("build.xml"); }
IFile file = project.getFile(path); }
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); public static void updateExpectedConflicts(IProject project,int number) throws BuildfileNotFoundException{
DocumentBuilder db = dbf.newDocumentBuilder(); try {
Document doc = db.parse(file.getContents()); boolean modified = false;
if(doc.getElementsByTagName("cup").getLength() != 1){
throw new IOException();
}
else {
Node n = doc.getElementsByTagName("cup").item(0);
Node expect = n.getAttributes().getNamedItem("expect"); Path path = new Path("build.xml");
IFile file = project.getFile(path);
if(expect == null){ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Attr expectedA = doc.createAttribute("expect"); DocumentBuilder db = dbf.newDocumentBuilder();
n.getAttributes().setNamedItem(expectedA); Document doc = db.parse(file.getContents());
expect = n.getAttributes().getNamedItem("expect");
expect.setTextContent(String.valueOf(number)); if(doc.getElementsByTagName("cup").getLength() != 1){
modified = true; throw new IOException();
} }
else if(!expect.getTextContent().equals(String.valueOf(number))){ else {
expect.setTextContent(String.valueOf(number)); Node n = doc.getElementsByTagName("cup").item(0);
modified = true;
Node expect = n.getAttributes().getNamedItem("expect");
if(expect == null){
Attr expectedA = doc.createAttribute("expect");
n.getAttributes().setNamedItem(expectedA);
expect = n.getAttributes().getNamedItem("expect");
expect.setTextContent(String.valueOf(number));
modified = true;
}
else if(!expect.getTextContent().equals(String.valueOf(number))){
expect.setTextContent(String.valueOf(number));
modified = true;
}
} }
}
if(modified){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(doc);
Result outputTarget = new StreamResult(outputStream);
TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
file.setContents(is,true,true,null); if(modified){
is.close(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(doc);
Result outputTarget = new StreamResult(outputStream);
TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
file.setContents(is,true,true,null);
is.close();
}
}
catch(Exception e){
throw new BuildfileNotFoundException(e);
} }
} }
......
...@@ -68,7 +68,7 @@ public class ConflictFilter { ...@@ -68,7 +68,7 @@ public class ConflictFilter {
} }
} }
private int computeIgnoreRRAfter() { public int computeIgnoreRRAfter() {
int offset = document.get().indexOf(TRESHOLD_STRING); int offset = document.get().indexOf(TRESHOLD_STRING);
if (offset == -1) { if (offset == -1) {
return -1; return -1;
......
...@@ -68,7 +68,6 @@ public class ReduceReduceReorder { ...@@ -68,7 +68,6 @@ public class ReduceReduceReorder {
rangesToRemove.add(new RangeToRemove(p.getBegin().getOffsetFromStart(), p.getEnd().getOffsetFromStart() - p.getBegin().getOffsetFromStart())); rangesToRemove.add(new RangeToRemove(p.getBegin().getOffsetFromStart(), p.getEnd().getOffsetFromStart() - p.getBegin().getOffsetFromStart()));
} catch (BadLocationException e) { } catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
...@@ -144,10 +143,21 @@ public class ReduceReduceReorder { ...@@ -144,10 +143,21 @@ public class ReduceReduceReorder {
private void insert() throws BadLocationException{ private void insert() throws BadLocationException{
int positionForProd = 0; int positionForProd = 0;
ParserResult r = Model.getInstanceForDocument(document).getAstModel(); ParserResult r = Model.getInstanceForDocument(document).getAstModel();
// Grammar can be assumed to have at least two productions -> safe // Grammar can be assumed to have at least two productions -> safe
positionForProd = r.productions.get(r.productions.size()-1).getEnd().getOffsetFromStart(); positionForProd = r.productions.get(r.productions.size()-1).getEnd().getOffsetFromStart();
//If separator string is present, but no productions afterwards
int ignoreLine= (new ConflictFilter(document)).computeIgnoreRRAfter();
if(ignoreLine > 0){
int ignoreOffset = document.getLineOffset(ignoreLine);
if(ignoreOffset > positionForProd){
positionForProd = ignoreOffset;
}
}
document.replace(positionForProd, 0, textToAppend); document.replace(positionForProd, 0, textToAppend);
} }
......
...@@ -15,7 +15,6 @@ import de.in.tum.www2.cup.ReduceReduceConflict; ...@@ -15,7 +15,6 @@ 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{
......
...@@ -86,6 +86,8 @@ public class CupConflictsView extends FailableView ...@@ -86,6 +86,8 @@ public class CupConflictsView extends FailableView
public int ignoreRRAfterLine; public int ignoreRRAfterLine;
private int expectedRR = -1;
public class ShiftReduceDetails { public class ShiftReduceDetails {
public terminal shift; public terminal shift;
public terminal reduce; public terminal reduce;
...@@ -214,14 +216,13 @@ public class CupConflictsView extends FailableView ...@@ -214,14 +216,13 @@ public class CupConflictsView extends FailableView
IProject project = file.getProject(); IProject project = file.getProject();
BuildfileUpdater.updateExpectedConflicts(project, number); BuildfileUpdater.updateExpectedConflicts(project, number);
} catch (ParserConfigurationException | SAXException | IOException | CoreException | TransformerException } catch (BuildfileUpdater.BuildfileNotFoundException e) {
| TransformerFactoryConfigurationError e1) {
Shell shell = new Shell(Display.getCurrent()); Shell shell = new Shell(Display.getCurrent());
MessageBox box = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK); MessageBox box = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
box.setText("Information"); box.setText("Information");
box.setMessage( box.setMessage(
"Failed to update the expect label in the buildfile.\nThere was no file named build.xml containing a cup section.\n" "Failed to update the expect label in the buildfile.\nThere was no file named build.xml containing a cup section.\n"
+ "Please update your buildfile and set the expect label to number."); + "Please update your buildfile and set the expect label to " + number + ".");
box.open(); box.open();
} }
...@@ -239,8 +240,15 @@ public class CupConflictsView extends FailableView ...@@ -239,8 +240,15 @@ public class CupConflictsView extends FailableView
addToOrderSet(c); addToOrderSet(c);
} }
// Do this less often int newResolved = cf.getResolvedCountLastRun();
updateBuildfile(cf.getResolvedCountLastRun());
// We assume that if there are already conflicts solved when we do this first, that they
//were solved on purpose and so the buildfile is up-to-date
if(newResolved != expectedRR && expectedRR != -1){
updateBuildfile(newResolved);
}
expectedRR = newResolved;
scrolled.setLayoutDeferred(true); scrolled.setLayoutDeferred(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