KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > ate > syntax > language > ATELanguageSyntaxEngine


1 package org.antlr.works.ate.syntax.language;
2
3 import org.antlr.works.ate.syntax.generic.ATESyntaxEngine;
4 import org.antlr.works.ate.syntax.generic.ATESyntaxLexer;
5 import org.antlr.works.ate.syntax.generic.ATESyntaxParser;
6 import org.antlr.works.ate.syntax.misc.ATEToken;
7
8 import javax.swing.text.AttributeSet JavaDoc;
9 import javax.swing.text.SimpleAttributeSet JavaDoc;
10 import javax.swing.text.StyleConstants JavaDoc;
11 import java.awt.*;
12 import java.util.Set JavaDoc;
13 /*
14
15 [The "BSD licence"]
16 Copyright (c) 2005-2006 Jean Bovet
17 All rights reserved.
18
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions
21 are met:
22
23 1. Redistributions of source code must retain the above copyright
24 notice, this list of conditions and the following disclaimer.
25 2. Redistributions in binary form must reproduce the above copyright
26 notice, this list of conditions and the following disclaimer in the
27 documentation and/or other materials provided with the distribution.
28 3. The name of the author may not be used to endorse or promote products
29 derived from this software without specific prior written permission.
30
31 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41
42 */

43
44 public class ATELanguageSyntaxEngine extends ATESyntaxEngine {
45
46     protected SimpleAttributeSet JavaDoc commentAttr;
47     protected SimpleAttributeSet JavaDoc stringAttr;
48     protected SimpleAttributeSet JavaDoc keywordAttr;
49
50     public ATELanguageSyntaxEngine() {
51         commentAttr = new SimpleAttributeSet JavaDoc();
52         stringAttr = new SimpleAttributeSet JavaDoc();
53         keywordAttr = new SimpleAttributeSet JavaDoc();
54     }
55
56     public void applyCommentAttribute(SimpleAttributeSet JavaDoc commentAttr) {
57         StyleConstants.setForeground(commentAttr, Color.lightGray);
58         StyleConstants.setItalic(commentAttr, true);
59     }
60
61     public void applyStringAttribute(SimpleAttributeSet JavaDoc stringAttr) {
62         StyleConstants.setForeground(stringAttr, new Color(0, 0.5f, 0));
63         StyleConstants.setBold(stringAttr, true);
64     }
65
66     public void applyKeywordAttribute(SimpleAttributeSet JavaDoc keywordAttr) {
67         StyleConstants.setForeground(keywordAttr, new Color(0, 0, 0.5f));
68         StyleConstants.setBold(keywordAttr, true);
69     }
70
71     public void refreshColoring() {
72         applyCommentAttribute(commentAttr);
73         applyStringAttribute(stringAttr);
74         applyKeywordAttribute(keywordAttr);
75     }
76
77     public ATESyntaxLexer createLexer() {
78         return new ATELanguageSyntaxLexer();
79     }
80
81     public ATESyntaxParser createParser() {
82         // No parser need for generic computer language
83
return null;
84     }
85
86     /** Returns the set of keyword for the language.
87      * Note: this method is called very often
88      *
89      * @return The set of keywords
90      */

91     public Set JavaDoc<String JavaDoc> getKeywords() {
92         return null;
93     }
94
95     public AttributeSet JavaDoc getAttributeForToken(ATEToken token) {
96         AttributeSet JavaDoc attr = null;
97         switch(token.type) {
98             case ATESyntaxLexer.TOKEN_COMPLEX_COMMENT:
99             case ATESyntaxLexer.TOKEN_SINGLE_COMMENT:
100                 attr = commentAttr;
101                 break;
102             case ATESyntaxLexer.TOKEN_DOUBLE_QUOTE_STRING:
103             case ATESyntaxLexer.TOKEN_SINGLE_QUOTE_STRING:
104                 attr = stringAttr;
105                 break;
106             default:
107                 Set JavaDoc<String JavaDoc> s = getKeywords();
108                 if(s != null && s.contains(token.getAttribute()))
109                     attr = keywordAttr;
110                 break;
111         }
112         return attr;
113     }
114
115 }
116
Popular Tags