1 31 32 package org.antlr.works.visualization.graphics; 33 34 import org.antlr.analysis.NFAState; 35 import org.antlr.tool.Grammar; 36 import org.antlr.works.grammar.EngineGrammar; 37 import org.antlr.works.grammar.EngineGrammarError; 38 import org.antlr.works.utils.Console; 39 import org.antlr.works.visualization.fa.FAFactory; 40 import org.antlr.works.visualization.fa.FAState; 41 import org.antlr.works.visualization.graphics.graph.GGraph; 42 import org.antlr.works.visualization.graphics.graph.GGraphGroup; 43 44 import java.util.ArrayList ; 45 import java.util.Collections ; 46 import java.util.List ; 47 48 public class GFactory { 49 50 protected GRenderer renderer = new GRenderer(); 51 protected boolean optimize = true; 52 protected Console console = null; 53 54 public GFactory() { 55 } 56 57 public void setOptimize(boolean flag) { 58 this.optimize = flag; 59 } 60 61 public void toggleNFAOptimization() { 62 optimize = !optimize; 63 } 64 65 public void setConsole(Console console) { 66 this.console = console; 67 } 68 69 public List buildGraphsForRule(EngineGrammar grammar, String rule, List <EngineGrammarError> errors) throws Exception { 70 if(grammar == null) 71 return null; 72 73 if(errors == null || errors.size() == 0) 74 return Collections.singletonList(buildGraphsForRule(grammar, rule)); 75 else 76 return buildGraphsForErrors(grammar, rule, errors); 77 } 78 79 public GGraph buildGraphsForRule(EngineGrammar grammar, String rule) throws Exception { 80 NFAState startState = grammar.getRuleStartState(rule); 81 if(startState == null) 82 return null; 83 84 FAState state = new FAFactory(grammar.getGrammarForRule(rule)).buildNFA(startState, optimize); 85 GGraph graph = renderer.render(state); 86 graph.setName(rule); 87 return graph; 88 } 89 90 public List <GGraphGroup> buildGraphsForErrors(EngineGrammar grammar, String rule, List <EngineGrammarError> errors) throws Exception { 91 List <GGraphGroup> graphs = new ArrayList <GGraphGroup>(); 92 93 for (EngineGrammarError error : errors) { 94 graphs.add(buildGraphGroup(grammar.getGrammarForRule(rule), error)); 95 } 96 97 return graphs; 98 } 99 100 private GGraphGroup buildGraphGroup(Grammar grammar, EngineGrammarError error) { 101 List <GGraph> graphs = new ArrayList <GGraph>(); 103 FAFactory factory = new FAFactory(grammar); 104 for (String rule : error.rules) { 105 NFAState startState = grammar.getRuleStartState(rule); 106 FAState state = factory.buildNFA(startState, optimize); 107 108 GGraph graph = renderer.render(state); 109 graph.setName(rule); 110 graphs.add(graph); 111 } 112 113 GGraphGroup gg = new GGraphGroup(); 118 for (GGraph graph : graphs) { 119 if (graph.containsAtLeastOneState(error.states)) 120 gg.add(graph); 121 } 122 123 for(int i=0; i<error.paths.size(); i++) { 125 List states = (List ) error.paths.get(i); 126 Boolean disabled = error.pathsDisabled.get(i); 127 try { 128 gg.addPath(states, disabled, factory.getSkippedStatesMap()); 129 } catch(Exception e) { 130 if(console == null) 131 e.printStackTrace(); 132 else 133 console.print(e); 134 } 135 } 136 137 for (Object [] unreachableAlt : error.unreachableAlts) { 139 gg.addUnreachableAlt((NFAState) unreachableAlt[0], (Integer ) unreachableAlt[1]); 140 } 141 142 if(error.paths.size() > 0) 143 gg.pathGroup.setPathVisible(0, true); 144 145 return gg; 146 } 147 148 } 149 | Popular Tags |