KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > menu > MenuGenerate


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.menu;
33
34 import org.antlr.xjlib.appkit.utils.XJAlert;
35 import org.antlr.works.components.grammar.CEditorGrammar;
36 import org.antlr.works.generate.CodeDisplay;
37 import org.antlr.works.generate.CodeGenerate;
38 import org.antlr.works.generate.CodeGenerateDelegate;
39 import org.antlr.works.grammar.CheckGrammar;
40 import org.antlr.works.grammar.CheckGrammarDelegate;
41 import org.antlr.works.prefs.AWPrefs;
42 import org.antlr.works.stats.StatisticsAW;
43 import org.antlr.works.syntax.element.ElementGrammarName;
44 import org.antlr.works.syntax.element.ElementRule;
45
46 public class MenuGenerate extends MenuAbstract implements CodeGenerateDelegate, CheckGrammarDelegate {
47
48     public CodeGenerate generateCode = null;
49     protected CheckGrammar checkGrammar;
50
51     protected String JavaDoc actionShowCodeRule;
52     protected int actionShowCodeType;
53     protected boolean actionShowCodeAfterGeneration = false;
54
55     public MenuGenerate(CEditorGrammar editor) {
56         super(editor);
57         generateCode = new CodeGenerate(editor, this);
58         checkGrammar = new CheckGrammar(editor, this);
59     }
60
61     public void generateCode() {
62         actionShowCodeRule = null;
63         generateCodeProcess();
64     }
65
66     protected void generateCodeProcess() {
67         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_GENERATE_CODE);
68
69         if(!editor.ensureDocumentSaved())
70             return;
71
72         checkGrammar.check();
73     }
74
75     protected void generateCodeProcessContinued() {
76         if(!editor.getDocument().performAutoSave())
77             return;
78
79         generateCode.setDebug(false);
80         generateCode.setOutputPath(AWPrefs.getOutputPath());
81         generateCode.generateInThread(editor.getJavaContainer());
82     }
83
84     public boolean checkLanguage() {
85         if(!isKnownLanguage()) {
86             XJAlert.display(editor.getWindowContainer(), "Error", "Can only show generated grammar for Java language");
87             return false;
88         } else
89             return true;
90     }
91
92     public boolean isKnownLanguage() {
93         String JavaDoc language = generateCode.getGrammarLanguage();
94         return language != null && language.equals("Java");
95     }
96
97     public void showGeneratedCode(int type) {
98         StatisticsAW.shared().recordEvent(type==ElementGrammarName.LEXER?StatisticsAW.EVENT_SHOW_LEXER_GENERATED_CODE:StatisticsAW.EVENT_SHOW_PARSER_GENERATED_CODE);
99
100         if(type == ElementGrammarName.LEXER) {
101             if(!generateCode.supportsLexer()) {
102                 XJAlert.display(editor.getWindowContainer(), "Error", "Cannot generate the lexer because there is no lexer in this grammar.");
103                 return;
104             }
105         } else {
106             if(!generateCode.supportsParser()) {
107                 XJAlert.display(editor.getWindowContainer(), "Error", "Cannot generate the parser because there is no parser in this grammar.");
108                 return;
109             }
110         }
111
112         checkAndShowGeneratedCode(null, type);
113     }
114
115     public void showRuleGeneratedCode() {
116         StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SHOW_RULE_GENERATED_CODE);
117
118         if(editor.getCurrentRule() == null) {
119             XJAlert.display(editor.getWindowContainer(), "Error", "A rule must be selected first.");
120         } else {
121             ElementRule r = editor.getCurrentRule();
122             checkAndShowGeneratedCode(r.name, r.lexer?ElementGrammarName.LEXER:ElementGrammarName.PARSER);
123         }
124     }
125
126     public void checkAndShowGeneratedCode(String JavaDoc rule, int type) {
127         if(!checkLanguage())
128             return;
129
130         if(!generateCode.isGeneratedTextFileExisting(type)
131                 || generateCode.isFileModifiedSinceLastGeneration()
132                 || editor.getDocument().isDirty()) {
133             // Generate automatically the code and call again
134
// this method (using actionShowCodeRule as flag)
135
actionShowCodeRule = rule;
136             actionShowCodeType = type;
137             actionShowCodeAfterGeneration = true;
138             generateCodeProcess();
139             return;
140         }
141
142         showGeneratedCode(rule, type);
143     }
144
145     private void showGeneratedCode(String JavaDoc rule, int type) {
146         CodeDisplay dc = new CodeDisplay(editor.getXJFrame());
147         String JavaDoc title;
148         try {
149             title = generateCode.getGeneratedClassName(type)+".java";
150         } catch (Exception JavaDoc e) {
151             XJAlert.display(editor.getWindowContainer(), "Error", "Cannot cannot get the name of the generated file:\n"+e.toString());
152             return;
153         }
154
155         String JavaDoc text;
156         try {
157             text = generateCode.getGeneratedText(type);
158         } catch (Exception JavaDoc e) {
159             XJAlert.display(editor.getWindowContainer(), "Error", "Exception while reading the generated file:\n"+e.toString());
160             return;
161         }
162
163         if(rule != null) {
164             int startIndex = text.indexOf("$ANTLR start "+rule);
165             startIndex = text.indexOf("\n", startIndex)+1;
166             int stopIndex = text.indexOf("$ANTLR end "+rule);
167             while(stopIndex>0 && text.charAt(stopIndex) != '\n')
168                 stopIndex--;
169
170             if(startIndex >= 0 && stopIndex >= 0) {
171                 text = text.substring(startIndex, stopIndex);
172                 title = rule;
173             } else {
174                 XJAlert.display(editor.getWindowContainer(), "Error", "Cannot find markers for rule \""+rule+"\"");
175                 return;
176             }
177         }
178         dc.setText(text);
179         dc.setTitle(title);
180
181         editor.addTab(dc);
182         editor.makeBottomComponentVisible();
183     }
184
185     public boolean codeGenerateDisplaySuccess() {
186         return !actionShowCodeAfterGeneration;
187     }
188
189     public void codeGenerateDidComplete() {
190         if(actionShowCodeAfterGeneration) {
191             actionShowCodeAfterGeneration = false;
192             showGeneratedCode(actionShowCodeRule, actionShowCodeType);
193         }
194     }
195
196     public void checkGrammarDidBegin() {
197         // do nothing
198
}
199
200     public void checkGrammarDidEnd(String JavaDoc errorMsg) {
201         if(errorMsg != null) {
202             XJAlert.display(editor.getWindowContainer(), "Failure", "Check Grammar failed:\n"+errorMsg+"\nConsult the console for more information.");
203         } else {
204             generateCodeProcessContinued();
205         }
206     }
207 }
208
Popular Tags