Commit dfc2c545 authored by Clemens Pflaum's avatar Clemens Pflaum

Syntax highlighting

parent e0d615ec
......@@ -83,6 +83,7 @@ public class CupTextEditor extends TextEditor
public static final Color JAVA_KEYWORD = brown;
public static final Color JAVA_TEXTUAL = brightBlue;
public static final Color JAVA_CUP_DEFINED = blueGreen;
public static final Color JAVA_NUMBER = purple;
public CupTextEditor(Jumper jumper) {
super();
......
package de.tum.in.www2.cupplugin.syntax;
import java.io.StringReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.stream.IntStream;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.compiler.IScanner;
import org.eclipse.jdt.core.compiler.ITerminalSymbols;
import org.eclipse.jdt.core.compiler.InvalidInputException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextAttribute;
......@@ -26,10 +32,66 @@ import de.tum.in.www2.cupplugin.editors.CupTextEditor;
public class JavaTokenScanner implements ITokenScanner {
private ParserResult ast;
private JavaScanner scanner;
private IScanner scanner;
private JavaSymbol last;
private int offset;
private Range previousCodeBlockRange;
private int[] keywords = {
ITerminalSymbols.TokenNameabstract,
ITerminalSymbols.TokenNameassert,
ITerminalSymbols.TokenNameboolean,
ITerminalSymbols.TokenNamebreak,
ITerminalSymbols.TokenNamebyte,
ITerminalSymbols.TokenNamecase,
ITerminalSymbols.TokenNamecatch,
ITerminalSymbols.TokenNamechar,
ITerminalSymbols.TokenNameclass,
ITerminalSymbols.TokenNameconst,
ITerminalSymbols.TokenNamecontinue,
ITerminalSymbols.TokenNamedefault,
ITerminalSymbols.TokenNamedo,
ITerminalSymbols.TokenNamedouble,
ITerminalSymbols.TokenNameelse,
ITerminalSymbols.TokenNameenum,
ITerminalSymbols.TokenNameextends,
ITerminalSymbols.TokenNamefinal,
ITerminalSymbols.TokenNamefinally,
ITerminalSymbols.TokenNamefloat,
ITerminalSymbols.TokenNamefor,
ITerminalSymbols.TokenNamegoto,
ITerminalSymbols.TokenNameif,
ITerminalSymbols.TokenNameimplements,
ITerminalSymbols.TokenNameimport,
ITerminalSymbols.TokenNameinstanceof,
ITerminalSymbols.TokenNameint,
ITerminalSymbols.TokenNameinterface,
ITerminalSymbols.TokenNamelong,
ITerminalSymbols.TokenNamenative,
ITerminalSymbols.TokenNamenew,
ITerminalSymbols.TokenNamepackage,
ITerminalSymbols.TokenNameprivate,
ITerminalSymbols.TokenNameprotected,
ITerminalSymbols.TokenNamepublic,
ITerminalSymbols.TokenNamereturn,
ITerminalSymbols.TokenNameshort,
ITerminalSymbols.TokenNamestatic,
ITerminalSymbols.TokenNamestrictfp,
ITerminalSymbols.TokenNamesuper,
ITerminalSymbols.TokenNameswitch,
ITerminalSymbols.TokenNamesynchronized,
ITerminalSymbols.TokenNamethis,
ITerminalSymbols.TokenNamethrow,
ITerminalSymbols.TokenNamethrows,
ITerminalSymbols.TokenNametransient,
ITerminalSymbols.TokenNametry,
ITerminalSymbols.TokenNamevoid,
ITerminalSymbols.TokenNamevolatile,
ITerminalSymbols.TokenNamewhile,
ITerminalSymbols.TokenNametrue,
ITerminalSymbols.TokenNamefalse,
ITerminalSymbols.TokenNamenull};
private HashSet<String> currentLabels = null;
......@@ -76,10 +138,11 @@ public class JavaTokenScanner implements ITokenScanner {
}
public void setRange(IDocument document, int offset, int length) {
StringReader reader = new StringReader(document.get().substring(
offset, offset + length));
this.scanner = new JavaScanner(reader);
this.scanner = ToolFactory.createScanner(true, false, false, false);
this.scanner.setSource(document.get().substring(
offset, offset + length).toCharArray());
this.last = null;
this.offset = offset;
......@@ -123,55 +186,69 @@ public class JavaTokenScanner implements ITokenScanner {
CupTextEditor.COMMENT));
static final Token javaKeyword = new Token(new TextAttribute(
CupTextEditor.JAVA_KEYWORD));
CupTextEditor.JAVA_KEYWORD, null, SWT.BOLD));
static final Token javaTextualLiteral = new Token(new TextAttribute(
CupTextEditor.JAVA_TEXTUAL));
static final Token javaNumberLiteral = new Token(new TextAttribute(
CupTextEditor.JAVA_NUMBER));
static final Token cupDefinedVariable = new Token(new TextAttribute(
CupTextEditor.JAVA_CUP_DEFINED, null, SWT.ITALIC));
public IToken nextToken() {
int token;
try {
last = scanner.next_token();
} catch (Exception e) {
last = null;
return Token.EOF;
} catch (Error err) {
last = null;
token = this.scanner.getNextToken();
} catch (InvalidInputException e) {
return Token.EOF;
}
if (last.isEOF())
if (token == ITerminalSymbols.TokenNameEOF)
return Token.EOF;
if (last.isComment())
// TODO: remove debugcode
// System.out.println(token + " : " + new String(scanner.getCurrentTokenSource())
// + "\t\t offset: " + scanner.getCurrentTokenStartPosition() + "/" + scanner.getCurrentTokenEndPosition());
if(token == ITerminalSymbols.TokenNameCOMMENT_BLOCK
|| token == ITerminalSymbols.TokenNameCOMMENT_JAVADOC
|| token == ITerminalSymbols.TokenNameCOMMENT_LINE)
return javaComment;
if (last.isKeyword() || last.isBooleanLiteral()
|| last.isNullLiteral())
if(IntStream.of(keywords).anyMatch(x -> x == token))
return javaKeyword;
if (last.isTextualLiteral())
if(token == ITerminalSymbols.TokenNameStringLiteral
|| token == ITerminalSymbols.TokenNameCharacterLiteral)
return javaTextualLiteral;
if (last.getValue() instanceof String) {
String lastValueStr = (String) last.getValue();
if (lastValueStr.equals("RESULT")
|| currentLabels.contains(lastValueStr))
return cupDefinedVariable;
if(token == ITerminalSymbols.TokenNameDoubleLiteral
|| token == ITerminalSymbols.TokenNameFloatingPointLiteral
|| token == ITerminalSymbols.TokenNameIntegerLiteral
|| token == ITerminalSymbols.TokenNameLongLiteral)
return javaNumberLiteral;
// TODO: highlight cupVariables
if((new String(scanner.getCurrentTokenSource())).equals("RESULT")){
return cupDefinedVariable;
}
// if (last.getValue() instanceof String) {
// String lastValueStr = (String) last.getValue();
// if (lastValueStr.equals("RESULT")
// || currentLabels.contains(lastValueStr))
// return cupDefinedVariable;
// }
return Token.UNDEFINED;
}
public int getTokenOffset() {
return this.offset + (last.getLeft());
return this.offset + scanner.getCurrentTokenStartPosition();
}
public int getTokenLength() {
return last.getRight() - last.getLeft() + 1;
return scanner.getCurrentTokenEndPosition() - scanner.getCurrentTokenStartPosition() + 1;
}
}
\ No newline at end of file
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