Commit 0da33152 authored by Sebastian Pretscher's avatar Sebastian Pretscher

Added model subscription and change after parser work

parent eb3ac192
......@@ -131,26 +131,55 @@ public class Controller {
char beforeDelete = document.get().charAt(
event.getOffset() - 1);
//System.out.println(beforeDelete);
if (!region.getType().equals(CupPartionScanner.JAVA_CODE)) {
if (beforeDelete == '*' || beforeDelete == '/'
|| beforeDelete == '{') {
if (region.getType().equals(CupPartionScanner.JAVA_CODE)) {
if (beforeDelete == '{') {
System.out.println("comment delete");
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
}
} else if (region.getType().equals(CupPartionScanner.SINGLE_COMMENT)){
if (beforeDelete == '/') {
System.out.println("comment delete");
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
}
} else if (region.getType().equals(CupPartionScanner.MULTILINE_COMMENT)){
if (beforeDelete == '/' || beforeDelete == '*') {
System.out.println("comment delete");
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
}
} else {
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
}
}
} else {
String regionType = region.getType();
switch (regionType) {
case CupPartionScanner.JAVA_CODE:
addJobToDo(JobsToDo.locationPatch);
if(event.getText().equals("{")) {
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
} else {
addJobToDo(JobsToDo.locationPatch);
}
break;
case CupPartionScanner.MULTILINE_COMMENT:
addJobToDo(JobsToDo.locationPatch);
if(event.getText().equals("/")) {
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
} else {
addJobToDo(JobsToDo.locationPatch);
}
break;
case CupPartionScanner.SINGLE_COMMENT:
addJobToDo(JobsToDo.locationPatch);
if(event.getText().equals("/")) {
addJobToDo(JobsToDo.parseCode);
addJobToDo(JobsToDo.buildTable);
} else {
addJobToDo(JobsToDo.locationPatch);
}
break;
default:
addJobToDo(JobsToDo.parseCode);
......
package de.tum.in.www2.cupplugin.controller;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.EnumSet;
import java.util.List;
......@@ -13,18 +14,26 @@ import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.ui.progress.UIJob;
import de.in.tum.www2.cup.CupContext;
import de.in.tum.www2.cup.CupParser;
import de.in.tum.www2.cup.DefaultErrorReporter;
import de.in.tum.www2.cup.analysis.LocationPatchVisitor;
import de.in.tum.www2.cup.ast.ParserResult;
import de.tum.in.www2.cupplugin.controller.Controller.JobsToDo;
import de.tum.in.www2.cupplugin.editors.CupPartionScanner;
import de.tum.in.www2.cupplugin.editors.CupTextEditor;
import de.tum.in.www2.cupplugin.model.Model;
public class DocumentDidChangeJob extends Job {
boolean iAmCurrentlyRunning = false;
CupTextEditor myEditor = null;
private List<DocumentEvent> documentEvents;
private EnumSet<JobsToDo> jobs;
private ParserResult result;
private CupContext context;
public DocumentDidChangeJob(CupTextEditor editor) {
super("" + editor.hashCode());
myEditor = editor;
......@@ -40,48 +49,56 @@ public class DocumentDidChangeJob extends Job {
/*
* TODO: Job bestücken, vll mit callbacks
*/
IDocument document = myEditor.getDocumentProvider().getDocument(
myEditor.getEditorInput());
Controller controller = Controller.getInstance(myEditor);
documentEvents = controller.popAllDocumentEvents();
jobs = controller.popJobsToDo();
System.out.println(jobs);
/*
String codeText = document.get();
InputStream in = new ByteArrayInputStream(codeText.getBytes());
CupParser p = new CupParser(new DefaultErrorReporter(), in);
ParserResult result = null;
CupContext context = null;
LALRResult lalrResult = null;
try {
result = p.parse();
context = p.getContext();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (jobs.contains(JobsToDo.locationPatch)
&& (jobs.contains(JobsToDo.parseCode) || jobs
.contains(JobsToDo.buildTable))) {
jobs.remove(JobsToDo.locationPatch);
}
if (result != null) {
System.out.println(result.toString());
if (jobs.contains(JobsToDo.locationPatch)) {
// TODO: location Patch verifizieren
LocationPatchVisitor visitor = new LocationPatchVisitor();
result.accept(visitor, null);
}
if (context == null) {
if (jobs.contains(JobsToDo.parseCode)) {
String codeText = document.get();
InputStream in = new ByteArrayInputStream(codeText.getBytes());
CupParser p = new CupParser(new DefaultErrorReporter(), in);
result = null;
context = null;
} else {
try {
lalrResult = LALRResult.Compute(context,
context.start_production);
} catch (internal_error e) {
result = p.parse();
context = p.getContext();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(lalrResult.action_table);
System.out.println(lalrResult.reduce_table);
if (result != null) {
System.out.println(result.toString());
ParserResultJob resultModelJob = new ParserResultJob(result);
resultModelJob.schedule();
}
}
if (jobs.contains(JobsToDo.buildTable)) {
}
System.out.println(jobs);
/*
*/
// nichts mehr ändern ab hier
iAmCurrentlyRunning = false;
......@@ -104,6 +121,25 @@ public class DocumentDidChangeJob extends Job {
Controller.getInstance(myEditor).notifyChangeJobFinished();
return Status.OK_STATUS;
}
}
class ParserResultJob extends UIJob {
ParserResult result;
public ParserResultJob(ParserResult result) {
super("Parser Result UI Job");
this.result = result;
}
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
Model model = Model.getInstance();
model.setASTModel(result);
return Status.OK_STATUS;
}
}
......
......@@ -34,7 +34,7 @@ public class CupGraphView implements CupGraphModelObserver {
this.graphComposite.setLayout(layout);
g = new Graph(parent, SWT.NONE);
for (int i = 0; i<500; i++) {
for (int i = 0; i<5; i++) {
GraphNode hello = new GraphNode(g, SWT.NONE, "Hello");
GraphNode world = new GraphNode(g, SWT.NONE, "World");
new GraphConnection(g, SWT.NONE, hello, world);
......
......@@ -3,10 +3,14 @@ package de.tum.in.www2.cupplugin.model;
import java.util.ArrayList;
import java.util.List;
import de.in.tum.www2.cup.ast.ParserResult;
public class Model {
// Static reference to the controller
static Model instance;
private ParserResult astModel;
// a lock, since some operations might not be threadsafe
private final Object lock = new Object();
......@@ -14,7 +18,7 @@ public class Model {
private List<Object> modelObservers;
/*
* Static singelton getter
* Static singleton getter
*/
public static Model getInstance() {
if (instance != null) {
......@@ -44,4 +48,22 @@ public class Model {
return false;
}
}
public ParserResult getAstModel() {
synchronized(astModel) {
return astModel;
}
}
public void setASTModel(ParserResult result) {
synchronized (astModel) {
this.astModel = result;
for(Object observer:modelObservers) {
if(observer instanceof CupParserASTChangeObserver) {
((CupParserASTChangeObserver)observer).modelChanged(this);
}
}
}
}
}
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