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;
  }
 /**
  * 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.
   }
 }
示例#3
0
  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);
  }
示例#4
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;
  }