Node.java

  1. /*
  2.  ***** BEGIN LICENSE BLOCK *****
  3.  * Version: EPL 1.0/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Eclipse Public
  6.  * License Version 1.0 (the "License"); you may not use this file
  7.  * except in compliance with the License. You may obtain a copy of
  8.  * the License at http://www.eclipse.org/legal/epl-v10.html
  9.  *
  10.  * Software distributed under the License is distributed on an "AS
  11.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  12.  * implied. See the License for the specific language governing
  13.  * rights and limitations under the License.
  14.  *
  15.  * Copyright (C) 2001-2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
  16.  * Copyright (C) 2001-2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
  17.  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
  18.  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
  19.  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
  20.  * Copyright (C) 2006 Thomas Corbat <tcorbat@hsr.ch>
  21.  *
  22.  * Alternatively, the contents of this file may be used under the terms of
  23.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  24.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  25.  * in which case the provisions of the GPL or the LGPL are applicable instead
  26.  * of those above. If you wish to allow use of your version of this file only
  27.  * under the terms of either the GPL or the LGPL, and not to allow others to
  28.  * use your version of this file under the terms of the EPL, indicate your
  29.  * decision by deleting the provisions above and replace them with the notice
  30.  * and other provisions required by the GPL or the LGPL. If you do not delete
  31.  * the provisions above, a recipient may use your version of this file under
  32.  * the terms of any one of the EPL, the GPL or the LGPL.
  33.  ***** END LICENSE BLOCK *****/
  34. package org.jruby.ast;

  35. import java.util.ArrayList;
  36. import java.util.List;

  37. import org.jruby.ParseResult;
  38. import org.jruby.ast.types.INameNode;
  39. import org.jruby.ast.visitor.AbstractNodeVisitor;
  40. import org.jruby.ast.visitor.NodeVisitor;
  41. import org.jruby.lexer.yacc.ISourcePosition;
  42. import org.jruby.lexer.yacc.ISourcePositionHolder;

  43. /**
  44.  * Base class for all Nodes in the AST
  45.  */
  46. public abstract class Node implements ISourcePositionHolder, ParseResult {    
  47.     // We define an actual list to get around bug in java integration (1387115)
  48.     static final List<Node> EMPTY_LIST = new ArrayList<Node>();
  49.    
  50.     private ISourcePosition position;

  51.     public Node(ISourcePosition position) {
  52.         this.position = position;
  53.     }

  54.     /**
  55.      * Location of this node within the source
  56.      */
  57.     public ISourcePosition getPosition() {
  58.         return position;
  59.     }

  60.     public void setPosition(ISourcePosition position) {
  61.         this.position = position;
  62.     }
  63.    
  64.     public abstract <T> T accept(NodeVisitor<T> visitor);
  65.     public abstract List<Node> childNodes();

  66.     protected static List<Node> createList(Node node) {
  67.         ArrayList<Node> list = new ArrayList<Node>(1);

  68.         list.add(node);

  69.         return list;
  70.     }

  71.     protected static List<Node> createList(Node node1, Node node2) {
  72.         ArrayList<Node> list = new ArrayList<Node>(2);

  73.         list.add(node1);
  74.         list.add(node2);

  75.         return list;
  76.     }

  77.     protected static List<Node> createList(Node node1, Node node2, Node node3) {
  78.         ArrayList<Node> list = new ArrayList<Node>(3);

  79.         list.add(node1);
  80.         list.add(node2);
  81.         list.add(node3);

  82.         return list;
  83.     }

  84.     protected static List<Node> createList(Node... nodes) {
  85.         ArrayList<Node> list = new ArrayList<Node>(nodes.length);
  86.        
  87.         for (Node node: nodes) {
  88.             if (node != null) list.add(node);
  89.         }
  90.        
  91.         return list;
  92.     }

  93.     @Override
  94.     public String toString() {
  95.         return toString(false, 0);
  96.     }

  97.     public String toString(boolean indent, int indentation) {
  98.         if (this instanceof InvisibleNode) return "";

  99.         StringBuilder builder = new StringBuilder(60);

  100.         if (indent) {
  101.             indent(indentation, builder);
  102.         }

  103.         builder.append("(").append(getNodeName());

  104.         if (this instanceof INameNode) {
  105.             builder.append(":").append(((INameNode) this).getName());
  106.         }

  107.         builder.append(" ").append(getPosition().getLine());

  108.         if (!childNodes().isEmpty() && indent) {
  109.             builder.append("\n");
  110.         }

  111.         for (Node child : childNodes()) {
  112.             if (!indent) {
  113.                 builder.append(", ");
  114.             }

  115.             if (child == null) {
  116.                 if (indent) {
  117.                     indent(indentation + 1, builder);
  118.                 }

  119.                 builder.append("null");
  120.             } else {
  121.                 if (indent && child instanceof NilImplicitNode) {
  122.                     if (indent) {
  123.                         indent(indentation + 1, builder);
  124.                     }

  125.                     builder.append(child.getClass().getSimpleName());
  126.                 } else {
  127.                     builder.append(child.toString(indent, indentation + 1));
  128.                 }
  129.             }

  130.             if (indent) {
  131.                 builder.append("\n");
  132.             }
  133.         }

  134.         if (!childNodes().isEmpty() && indent) {
  135.             indent(indentation, builder);
  136.         }

  137.         builder.append(")");

  138.         return builder.toString();
  139.     }

  140.     private static void indent(int indentation, StringBuilder builder) {
  141.         for (int n = 0; n < indentation; n++) {
  142.             builder.append("  ");
  143.         }
  144.     }

  145.     protected String getNodeName() {
  146.         String name = getClass().getName();
  147.         int i = name.lastIndexOf('.');
  148.         String nodeType = name.substring(i + 1);
  149.         return nodeType;
  150.     }

  151.     public <T extends org.jruby.ast.Node> T findFirstChild(final Class<T> nodeClass) {
  152.         return accept(new AbstractNodeVisitor<T>() {

  153.             @Override
  154.             protected T defaultVisit(Node node) {
  155.                 if (nodeClass.isAssignableFrom(node.getClass())) {
  156.                     return (T) node;
  157.                 } else {
  158.                     return visitFirstChild(node);
  159.                 }
  160.             }

  161.         });
  162.     }

  163.     /**
  164.      * @return the nodeId
  165.      */
  166.     public abstract NodeType getNodeType();

  167.     /**
  168.      * Whether the node evaluates to nil and has no side effects.
  169.      *
  170.      * @return true if nil, false otherwise
  171.      */
  172.     public boolean isNil() {
  173.         return false;
  174.     }

  175.     /**
  176.      * Check whether the given node is considered always "defined" or whether it
  177.      * has some form of definition check.
  178.      *
  179.      * @return Whether the type of node represents a possibly undefined construct
  180.      */
  181.     public boolean needsDefinitionCheck() {
  182.         return true;
  183.     }
  184. }