KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > grammar > decisiondfa > DecisionDFA


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

43
44 public class DecisionDFA extends GrammarDOTTab {
45
46     protected int line;
47     protected int column;
48
49     protected int decisionNumber;
50
51     public DecisionDFA(CEditorGrammar editor) {
52         super(editor);
53     }
54
55     @Override JavaDoc
56     protected boolean willLaunch() {
57         return checkForCurrentRule();
58     }
59
60     @Override JavaDoc
61     public void willRun() {
62         line = editor.getTextEditor().getCurrentLinePosition();
63         column = editor.getTextEditor().getCurrentColumnPosition();
64     }
65
66     @Override JavaDoc
67     public String JavaDoc getDOTString() throws Exception JavaDoc {
68         EngineGrammar eg = editor.getEngineGrammar();
69         eg.analyze();
70
71         Grammar g;
72
73         int adjustedColumn = getDecisionColumn(g = eg.getParserGrammar());
74         if(adjustedColumn == -1)
75             adjustedColumn = getDecisionColumn(g = eg.getLexerGrammar());
76
77         if(adjustedColumn == -1)
78             throw new Exception JavaDoc("No decision in the current line");
79
80         CodeGenerator generator = new CodeGenerator(new Tool(), g,
81                 (String JavaDoc) eg.getParserGrammar().getOption("language"));
82
83         DFA dfa = g.getLookaheadDFAFromPositionInFile(line, adjustedColumn);
84         decisionNumber = dfa.getDecisionNumber();
85         DOTGenerator dg = new DOTGenerator(g);
86         g.setCodeGenerator(generator);
87         dg.setArrowheadType("none");
88         dg.setRankdir("LR"); // Left-to-right
89
return dg.getDOT( dfa.startState );
90     }
91
92     public int getDecisionColumn(Grammar g) {
93         List JavaDoc columns = g.getLookaheadDFAColumnsForLineInFile(line);
94         int adjustedColumn = -1;
95         for(int index = columns.size()-1; index >=0; index--) {
96             Integer JavaDoc match = (Integer JavaDoc)columns.get(index);
97             if(match <= column) {
98                 adjustedColumn = match;
99                 break;
100             } else if(index == 0)
101                 adjustedColumn = match;
102         }
103         return adjustedColumn;
104     }
105
106     public String JavaDoc getTabName() {
107         return "Decision "+decisionNumber+" of \""+rule.name+"\"";
108     }
109
110 }
111
Popular Tags