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 ; 13 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 56 protected boolean willLaunch() { 57 return checkForCurrentRule(); 58 } 59 60 @Override 61 public void willRun() { 62 line = editor.getTextEditor().getCurrentLinePosition(); 63 column = editor.getTextEditor().getCurrentColumnPosition(); 64 } 65 66 @Override 67 public String getDOTString() throws Exception { 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 ("No decision in the current line"); 79 80 CodeGenerator generator = new CodeGenerator(new Tool(), g, 81 (String ) 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"); return dg.getDOT( dfa.startState ); 90 } 91 92 public int getDecisionColumn(Grammar g) { 93 List columns = g.getLookaheadDFAColumnsForLineInFile(line); 94 int adjustedColumn = -1; 95 for(int index = columns.size()-1; index >=0; index--) { 96 Integer match = (Integer )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 getTabName() { 107 return "Decision "+decisionNumber+" of \""+rule.name+"\""; 108 } 109 110 } 111 | Popular Tags |