KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > DefaultToolErrorHandler


1 package antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/DefaultToolErrorHandler.java#6 $
8  */

9
10 import antlr.collections.impl.BitSet;
11
12 class DefaultToolErrorHandler implements ToolErrorHandler {
13     CharFormatter javaCharFormatter = new JavaCharFormatter();
14
15     /** Dump token/character sets to System.out
16      * @param lexicalAnalysis true for lexical rule
17      * @param depth The depth of the ambiguity
18      * @param sets An array of bitsets containing the ambiguities
19      */

20     private void dumpSets(Grammar grammar,
21                           boolean lexicalAnalysis,
22                           int depth,
23                           Lookahead[] sets,
24                           String JavaDoc linePrefix) {
25         for (int i = 1; i <= depth; i++) {
26             System.out.print(linePrefix + "\tk==" + i + ":");
27             if (lexicalAnalysis) {
28                 String JavaDoc bits = sets[i].fset.toStringWithRanges(",", javaCharFormatter);
29                 if (sets[i].containsEpsilon()) {
30                     System.out.print("<end-of-token>");
31                     if (bits.length() > 0) {
32                         System.out.print(",");
33                     }
34                 }
35                 System.out.println(bits);
36             }
37             else {
38                 System.out.println(sets[i].fset.toString(",", grammar.tokenManager.getVocabulary()));
39             }
40         }
41     }
42
43     /** Issue a warning about ambiguity between a alternates
44      * @param blk The block being analyzed
45      * @param lexicalAnalysis true for lexical rule
46      * @param depth The depth of the ambiguity
47      * @param sets An array of bitsets containing the ambiguities
48      * @param altIdx1 The zero-based index of the first ambiguous alternative
49      * @param altIdx2 The zero-based index of the second ambiguous alternative
50      */

51     public void warnAltAmbiguity(Grammar grammar,
52                                  AlternativeBlock blk,
53                                  boolean lexicalAnalysis,
54                                  int depth,
55                                  Lookahead[] sets,
56                                  int altIdx1,
57                                  int altIdx2) {
58         String JavaDoc fileline = FileLineFormatter.getFormatter().getFormatString(grammar.getFilename(), blk.getLine(), blk.getColumn());
59         if (blk instanceof RuleBlock && ((RuleBlock)blk).isLexerAutoGenRule()) {
60             System.out.print("warning: lexical nondeterminism between rules ");
61             Alternative ai = blk.getAlternativeAt(altIdx1);
62             Alternative aj = blk.getAlternativeAt(altIdx2);
63             RuleRefElement rri = (RuleRefElement)ai.head;
64             RuleRefElement rrj = (RuleRefElement)aj.head;
65             String JavaDoc ri = CodeGenerator.reverseLexerRuleName(rri.targetRule);
66             String JavaDoc rj = CodeGenerator.reverseLexerRuleName(rrj.targetRule);
67             System.out.println(ri + " and " + rj + " upon");
68             dumpSets(grammar, lexicalAnalysis, depth, sets, fileline);
69             return;
70         }
71         System.out.println(
72             // "warning: line " + blk.getLine() + ": " +
73
fileline + "warning: " +
74             (lexicalAnalysis ? "lexical " : "") + "nondeterminism upon"
75         );
76         dumpSets(grammar, lexicalAnalysis, depth, sets, fileline);
77         System.out.println(fileline + "\tbetween alts " + (altIdx1 + 1) + " and " + (altIdx2 + 1) + " of block");
78     }
79
80     /** Issue a warning about ambiguity between an alternate and exit path.
81      * @param blk The block being analyzed
82      * @param lexicalAnalysis true for lexical rule
83      * @param depth The depth of the ambiguity
84      * @param sets An array of bitsets containing the ambiguities
85      * @param altIdx The zero-based index of the ambiguous alternative
86      */

87     public void warnAltExitAmbiguity(Grammar grammar,
88                                      BlockWithImpliedExitPath blk,
89                                      boolean lexicalAnalysis,
90                                      int depth,
91                                      Lookahead[] sets,
92                                      int altIdx
93                                      ) {
94         String JavaDoc fileline = FileLineFormatter.getFormatter().getFormatString(grammar.getFilename(), blk.getLine(), blk.getColumn());
95         System.out.println(
96             // "warning: line " + blk.getLine() + ": " +
97
fileline + "warning: " +
98             (lexicalAnalysis ? "lexical " : "") + "nondeterminism upon"
99         );
100         dumpSets(grammar, lexicalAnalysis, depth, sets, fileline);
101         System.out.println(fileline + "\tbetween alt " + (altIdx + 1) + " and exit branch of block");
102     }
103 }
104
Popular Tags