Commit 67fc2add authored by petter's avatar petter

Implemented Syntax-Highlighting

parent 9ab38eec
<?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 exported="true" kind="lib" path="Compiler.jar" sourcepath="/MiniJava/src"/>
<classpathentry exported="true" kind="lib" path="java-cup-11b-runtime.jar" sourcepath="/CUP/java"/>
<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 kind="lib" path="/MiniJava/dist/Compiler.jar" sourcepath="/MiniJava/src"/>
<classpathentry kind="output" path="bin"/>
<referencedentry kind="lib" path="/MiniJava/dist/java-cup-11b-runtime.jar" sourcepath="/CUP/java"/>
</classpath>
......@@ -7,10 +7,13 @@ Bundle-Activator: mjplugin.Activator
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.ui.editors;bundle-version="3.8.200",
org.eclipse.core.resources
org.eclipse.core.resources,
org.eclipse.ui.workbench,
org.eclipse.ui.ide;bundle-version="3.10.1"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Import-Package: org.eclipse.jface.text,
org.eclipse.jface.text.presentation,
org.eclipse.jface.text.rules,
org.eclipse.jface.text.source,
org.eclipse.ui.editors.text
......
......@@ -10,6 +10,17 @@
id="MJPlugin.MJEditor"
name="MJ Editor">
</editor>
</extension>
<extension
id="minijava.problem"
name="MiniJava Problem"
point="org.eclipse.core.resources.markers">
<super
type="org.eclipse.core.resources.problemmarker">
</super>
<persistent
value="true">
</persistent>
</extension>
</plugin>
......@@ -2,15 +2,16 @@ package mjplugin.controller;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import minijava.Program;
import miniparser.InMemoryParser;
import mjplugin.ast.MiniJavaAST;
import mjplugin.controller.Controller.JobsToDo;
import mjplugin.editors.GUIErrorReporter;
import mjplugin.editors.MJEditor;
import org.eclipse.core.resources.IFile;
......@@ -19,6 +20,7 @@ import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.ui.part.FileEditorInput;
public class DocumentDidChangeJob extends Job {
......@@ -65,22 +67,28 @@ public class DocumentDidChangeJob extends Job {
return Status.CANCEL_STATUS;
}
Date parserDate = null;
Date lalrDate = null;
boolean hasParserErrors = false;
IFile file = ((FileEditorInput) editor.getEditorInput()).getFile();
GUIErrorReporter reporter = new GUIErrorReporter(file);
InMemoryParser parser = null;
//Parser parser = null;
if (jobs.contains(JobsToDo.parseCode)) {
InputStream in = new ByteArrayInputStream(codeText.getBytes());
parser = new InMemoryParser(in);
parser = new InMemoryParser(in,reporter);
result = null;
try {
parserDate = new Date();
result = new MiniJavaAST(parser.parse());
} catch (Exception e1) {
Program program = parser.parse();
result = new MiniJavaAST(program);
}catch (InMemoryParser.NoRecoveryException nre){
hasParserErrors = true;
}catch(ClassCastException cce){
hasParserErrors = true;
}
catch (Exception e1) {
e1.printStackTrace();
}
......@@ -88,6 +96,7 @@ public class DocumentDidChangeJob extends Job {
//if (parser.hasParseErrors())
// hasParserErrors = true;
jobStatusList.add(new JobStatus(JobsToDo.parseCode, false));
ParserResultJob resultModelJob = new ParserResultJob(editor,
result, revNumber);
......@@ -104,7 +113,8 @@ public class DocumentDidChangeJob extends Job {
jobStatusList.add(new JobStatus(JobsToDo.parseCode, true));
}
}
reporter.commitErrors();
documentEvents.clear();
jobs.clear();
......
package mjplugin.editors;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.widgets.Display;
import java_cup.runtime.ComplexSymbolFactory.Location;
import miniparser.InMemoryParser.IFrontendObserver;
public class GUIErrorReporter implements IFrontendObserver {
private static final String MINIJAVA_MARKER = "minijava.problem";
public static class Message {
public Message(String message, Location left, Location right) {
this.message = message;
this.left = left;
this.right = right;
}
public String message;
public Location left, right;
}
private List<Message> errors = new LinkedList<>();
private IFile file;
public void commitErrors() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
IMarker[] oldMarkers;
try {
oldMarkers = file.findMarkers(MINIJAVA_MARKER, false, 10);
for (IMarker m : oldMarkers) m.delete();
for (Message m : errors) {
IMarker marker;
marker = file.createMarker(MINIJAVA_MARKER);
marker.setAttribute(IMarker.TRANSIENT, true);
marker.setAttribute(IMarker.SEVERITY,IMarker.SEVERITY_ERROR);
marker.setAttribute(IMarker.MESSAGE, m.message);
marker.setAttribute(IMarker.LOCATION, m.left.toString());
marker.setAttribute(IMarker.LINE_NUMBER,m.left.getLine());
marker.setAttribute(IMarker.CHAR_START, m.left.getOffset());
marker.setAttribute(IMarker.CHAR_END,m.right.getOffset());
}
} catch (CoreException e) {
e.printStackTrace();
}
}
});
}
public GUIErrorReporter(IFile file) {
this.file = file;
}
@Override
public void report(String message, Location left, Location right,List expected) {
errors.add(new Message(message, left, right));
}
@Override
public void fatal(String message, Location left, Location right) {
errors.add(new Message(message, left, right));
}
public void clear() {
errors.clear();
}
}
......@@ -15,6 +15,8 @@ import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
......@@ -26,9 +28,46 @@ public class MJEditor extends TextEditor implements IMJParserChangeObserver,IReg
private boolean hasRequestedPostSaveSyntaxRefresh;
private boolean disposed = false;
private static Color gray;
private static Color blue;
private static Color brightBlue;
private static Color green;
private static Color orange;
private static Color purple;
private static Color blueGreen;
private static Color red;
private static Color pink;
private static Color brown;
static {
Display display = Display.getCurrent();
gray = new Color (display, 150, 150, 150);
blue = new Color (display, 53, 87, 197);
brightBlue = new Color (display, 38, 99, 236);
green = new Color (display, 44, 173, 57);
orange = new Color (display, 220, 150, 50);
purple = new Color (display, 177, 33, 114);
blueGreen = new Color (display, 43, 126, 136);
red = new Color (display, 255, 0, 0);
pink = new Color (display, 255, 0, 255);
brown = new Color (display, 132, 90, 49);
}
public static final Color HYPERLINK = blue;
public static final Color KEYWORD = blue;
public static final Color COMMENT = gray;
public static final Color TERMINAL = orange;
public static final Color NONTERMINAL = green;
public static final Color SPECIAL_ERROR_TERMINAL = purple;
public static final Color STRING = red;
public static final Color IDENTIFIER = pink;
public MJEditor() {
super();
setSourceViewerConfiguration(new MJSourceViewerConfiguration(this));
}
@Override
......
package mjplugin.editors;
import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
import java.util.List;
import java_cup.runtime.ComplexSymbolFactory;
import java_cup.runtime.Symbol;
import miniparser.Lexer;
import miniparser.sym;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.ITokenScanner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.SWT;
public class MJSourceViewerConfiguration extends SourceViewerConfiguration {
private MJEditor editor;
public MJSourceViewerConfiguration(MJEditor mjEditor) {
this.editor=mjEditor;
}
@Override
public PresentationReconciler getPresentationReconciler(
ISourceViewer sourceViewer) {
PresentationReconciler pr = new PresentationReconciler();
DefaultDamagerRepairer ddr = new DefaultDamagerRepairer(new TokenScanner());
pr.setRepairer(ddr, IDocument.DEFAULT_CONTENT_TYPE);
pr.setDamager(ddr, IDocument.DEFAULT_CONTENT_TYPE);
return pr;
}
static class TokenScanner implements ITokenScanner {
private Lexer scanner;
private ComplexSymbolFactory csf = new ComplexSymbolFactory();
private int offset;
private Symbol last;
@Override
public void setRange(IDocument document, int offset, int length) {
scanner = new Lexer(new StringReader(document.get()
.substring(offset,offset+length)
),csf);
last=null;
this.offset=offset;
}
static final Token keyword = new Token(new TextAttribute(MJEditor.KEYWORD, null, SWT.BOLD));
static final Token literal = new Token(new TextAttribute(MJEditor.STRING));
static final Token comment = new Token(new TextAttribute(MJEditor.COMMENT));
static final Token identifier = new Token(new TextAttribute(MJEditor.IDENTIFIER));
private List<Integer> keywords = Arrays.asList(sym.IF, sym.ELSE, sym.WHILE,sym.TYPE);
private List<Integer> constants = Arrays.asList(sym.INTCONST,sym.STRINGCONST);
@Override
public IToken nextToken() {
try {
last = scanner.next_token();
} catch (IOException e) {
last=null;
return Token.EOF;
}
if (last.sym== sym.EOF){
return Token.EOF;
}
if (keywords.contains(last.sym))
return keyword;
if (constants.contains(last.sym))
return literal;
return Token.UNDEFINED; }
@Override
public int getTokenOffset() {
return last.left+offset;
}
@Override
public int getTokenLength() {
return last.right-last.left;
}
}
}
......@@ -101,7 +101,7 @@ public class Model {
}
public void setASTModel(MiniJavaAST result, long revisionNumber) {
this.astModel = result;
System.out.println(result.program);
System.out.println("propagated new AST model");
this.astModelRevisionNumber = revisionNumber;
notifyObservers(IMJParserChangeObserver.class);
}
......
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