1 31 32 package org.antlr.works.menu; 33 34 import org.antlr.tool.Grammar; 35 import org.antlr.works.ate.syntax.misc.ATEToken; 36 import org.antlr.works.components.grammar.CEditorGrammar; 37 import org.antlr.works.grammar.CheckGrammar; 38 import org.antlr.works.grammar.CheckGrammarDelegate; 39 import org.antlr.works.grammar.RulesDependency; 40 import org.antlr.works.grammar.TokensDFA; 41 import org.antlr.works.grammar.decisiondfa.DecisionDFA; 42 import org.antlr.works.stats.StatisticsAW; 43 import org.antlr.works.syntax.GrammarSyntaxParser; 44 import org.antlr.works.syntax.element.ElementGroup; 45 import org.antlr.works.syntax.element.ElementRule; 46 import org.antlr.xjlib.appkit.utils.XJAlert; 47 import org.antlr.xjlib.appkit.utils.XJDialogProgressDelegate; 48 49 import javax.swing.*; 50 import java.util.List ; 51 52 public class MenuGrammar extends MenuAbstract implements CheckGrammarDelegate, XJDialogProgressDelegate { 53 54 protected CheckGrammar checkGrammar; 55 protected boolean checkingGrammar; 56 57 public MenuGrammar(CEditorGrammar editor) { 58 super(editor); 59 checkGrammar = new CheckGrammar(editor, this); 60 } 61 62 public void showTokensSD() { 63 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SHOW_TOKENS_SD); 64 editor.visual.setRule(new ElementRule(Grammar.ARTIFICIAL_TOKENS_RULENAME), true); 65 editor.makeBottomComponentVisible(); 66 } 67 68 public void showTokensDFA() { 69 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SHOW_TOKENS_DFA); 70 TokensDFA decision = new TokensDFA(editor); 71 decision.launch(); 72 } 73 74 public void showDecisionDFA() { 75 DecisionDFA decision = new DecisionDFA(editor); 76 decision.launch(); 77 } 78 79 public void highlightDecisionDFA() { 80 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SHOW_DECISION_DFA); 81 try { 82 if(editor.decisionDFAEngine.getDecisionDFACount() == 0) { 83 editor.decisionDFAEngine.discoverAllDecisions(); 84 } else { 85 editor.decisionDFAEngine.reset(); 86 } 87 editor.decisionDFAEngine.refresh(); 88 editor.decisionDFAEngine.refreshMenu(); 89 } catch (Exception e) { 90 e.printStackTrace(); 91 XJAlert.display(editor.getWindowContainer(), "Error", "Cannot show the DFA:\n"+e.toString()); 92 } 93 } 94 95 public void showDependency() { 96 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_SHOW_RULE_DEPENDENCY); 97 RulesDependency dependency = new RulesDependency(editor); 98 dependency.launch(); 99 } 100 101 public void insertRuleFromTemplate() { 102 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_INSERT_RULE_TEMPLATE); 103 editor.ruleTemplates.display(); 104 } 105 106 public void group() { 107 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_RULE_GROUP); 108 109 String s = (String )JOptionPane.showInputDialog(editor.getWindowContainer(), "Group Name:", "Group", 110 JOptionPane.QUESTION_MESSAGE, null, null, "Group"); 111 if(s != null && s.length() > 0) { 112 List <ElementRule> rules = editor.rules.getSelectedRules(); 113 if(!rules.isEmpty()) { 114 editor.beginGroupChange("Group"); 115 116 ElementRule firstRule = rules.get(0); 117 ElementRule lastRule = rules.get(rules.size()-1); 118 119 int end = lastRule.getEndIndex(); 120 editor.textEditor.insertText(end+1, "\n"+GrammarSyntaxParser.END_GROUP+"\n"); 121 122 int start = firstRule.getStartIndex(); 123 editor.textEditor.insertText(start-1, "\n"+ GrammarSyntaxParser.BEGIN_GROUP+s+"\n"); 124 125 editor.endGroupChange(); 126 } 127 } 128 } 129 130 public void ungroup() { 131 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_RULE_UNGROUP); 132 133 ElementGroup openGroup = editor.rules.getSelectedGroup(); 134 if(openGroup == null) { 135 openGroup = editor.rules.findOpenGroupClosestToLocation(editor.getTextPane().getSelectionStart()); 138 if(openGroup == null) { 139 XJAlert.display(editor.getWindowContainer(), "Ungroup", "Cannot ungroup because no enclosing group has been found."); 141 return; 142 } 143 } 144 145 ElementGroup closingGroup = editor.rules.findClosingGroupForGroup(openGroup); 146 147 editor.beginGroupChange("Ungroup"); 148 149 if(closingGroup != null) { 150 ATEToken t = closingGroup.token; 153 editor.replaceText(t.getStartIndex()-1, t.getEndIndex(), ""); 154 } 155 156 ATEToken t = openGroup.token; 157 editor.replaceText(t.getStartIndex()-1, t.getEndIndex(), ""); 158 159 editor.endGroupChange(); 160 } 161 162 public void ignore() { 163 editor.rules.ignoreSelectedRules(true); 164 } 165 166 public void consider() { 167 editor.rules.ignoreSelectedRules(false); 168 } 169 170 public void checkGrammar() { 171 editor.showProgress("Checking Grammar...", this); 172 173 editor.console.makeCurrent(); 174 editor.console.println("Checking Grammar..."); 175 checkGrammar.check(); 176 177 StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_CHECK_GRAMMAR); 178 } 179 180 public void checkGrammarDidBegin() { 181 checkingGrammar = true; 182 } 183 184 public void checkGrammarDidEnd(String errorMsg) { 185 checkingGrammar = false; 186 editor.hideProgress(); 187 if(errorMsg != null) { 188 XJAlert.display(editor.getWindowContainer(), "Failure", "Check Grammar failed:\n"+errorMsg+"\nConsult the console for more information."); 189 } else { 190 XJAlert.display(editor.getWindowContainer(), "Success", "Check Grammar succeeded."); 191 } 192 } 193 194 public void dialogDidCancel() { 195 if(checkingGrammar) 196 checkGrammar.cancel(); 197 } 198 } 199 | Popular Tags |