Home | History | Annotate | Download | only in Antlr3.Runtime.Debug
      1 /*
      2  * [The "BSD licence"]
      3  * Copyright (c) 2005-2008 Terence Parr
      4  * All rights reserved.
      5  *
      6  * Conversion to C#:
      7  * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 namespace Antlr.Runtime.Debug
     34 {
     35     using Obsolete = System.ObsoleteAttribute;
     36     using ITreeAdaptor = Antlr.Runtime.Tree.ITreeAdaptor;
     37     using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream;
     38 
     39     /** <summary>
     40      *  Debug any tree node stream.  The constructor accepts the stream
     41      *  and a debug listener.  As node stream calls come in, debug events
     42      *  are triggered.
     43      *  </summary>
     44      */
     45     public class DebugTreeNodeStream : ITreeNodeStream
     46     {
     47         protected IDebugEventListener dbg;
     48         protected ITreeAdaptor adaptor;
     49         protected ITreeNodeStream input;
     50         protected bool initialStreamState = true;
     51 
     52         /** <summary>Track the last mark() call result value for use in rewind().</summary> */
     53         protected int lastMarker;
     54 
     55         public DebugTreeNodeStream( ITreeNodeStream input,
     56                                    IDebugEventListener dbg )
     57         {
     58             this.input = input;
     59             this.adaptor = input.TreeAdaptor;
     60             this.input.UniqueNavigationNodes = true;
     61             DebugListener = dbg;
     62         }
     63 
     64         #region Properties
     65         public virtual IDebugEventListener DebugListener
     66         {
     67             get
     68             {
     69                 return dbg;
     70             }
     71             set
     72             {
     73                 dbg = value;
     74             }
     75         }
     76         public virtual int Index
     77         {
     78             get
     79             {
     80                 return input.Index;
     81             }
     82         }
     83         public virtual ITokenStream TokenStream
     84         {
     85             get
     86             {
     87                 return input.TokenStream;
     88             }
     89         }
     90         public virtual ITreeAdaptor TreeAdaptor
     91         {
     92             get
     93             {
     94                 return adaptor;
     95             }
     96         }
     97         public virtual object TreeSource
     98         {
     99             get
    100             {
    101                 return input;
    102             }
    103         }
    104         /** <summary>
    105          *  It is normally this object that instructs the node stream to
    106          *  create unique nav nodes, but to satisfy interface, we have to
    107          *  define it.  It might be better to ignore the parameter but
    108          *  there might be a use for it later, so I'll leave.
    109          *  </summary>
    110          */
    111         public bool UniqueNavigationNodes
    112         {
    113             get
    114             {
    115                 return input.UniqueNavigationNodes;
    116             }
    117             set
    118             {
    119                 input.UniqueNavigationNodes = value;
    120             }
    121         }
    122 
    123         #endregion
    124 
    125         public virtual void Consume()
    126         {
    127             object node = input.LT( 1 );
    128             input.Consume();
    129             dbg.ConsumeNode( node );
    130         }
    131 
    132         public virtual object this[int i]
    133         {
    134             get
    135             {
    136                 return input[i];
    137             }
    138         }
    139 
    140         public virtual object LT( int i )
    141         {
    142             object node = input.LT( i );
    143             int ID = adaptor.GetUniqueID( node );
    144             string text = adaptor.GetText( node );
    145             int type = adaptor.GetType( node );
    146             dbg.LT( i, node );
    147             return node;
    148         }
    149 
    150         public virtual int LA( int i )
    151         {
    152             object node = input.LT( i );
    153             int ID = adaptor.GetUniqueID( node );
    154             string text = adaptor.GetText( node );
    155             int type = adaptor.GetType( node );
    156             dbg.LT( i, node );
    157             return type;
    158         }
    159 
    160         public virtual int Mark()
    161         {
    162             lastMarker = input.Mark();
    163             dbg.Mark( lastMarker );
    164             return lastMarker;
    165         }
    166 
    167         public virtual void Rewind( int marker )
    168         {
    169             dbg.Rewind( marker );
    170             input.Rewind( marker );
    171         }
    172 
    173         public virtual void Rewind()
    174         {
    175             dbg.Rewind();
    176             input.Rewind( lastMarker );
    177         }
    178 
    179         public virtual void Release( int marker )
    180         {
    181         }
    182 
    183         public virtual void Seek( int index )
    184         {
    185             // TODO: implement seek in dbg interface
    186             // db.seek(index);
    187             input.Seek( index );
    188         }
    189 
    190         public virtual int Count
    191         {
    192             get
    193             {
    194                 return input.Count;
    195             }
    196         }
    197 
    198         public virtual string SourceName
    199         {
    200             get
    201             {
    202                 return TokenStream.SourceName;
    203             }
    204         }
    205 
    206         public virtual void ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t )
    207         {
    208             input.ReplaceChildren( parent, startChildIndex, stopChildIndex, t );
    209         }
    210 
    211         public virtual string ToString( object start, object stop )
    212         {
    213             return input.ToString( start, stop );
    214         }
    215     }
    216 }
    217