protected String getErrorMessage(RecognitionException re) {
    String message = "";

    Parser recognizer = (Parser) re.getRecognizer();
    TokenStream tokens = recognizer.getInputStream();

    if (re instanceof NoViableAltException) {
      NoViableAltException e = (NoViableAltException) re;
      Token startToken = e.getStartToken();
      String input =
          (startToken.getType() == Token.EOF)
              ? "end of text"
              : quote(tokens.getText(startToken, e.getOffendingToken()));

      message = "no viable date format found at " + input;
    } else if (re instanceof InputMismatchException) {
      InputMismatchException e = (InputMismatchException) re;
      message =
          "did not expect "
              + getTokenDisplayString(e.getOffendingToken())
              + " while looking for "
              + e.getExpectedTokens().toString(recognizer.getTokenNames());
    } else if (re instanceof FailedPredicateException) {
      FailedPredicateException e = (FailedPredicateException) re;
      String ruleName = recognizer.getRuleNames()[recognizer.getContext().getRuleIndex()];

      message = "failed predicate " + ruleName + ": " + e.getMessage();
    }

    return message;
  }
Beispiel #2
0
 /**
  * Convert word like {@code *} or {@code ID} or {@code expr} to a path element. {@code anywhere}
  * is {@code true} if {@code //} precedes the word.
  */
 protected XPathElement getXPathElement(Token wordToken, boolean anywhere) {
   if (wordToken.getType() == Token.EOF) {
     throw new IllegalArgumentException("Missing path element at end of path");
   }
   String word = wordToken.getText();
   int ttype = parser.getTokenType(word);
   int ruleIndex = parser.getRuleIndex(word);
   switch (wordToken.getType()) {
     case XPathLexer.WILDCARD:
       return anywhere ? new XPathWildcardAnywhereElement() : new XPathWildcardElement();
     case XPathLexer.TOKEN_REF:
     case XPathLexer.STRING:
       if (ttype == Token.INVALID_TYPE) {
         throw new IllegalArgumentException(
             word + " at index " + wordToken.getStartIndex() + " isn't a valid token name");
       }
       return anywhere
           ? new XPathTokenAnywhereElement(word, ttype)
           : new XPathTokenElement(word, ttype);
     default:
       if (ruleIndex == -1) {
         throw new IllegalArgumentException(
             word + " at index " + wordToken.getStartIndex() + " isn't a valid rule name");
       }
       return anywhere
           ? new XPathRuleAnywhereElement(word, ruleIndex)
           : new XPathRuleElement(word, ruleIndex);
   }
 }
 @Override
 public void reportContextSensitivity(
     @NotNull Parser recognizer,
     @NotNull DFA dfa,
     int startIndex,
     int stopIndex,
     @NotNull ATNConfigSet configs) {
   recognizer.notifyErrorListeners(
       "reportContextSensitivity d="
           + dfa.decision
           + ", input='"
           + recognizer.getTokenStream().getText(Interval.of(startIndex, stopIndex))
           + "'");
 }
 @Override
 public void reportContextSensitivity(
     @NotNull Parser recognizer,
     @NotNull DFA dfa,
     int startIndex,
     int stopIndex,
     int prediction,
     @NotNull ATNConfigSet configs) {
   String format = "reportContextSensitivity d=%s, input='%s'";
   String decision = getDecisionDescription(recognizer, dfa);
   String text = recognizer.getTokenStream().getText(Interval.of(startIndex, stopIndex));
   String message = String.format(format, decision, text);
   recognizer.notifyErrorListeners(message);
 }
 @Override
 public void reportAmbiguity(
     @NotNull Parser recognizer,
     DFA dfa,
     int startIndex,
     int stopIndex,
     @NotNull IntervalSet ambigAlts,
     @NotNull ATNConfigSet configs) {
   recognizer.notifyErrorListeners(
       "reportAmbiguity d="
           + dfa.decision
           + ": ambigAlts="
           + ambigAlts
           + ", input='"
           + recognizer.getTokenStream().getText(Interval.of(startIndex, stopIndex))
           + "'");
 }
 /**
  * Prints the postscript parsetree to a .ps file with the given name
  *
  * @param filename used to generate a file of the form <filename>.ps
  */
 public void outputPSGraphToFile(String filename) {
   try {
     rootCtx.save(Arrays.asList(parser.getRuleNames()), filename + ".ps");
   } catch (IOException | PrintException e) {
     e
         .printStackTrace(); // To change body of catch statement use File | Settings | File
                             // Templates.
   }
 }
Beispiel #7
0
  public List<String> getNodeStrings(
      String input, String xpath, String startRuleName, String parserName, String lexerName)
      throws Exception {
    Pair<Parser, Lexer> pl = getParserAndLexer(input, parserName, lexerName);
    Parser parser = pl.a;
    ParseTree tree = execStartRule(startRuleName, parser);

    List<String> nodes = new ArrayList<String>();
    for (ParseTree t : XPath.findAll(tree, xpath, parser)) {
      if (t instanceof RuleContext) {
        RuleContext r = (RuleContext) t;
        nodes.add(parser.getRuleNames()[r.getRuleIndex()]);
      } else {
        TerminalNode token = (TerminalNode) t;
        nodes.add(token.getText());
      }
    }
    return nodes;
  }
  @Override
  public void reportAmbiguity(
      @NotNull Parser recognizer,
      @NotNull DFA dfa,
      int startIndex,
      int stopIndex,
      boolean exact,
      @Nullable BitSet ambigAlts,
      @NotNull ATNConfigSet configs) {
    if (exactOnly && !exact) {
      return;
    }

    String format = "reportAmbiguity d=%s: ambigAlts=%s, input='%s'";
    String decision = getDecisionDescription(recognizer, dfa);
    BitSet conflictingAlts = getConflictingAlts(ambigAlts, configs);
    String text = recognizer.getTokenStream().getText(Interval.of(startIndex, stopIndex));
    String message = String.format(format, decision, conflictingAlts, text);
    recognizer.notifyErrorListeners(message);
  }
  protected String getDecisionDescription(@NotNull Parser recognizer, @NotNull DFA dfa) {
    int decision = dfa.decision;
    int ruleIndex = dfa.atnStartState.ruleIndex;

    String[] ruleNames = recognizer.getRuleNames();
    if (ruleIndex < 0 || ruleIndex >= ruleNames.length) {
      return String.valueOf(decision);
    }

    String ruleName = ruleNames[ruleIndex];
    if (ruleName == null || ruleName.isEmpty()) {
      return String.valueOf(decision);
    }

    return String.format("%d (%s)", decision, ruleName);
  }
  public static RuleContext findTopContext(
      Parser parser, RuleContext context, IntervalSet values, boolean checkTop) {
    if (checkTop && values.contains(context.getRuleIndex())) {
      return context;
    }

    if (context.isEmpty()) {
      return null;
    }

    if (values.contains(parser.getATN().states.get(context.invokingState).ruleIndex)) {
      return context.parent;
    }

    return findTopContext(parser, context.parent, values, false);
  }
Beispiel #11
0
 /** Used for rule context info debugging during parse-time, not so much for ATN debugging */
 public String toInfoString(Parser recognizer) {
   List<String> rules = recognizer.getRuleInvocationStack(this);
   Collections.reverse(rules);
   return "ParserRuleContext" + rules + "{" + "start=" + start + ", stop=" + stop + '}';
 }