private static void print(StringBuilder builder, Tree node, String prefix, boolean isTail) {
    final String name =
        node.getText() == null ? "" : node.getText().replaceAll("\r\n|\r|\n", "\u23CE");

    builder
        .append(prefix)
        .append(isTail ? "└─ " : "├─ ") //
        .append(String.format("%04d", node.getLine())) //
        .append(':')
        .append(String.format("%04d", node.getCharPositionInLine()))
        .append(' ') //
        .append(String.format("%02d", node.getType()))
        .append(": ")
        .append(name) //
        .append("\n");

    for (int i = 0; i < (node.getChildCount() - 1); i++) {
      CommonTreePrinter.print(
          builder, node.getChild(i), prefix + (isTail ? "    " : "│   "), false);
    }

    if (node.getChildCount() >= 1) {
      CommonTreePrinter.print(
          builder,
          node.getChild(node.getChildCount() - 1),
          prefix + (isTail ? "    " : "│   "),
          true);
    }
  }
Beispiel #2
0
  public Return(Tree tree, ErrorsCollector ec, SymbolTable st) {
    assert (tree.getType() == CommonCppWithStreamsLexer.RETURN);
    assert (tree.getChildCount() <= 1);

    expr = null;

    Tree p = tree.getParent();
    while (p != null && p.getType() != CommonCppWithStreamsLexer.FUNCTION) {
      p = p.getParent();
    }
    if (p == null) {
      ec.check(false, tree.getLine(), "return from no function");
      return;
    }
    assert (p.getChild(1).getType() == CommonCppWithStreamsLexer.NAME);
    Function funcDef = st.referenceFunctionAndGetIt(p.getChild(1).getText(), tree.getLine());
    assert (funcDef != null);
    funcName = funcDef.getName();
    if (funcDef.getType() == Type.VOID) {
      ec.check(
          tree.getChildCount() == 0,
          tree.getLine(),
          "can not return value out of 'void' function '" + funcName + "'");
    } else {
      if (tree.getChildCount() != 1) {
        ec.check(
            false,
            tree.getLine(),
            "can not return from '"
                + TypeConverter.typeToString(funcDef)
                + "' function '"
                + funcName
                + "' without returning a value");
        return;
      }
      expr = new Expression(tree.getChild(0), ec, st);
    }
  }
 @Override
 public int getLine() {
   int line = 0;
   if (token != null) {
     line = token.getLine();
   }
   if (line == 0) {
     Tree child = getChild(0);
     if (child != null) {
       line = child.getLine();
     }
   }
   return line;
 }
  public FilteredTreeNodeStream(Tree tree, Filter f) {
    this.tokenizer = new FilteringTokenizer(tree);

    final boolean success = new BracketedFilter(f).filter(this.tokenizer);

    if (success) {
      this.tokens = new ArrayList<Tree>(this.tokenizer.getConsumedTokens());

      if (DEBUG) {
        for (Tree t : this.tokens) {
          System.out.println(
              "--> " + t + "; line " + t.getLine() + ", char " + t.getCharPositionInLine());
        }
      }

    } else {
      // TODO Flag the mismatch somehow.
      this.tokens = new ArrayList<Tree>(0);
    }
  }
 private String _source(Tree ast) {
   return String.format(SOURCE_FILE_FORMAT, sourceFile, ast.getLine());
 }