Commit f3d451ef authored by Administrator's avatar Administrator
Browse files

MiniJava Parser liest nun Editor-Inhalte ein und gibt erkannte Syntax auf die Konsole aus

parent ac92d2d6
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="lib" path="Compiler.jar"/>
<classpathentry exported="true" kind="lib" path="java-cup-11b-runtime.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="lib" path="java-cup-11b-runtime.jar"/>
<classpathentry kind="lib" path="/MiniJava/dist/Compiler.jar"/>
<classpathentry kind="lib" path="/MiniJava/dist/Compiler.jar" sourcepath="/MiniJava/src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
......@@ -25,4 +25,16 @@
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<linkedResources>
<link>
<name>Compiler.jar</name>
<type>1</type>
<locationURI>WORKSPACE_LOC/MiniJava/dist/Compiler.jar</locationURI>
</link>
<link>
<name>java-cup-11b-runtime.jar</name>
<type>1</type>
<locationURI>WORKSPACE_LOC/MiniJava/dist/java-cup-11b-runtime.jar</locationURI>
</link>
</linkedResources>
</projectDescription>
......@@ -11,5 +11,9 @@ Require-Bundle: org.eclipse.ui,
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Import-Package: org.eclipse.jface.text,
org.eclipse.jface.text.rules,
org.eclipse.jface.text.source,
org.eclipse.ui.editors.text
Bundle-ClassPath: java-cup-11b-runtime.jar,
.,
Compiler.jar
......@@ -2,4 +2,6 @@ source.. = src/
output.. = bin/
bin.includes = plugin.xml,\
META-INF/,\
.
.,\
java-cup-11b-runtime.jar,\
Compiler.jar
......@@ -8,12 +8,16 @@ import java.util.Set;
import java.util.WeakHashMap;
import mjplugin.editors.MJEditor;
import mjplugin.editors.MJPartitionScanner;
import mjplugin.editors.RevisionManager;
import mjplugin.model.Model;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITypedRegion;
public class Controller {
public static enum JobsToDo {
......@@ -146,4 +150,96 @@ public class Controller {
return popList;
}
}
public void registerObserver(IRegisterForControllerChanges observer) {
synchronized (controllerObservers) {
controllerObservers.add(observer);
}
}
public void unregisterObserver(IRegisterForControllerChanges observer) {
synchronized (controllerObservers) {
controllerObservers.remove(observer);
}
}
/*
* notify method that's to be called, when a change in the document happens
*/
public void notifyChange(DocumentEvent event) {
synchronized (lock) {
addDocumentEvent(event);
IDocument document = editor.getDocument();
RevisionManager.increment(document);
EnumSet<JobsToDo> observedJobs = EnumSet.noneOf(JobsToDo.class);
synchronized (controllerObservers) {
for (IRegisterForControllerChanges observer : controllerObservers) {
observedJobs.addAll(observer.getRequiredJobs());
}
}
EnumSet<JobsToDo> possibleJobs = EnumSet.noneOf(JobsToDo.class);
try {
ITypedRegion region = document.getPartition(event.getOffset());
if (event.getText().trim().length() == 0
&& (document.getLength() <= (event.getOffset() + 1) || document
.getChar(event.getOffset() + 1) == (' '))) {
if (event.getText().equals("")) {
char beforeDelete = '+'; // DUMMY
int offset = event.getOffset();
if (offset > 0)
beforeDelete = document.get().charAt(offset - 1);
if (region.getType().equals(
MJPartitionScanner.SINGLE_COMMENT)) {
if (beforeDelete == '/') {
possibleJobs.add(JobsToDo.parseCode);
}
} else if (region.getType().equals(
MJPartitionScanner.MULTILINE_COMMENT)) {
if (beforeDelete == '/' || beforeDelete == '*') {
possibleJobs.add(JobsToDo.parseCode);
}
} else {
possibleJobs.add(JobsToDo.parseCode);
}
}
} else {
String regionType = region.getType();
switch (regionType) {
case MJPartitionScanner.MULTILINE_COMMENT:
if (event.getText().equals("/")) {
possibleJobs.add(JobsToDo.parseCode);
}
break;
case MJPartitionScanner.SINGLE_COMMENT:
if (event.getText().equals("/")) {
possibleJobs.add(JobsToDo.parseCode);
}
break;
default:
possibleJobs.add(JobsToDo.parseCode);
break;
}
}
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (JobsToDo job : possibleJobs) {
if (observedJobs.contains(job)) {
addJobToDo(job);
}
}
runJobs(document);
}
}
public void addDocumentEvent(DocumentEvent e) {
synchronized (documentEvents) {
this.documentEvents.add(e);
}
}
}
package mjplugin.editors;
import mjplugin.ast.MiniJavaAST;
import mjplugin.controller.Controller;
import mjplugin.model.Model;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
public class MJDocumentListener implements IDocumentListener {
private MJEditor editor;
public MJDocumentListener(MJEditor e) {
this.editor=e;
}
private int linesRemoved = 0;
private int removedLastLineColumns = 0;
public void documentAboutToBeChanged(DocumentEvent event) {
IDocument document = event.getDocument();
try {
int offset = event.getOffset();
int startLineNumber = document.getLineOfOffset(offset);
int endOffset = offset + event.getLength();
int endLineNumber = document.getLineOfOffset(endOffset);
linesRemoved = endLineNumber - startLineNumber;
removedLastLineColumns = endOffset
- document.getLineOffset(endLineNumber);
} catch (BadLocationException e) {
e.printStackTrace();
}
return;
}
private void patchModel(DocumentEvent event) {
IDocument document = event.getDocument();
Model model = Model.getInstanceForDocument(document);
if (model == null)
return;
MiniJavaAST ast = model.getAstModel();
if (ast == null)
return;
int offset = event.getOffset();
int textLength = event.getText().length();
int removedLength = event.getLength();
int linesAdded = 0;
int addedLastLineColumns = 0;
try {
int startLineNumber = document.getLineOfOffset(offset);
int endOffset = offset + textLength;
int endLineNumber = document.getLineOfOffset(endOffset);
addedLastLineColumns = endOffset
- document.getLineOffset(endLineNumber);
linesAdded = endLineNumber - startLineNumber;
System.out.println("PROCESSING DocumentEvent: ");
System.out.println(" Offset: " + offset);
System.out.println(" Replaced: " + event.getLength());
System.out.println(" New: " + event.getText().length());
System.out.println(" linesRemoved: " + linesRemoved);
System.out.println(" linesAdded: " + linesAdded);
} catch (BadLocationException e) {
e.printStackTrace();
}
// NOTE: we do not try to fix the replaced part.
int fixColumnUntilOffset = removedLength;
int fixAfterOffset = offset + removedLength;
int diffOffset = textLength - removedLength;
int diffLines = linesAdded - linesRemoved;
int diffColumns = addedLastLineColumns - removedLastLineColumns;
//LocationPatchVisitor visitor = new LocationPatchVisitor(fixAfterOffset,
// fixColumnUntilOffset, diffLines, diffColumns, diffOffset);
//ast.accept(visitor, null);
}
@Override
public void documentChanged(DocumentEvent event) {
patchModel(event);
Controller controller = Controller.getInstance(editor);
controller.notifyChange(event);
}
}
package mjplugin.editors;
import java.util.EnumSet;
import java_cup.runtime.ComplexSymbolFactory.Location;
import mjplugin.controller.Controller;
import mjplugin.controller.Controller.JobsToDo;
import mjplugin.controller.IRegisterForControllerChanges;
import mjplugin.controller.JobStatus;
import mjplugin.model.IMJParserChangeObserver;
import mjplugin.model.Model;
......@@ -17,7 +22,7 @@ import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.IDocumentProvider;
public class MJEditor extends TextEditor implements IMJParserChangeObserver {
public class MJEditor extends TextEditor implements IMJParserChangeObserver,IRegisterForControllerChanges {
private boolean hasRequestedPostSaveSyntaxRefresh;
private boolean disposed = false;
......@@ -30,7 +35,14 @@ public class MJEditor extends TextEditor implements IMJParserChangeObserver {
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
super.init(site, input);
getModel().registerModelObserver(this);
getModel().registerModelObserver(this);
Controller.getInstance(this).registerObserver(this);
IDocumentProvider dp = getDocumentProvider();
if (dp == null) return;
IDocument doc = dp.getDocument(getEditorInput());
if (doc == null) return;
doc.addDocumentListener(new MJDocumentListener(this));
}
/**
......@@ -117,4 +129,15 @@ public class MJEditor extends TextEditor implements IMJParserChangeObserver {
super.dispose();
disposed = true;
}
@Override
public EnumSet<JobsToDo> getRequiredJobs() {
return EnumSet.of(JobsToDo.parseCode);
}
@Override
public void jobStatusChanged(JobStatus status) {
// do nothing.
}
}
package mjplugin.editors;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.text.rules.EndOfLineRule;
import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.MultiLineRule;
import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WordRule;
public class MJPartitionScanner extends RuleBasedPartitionScanner {
public final static String MULTILINE_COMMENT = "__multiline_comment";
public final static String SINGLE_COMMENT = "__single_comment";
public MJPartitionScanner() {
super();
//IToken cupCode = new Token(CupPartionScanner.CUP_CODE);
IToken mLComment = new Token(MJPartitionScanner.MULTILINE_COMMENT);
IToken sLComment = new Token(MJPartitionScanner.SINGLE_COMMENT);
List<IRule> rules = new ArrayList<IRule>();
// MultiLineComment
rules.add(new MultiLineRule("/*", "*/", mLComment, (char) 0, true));
// SingleLineComment
rules.add(new EndOfLineRule("//", sLComment));
// Add special case word rule.
rules.add(new WordPredicateRule(sLComment));
IPredicateRule[] result = new IPredicateRule[rules.size()];
rules.toArray(result);
setPredicateRules(result);
}
static class EmptyCommentDetector implements IWordDetector {
/* (non-Javadoc)
* Method declared on IWordDetector
*/
public boolean isWordStart(char c) {
return (c == '/');
}
/* (non-Javadoc)
* Method declared on IWordDetector
*/
public boolean isWordPart(char c) {
return (c == '*' || c == '/');
}
};
/**
*
*/
static class WordPredicateRule extends WordRule implements IPredicateRule {
private IToken fSuccessToken;
public WordPredicateRule(IToken successToken) {
super(new EmptyCommentDetector());
fSuccessToken= successToken;
addWord("/**/", fSuccessToken); //$NON-NLS-1$
}
/*
* @see org.eclipse.jface.text.rules.IPredicateRule#getSuccessToken()
*/
public IToken getSuccessToken() {
return fSuccessToken;
}
/*
* @see org.eclipse.jface.text.rules.IPredicateRule#evaluate(ICharacterScanner, boolean)
*/
public IToken evaluate(ICharacterScanner scanner, boolean resume) {
return super.evaluate(scanner);
}
};
}
......@@ -96,6 +96,9 @@ public class Model {
private long astModelRevisionNumber;
private MiniJavaAST astModel;
public MiniJavaAST getAstModel(){
return astModel;
}
public void setASTModel(MiniJavaAST result, long revisionNumber) {
this.astModel = result;
System.out.println(result.program);
......
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