Commit 540d7fa5 authored by Johannes Roith's avatar Johannes Roith
Browse files

Java syntax highlighting support.

parent ede1d6df
......@@ -9,6 +9,7 @@ import de.in.tum.www2.cup.IErrorReporter;
import de.in.tum.www2.cup.ErrorManager;
import java_cup.runtime.ComplexSymbolFactory;
import java_cup.runtime.ComplexSymbolFactory.ComplexSymbol;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Lexer;
public class JavaScanner
......@@ -33,20 +34,16 @@ public class JavaScanner
//
// Finally, there are types. These are not colored in eclipse anyway.
/*
private Lexer lexer;
public JavaScanner(IErrorReporter er, Reader r) {
ComplexSymbolFactory factory = new ComplexSymbolFactory();
ErrorManager errMan = new ErrorManager(er);
this.lexer = new Lexer(errMan, factory, r);
public JavaScanner(Reader r) {
this.lexer = new Lexer(r);
}
public JavaSymbol next_token() throws IOException {
ComplexSymbol symbol = (ComplexSymbol) lexer.next_token();
Symbol symbol = lexer.next_token();
return new JavaSymbol(symbol);
}
*/
}
package de.in.tum.www2.java;
import java.util.HashSet;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
import java_cup.runtime.Symbol;
public class JavaSymbol {
private Symbol s;
public Object getValue() {
if (s == null)
return null;
return s.value;
}
public int getSymbolId() {
if (s == null)
return -1;
return s.sym;
}
// TODO: why not take this from xleft?
public int getLeft() {
if (s == null)
return -1;
return s.left;
}
// TODO: why not take this from xright?
public int getRight() {
if (s == null)
return -1;
return s.right;
}
private static final HashSet<Integer> keywords = new HashSet<Integer>();
static {
keywords.add(Sym.ABSTRACT);
keywords.add(Sym.ASSERT);
keywords.add(Sym.BOOLEAN);
keywords.add(Sym.BYTE);
keywords.add(Sym.BYTE);
keywords.add(Sym.CASE);
keywords.add(Sym.CATCH);
keywords.add(Sym.CHAR);
keywords.add(Sym.CLASS);
keywords.add(Sym.CONST);
keywords.add(Sym.CONTINUE);
keywords.add(Sym.DEFAULT);
keywords.add(Sym.DO);
keywords.add(Sym.DOUBLE);
keywords.add(Sym.ELSE);
keywords.add(Sym.ENUM);
keywords.add(Sym.EXTENDS);
keywords.add(Sym.FINAL);
keywords.add(Sym.FINALLY);
keywords.add(Sym.FLOAT);
keywords.add(Sym.FOR);
keywords.add(Sym.GOTO);
keywords.add(Sym.IF);
keywords.add(Sym.IMPLEMENTS);
keywords.add(Sym.IMPORT);
keywords.add(Sym.INSTANCEOF);
keywords.add(Sym.INT);
keywords.add(Sym.INTERFACE);
keywords.add(Sym.LONG);
keywords.add(Sym.NATIVE);
keywords.add(Sym.NEW);
keywords.add(Sym.PACKAGE);
keywords.add(Sym.PRIVATE);
keywords.add(Sym.PROTECTED);
keywords.add(Sym.PUBLIC);
keywords.add(Sym.RETURN);
keywords.add(Sym.SHORT);
keywords.add(Sym.STATIC);
keywords.add(Sym.STRICTFP);
keywords.add(Sym.SUPER);
keywords.add(Sym.SWITCH);
keywords.add(Sym.SYNCHRONIZED);
keywords.add(Sym.THIS);
keywords.add(Sym.THROW);
keywords.add(Sym.THROWS);
keywords.add(Sym.TRANSIENT);
keywords.add(Sym.TRY);
keywords.add(Sym.VOID);
keywords.add(Sym.VOLATILE);
keywords.add(Sym.WHILE);
}
public boolean isKeyword() {
if (s != null)
return keywords.contains(s.sym);
return false;
}
public JavaSymbol(Symbol s) {
this.s = s;
}
public boolean isEOF() {
return getSymbolId() == Sym.EOF;
}
}
......@@ -25,12 +25,17 @@ import de.in.tum.www2.cup.CupSymbol;
import de.in.tum.www2.cup.Declarations;
import de.in.tum.www2.cup.NoopErrorReporter;
import de.in.tum.www2.cup.ast.ParserResult;
import de.in.tum.www2.cup.internal.sym;
import de.in.tum.www2.java.JavaScanner;
import de.in.tum.www2.java.JavaSymbol;
import de.tum.in.www2.cupplugin.model.CupParserASTChangeObserver;
import de.tum.in.www2.cupplugin.model.Model;
public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
CupTextEditor editor;
private static final int AUTO_ACTIVATION_DELAY = 500;
public CupSourceViewerConfiguration(CupTextEditor editor) {
this.editor = editor;
}
......@@ -50,7 +55,7 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
IDocument.DEFAULT_CONTENT_TYPE);
assistant.enableAutoActivation(true);
assistant.setAutoActivationDelay(500);
assistant.setAutoActivationDelay(AUTO_ACTIVATION_DELAY);
assistant
.setProposalPopupOrientation(IContentAssistant.CONTEXT_INFO_BELOW);
assistant
......@@ -60,19 +65,17 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
public class DeclarationPresentationObserver implements CupParserASTChangeObserver
{
private TokenScanner tokenScanner;
private CupTokenScanner tokenScanner;
private ISourceViewer sourceViewer;
public DeclarationPresentationObserver(
TokenScanner tokenScanner,
CupTokenScanner tokenScanner,
ISourceViewer sourceViewer) {
this.tokenScanner = tokenScanner;
this.sourceViewer = sourceViewer;
// TODO: correct to get the doc like that?
IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
Model model = Model.getInstanceForDocument(doc);
Model model = editor.getModel();
model.registerModelObserver(this);
// TODO: this model will likely be empty on first call?!
......@@ -101,13 +104,13 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
public PresentationReconciler getPresentationReconciler(
ISourceViewer sourceViewer) {
PresentationReconciler pr = new PresentationReconciler();
TokenScanner tokenScanner = new TokenScanner();
CupTokenScanner cupTokenScanner = new CupTokenScanner();
// will observe new declarations and trigger rescan of document
new DeclarationPresentationObserver(tokenScanner, sourceViewer);
CupDamagerRepairer cupDamagerRepairer = new CupDamagerRepairer(tokenScanner);
new DeclarationPresentationObserver(cupTokenScanner, sourceViewer);
CupDamagerRepairer cupDamagerRepairer = new CupDamagerRepairer(cupTokenScanner);
pr.setRepairer(cupDamagerRepairer, IDocument.DEFAULT_CONTENT_TYPE);
pr.setDamager(cupDamagerRepairer, IDocument.DEFAULT_CONTENT_TYPE);
......@@ -127,6 +130,8 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
pr.setDamager(sLCommentDamagerRepairer,
CupPartionScanner.SINGLE_COMMENT);
JavaTokenScanner javaTokenScanner = new JavaTokenScanner();
// DefaultDamagerRepairer javaCommentDamagerRepairer = new
// DefaultDamagerRepairer(new SingleTokenScanner(new
// TextAttribute(CupTextEditor.KEYWORD)));
......@@ -135,19 +140,67 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
// CupPartionScanner.JAVA_CODE);
// pr.setDamager(javaCommentDamagerRepairer,
// CupPartionScanner.JAVA_CODE);
// JavaColorProvider provider = new JavaColorProvider();
// DefaultDamagerRepairer javaDamagerRepairer = new
// DefaultDamagerRepairer(new JavaCodeScanner(provider));
// pr.setRepairer(javaDamagerRepairer, CupPartionScanner.JAVA_CODE);
// pr.setDamager(javaDamagerRepairer, CupPartionScanner.JAVA_CODE);
CupDamagerRepairer javaDamagerRepairer = new CupDamagerRepairer(javaTokenScanner);
pr.setRepairer(javaDamagerRepairer, CupPartionScanner.JAVA_CODE);
pr.setDamager(javaDamagerRepairer, CupPartionScanner.JAVA_CODE);
return pr;
}
static class JavaTokenScanner implements ITokenScanner {
private JavaScanner scanner;
private JavaSymbol last;
private int offset;
public JavaTokenScanner () {
}
public void setRange(IDocument document, int offset, int length) {
StringReader reader = new StringReader(document.get().substring(
offset, offset + length));
scanner = new JavaScanner(reader);
this.last = null;
this.offset = offset;
}
static final Token javaKeyword = new Token(new TextAttribute(
CupTextEditor.JAVA_KEYWORD));
public IToken nextToken() {
try {
last = scanner.next_token();
} catch (IOException e) {
last = null;
return Token.EOF;
}
if (last.isEOF())
return Token.EOF;
if (last.isKeyword())
return javaKeyword;
return Token.UNDEFINED;
}
public int getTokenOffset() {
return this.offset + (last.getLeft());
}
public int getTokenLength() {
return last.getRight() - last.getLeft() + 1;
}
}
// TODO: move the TokenScanner into a separate file, along with a custom
// presentation reconciler.
static class TokenScanner implements ITokenScanner {
static class CupTokenScanner implements ITokenScanner {
private CupScanner scanner;
private CupSymbol last;
......@@ -163,7 +216,7 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
this.decls = decls;
}
public TokenScanner () {
public CupTokenScanner () {
constants = new HashSet<Integer>();
}
......
......@@ -36,6 +36,7 @@ public class CupTextEditor extends TextEditor {
static Color purple;
static Color red;
static Color pink;
static Color brown;
static {
Display display = Display.getCurrent();
......@@ -48,6 +49,8 @@ public class CupTextEditor extends TextEditor {
purple = new Color (display, 177, 33, 114);
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 KEYWORD = blue;
......@@ -57,6 +60,7 @@ public class CupTextEditor extends TextEditor {
public static final Color SPECIAL_ERROR_TERMINAL = purple;
public static final Color STRING = red;
public static final Color IDENTIFIER = pink;
public static final Color JAVA_KEYWORD = brown;
public CupTextEditor() {
super();
......
Supports Markdown
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