1 31 32 package org.antlr.works.visualization; 33 34 import org.antlr.analysis.NFAState; 35 import org.antlr.works.ate.syntax.misc.ATEThread; 36 import org.antlr.works.prefs.AWPrefs; 37 import org.antlr.works.syntax.element.ElementRule; 38 import org.antlr.works.utils.Console; 39 import org.antlr.works.utils.ErrorListener; 40 import org.antlr.works.visualization.graphics.GFactory; 41 42 import java.util.HashMap ; 43 import java.util.List ; 44 import java.util.Map ; 45 46 public class VisualDrawing extends ATEThread { 47 48 protected Visual visual; 49 50 protected GFactory factory = new GFactory(); 51 52 protected String text; 53 protected ElementRule rule; 54 55 protected String threadText; 56 protected ElementRule threadRule; 57 protected ElementRule threadLastProcessedRule; 58 59 protected Map <ElementRule,List > cacheGraphs = new HashMap <ElementRule, List >(); 60 61 public VisualDrawing(Visual visual) { 62 this.visual = visual; 63 start(); 64 } 65 66 public void toggleNFAOptimization() { 67 factory.toggleNFAOptimization(); 68 clearCacheGraphs(); 69 } 70 71 public synchronized void setText(String text) { 72 this.text = text; 73 awakeThread(500); 74 } 75 76 public synchronized void setRule(ElementRule rule, boolean immediate) { 77 this.rule = rule; 78 awakeThread(immediate?0:500); 79 } 80 81 public synchronized void clearCacheGraphs() { 82 cacheGraphs.clear(); 83 } 84 85 88 public synchronized boolean refresh() { 89 List graphs = cacheGraphs.get(threadLastProcessedRule); 90 if(graphs == null || graphs.isEmpty()) { 91 return false; 92 } else { 93 visual.panel.setRule(threadLastProcessedRule); 94 visual.panel.setGraphs(graphs); 95 visual.panel.update(); 96 return true; 97 } 98 } 99 100 public synchronized boolean threadShouldProcess() { 101 return text != null || rule != null; 102 } 103 104 public synchronized void threadPrepareProcess() { 105 this.threadText = text; 106 this.threadRule = rule; 107 108 text = null; 109 rule = null; 110 } 111 112 private void threadProcessText() { 113 if(threadText == null) 114 return; 115 116 ErrorListener.shared().setPrintToConsole(false); 117 try { 118 visual.getEngineGrammar().createGrammars(); 119 } catch (Exception e) { 120 } finally { 123 clearCacheGraphs(); 125 } 126 } 127 128 private void threadProcessRule() throws Exception { 129 if(threadRule == null) 130 return; 131 132 String error = null; 133 134 ErrorListener.shared().setPrintToConsole(false); 135 136 if(visual.getEngineGrammar().hasGrammar()) { 137 NFAState startState = null; 138 try { 139 startState = visual.getEngineGrammar().getRuleStartState(threadRule.name); 140 144 } catch (Exception e) { 145 } 148 if(startState == null) 149 error = "Cannot display rule \"" + threadRule + "\" because start state not found"; 150 } else { 151 error = "Cannot display rule \""+threadRule+"\" because grammar cannot be generated"; 152 } 153 154 if(error != null) { 155 visual.setPlaceholder(error); 156 visual.getConsole().println(error, Console.LEVEL_ERROR); 157 return; 158 } 159 160 createGraphsForRule(threadRule); 163 164 threadLastProcessedRule = threadRule; 165 166 refresh(); 167 } 168 169 protected synchronized void createGraphsForRule(ElementRule rule) throws Exception { 170 List graphs = cacheGraphs.get(rule); 171 if(graphs == null) { 172 factory.setOptimize(!AWPrefs.getDebugDontOptimizeNFA()); 173 factory.setConsole(visual.getConsole()); 174 graphs = factory.buildGraphsForRule(visual.getEngineGrammar(), rule.name, rule.errors); 175 if(graphs != null) 176 cacheGraphs.put(rule, graphs); 177 } 178 } 179 180 public void threadReportException(Exception e) { 181 visual.getConsole().print(e); 182 } 183 184 public void threadRun() throws Exception { 185 visual.getConsole().setMode(Console.MODE_QUIET); 186 187 if(threadShouldProcess()) { 188 threadPrepareProcess(); 189 190 threadProcessText(); 192 193 threadProcessRule(); 195 } 196 } 197 198 } 199 | Popular Tags |