KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > grammar > RulesDependency


1 package org.antlr.works.grammar;
2
3 import org.antlr.works.ate.syntax.misc.ATEToken;
4 import org.antlr.works.components.grammar.CEditorGrammar;
5 import org.antlr.works.syntax.element.ElementGrammarName;
6 import org.antlr.works.syntax.element.ElementReference;
7 import org.antlr.works.syntax.element.ElementRule;
8 import org.antlr.xjlib.appkit.utils.XJAlert;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.List JavaDoc;
12 /*
13
14 [The "BSD licence"]
15 Copyright (c) 2005-2006 Jean Bovet
16 All rights reserved.
17
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions
20 are met:
21
22 1. Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
24 2. Redistributions in binary form must reproduce the above copyright
25 notice, this list of conditions and the following disclaimer in the
26 documentation and/or other materials provided with the distribution.
27 3. The name of the author may not be used to endorse or promote products
28 derived from this software without specific prior written permission.
29
30 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41 */

42
43 public class RulesDependency extends GrammarDOTTab {
44
45     protected List JavaDoc<String JavaDoc> visitedRules = new ArrayList JavaDoc<String JavaDoc>();
46     protected List JavaDoc<String JavaDoc> visitedRefs = new ArrayList JavaDoc<String JavaDoc>();
47     protected StringBuffer JavaDoc dependency;
48
49     protected boolean includeLexerRefs;
50
51     public RulesDependency(CEditorGrammar editor) {
52         super(editor);
53     }
54
55     @Override JavaDoc
56     protected boolean willLaunch() {
57
58         if(!checkForCurrentRule())
59             return false;
60
61         ElementRule rule = editor.getCurrentRule();
62         List JavaDoc<ElementReference> refs = editor.rules.getReferencesInRule(rule);
63         if(refs == null || refs.isEmpty()) {
64             XJAlert.display(editor.getWindowContainer(), "Error", "The selected rule doesn't contain any references");
65             return false;
66         }
67
68         includeLexerRefs = true;
69         if(!rule.lexer && editor.getEngineGrammar().getType() == ElementGrammarName.COMBINED) {
70             includeLexerRefs = XJAlert.displayAlertYESNO(editor.getWindowContainer(), "Rule Dependency Graph", "Do you want to include lexer references ?") == XJAlert.YES;
71         }
72
73         return true;
74     }
75
76     @Override JavaDoc
77     public String JavaDoc getDOTString() throws Exception JavaDoc {
78         ElementRule rule = editor.getCurrentRule();
79
80         visitedRules.clear();
81         visitedRefs.clear();
82
83         dependency = new StringBuffer JavaDoc();
84         dependency.append("digraph {\n");
85         buildGraph(rule);
86         dependency.append("}");
87
88         return dependency.toString();
89     }
90
91     protected void buildGraph(ElementRule rule) {
92         if(rule == null)
93             return;
94
95         visitedRules.add(rule.name);
96
97         List JavaDoc<ElementReference> refs = editor.rules.getReferencesInRule(rule);
98         if(refs == null || refs.isEmpty())
99             return;
100
101         for (ElementReference reference : refs) {
102             String JavaDoc refRuleName = reference.token.getAttribute();
103             String JavaDoc visitedRef = rule.name + " -> " + refRuleName;
104
105             if (visitedRefs.contains(visitedRef))
106                 continue;
107
108             if (ATEToken.isLexerName(reference.token.getAttribute()) && !includeLexerRefs)
109                 continue;
110
111             visitedRefs.add(visitedRef);
112
113             dependency.append(visitedRef);
114             dependency.append(";\n");
115
116             if (!visitedRules.contains(refRuleName))
117                 buildGraph(editor.rules.getRuleWithName(refRuleName));
118         }
119     }
120
121     public String JavaDoc getTabName() {
122         return "Dependency of \""+rule.name+"\"";
123     }
124
125 }
126
Popular Tags