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;
public class BuildfileUpdater {
public static void updateExpectedConflicts(IProject project,int number) throws ParserConfigurationException, SAXException, CoreException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError, IOException{
boolean modified = false;
Path path = new Path("build.xml");
IFile file = project.getFile(path);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file.getContents());
if(doc.getElementsByTagName("cup").getLength() != 1){
throw new IOException();
}
else {
Node n = doc.getElementsByTagName("cup").item(0);
public static class BuildfileNotFoundException extends Exception{
public BuildfileNotFoundException(Exception e) {
super(e);
}
}
public static void updateExpectedConflicts(IProject project,int number) throws BuildfileNotFoundException{
try {
boolean modified = false;
Node expect = n.getAttributes().getNamedItem("expect");
Path path = new Path("build.xml");
IFile file = project.getFile(path);
if(expect == null){
Attr expectedA = doc.createAttribute("expect");
n.getAttributes().setNamedItem(expectedA);
expect = n.getAttributes().getNamedItem("expect");
expect.setTextContent(String.valueOf(number));
modified = true;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file.getContents());
if(doc.getElementsByTagName("cup").getLength() != 1){
throw new IOException();
}
else if(!expect.getTextContent().equals(String.valueOf(number))){
expect.setTextContent(String.valueOf(number));
modified = true;
else {
Node n = doc.getElementsByTagName("cup").item(0);
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);
is.close();
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);
is.close();
}
}
catch(Exception e){
throw new BuildfileNotFoundException(e);
}
}
......
......@@ -68,7 +68,7 @@ public class ConflictFilter {
}
}
private int computeIgnoreRRAfter() {
public int computeIgnoreRRAfter() {
int offset = document.get().indexOf(TRESHOLD_STRING);
if (offset == -1) {
return -1;
......
......@@ -68,7 +68,6 @@ public class ReduceReduceReorder {
rangesToRemove.add(new RangeToRemove(p.getBegin().getOffsetFromStart(), p.getEnd().getOffsetFromStart() - p.getBegin().getOffsetFromStart()));
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
......@@ -144,10 +143,21 @@ public class ReduceReduceReorder {
private void insert() throws BadLocationException{
int positionForProd = 0;
ParserResult r = Model.getInstanceForDocument(document).getAstModel();
// Grammar can be assumed to have at least two productions -> safe
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);
}
......
......@@ -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.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{
......
......@@ -86,6 +86,8 @@ public class CupConflictsView extends FailableView
public int ignoreRRAfterLine;
private int expectedRR = -1;
public class ShiftReduceDetails {
public terminal shift;
public terminal reduce;
......@@ -214,14 +216,13 @@ public class CupConflictsView extends FailableView
IProject project = file.getProject();
BuildfileUpdater.updateExpectedConflicts(project, number);
} catch (ParserConfigurationException | SAXException | IOException | CoreException | TransformerException
| TransformerFactoryConfigurationError e1) {
} catch (BuildfileUpdater.BuildfileNotFoundException e) {
Shell shell = new Shell(Display.getCurrent());
MessageBox box = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
box.setText("Information");
box.setMessage(
"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();
}
......@@ -239,8 +240,15 @@ public class CupConflictsView extends FailableView
addToOrderSet(c);
}
// Do this less often
updateBuildfile(cf.getResolvedCountLastRun());
int newResolved = 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);
......
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