[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Serialization of parse trees



Title: Serialization of parse trees

Hi,

I have attached to sablecc macro files which can be used to generate a parser
that generates serilizable parse trees. The adjustments I made are marked with
[CHANGED] and [ADDED] tags. After that a short description of the adjustment
follows.
Two changes might not be wanted by all. This is the change in class Token, which
changes the behavior of toString. And the change in class Node, because the
additional methods won't be needed.

One thing I changed is that the LinkedLists in Macro:ListElement are now realy
of type TypeLinkedList. It's needed and I don't understand why they where
of type LinkedList anyway.

Cheers, Leo

===============================================================================
Thomas Leonhardt          email  leonhardt@informatik.tu-darmstadt.de
TU Darmstadt              WWW    http://www.pi.informatik.tu-darmstadt.de
FG Praktische Informatik  phone  +49 / (0)6151 / 16 - 5313
Wilhelminenstr. 7         fax    +49 / (0)6151 / 16 - 5472
D-64283 Darmstadt         
Germany
===============================================================================
 

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of SableCC.                             *
 * See the file "LICENSE" for copyright information and the  *
 * terms and conditions for copying, distribution and        *
 * modification of SableCC.                                  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

Macro:Start
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

import $1$.*;

public final class Start extends Node
{
    private $2$ _$3$_;
    private EOF _eof_;

    public Start()
    {
    }

    public Start(
        $2$ _$3$_,
        EOF _eof_)
    {
        set$2$(_$3$_);
        setEOF(_eof_);
    }

    public Object clone()
    {
        return new Start(
            ($2$) cloneNode(_$3$_),
            (EOF) cloneNode(_eof_));
    }

    public void apply(Switch sw)
    {
        ((Analysis) sw).caseStart(this);
    }

    public $2$ get$2$()
    {
        return _$3$_;
    }

    public void set$2$($2$ node)
    {
        if(_$3$_ != null)
        {
            _$3$_.parent(null);
        }

        if(node != null)
        {
            /** Error recovery
             * if(node.parent() != null) {  node.parent().removeChild(node); }
             */

            node.parent(this);
        }

        _$3$_ = node;
    }

    public EOF getEOF()
    {
        return _eof_;
    }

    public void setEOF(EOF node)
    {
        if(_eof_ != null)
        {
            _eof_.parent(null);
        }

        if(node != null)
        {
           /**
            * if(node.parent() != null) { node.parent().removeChild(node); }
            */

            node.parent(this);
        }

        _eof_ = node;
    }

    void removeChild(Node child)
    {
        if(_$3$_ == child)
        {
            _$3$_ = null;
            return;
        }

        if(_eof_ == child)
        {
            _eof_ = null;
            return;
        }
    }

    void replaceChild(Node oldChild, Node newChild)
    {
        if(_$3$_ == oldChild)
        {
            set$2$(($2$) newChild);
            return;
        }

        if(_eof_ == oldChild)
        {
            setEOF((EOF) newChild);
            return;
        }
    }

    public String toString()
    {
        return "" +
            toString(_$3$_) +
            toString(_eof_);
    }
}

$

Macro:EOF
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

import $1$.*;

public final class EOF extends Token
{
    public EOF()
    {
        setText("");
    }

    public EOF(int line, int pos)
    {
        setText("");
        setLine(line);
        setPos(pos);
    }

    public Object clone()
    {
        return new EOF(getLine(), getPos());
    }

    public void apply(Switch sw)
    {
        ((Analysis) sw).caseEOF(this);
    }
}

$

Macro:Token
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

public abstract class Token extends Node
{
    private String text;
    private int line;
    private int pos;

    public String getText()
    {
        return text;
    }

    public void setText(String text)
    {
        this.text = text;
    }

    public int getLine()
    {
        return line;
    }

    public void setLine(int line)
    {
        this.line = line;
    }

    public int getPos()
    {
        return pos;
    }

    public void setPos(int pos)
    {
        this.pos = pos;
    }

    public String toString()
    {
        // [CHANGED] Leo: 
        //return text + " ";
        
        return text;
    }

    void removeChild(Node child)
    {
    }

    void replaceChild(Node oldChild, Node newChild)
    {
    }
}

$

Macro:Node
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

import java.util.*;
import $1$.*;
// [ADDED] Leo: Needed for additional methods
import tud.inf.pi.analysisToolkit.classNodeBuilder.EnvironmentType;

// [CHANGED] Leo: Need to be able to serialize objects of this class
//public abstract class Node implements Switchable, Cloneable
public abstract class Node implements Switchable, Cloneable, java.io.Serializable
{
    private Node parent;

    public abstract Object clone();

    public Node parent()
    {
        return parent;
    }

    void parent(Node parent)
    {
        this.parent = parent;
    }

    abstract void removeChild(Node child);
    abstract void replaceChild(Node oldChild, Node newChild);

    public void replaceBy(Node node)
    {
        if(parent != null)
        {
            parent.replaceChild(this, node);
        }
    }

    protected String toString(Node node)
    {
        if(node != null)
        {
            return node.toString();
        }

        return "";
    }

    protected String toString(List list)
    {
        StringBuffer s = new StringBuffer();

        for(Iterator i = list.iterator(); i.hasNext();)
        {
            s.append(i.next());
        }

        return s.toString();
    }

    protected Node cloneNode(Node node)
    {
        if(node != null)
        {
            return (Node) node.clone();
        }

        return null;
    }

    protected List cloneList(List list)
    {
        List clone = new LinkedList();

        for(Iterator i = list.iterator(); i.hasNext();)
        {
            clone.add(((Node) i.next()).clone());
        }

        return clone;
    }


   //
   // [ADDED] Leo
   // The following methods have been added for the CKTEST project
   //
	
            public
            Object
      getTypeAttributeIntern()
      {
            //
            // This if translates the "NILL" string to
            // an undefined type object.
            //

         if(oTypeAttribute instanceof EnvironmentType)
         {
            return oTypeAttribute;
         }
         else
         {
            return new EnvironmentType(EnvironmentType.TYPE_UNDEF);
         }
      }


            public
            Object
      getTypeAttribute()
      {
            //
            // This if translates the "NILL" string to
            // an undefined type object.
            //

         if(oTypeAttribute instanceof EnvironmentType)
         {
            return oTypeAttribute;
         }
         else
         {
            return new EnvironmentType(EnvironmentType.TYPE_UNDEF);
         }
      }


            public
            void
      setTypeAttribute(Object oTypeAttribute)
      {
         this.oTypeAttribute=oTypeAttribute;
      }


      /**
       * The concrete value of this attribute must be an instanze of class ClassNode
       */
      protected Object oTypeAttribute;
}

$

Macro:NodeCast
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;


public class NodeCast implements Cast
{
    public final static NodeCast instance = new NodeCast();

    private NodeCast()
    {
    }

    public Object cast(Object o)
    {
        return (Node) o;
    }
}

$

Macro:Switch
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

public interface Switch
{
}

$

Macro:Switchable
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

public interface Switchable
{
    void apply(Switch sw);
}

$

Macro:TypedLinkedList
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

// [ADDED] Leo: The following two imports where added. Needed to implement
//              the Externalizable interface.
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.*;

// [CHANGED] Leo: If the interface Externalizable isn't implemented the
//                serialization mechanisem of LinkeList is used which
//                doesn't function properly for sub-classes.
//public class TypedLinkedList extends LinkedList
public class TypedLinkedList extends LinkedList implements java.io.Externalizable
{
    Cast cast;

    public TypedLinkedList()
    {
        super();

        cast = NoCast.instance;
    }

    public TypedLinkedList(Collection c)
    {
        super(c);

        cast = NoCast.instance;
    }

    public TypedLinkedList(Cast cast)
    {
        super();

        this.cast = cast;
    }

    public TypedLinkedList(Collection c, Cast cast)
    {
        super(c);

        this.cast = cast;
    }

    public Cast getCast()
    {
        return cast;
    }

    public void add(int index, Object element)
    {
        super.add(index, cast.cast(element));
    }

    public boolean add(Object o)
    {
        return super.add(cast.cast(o));
    }

    public boolean addAll(Collection c)
    {
        for(Iterator i = c.iterator(); i.hasNext(); )
        {
            super.add(cast.cast(i.next()));
        }
        return true;
    }

    public boolean addAll(int index, Collection c)
    {
        int pos = index;
        for(Iterator i = c.iterator(); i.hasNext(); )
        {
            super.add(pos++, cast.cast(i.next()));
        }
        return true;
    }

    public void addFirst(Object o)
    {
        super.addFirst(cast.cast(o));
    }

    public void addLast(Object o)
    {
        super.addLast(cast.cast(o));
    }

    public ListIterator listIterator(int index)
    {
        return new TypedLinkedListIterator(super.listIterator(index));
    }

    private class TypedLinkedListIterator implements ListIterator
    {
        ListIterator iterator;

        TypedLinkedListIterator(ListIterator iterator)
        {
            this.iterator = iterator;
        }

        public boolean hasNext()
        {
            return iterator.hasNext();
        }

        public Object next()
        {
            return iterator.next();
        }

        public boolean hasPrevious()
        {
            return iterator.hasPrevious();
        }

        public Object previous()
        {
            return iterator.previous();
        }

        public int nextIndex()
        {
            return iterator.nextIndex();
        }

        public int previousIndex()
        {
            return iterator.previousIndex();
        }

        public void remove()
        {
            iterator.remove();
        }

        public void set(Object o)
        {
            iterator.set(cast.cast(o));
        }

        public void add(Object o)
        {
            iterator.add(cast.cast(o));
        }
    }


   //
   // [ADDED] Leo: Added the following two methods to implement interface
   //              Externilizable
   //

   public synchronized void writeExternal(ObjectOutput out)
        throws java.io.IOException
   {
      out.writeObject(cast);

      LinkedList oDummy = new LinkedList(this);

      out.writeObject(oDummy);
   }


   public synchronized void readExternal(ObjectInput in)
        throws java.io.IOException, ClassNotFoundException
   {
      cast = (Cast)in.readObject();

      this.clear();

      LinkedList oDummy = (LinkedList)in.readObject();

      this.addAll(oDummy);
   }
}

$

Macro:Cast
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

// [CHANGED] Leo: Need to be able to serialize objects of this class
//public interface Cast
public interface Cast extends java.io.Serializable
{
    Object cast(Object o);
}

$

Macro:NoCast
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

public class NoCast implements Cast
{
    public final static NoCast instance = new NoCast();

    private NoCast()
    {
    }

    public Object cast(Object o)
    {
        return o;
    }
}

$
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of SableCC.                             *
 * See the file "LICENSE" for copyright information and the  *
 * terms and conditions for copying, distribution and        *
 * modification of SableCC.                                  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

Macro:AlternativeHeader
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

import java.util.*;
import $1$.*;

public final class $2$ extends $3$
{

$

Macro:NodeElement
    private $0$ _$1$_;

$

Macro:ListElement
    // [CHANGED] Leo: Need TypedLinkedList for serialization
    //private final LinkedList _$1$_ = new TypedLinkedList(new $0$_Cast());
    private final TypedLinkedList _$1$_ = new TypedLinkedList(new $0$_Cast());

$

Macro:ConstructorHeader

    public $0$(
$

Macro:ConstructorHeaderDeclNode

        $0$ _$1$_$2$
$

Macro:ConstructorHeaderDeclList

        $0$ _$1$_$2$
$

Macro:ConstructorBodyHeader
)
    {

$

Macro:ConstructorBodyNode
        set$0$(_$1$_);


$

Macro:ConstructorBodyList
        {
            this._$0$_.clear();
            this._$0$_.addAll(_$0$_);
        }


$

Macro:ConstructorBodyQMark
        if(_$2$_ instanceof X1QMark$1$)
        {
            set$0$(((X1QMark$1$) _$2$_).get$1$());
        }
        else
        {
            set$0$(null);
        }


$

Macro:ConstructorBodyStar
        while(_$0$_ instanceof X1Star$1$)
        {
            this._$0$_.add(((X1Star$1$) _$0$_).get$1$());
            _$0$_ = ((X1Star$1$) _$0$_).getXStar$1$();
        }


$

Macro:ConstructorBodyPlus
        if(_$0$_ != null)
        {
            while(_$0$_ instanceof X1$1$)
            {
                this._$0$_.addFirst(((X1$1$) _$0$_).get$1$());
                _$0$_ = ((X1$1$) _$0$_).getX$1$();
            }
            this._$0$_.addFirst(((X2$1$) _$0$_).get$1$());
        }


$

Macro:ConstructorBodyTail
    }

$

Macro:CloneHeader
    public Object clone()
    {
        return new $0$(
$

Macro:CloneBodyNode

            ($0$) cloneNode(_$1$_)$2$
$

Macro:CloneBodyList

            cloneList(_$0$_)$1$
$

Macro:CloneTail
);
    }

$

Macro:Apply

    public void apply(Switch sw)
    {
        ((Analysis) sw).case$0$(this);
    }

$

Macro:GetSetNode

    public $0$ get$1$()
    {
        return _$2$_;
    }

    public void set$1$($0$ node)
    {
        if(_$2$_ != null)
        {
            _$2$_.parent(null);
        }

        if(node != null)
        {
            /**  Error recovery.
             * if(node.parent() != null) {  node.parent().removeChild(node); }
             */
            node.parent(this);
        }

        _$2$_ = node;
    }

$

Macro:GetSetList

    public LinkedList get$0$()
    {
        return _$1$_;
    }

    public void set$0$(List list)
    {
        _$1$_.clear();
        _$1$_.addAll(list);
    }

$

Macro:ToStringHeader

    public String toString()
    {
        return ""
$

Macro:ToStringBodyNode

            + toString(_$0$_)
$

Macro:ToStringBodyList

            + toString(_$0$_)
$

Macro:ToStringTail
;
    }

$

Macro:RemoveChildHeader

    void removeChild(Node child)
    {

$

Macro:RemoveChildNode
        if(_$0$_ == child)
        {
            /** Error recovery  _$0$_ = null; */
            return;
        }


$

Macro:RemoveChildList
        if(_$0$_.remove(child))
        {
            return;
        }


$

Macro:RemoveChildTail
    }

$

Macro:ReplaceChildHeader

    void replaceChild(Node oldChild, Node newChild)
    {

$

Macro:ReplaceChildNode
        if(_$0$_ == oldChild)
        {
            set$1$(($2$) newChild);
            return;
        }


$

Macro:ReplaceChildList
        for(ListIterator i = _$0$_.listIterator(); i.hasNext();)
        {
            if(i.next() == oldChild)
            {
                if(newChild != null)
                {
                    i.set(newChild);
                    oldChild.parent(null);
                    return;
                }

                i.remove();
                oldChild.parent(null);
                return;
            }
        }


$

Macro:ReplaceChildTail
    }

$

Macro:Cast

    // [CHANGED] Leo: Need to be able to serialize objects of this class
    //private class $0$_Cast implements Cast
    private class $0$_Cast implements Cast, java.io.Serializable
    {
        public Object cast(Object o)
        {
            $1$ node = ($1$) o;

            /** Error recovery
              *if((node.parent() != null) &&
              *  (node.parent() != $2$.this))
              * {   node.parent().removeChild(node); }
              */         

            if((node.parent() == null) ||
                (node.parent() != $2$.this))
            {
                node.parent($2$.this);
            }

            return node;
        }
    }

$

Macro:AlternativeTail
}

$