Commit 238678ee authored by Sebastian Pretscher's avatar Sebastian Pretscher

Added some syntax highlighting

parent 6c80d6b9
......@@ -3,7 +3,5 @@
<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="/CupParser/lib/java-cup-11b-runtime.jar"/>
<classpathentry kind="lib" path="/CupParser/bin/jar/CupParser.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
......@@ -10,6 +10,8 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.jface.text,
org.eclipse.core.resources,
org.eclipse.ui.editors,
org.eclipse.ui.ide
org.eclipse.ui.ide,
CupReferencedLibraries;bundle-version="1.0.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Bundle-ClassPath: .
......@@ -4,3 +4,4 @@ bin.includes = plugin.xml,\
META-INF/,\
.,\
icons/
jars.compile.order = .
......@@ -13,6 +13,7 @@
name="Cup Multipage Editor">
</editor>
</extension>
<!--
<extension
point="org.eclipse.core.contenttype.contentTypes">
<content-type
......@@ -24,6 +25,7 @@
</content-type>
</extension>
-->
<extension
id="CupDocumentSetupParticipant"
point="org.eclipse.core.filebuffers.documentSetup">
......
package de.tum.in.www2.cupplugin.editors;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.TextPresentation;
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;
public class CupDamagerRepairer extends DefaultDamagerRepairer {
public CupDamagerRepairer(ITokenScanner scanner) {
super(scanner);
}
@Deprecated
public CupDamagerRepairer(ITokenScanner scanner,
TextAttribute defaultTextAttribute) {
super(scanner, defaultTextAttribute);
}
@Override
public void createPresentation(TextPresentation presentation, ITypedRegion region) {
if (fScanner == null) {
// will be removed if deprecated constructor will be removed
addRange(presentation, region.getOffset(), region.getLength(), fDefaultTextAttribute);
return;
}
int lastStart= region.getOffset();
int length= 0;
boolean firstToken= true;
IToken lastToken= Token.UNDEFINED;
TextAttribute lastAttribute= getTokenTextAttribute(lastToken);
fScanner.setRange(fDocument, lastStart, region.getLength());
while (true) {
IToken token= fScanner.nextToken();
if (token.isEOF())
break;
TextAttribute attribute= getTokenTextAttribute(token);
/*
if (lastAttribute != null && lastAttribute.equals(attribute)) {
length += fScanner.getTokenLength();
firstToken= false;
} else {
*/
if (!firstToken)
addRange(presentation, lastStart, length, lastAttribute);
firstToken= false;
lastToken= token;
lastAttribute= attribute;
lastStart= fScanner.getTokenOffset();
length= fScanner.getTokenLength();
//}
}
addRange(presentation, lastStart, length, lastAttribute);
}
}
package de.tum.in.www2.cupplugin.editors;
import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
import java.util.List;
import java_cup.Lexer;
import java_cup.sym;
import java_cup.runtime.ComplexSymbolFactory;
import java_cup.runtime.ComplexSymbolFactory.ComplexSymbol;
import java_cup.runtime.Symbol;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.TextSelection;
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 CupSourceViewerConfiguration extends SourceViewerConfiguration {
CupTextEditor editor;
public CupSourceViewerConfiguration(CupTextEditor editor) {
this.editor = editor;
}
public PresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
PresentationReconciler pr = new PresentationReconciler();
CupDamagerRepairer defaultRepairer = new CupDamagerRepairer(new TokenScanner());
pr.setRepairer(defaultRepairer, IDocument.DEFAULT_CONTENT_TYPE);
pr.setDamager(defaultRepairer, IDocument.DEFAULT_CONTENT_TYPE);
return pr;
}
static class TokenScanner implements ITokenScanner {
Lexer scanner;
ComplexSymbolFactory csf = new ComplexSymbolFactory();
int offset;
ComplexSymbol last;
static final Token keyword = new Token(new TextAttribute(CupTextEditor.KEYWORD, null, SWT.BOLD));
static final Token literal = new Token(new TextAttribute(CupTextEditor.STRING));
private List<Integer> keywords = Arrays.asList(sym.IMPORT, sym.PACKAGE, sym.TERMINAL, sym.NONTERMINAL, sym.NON, sym.PRECEDENCE, sym.INIT, sym.CODE);
private List<Integer> constants = Arrays.asList();
@Override
public void setRange(IDocument document, int offset, int length) {
scanner = new Lexer(csf, new StringReader(document.get()
.substring(offset,offset+length)));
this.last=null;
this.offset=offset;
}
@Override
public IToken nextToken() {
try {
last = (ComplexSymbol)scanner.next_token();
} catch (IOException e) {
last = null;
return Token.EOF;
}
if (last.sym== sym.EOF){
return Token.EOF;
} else if (keywords.contains(last.sym)) {
return keyword;
} else if (constants.contains(last.sym)) {
return literal;
} else {
return Token.UNDEFINED;
}
}
@Override
public int getTokenOffset() {
int mOffset = this.offset + last.xleft.getColumn() - 1;
return mOffset;
}
@Override
public int getTokenLength() {
int length = last.xright.getColumn()-last.xleft.getColumn()+1;
return length;
}
}
}
......@@ -4,6 +4,7 @@ import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
......@@ -14,9 +15,18 @@ import java_cup.runtime.ComplexSymbolFactory;
public class CupTextEditor extends TextEditor {
static Color blue = new Color (Display.getCurrent(), 0, 0, 255);
static Color green = new Color (Display.getCurrent(), 0, 255, 0);
static Color red = new Color (Display.getCurrent(), 255, 0, 0);
static Color pink = new Color (Display.getCurrent(), 255, 0, 255);
public static final Color KEYWORD = blue;
public static final Color COMMENT = green;
public static final Color STRING = red;
public static final Color IDENTIFIER = pink;
public CupTextEditor() {
super();
setSourceViewerConfiguration(new CupSourceViewerConfiguration(this));
// ComplexSymbolFactory f = new ComplexSymbolFactory();
// java_cup.Lexer = new java_cup.Lexer(arg0, f);
}
......
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<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 exported="true" kind="lib" path=""/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>CupReferencedLibraries</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: CupReferencedLibraries
Bundle-SymbolicName: CupReferencedLibraries
Bundle-Version: 1.0.0
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: java_cup,
java_cup.runtime
source.. = .
output.. = .
bin.includes = META-INF/,\
java_cup/
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