001: package antlr;
002:
003: /* ANTLR Translator Generator
004: * Project led by Terence Parr at http://www.cs.usfca.edu
005: * Software rights: http://www.antlr.org/license.html
006: */
007:
008: import antlr.collections.impl.BitSet;
009:
010: class DefaultToolErrorHandler implements ToolErrorHandler {
011: DefaultToolErrorHandler(antlr.Tool tool) {
012: antlrTool = tool;
013: }
014:
015: private final antlr.Tool antlrTool;
016:
017: CharFormatter javaCharFormatter = new JavaCharFormatter();
018:
019: /** Dump token/character sets to a string array suitable for
020: * {@link antlr.Tool.warning(String[], String, int, int)
021: * @param output The array that will contain the token/character set dump,
022: * one element per k (lookahead) value
023: * @param outputStartIndex The index into <code>output</code> that the
024: * dump should start at.
025: * @param lexicalAnalysis true for lexical rule
026: * @param depth The depth of the ambiguity
027: * @param sets An array of bitsets containing the ambiguities
028: */
029: private void dumpSets(String[] output, int outputStartIndex,
030: Grammar grammar, boolean lexicalAnalysis, int depth,
031: Lookahead[] sets) {
032: StringBuffer line = new StringBuffer(100);
033: for (int i = 1; i <= depth; i++) {
034: line.append("k==").append(i).append(':');
035: if (lexicalAnalysis) {
036: String bits = sets[i].fset.toStringWithRanges(",",
037: javaCharFormatter);
038: if (sets[i].containsEpsilon()) {
039: line.append("<end-of-token>");
040: if (bits.length() > 0) {
041: line.append(',');
042: }
043: }
044: line.append(bits);
045: } else {
046: line.append(sets[i].fset.toString(",",
047: grammar.tokenManager.getVocabulary()));
048: }
049: output[outputStartIndex++] = line.toString();
050: line.setLength(0);
051: }
052: }
053:
054: /** Issue a warning about ambiguity between a alternates
055: * @param blk The block being analyzed
056: * @param lexicalAnalysis true for lexical rule
057: * @param depth The depth of the ambiguity
058: * @param sets An array of bitsets containing the ambiguities
059: * @param altIdx1 The zero-based index of the first ambiguous alternative
060: * @param altIdx2 The zero-based index of the second ambiguous alternative
061: */
062: public void warnAltAmbiguity(Grammar grammar, AlternativeBlock blk,
063: boolean lexicalAnalysis, int depth, Lookahead[] sets,
064: int altIdx1, int altIdx2) {
065: final StringBuffer line = new StringBuffer(100);
066: if (blk instanceof RuleBlock
067: && ((RuleBlock) blk).isLexerAutoGenRule()) {
068: Alternative ai = blk.getAlternativeAt(altIdx1);
069: Alternative aj = blk.getAlternativeAt(altIdx2);
070: RuleRefElement rri = (RuleRefElement) ai.head;
071: RuleRefElement rrj = (RuleRefElement) aj.head;
072: String ri = CodeGenerator
073: .reverseLexerRuleName(rri.targetRule);
074: String rj = CodeGenerator
075: .reverseLexerRuleName(rrj.targetRule);
076: line.append("lexical nondeterminism between rules ");
077: line.append(ri).append(" and ").append(rj).append(" upon");
078: } else {
079: if (lexicalAnalysis) {
080: line.append("lexical ");
081: }
082: line.append("nondeterminism between alts ");
083: line.append(altIdx1 + 1).append(" and ");
084: line.append(altIdx2 + 1).append(" of block upon");
085: }
086: final String[] output = new String[depth + 1];
087: ;
088: output[0] = line.toString();
089: dumpSets(output, 1, grammar, lexicalAnalysis, depth, sets);
090: antlrTool.warning(output, grammar.getFilename(), blk.getLine(),
091: blk.getColumn());
092:
093: }
094:
095: /** Issue a warning about ambiguity between an alternate and exit path.
096: * @param blk The block being analyzed
097: * @param lexicalAnalysis true for lexical rule
098: * @param depth The depth of the ambiguity
099: * @param sets An array of bitsets containing the ambiguities
100: * @param altIdx The zero-based index of the ambiguous alternative
101: */
102: public void warnAltExitAmbiguity(Grammar grammar,
103: BlockWithImpliedExitPath blk, boolean lexicalAnalysis,
104: int depth, Lookahead[] sets, int altIdx) {
105: String[] output = new String[depth + 2];
106: output[0] = (lexicalAnalysis ? "lexical " : "")
107: + "nondeterminism upon";
108: dumpSets(output, 1, grammar, lexicalAnalysis, depth, sets);
109: output[depth + 1] = "between alt " + (altIdx + 1)
110: + " and exit branch of block";
111: antlrTool.warning(output, grammar.getFilename(), blk.getLine(),
112: blk.getColumn());
113: }
114: }
|