KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > visualization > graphics > GFactory


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.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 JavaDoc;
45 import java.util.Collections JavaDoc;
46 import java.util.List JavaDoc;
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 JavaDoc buildGraphsForRule(EngineGrammar grammar, String JavaDoc rule, List JavaDoc<EngineGrammarError> errors) throws Exception JavaDoc {
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 JavaDoc rule) throws Exception JavaDoc {
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 JavaDoc<GGraphGroup> buildGraphsForErrors(EngineGrammar grammar, String JavaDoc rule, List JavaDoc<EngineGrammarError> errors) throws Exception JavaDoc {
91         List JavaDoc<GGraphGroup> graphs = new ArrayList JavaDoc<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         // Create one GGraph for each error rules
102
List JavaDoc<GGraph> graphs = new ArrayList JavaDoc<GGraph>();
103         FAFactory factory = new FAFactory(grammar);
104         for (String JavaDoc 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         // Add only graphs that are referenced by at least one error path.
114
// For example, the statement rule of the java.g grammar produces
115
// states that do not exist in the graph (they are after the accepted state
116
// and are ignored by the FAFactory)
117
GGraphGroup gg = new GGraphGroup();
118         for (GGraph graph : graphs) {
119             if (graph.containsAtLeastOneState(error.states))
120                 gg.add(graph);
121         }
122
123         // Attach all error paths to the GGraphGroup
124
for(int i=0; i<error.paths.size(); i++) {
125             List JavaDoc states = (List JavaDoc) error.paths.get(i);
126             Boolean JavaDoc disabled = error.pathsDisabled.get(i);
127             try {
128                 gg.addPath(states, disabled, factory.getSkippedStatesMap());
129             } catch(Exception JavaDoc e) {
130                 if(console == null)
131                     e.printStackTrace();
132                 else
133                     console.print(e);
134             }
135         }
136
137         // Attach all unreacheable alts to the GGraphGroup
138
for (Object JavaDoc[] unreachableAlt : error.unreachableAlts) {
139             gg.addUnreachableAlt((NFAState) unreachableAlt[0], (Integer JavaDoc) 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