コード例 #1
0
  /**
   * Parses Javadoc comment as DetailNode tree.
   *
   * @param javadocCommentAst DetailAST of Javadoc comment
   * @return DetailNode tree of Javadoc comment
   */
  public ParseStatus parseJavadocAsDetailNode(DetailAST javadocCommentAst) {
    blockCommentLineNumber = javadocCommentAst.getLineNo();
    final String javadocComment = JavadocUtils.getJavadocCommentContent(javadocCommentAst);

    // Use a new error listener each time to be able to use
    // one check instance for multiple files to be checked
    // without getting side effects.
    errorListener = new DescriptiveErrorListener();

    // Log messages should have line number in scope of file,
    // not in scope of Javadoc comment.
    // Offset is line number of beginning of Javadoc comment.
    errorListener.setOffset(javadocCommentAst.getLineNo() - 1);

    final ParseStatus result = new ParseStatus();

    try {
      final ParseTree parseTree = parseJavadocAsParseTree(javadocComment);

      final DetailNode tree = convertParseTreeToDetailNode(parseTree);
      result.setTree(tree);
    } catch (ParseCancellationException ex) {
      // If syntax error occurs then message is printed by error listener
      // and parser throws this runtime exception to stop parsing.
      // Just stop processing current Javadoc comment.
      ParseErrorMessage parseErrorMessage = errorListener.getErrorMessage();

      // There are cases when antlr error listener does not handle syntax error
      if (parseErrorMessage == null) {
        parseErrorMessage =
            new ParseErrorMessage(
                javadocCommentAst.getLineNo(),
                MSG_KEY_UNRECOGNIZED_ANTLR_ERROR,
                javadocCommentAst.getColumnNo(),
                ex.getMessage());
      }

      result.setParseErrorMessage(parseErrorMessage);
    }

    return result;
  }
コード例 #2
0
  @Override
  public StructuredDate evaluate(String displayDate) throws StructuredDateFormatException {
    stack = new Stack<Object>();

    result = new StructuredDate();
    result.setDisplayDate(displayDate);

    // Instantiate a parser from the lowercased display date, so that parsing will be
    // case insensitive.
    ANTLRInputStream inputStream = new ANTLRInputStream(displayDate.toLowerCase());
    StructuredDateLexer lexer = new StructuredDateLexer(inputStream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    StructuredDateParser parser = new StructuredDateParser(tokenStream);

    // Don't try to recover from parse errors, just bail.
    parser.setErrorHandler(new BailErrorStrategy());

    // Don't print error messages to the console.
    parser.removeErrorListeners();

    // Generate our own custom error messages.
    parser.addParseListener(this);

    try {
      // Attempt to fulfill the oneDisplayDate rule of the grammar.
      parser.oneDisplayDate();
    } catch (ParseCancellationException e) {
      // ParseCancellationException is thrown by the BailErrorStrategy when there is a
      // parse error, with the underlying RecognitionException as the cause.
      RecognitionException re = (RecognitionException) e.getCause();

      throw new StructuredDateFormatException(getErrorMessage(re), re);
    }

    // The parsing was successful. Return the result.
    return result;
  }
コード例 #3
0
ファイル: TreeParser.java プロジェクト: moskalenko/beast2
  /** Ensure the class behaves properly, even when inputs are not specified. */
  @Override
  public void initAndValidate() throws Exception {
    boolean sortNodesAlphabetically = false;

    if (dataInput.get() != null) {
      labels = dataInput.get().getTaxaNames();
    } else if (m_taxonset.get() != null) {
      if (labels == null) {
        labels = m_taxonset.get().asStringList();
      } else { // else labels were set by TreeParser c'tor
        sortNodesAlphabetically = true;
      }
    } else {
      if (isLabelledNewickInput.get()) {
        if (m_initial.get() != null) {
          labels = m_initial.get().getTaxonset().asStringList();
        } else {
          labels = new ArrayList<>();
          createUnrecognizedTaxa = true;
          sortNodesAlphabetically = true;
        }
      } else {
        if (m_initial.get() != null) {
          // try to pick up taxa from initial tree
          final Tree tree = m_initial.get();
          if (tree.m_taxonset.get() != null) {
            labels = tree.m_taxonset.get().asStringList();
          } else {
            // m_sLabels = null;
          }
        } else {
          // m_sLabels = null;
        }
      }
      //            m_bIsLabelledNewick = false;
    }
    final String newick = newickInput.get();
    if (newick == null || newick.equals("")) {
      // can happen while initalising Beauti
      final Node dummy = new Node();
      setRoot(dummy);
    } else {
      try {
        setRoot(parseNewick(newickInput.get()));
      } catch (ParseCancellationException e) {
        throw new RuntimeException(
            "TreeParser cannot make sense of the Newick string "
                + "provided.  It gives the following clue:\n"
                + e.getMessage());
      }
    }

    super.initAndValidate();

    if (sortNodesAlphabetically) {
      // correct for node ordering: ensure order is alphabetical
      for (int i = 0; i < getNodeCount() && i < labels.size(); i++) {
        m_nodes[i].setID(labels.get(i));
      }

      Node[] nodes = new Node[labels.size()];
      System.arraycopy(m_nodes, 0, nodes, 0, labels.size());

      Arrays.sort(nodes, (o1, o2) -> o1.getID().compareTo(o2.getID()));
      for (int i = 0; i < labels.size(); i++) {
        m_nodes[i] = nodes[i];
        nodes[i].setNr(i);
      }
    }

    if (m_initial.get() != null) processTraits(m_initial.get().m_traitList.get());
    else processTraits(m_traitList.get());

    if (timeTraitSet != null) {
      adjustTreeNodeHeights(root);
    } else if (adjustTipHeightsInput.get()) {

      double treeLength = TreeUtils.getTreeLength(this, getRoot());

      double extraTreeLength = 0.0;
      double maxTipHeight = 0.0;

      // all nodes should be at zero height if no date-trait is available
      for (int i = 0; i < getLeafNodeCount(); i++) {
        double height = getNode(i).getHeight();
        if (maxTipHeight < height) {
          maxTipHeight = height;
        }
        extraTreeLength += height;
        getNode(i).setHeight(0);
      }

      double scaleFactor = (treeLength + extraTreeLength) / treeLength;

      final double SCALE_FACTOR_THRESHOLD = 0.001;

      // if the change in total tree length is more than 0.1% then give the user a warning!
      if (scaleFactor > 1.0 + SCALE_FACTOR_THRESHOLD) {

        DecimalFormat format = new DecimalFormat("#.##");

        Log.info.println(
            "WARNING: Adjust tip heights attribute set to 'true' in " + getClass().getSimpleName());
        Log.info.println(
            "         has resulted in significant (>"
                + format.format(SCALE_FACTOR_THRESHOLD * 100.0)
                + "%) change in tree length.");
        Log.info.println(
            "         Use "
                + adjustTipHeightsInput.getName()
                + "='false' to override this default.");
        Log.info.printf("  original max tip age = %8.3f\n", maxTipHeight);
        Log.info.printf("       new max tip age = %8.3f\n", 0.0);
        Log.info.printf("  original tree length = %8.3f\n", treeLength);
        Log.info.printf("       new tree length = %8.3f\n", treeLength + extraTreeLength);
        Log.info.printf("       TL scale factor = %8.3f\n", scaleFactor);
      }
    }

    if (m_taxonset.get() == null && labels != null && isLabelledNewickInput.get()) {
      m_taxonset.setValue(new TaxonSet(Taxon.createTaxonList(labels)), this);
    }

    initStateNodes();
  } // init