KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > DefaultToolErrorHandler


1 package persistence.antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

8
9 import persistence.antlr.collections.impl.BitSet;
10
11 class DefaultToolErrorHandler implements ToolErrorHandler {
12     DefaultToolErrorHandler(persistence.antlr.Tool tool) {
13         antlrTool = tool;
14     }
15     private final persistence.antlr.Tool antlrTool;
16
17     CharFormatter javaCharFormatter = new JavaCharFormatter();
18
19     /** Dump token/character sets to a string array suitable for
20      * {@link persistence.antlr.Tool.warning(String[], String, int, int)
21      * @param output The array that will contain the token/character set dump,
22      * one element per k (lookahead) value
23      * @param outputStartIndex The index into <code>output</code> that the
24      * dump should start at.
25      * @param lexicalAnalysis true for lexical rule
26      * @param depth The depth of the ambiguity
27      * @param sets An array of bitsets containing the ambiguities
28      */

29     private void dumpSets(String JavaDoc[] output,
30                           int outputStartIndex,
31                           Grammar grammar,
32                           boolean lexicalAnalysis,
33                           int depth,
34                           Lookahead[] sets) {
35         StringBuffer JavaDoc line = new StringBuffer JavaDoc(100);
36         for (int i = 1; i <= depth; i++) {
37             line.append("k==").append(i).append(':');
38             if (lexicalAnalysis) {
39                 String JavaDoc bits = sets[i].fset.toStringWithRanges(",", javaCharFormatter);
40                 if (sets[i].containsEpsilon()) {
41                     line.append("<end-of-token>");
42                     if (bits.length() > 0) {
43                         line.append(',');
44                     }
45                 }
46                 line.append(bits);
47             } else {
48                 line.append(sets[i].fset.toString(",", grammar.tokenManager.getVocabulary()));
49             }
50             output[outputStartIndex++] = line.toString();
51             line.setLength(0);
52         }
53     }
54
55     /** Issue a warning about ambiguity between a alternates
56      * @param blk The block being analyzed
57      * @param lexicalAnalysis true for lexical rule
58      * @param depth The depth of the ambiguity
59      * @param sets An array of bitsets containing the ambiguities
60      * @param altIdx1 The zero-based index of the first ambiguous alternative
61      * @param altIdx2 The zero-based index of the second ambiguous alternative
62      */

63     public void warnAltAmbiguity(Grammar grammar,
64                                  AlternativeBlock blk,
65                                  boolean lexicalAnalysis,
66                                  int depth,
67                                  Lookahead[] sets,
68                                  int altIdx1,
69                                  int altIdx2)
70     {
71         final StringBuffer JavaDoc line = new StringBuffer JavaDoc(100);
72         if (blk instanceof RuleBlock && ((RuleBlock)blk).isLexerAutoGenRule()) {
73             Alternative ai = blk.getAlternativeAt(altIdx1);
74             Alternative aj = blk.getAlternativeAt(altIdx2);
75             RuleRefElement rri = (RuleRefElement)ai.head;
76             RuleRefElement rrj = (RuleRefElement)aj.head;
77             String JavaDoc ri = CodeGenerator.reverseLexerRuleName(rri.targetRule);
78             String JavaDoc rj = CodeGenerator.reverseLexerRuleName(rrj.targetRule);
79             line.append("lexical nondeterminism between rules ");
80             line.append(ri).append(" and ").append(rj).append(" upon");
81         }
82         else {
83             if (lexicalAnalysis) {
84                 line.append("lexical ");
85             }
86             line.append("nondeterminism between alts ");
87             line.append(altIdx1 + 1).append(" and ");
88             line.append(altIdx2 + 1).append(" of block upon");
89         }
90         final String JavaDoc [] output = new String JavaDoc [depth + 1];;
91         output[0] = line.toString();
92         dumpSets(output, 1, grammar, lexicalAnalysis, depth, sets);
93         antlrTool.warning(output, grammar.getFilename(), blk.getLine(), blk.getColumn());
94
95     }
96
97     /** Issue a warning about ambiguity between an alternate and exit path.
98      * @param blk The block being analyzed
99      * @param lexicalAnalysis true for lexical rule
100      * @param depth The depth of the ambiguity
101      * @param sets An array of bitsets containing the ambiguities
102      * @param altIdx The zero-based index of the ambiguous alternative
103      */

104     public void warnAltExitAmbiguity(Grammar grammar,
105                                      BlockWithImpliedExitPath blk,
106                                      boolean lexicalAnalysis,
107                                      int depth,
108                                      Lookahead[] sets,
109                                      int altIdx
110                                      )
111     {
112         String JavaDoc [] output = new String JavaDoc[depth + 2];
113         output[0] = (lexicalAnalysis ? "lexical " : "") + "nondeterminism upon";
114         dumpSets(output, 1, grammar, lexicalAnalysis, depth, sets);
115         output[depth + 1] = "between alt " + (altIdx + 1) + " and exit branch of block";
116         antlrTool.warning(output, grammar.getFilename(), blk.getLine(), blk.getColumn());
117     }
118 }
119
Popular Tags