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