001: package org.drools.eclipse.editors.scanners;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.drools.eclipse.editors.ColorManager;
007: import org.drools.eclipse.editors.Keywords;
008: import org.eclipse.jface.text.TextAttribute;
009: import org.eclipse.jface.text.rules.EndOfLineRule;
010: import org.eclipse.jface.text.rules.IRule;
011: import org.eclipse.jface.text.rules.IToken;
012: import org.eclipse.jface.text.rules.RuleBasedScanner;
013: import org.eclipse.jface.text.rules.SingleLineRule;
014: import org.eclipse.jface.text.rules.Token;
015: import org.eclipse.jface.text.rules.WhitespaceRule;
016: import org.eclipse.jface.text.rules.WordRule;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.graphics.Color;
019:
020: /**
021: * Basic keyword scanner for syntax highlighting.
022: *
023: * @author Michael Neale
024: */
025: public class DRLScanner extends RuleBasedScanner {
026:
027: private static final Color KEYWORD_COLOR = ColorManager
028: .getInstance().getColor(ColorManager.KEYWORD);
029: private static final Color COMMENT_COLOR = ColorManager
030: .getInstance().getColor(ColorManager.SINGLE_LINE_COMMENT);
031: private static final Color STRING_COLOR = ColorManager
032: .getInstance().getColor(ColorManager.STRING);
033: private static final Color DEFAULT_COLOR = ColorManager
034: .getInstance().getColor(ColorManager.DEFAULT);
035:
036: private static final String[] DROOLS_KEYWORDS = Keywords
037: .getInstance().getAllDroolsKeywords();
038: private static final String[] JAVA_KEYWORDS = Keywords
039: .getInstance().getAllJavaKeywords();
040: private static final String[] MVEL_KEYWORDS = Keywords
041: .getInstance().getAllMvelKeywords();
042:
043: private static final String[] JAVA_TYPES = { "void", "boolean",
044: "char", "byte", "short", "strictfp", "int", "long",
045: "float", "double" };
046:
047: private static final String[] JAVA_CONSTANTS = { "false", "true",
048: "null" };
049: private static final String[] MVEL_CONSTANTS = { "false", "true",
050: "null", "nil", "empty", "this" };
051:
052: public DRLScanner() {
053:
054: IToken keyword = new Token(new TextAttribute(KEYWORD_COLOR,
055: null, SWT.BOLD));
056: IToken comment = new Token(new TextAttribute(COMMENT_COLOR));
057: IToken string = new Token(new TextAttribute(STRING_COLOR));
058: IToken other = new Token(new TextAttribute(DEFAULT_COLOR));
059:
060: List rules = new ArrayList();
061:
062: rules.add(new EndOfLineRule("//", comment));
063: rules.add(new EndOfLineRule("#", comment));
064:
065: // Add rule for strings and character constants.
066: rules.add(new SingleLineRule("\"", "\"", string, '\\'));
067: rules.add(new SingleLineRule("'", "'", string, '\\'));
068:
069: //for unfilled "holes"
070: //rules.add(new SingleLineRule("{", "}", comment));
071:
072: // Add generic whitespace rule.
073: rules.add(new WhitespaceRule(new WhitespaceDetector()));
074:
075: // Add word rule for keywords, types, and constants.
076: WordRule wordRule = new WordRule(new RuleWordDetector(), other);
077: for (int i = 0; i < DROOLS_KEYWORDS.length; i++)
078: wordRule.addWord(DROOLS_KEYWORDS[i], keyword);
079:
080: for (int i = 0; i < JAVA_KEYWORDS.length; i++)
081: wordRule.addWord(JAVA_KEYWORDS[i], keyword);
082: for (int i = 0; i < JAVA_TYPES.length; i++)
083: wordRule.addWord(JAVA_TYPES[i], keyword);
084: for (int i = 0; i < JAVA_CONSTANTS.length; i++)
085: wordRule.addWord(JAVA_CONSTANTS[i], keyword);
086:
087: //FIXME: this a bit brutal. we should identify different highlighting for Java and Mvel
088: for (int i = 0; i < MVEL_KEYWORDS.length; i++)
089: wordRule.addWord(MVEL_KEYWORDS[i], keyword);
090: for (int i = 0; i < MVEL_CONSTANTS.length; i++)
091: wordRule.addWord(MVEL_CONSTANTS[i], keyword);
092:
093: rules.add(wordRule);
094:
095: IRule[] result = new IRule[rules.size()];
096: rules.toArray(result);
097:
098: setRules(result);
099: }
100:
101: }
|