KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > syntax > GrammarSyntaxEngine


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

31
32 package org.antlr.works.syntax;
33
34 import org.antlr.works.ate.syntax.generic.ATESyntaxLexer;
35 import org.antlr.works.ate.syntax.generic.ATESyntaxParser;
36 import org.antlr.works.ate.syntax.language.ATELanguageSyntaxEngine;
37 import org.antlr.works.ate.syntax.misc.ATEToken;
38 import org.antlr.works.prefs.AWPrefs;
39 import org.antlr.works.syntax.element.*;
40
41 import javax.swing.text.AttributeSet JavaDoc;
42 import javax.swing.text.SimpleAttributeSet JavaDoc;
43 import javax.swing.text.StyleConstants JavaDoc;
44 import java.awt.*;
45 import java.util.ArrayList JavaDoc;
46 import java.util.List JavaDoc;
47
48 public class GrammarSyntaxEngine extends ATELanguageSyntaxEngine {
49
50     public static final Color COLOR_PARSER = new Color(0.42f, 0, 0.42f);
51     public static final Color COLOR_LEXER = new Color(0, 0, 0.5f);
52
53     protected List JavaDoc<ElementRule> rules;
54     protected List JavaDoc<ElementGroup> groups;
55     protected List JavaDoc<ElementBlock> blocks;
56     protected List JavaDoc<ElementAction> actions;
57     protected List JavaDoc<ElementReference> references;
58     protected List JavaDoc<ATEToken> decls;
59
60     protected ElementGrammarName name;
61
62     protected SimpleAttributeSet JavaDoc parserRefAttr;
63     protected SimpleAttributeSet JavaDoc lexerRefAttr;
64     protected SimpleAttributeSet JavaDoc labelAttr;
65     protected SimpleAttributeSet JavaDoc actionRefAttr;
66     // todo prefs for that
67
protected SimpleAttributeSet JavaDoc blockLabelAttr;
68
69     public GrammarSyntaxEngine() {
70         parserRefAttr = new SimpleAttributeSet JavaDoc();
71         lexerRefAttr = new SimpleAttributeSet JavaDoc();
72         labelAttr = new SimpleAttributeSet JavaDoc();
73         actionRefAttr = new SimpleAttributeSet JavaDoc();
74         blockLabelAttr = new SimpleAttributeSet JavaDoc();
75     }
76
77     public void applyCommentAttribute(SimpleAttributeSet JavaDoc commentAttr) {
78         applyAttribute(commentAttr, AWPrefs.PREF_SYNTAX_COMMENT);
79     }
80
81     public void applyStringAttribute(SimpleAttributeSet JavaDoc stringAttr) {
82         applyAttribute(stringAttr, AWPrefs.PREF_SYNTAX_STRING);
83     }
84
85     public void applyKeywordAttribute(SimpleAttributeSet JavaDoc keywordAttr) {
86         applyAttribute(keywordAttr, AWPrefs.PREF_SYNTAX_KEYWORD);
87     }
88
89     public void applyAttribute(SimpleAttributeSet JavaDoc attr, String JavaDoc identifier) {
90         StyleConstants.setForeground(attr, AWPrefs.getSyntaxColor(identifier));
91         StyleConstants.setBold(attr, AWPrefs.getSyntaxBold(identifier));
92         StyleConstants.setItalic(attr, AWPrefs.getSyntaxItalic(identifier));
93     }
94
95     public ATESyntaxLexer createLexer() {
96         return new GrammarSyntaxLexer();
97     }
98
99     public ATESyntaxParser createParser() {
100         return new GrammarSyntaxParser();
101     }
102
103     public void refreshColoring() {
104         super.refreshColoring();
105
106         applyAttribute(parserRefAttr, AWPrefs.PREF_SYNTAX_PARSER);
107         applyAttribute(lexerRefAttr, AWPrefs.PREF_SYNTAX_LEXER);
108         applyAttribute(labelAttr, AWPrefs.PREF_SYNTAX_LABEL);
109         applyAttribute(actionRefAttr, AWPrefs.PREF_SYNTAX_REFS);
110
111         StyleConstants.setBold(blockLabelAttr, true);
112     }
113
114     public AttributeSet JavaDoc getAttributeForToken(ATEToken token) {
115         AttributeSet JavaDoc attr = super.getAttributeForToken(token);
116         switch(token.type) {
117             case GrammarSyntaxLexer.TOKEN_DECL:
118             case GrammarSyntaxLexer.TOKEN_REFERENCE:
119                 if(((ElementToken)token).lexer)
120                     attr = lexerRefAttr;
121                 else
122                     attr = parserRefAttr;
123                 break;
124
125             case GrammarSyntaxLexer.TOKEN_LABEL:
126                 attr = labelAttr;
127                 break;
128
129             case GrammarSyntaxLexer.TOKEN_BLOCK_LIMIT:
130             case GrammarSyntaxLexer.TOKEN_BLOCK_LABEL:
131                 attr = blockLabelAttr;
132                 break;
133
134             case GrammarSyntaxLexer.TOKEN_INTERNAL_REF:
135                 attr = actionRefAttr;
136                 break;
137         }
138         return attr;
139     }
140
141     public static void setDelay(int delay) {
142         GrammarSyntaxEngine.delay = delay;
143     }
144
145     public synchronized List JavaDoc<ElementRule> getRules() {
146         return rules;
147     }
148
149     public synchronized List JavaDoc<ElementGroup> getGroups() {
150         return groups;
151     }
152
153     public synchronized List JavaDoc<ElementBlock> getBlocks() {
154         return blocks;
155     }
156
157     public synchronized List JavaDoc<ElementAction> getActions() {
158         return actions;
159     }
160
161     public synchronized List JavaDoc<ElementReference> getReferences() {
162         return references;
163     }
164
165     public synchronized List JavaDoc<ATEToken> getDecls() {
166         return decls;
167     }
168
169     public synchronized ElementGrammarName getName() {
170         return name;
171     }
172
173     public synchronized List JavaDoc<String JavaDoc> getDeclaredTokenNames() {
174         List JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>();
175         if(blocks != null) {
176             for(int index=0; index<blocks.size(); index++) {
177                 ElementBlock block = blocks.get(index);
178                 if(block.isTokenBlock) {
179                     names.addAll(block.getDeclaredTokensAsString());
180                 }
181             }
182         }
183         return names;
184     }
185
186     public List JavaDoc<String JavaDoc> getPredefinedReferences() {
187         return GrammarSyntaxParser.predefinedReferences;
188     }
189
190     public synchronized String JavaDoc getTokenVocab() {
191         if(blocks == null)
192             return null;
193
194         for(int index=0; index<blocks.size(); index++) {
195             ElementBlock block = blocks.get(index);
196             if(block.isOptionsBlock)
197                 return block.getTokenVocab();
198         }
199         return null;
200     }
201
202     public synchronized List JavaDoc<String JavaDoc> getRuleNames() {
203         List JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>();
204         if(rules != null) {
205             for (int index=0; index<rules.size(); index++) {
206                 ElementRule rule = rules.get(index);
207                 names.add(rule.name);
208             }
209         }
210         return names;
211     }
212
213     public synchronized ElementRule getRuleAtIndex(int index) {
214         if(index < 0 || index >= rules.size())
215             return null;
216         else
217             return rules.get(index);
218     }
219
220     /** Cache the attributes of the parser so we can use them later
221      * even if the parser is running again.
222      */

223
224     protected synchronized void parserDidRun(ATESyntaxParser parser) {
225         GrammarSyntaxParser gp = (GrammarSyntaxParser)parser;
226         this.rules = new ArrayList JavaDoc<ElementRule>(gp.rules);
227         this.groups = new ArrayList JavaDoc<ElementGroup>(gp.groups);
228         this.blocks = new ArrayList JavaDoc<ElementBlock>(gp.blocks);
229         this.actions = new ArrayList JavaDoc<ElementAction>(gp.actions);
230         this.references = new ArrayList JavaDoc<ElementReference>(gp.references);
231         this.decls = new ArrayList JavaDoc<ATEToken>(gp.decls);
232         this.name = gp.name;
233     }
234
235 }
236
Popular Tags