1	package antlr;
2	
3	/**
4	 * <b>SOFTWARE RIGHTS</b>
5	 * <p>
6	 * ANTLR 2.5.0 MageLang Institute, 1998
7	 * <p>
8	 * We reserve no legal rights to the ANTLR--it is fully in the
9	 * public domain. An individual or company may do whatever
10	 * they wish with source code distributed with ANTLR or the
11	 * code generated by ANTLR, including the incorporation of
12	 * ANTLR, or its output, into commerical software.
13	 * <p>
14	 * We encourage users to develop software with ANTLR. However,
15	 * we do ask that credit is given to us for developing
16	 * ANTLR. By "credit", we mean that if you use ANTLR or
17	 * incorporate any source code into one of your programs
18	 * (commercial product, research project, or otherwise) that
19	 * you acknowledge this fact somewhere in the documentation,
20	 * research report, etc... If you like ANTLR and have
21	 * developed a nice tool with the output, please mention that
22	 * you developed it using ANTLR. In addition, we ask that the
23	 * headers remain intact in our source code. As long as these
24	 * guidelines are kept, we expect to continue enhancing this
25	 * system and expect to make other tools available as they are
26	 * completed.
27	 * <p>
28	 * The ANTLR gang:
29	 * @version ANTLR 2.5.0 MageLang Institute, 1998
30	 * @author Terence Parr, <a href=http://www.MageLang.com>MageLang Institute</a>
31	 * @author <br>John Lilley, <a href=http://www.Empathy.com>Empathy Software</a>
32	 */
33	import antlr.collections.impl.BitSet;
34	
35	class DefaultToolErrorHandler implements ToolErrorHandler {
36		CharFormatter javaCharFormatter = new JavaCharFormatter();
37	
38	
39		public DefaultToolErrorHandler() {
40		}
41		/** Dump token/character sets to System.out
42		 * @param lexicalAnalysis  true for lexical rule
43		 * @param depth  The depth of the ambiguity
44		 * @param sets  An array of bitsets containing the ambiguities
45		 */
46		private void dumpSets(Grammar grammar, boolean lexicalAnalysis, int depth, Lookahead[] sets) {
47			for (int i = 1; i <= depth; i++) {
48				System.out.print("\tk==" + i + ":");
49				if (lexicalAnalysis) {
50					String bits = sets[i].fset.toStringWithRanges(",", javaCharFormatter);
51					if ( sets[i].containsEpsilon() ) {
52						System.out.print("<end-of-token>");
53						if ( bits.length()>0 ) {
54							System.out.print(",");
55						}	
56					}
57					System.out.println(bits);
58				}
59				else {
60					System.out.println(sets[i].fset.toString(",", grammar.tokenManager.getVocabulary()));
61				}
62			}
63		}
64		/** Issue a warning about ambiguity between a alternates
65		 * @param blk  The block being analyzed
66		 * @param lexicalAnalysis  true for lexical rule
67		 * @param depth  The depth of the ambiguity
68		 * @param sets  An array of bitsets containing the ambiguities
69		 * @param altIdx1  The zero-based index of the first ambiguous alternative
70		 * @param altIdx2  The zero-based index of the second ambiguous alternative
71		 */
72		public void warnAltAmbiguity(
73			Grammar grammar,
74			AlternativeBlock blk,
75			boolean lexicalAnalysis,
76			int depth,
77			Lookahead[] sets,
78			int altIdx1,
79			int altIdx2)
80		{
81			if ( blk instanceof RuleBlock && ((RuleBlock)blk).isLexerAutoGenRule() ) {
82				System.out.print("warning: lexical nondeterminism between rules ");
83				Alternative ai = blk.getAlternativeAt(altIdx1);
84				Alternative aj = blk.getAlternativeAt(altIdx2);
85				RuleRefElement rri = (RuleRefElement)ai.head;
86				RuleRefElement rrj = (RuleRefElement)aj.head;
87				String ri = CodeGenerator.reverseLexerRuleName(rri.targetRule);
88				String rj = CodeGenerator.reverseLexerRuleName(rrj.targetRule);
89				System.out.println(ri+" and "+rj+" upon");
90				dumpSets(grammar, lexicalAnalysis, depth, sets);
91				return;
92			}	
93			System.out.println(
94				"warning: line " + blk.getLine() + ": " +
95				(lexicalAnalysis ? "lexical " : "") + "nondeterminism upon"
96			);
97			dumpSets(grammar, lexicalAnalysis, depth, sets);
98			System.out.println("\tbetween alts " + (altIdx1+1) + " and " + (altIdx2+1) + " of block");
99		}
100		/** Issue a warning about ambiguity between an alternate and exit path.
101		 * @param blk  The block being analyzed
102		 * @param lexicalAnalysis  true for lexical rule
103		 * @param depth  The depth of the ambiguity
104		 * @param sets  An array of bitsets containing the ambiguities
105		 * @param altIdx  The zero-based index of the ambiguous alternative
106		 */
107		public void warnAltExitAmbiguity(
108			Grammar grammar,
109			BlockWithImpliedExitPath blk,
110			boolean lexicalAnalysis,
111			int depth,
112			Lookahead[] sets,
113			int altIdx
114		) {
115			System.out.println(
116				"warning: line " + blk.getLine() + ": " +
117				(lexicalAnalysis ? "lexical " : "") + "nondeterminism upon"
118			);
119			dumpSets(grammar, lexicalAnalysis, depth, sets);
120			System.out.println("\tbetween alt " + (altIdx+1) + " and exit branch of block");
121		}
122	}
123