01: /*******************************************************************************
02: * Copyright (c) 2000, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.examples.javaeditor.java;
11:
12: import java.util.ArrayList;
13: import java.util.List;
14:
15: import org.eclipse.jface.text.TextAttribute;
16: import org.eclipse.jface.text.rules.*;
17: import org.eclipse.ui.examples.javaeditor.util.*;
18:
19: /**
20: * A Java code scanner.
21: */
22: public class JavaCodeScanner extends RuleBasedScanner {
23:
24: private static String[] fgKeywords = {
25: "abstract", "break", "case", "catch", "class", "continue", "default", "do", "else", "extends", "final", "finally", "for", "if", "implements", "import", "instanceof", "interface", "native", "new", "package", "private", "protected", "public", "return", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "volatile", "while" }; //$NON-NLS-36$ //$NON-NLS-35$ //$NON-NLS-34$ //$NON-NLS-33$ //$NON-NLS-32$ //$NON-NLS-31$ //$NON-NLS-30$ //$NON-NLS-29$ //$NON-NLS-28$ //$NON-NLS-27$ //$NON-NLS-26$ //$NON-NLS-25$ //$NON-NLS-24$ //$NON-NLS-23$ //$NON-NLS-22$ //$NON-NLS-21$ //$NON-NLS-20$ //$NON-NLS-19$ //$NON-NLS-18$ //$NON-NLS-17$ //$NON-NLS-16$ //$NON-NLS-15$ //$NON-NLS-14$ //$NON-NLS-13$ //$NON-NLS-12$ //$NON-NLS-11$ //$NON-NLS-10$ //$NON-NLS-9$ //$NON-NLS-8$ //$NON-NLS-7$ //$NON-NLS-6$ //$NON-NLS-5$ //$NON-NLS-4$ //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
26:
27: private static String[] fgTypes = {
28: "void", "boolean", "char", "byte", "short", "int", "long", "float", "double" }; //$NON-NLS-1$ //$NON-NLS-5$ //$NON-NLS-7$ //$NON-NLS-6$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-2$
29:
30: private static String[] fgConstants = { "false", "null", "true" }; //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
31:
32: /**
33: * Creates a Java code scanner with the given color provider.
34: *
35: * @param provider the color provider
36: */
37: public JavaCodeScanner(JavaColorProvider provider) {
38:
39: IToken keyword = new Token(new TextAttribute(provider
40: .getColor(JavaColorProvider.KEYWORD)));
41: IToken type = new Token(new TextAttribute(provider
42: .getColor(JavaColorProvider.TYPE)));
43: IToken string = new Token(new TextAttribute(provider
44: .getColor(JavaColorProvider.STRING)));
45: IToken comment = new Token(new TextAttribute(provider
46: .getColor(JavaColorProvider.SINGLE_LINE_COMMENT)));
47: IToken other = new Token(new TextAttribute(provider
48: .getColor(JavaColorProvider.DEFAULT)));
49:
50: List rules = new ArrayList();
51:
52: // Add rule for single line comments.
53: rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$
54:
55: // Add rule for strings and character constants.
56: rules.add(new SingleLineRule("\"", "\"", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
57: rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
58:
59: // Add generic whitespace rule.
60: rules.add(new WhitespaceRule(new JavaWhitespaceDetector()));
61:
62: // Add word rule for keywords, types, and constants.
63: WordRule wordRule = new WordRule(new JavaWordDetector(), other);
64: for (int i = 0; i < fgKeywords.length; i++)
65: wordRule.addWord(fgKeywords[i], keyword);
66: for (int i = 0; i < fgTypes.length; i++)
67: wordRule.addWord(fgTypes[i], type);
68: for (int i = 0; i < fgConstants.length; i++)
69: wordRule.addWord(fgConstants[i], type);
70: rules.add(wordRule);
71:
72: IRule[] result = new IRule[rules.size()];
73: rules.toArray(result);
74: setRules(result);
75: }
76: }
|