| 1 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 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 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 rule, int type) { 127 if(!checkLanguage()) 128 return; 129 130 if(!generateCode.isGeneratedTextFileExisting(type) 131 || generateCode.isFileModifiedSinceLastGeneration() 132 || editor.getDocument().isDirty()) { 133 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 rule, int type) { 146 CodeDisplay dc = new CodeDisplay(editor.getXJFrame()); 147 String title; 148 try { 149 title = generateCode.getGeneratedClassName(type)+".java"; 150 } catch (Exception 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 text; 156 try { 157 text = generateCode.getGeneratedText(type); 158 } catch (Exception 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 } 199 200 public void checkGrammarDidEnd(String 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 |