www.pudn.com > cppcc.rar > ebnf_node.hh


/*
 *  File:       ebnf_node.hh
 *              $Id: ebnf_node.hh,v 1.7 2002/05/27 03:00:31 alec Exp $
 *
 *  Author:     Alec Panovici (alecu@email.com)
 * 
 *  Comments:
 *
 *  Revision history:
 *
 *  $Log: ebnf_node.hh,v $
 *  Revision 1.7  2002/05/27 03:00:31  alec
 *  doc update
 *
 *  Revision 1.6  2002/05/16 21:36:01  alec
 *  parser generation done
 *
 *  Revision 1.5  2002/05/10 18:08:36  alec
 *  *** empty log message ***
 *
 *  Revision 1.4  2002/05/08 18:18:06  alec
 *  *** empty log message ***
 *
 *  Revision 1.3  2002/04/29 09:34:10  alec
 *  scanner ptree building compiles
 *
 */


/*
  Copyright (C) 2002 Alexandru Panoviciu (alecu@email.com)

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 */

#ifndef __EBNF_NODE_HH__
#define __EBNF_NODE_HH__

#include "debug.h"
#include "parse_util.hh"
#include "ebnf_node_algo.hh"

class EbnfNodeAlgo;
class EbnfNodeAlgoException;


/**
 * This is the EBNF expansion nodes' interface. Nodes are built by an
 * implementation of the IEbnfNodeBuilder. This interface only specifies the
 * basic tree structure operations.
 */
class EbnfNode
{
public:

  EbnfNode (const Position &pos_) : pos(pos_)
  {}

  /**
   * Virtual copy creation.
   */
  virtual EbnfNode* clone () = 0;
  
  /**
   * Returns true if this node is a leaf node.
   */
  virtual bool isLeaf () { return getChildCount() == 0; }


  /**
   * Returns the number of children nodes of this node.
   */
  virtual int getChildCount () = 0;

  /**
   * Returns the index'th child of this node.
   *
   * \pre 0 <= index <= getChildCount()
   */
  virtual EbnfNode& operator [] (int index) = 0;

  /**
   * Generic depth-first traversal method. At each node, the () operator of
   * the EbnfNodeAlgo object is invoked, with *this as argument. It returns
   * true if the whole tree was traversed, and false if the traversal was
   * interrupted due to a call to the () operator of the EbnfNodeAlgo object
   * returning false.
   *
   * \throw EbnfNodeAlgoException if the algorithm failed for some reason.
   */
  virtual bool dfTraverse (EbnfNodeAlgo &algo) throw (EbnfNodeAlgoException);

  /**
   * Generic depth-first reversed order traversal method. At each node, the ()
   * operator of the EbnfNodeAlgo object is invoked, with *this as
   * argument. It returns true if the whole tree was traversed, and false if
   * the traversal was interrupted due to a call to the () operator of the
   * EbnfNodeAlgo object returning false.
   *
   * The difference between this and dfTraverse is that here the () operator
   * is called for the root before recurring to the children nodes. This is
   * particularly useful for inherited attributes.
   *
   * \throw EbnfNodeAlgoException if the algorithm failed for some reason.
   */
  virtual bool rdfTraverse (EbnfNodeAlgo &algo) throw (EbnfNodeAlgoException);

  
  /**
   * Force virtual destructors.
   */
  virtual ~EbnfNode () {}

  /**
   * Returns the position in the input file that corresponds to this node.
   */
  const Position& getPos () const { return pos; }
  
#ifdef DEBUG

  virtual void dump (ostream &os) const = 0;
  
#endif

private:

  /**
   * The position in the input grammar file that is closest to where this node
   * belongs.
   */
  Position pos;
};

#endif /* #ifndef __EBNF_NODE_HH__ */