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 System.Collections.Generic;
     36 
     37     /** <summary>
     38      *  Broadcast debug events to multiple listeners.  Lets you debug and still
     39      *  use the event mechanism to build parse trees etc...  Not thread-safe.
     40      *  Don't add events in one thread while parser fires events in another.
     41      *  </summary>
     42      *
     43      *  <seealso cref="DebugEventRepeater"/>
     44      */
     45     public class DebugEventHub : IDebugEventListener
     46     {
     47         List<IDebugEventListener> _listeners = new List<IDebugEventListener>();
     48 
     49         public DebugEventHub( params IDebugEventListener[] listeners )
     50         {
     51             _listeners = new List<IDebugEventListener>( listeners );
     52         }
     53 
     54         public virtual void Initialize()
     55         {
     56         }
     57 
     58         /** <summary>
     59          *  Add another listener to broadcast events too.  Not thread-safe.
     60          *  Don't add events in one thread while parser fires events in another.
     61          *  </summary>
     62          */
     63         public virtual void AddListener( IDebugEventListener listener )
     64         {
     65             _listeners.Add( listener );
     66         }
     67 
     68         /* To avoid a mess like this:
     69             public void enterRule(final String ruleName) {
     70                 broadcast(new Code(){
     71                     public void exec(DebugEventListener listener) {listener.enterRule(ruleName);}}
     72                     );
     73             }
     74             I am dup'ing the for-loop in each.  Where are Java closures!? blech!
     75          */
     76 
     77         public virtual void EnterRule( string grammarFileName, string ruleName )
     78         {
     79             for ( int i = 0; i < _listeners.Count; i++ )
     80             {
     81                 IDebugEventListener listener = _listeners[i];
     82                 listener.EnterRule( grammarFileName, ruleName );
     83             }
     84         }
     85 
     86         public virtual void ExitRule( string grammarFileName, string ruleName )
     87         {
     88             for ( int i = 0; i < _listeners.Count; i++ )
     89             {
     90                 IDebugEventListener listener = _listeners[i];
     91                 listener.ExitRule( grammarFileName, ruleName );
     92             }
     93         }
     94 
     95         public virtual void EnterAlt( int alt )
     96         {
     97             for ( int i = 0; i < _listeners.Count; i++ )
     98             {
     99                 IDebugEventListener listener = _listeners[i];
    100                 listener.EnterAlt( alt );
    101             }
    102         }
    103 
    104         public virtual void EnterSubRule( int decisionNumber )
    105         {
    106             for ( int i = 0; i < _listeners.Count; i++ )
    107             {
    108                 IDebugEventListener listener = _listeners[i];
    109                 listener.EnterSubRule( decisionNumber );
    110             }
    111         }
    112 
    113         public virtual void ExitSubRule( int decisionNumber )
    114         {
    115             for ( int i = 0; i < _listeners.Count; i++ )
    116             {
    117                 IDebugEventListener listener = _listeners[i];
    118                 listener.ExitSubRule( decisionNumber );
    119             }
    120         }
    121 
    122         public virtual void EnterDecision(int decisionNumber, bool couldBacktrack)
    123         {
    124             for ( int i = 0; i < _listeners.Count; i++ )
    125             {
    126                 IDebugEventListener listener = _listeners[i];
    127                 listener.EnterDecision( decisionNumber, couldBacktrack );
    128             }
    129         }
    130 
    131         public virtual void ExitDecision( int decisionNumber )
    132         {
    133             for ( int i = 0; i < _listeners.Count; i++ )
    134             {
    135                 IDebugEventListener listener = _listeners[i];
    136                 listener.ExitDecision( decisionNumber );
    137             }
    138         }
    139 
    140         public virtual void Location( int line, int pos )
    141         {
    142             for ( int i = 0; i < _listeners.Count; i++ )
    143             {
    144                 IDebugEventListener listener = _listeners[i];
    145                 listener.Location( line, pos );
    146             }
    147         }
    148 
    149         public virtual void ConsumeToken( IToken token )
    150         {
    151             for ( int i = 0; i < _listeners.Count; i++ )
    152             {
    153                 IDebugEventListener listener = _listeners[i];
    154                 listener.ConsumeToken( token );
    155             }
    156         }
    157 
    158         public virtual void ConsumeHiddenToken( IToken token )
    159         {
    160             for ( int i = 0; i < _listeners.Count; i++ )
    161             {
    162                 IDebugEventListener listener = _listeners[i];
    163                 listener.ConsumeHiddenToken( token );
    164             }
    165         }
    166 
    167         public virtual void LT( int index, IToken t )
    168         {
    169             for ( int i = 0; i < _listeners.Count; i++ )
    170             {
    171                 IDebugEventListener listener = _listeners[i];
    172                 listener.LT( index, t );
    173             }
    174         }
    175 
    176         public virtual void Mark( int index )
    177         {
    178             for ( int i = 0; i < _listeners.Count; i++ )
    179             {
    180                 IDebugEventListener listener = _listeners[i];
    181                 listener.Mark( index );
    182             }
    183         }
    184 
    185         public virtual void Rewind( int index )
    186         {
    187             for ( int i = 0; i < _listeners.Count; i++ )
    188             {
    189                 IDebugEventListener listener = _listeners[i];
    190                 listener.Rewind( index );
    191             }
    192         }
    193 
    194         public virtual void Rewind()
    195         {
    196             for ( int i = 0; i < _listeners.Count; i++ )
    197             {
    198                 IDebugEventListener listener = _listeners[i];
    199                 listener.Rewind();
    200             }
    201         }
    202 
    203         public virtual void BeginBacktrack( int level )
    204         {
    205             for ( int i = 0; i < _listeners.Count; i++ )
    206             {
    207                 IDebugEventListener listener = _listeners[i];
    208                 listener.BeginBacktrack( level );
    209             }
    210         }
    211 
    212         public virtual void EndBacktrack( int level, bool successful )
    213         {
    214             for ( int i = 0; i < _listeners.Count; i++ )
    215             {
    216                 IDebugEventListener listener = _listeners[i];
    217                 listener.EndBacktrack( level, successful );
    218             }
    219         }
    220 
    221         public virtual void RecognitionException( RecognitionException e )
    222         {
    223             for ( int i = 0; i < _listeners.Count; i++ )
    224             {
    225                 IDebugEventListener listener = _listeners[i];
    226                 listener.RecognitionException( e );
    227             }
    228         }
    229 
    230         public virtual void BeginResync()
    231         {
    232             for ( int i = 0; i < _listeners.Count; i++ )
    233             {
    234                 IDebugEventListener listener = _listeners[i];
    235                 listener.BeginResync();
    236             }
    237         }
    238 
    239         public virtual void EndResync()
    240         {
    241             for ( int i = 0; i < _listeners.Count; i++ )
    242             {
    243                 IDebugEventListener listener = _listeners[i];
    244                 listener.EndResync();
    245             }
    246         }
    247 
    248         public virtual void SemanticPredicate( bool result, string predicate )
    249         {
    250             for ( int i = 0; i < _listeners.Count; i++ )
    251             {
    252                 IDebugEventListener listener = _listeners[i];
    253                 listener.SemanticPredicate( result, predicate );
    254             }
    255         }
    256 
    257         public virtual void Commence()
    258         {
    259             for ( int i = 0; i < _listeners.Count; i++ )
    260             {
    261                 IDebugEventListener listener = _listeners[i];
    262                 listener.Commence();
    263             }
    264         }
    265 
    266         public virtual void Terminate()
    267         {
    268             for ( int i = 0; i < _listeners.Count; i++ )
    269             {
    270                 IDebugEventListener listener = _listeners[i];
    271                 listener.Terminate();
    272             }
    273         }
    274 
    275 
    276         #region Tree parsing stuff
    277 
    278         public virtual void ConsumeNode( object t )
    279         {
    280             for ( int i = 0; i < _listeners.Count; i++ )
    281             {
    282                 IDebugEventListener listener = _listeners[i];
    283                 listener.ConsumeNode( t );
    284             }
    285         }
    286 
    287         public virtual void LT( int index, object t )
    288         {
    289             for ( int i = 0; i < _listeners.Count; i++ )
    290             {
    291                 IDebugEventListener listener = _listeners[i];
    292                 listener.LT( index, t );
    293             }
    294         }
    295 
    296         #endregion
    297 
    298 
    299         #region AST Stuff
    300 
    301         public virtual void NilNode( object t )
    302         {
    303             for ( int i = 0; i < _listeners.Count; i++ )
    304             {
    305                 IDebugEventListener listener = _listeners[i];
    306                 listener.NilNode( t );
    307             }
    308         }
    309 
    310         public virtual void ErrorNode( object t )
    311         {
    312             for ( int i = 0; i < _listeners.Count; i++ )
    313             {
    314                 IDebugEventListener listener = _listeners[i];
    315                 listener.ErrorNode( t );
    316             }
    317         }
    318 
    319         public virtual void CreateNode( object t )
    320         {
    321             for ( int i = 0; i < _listeners.Count; i++ )
    322             {
    323                 IDebugEventListener listener = _listeners[i];
    324                 listener.CreateNode( t );
    325             }
    326         }
    327 
    328         public virtual void CreateNode( object node, IToken token )
    329         {
    330             for ( int i = 0; i < _listeners.Count; i++ )
    331             {
    332                 IDebugEventListener listener = _listeners[i];
    333                 listener.CreateNode( node, token );
    334             }
    335         }
    336 
    337         public virtual void BecomeRoot( object newRoot, object oldRoot )
    338         {
    339             for ( int i = 0; i < _listeners.Count; i++ )
    340             {
    341                 IDebugEventListener listener = _listeners[i];
    342                 listener.BecomeRoot( newRoot, oldRoot );
    343             }
    344         }
    345 
    346         public virtual void AddChild( object root, object child )
    347         {
    348             for ( int i = 0; i < _listeners.Count; i++ )
    349             {
    350                 IDebugEventListener listener = _listeners[i];
    351                 listener.AddChild( root, child );
    352             }
    353         }
    354 
    355         public virtual void SetTokenBoundaries( object t, int tokenStartIndex, int tokenStopIndex )
    356         {
    357             for ( int i = 0; i < _listeners.Count; i++ )
    358             {
    359                 IDebugEventListener listener = _listeners[i];
    360                 listener.SetTokenBoundaries( t, tokenStartIndex, tokenStopIndex );
    361             }
    362         }
    363 
    364         #endregion
    365     }
    366 }
    367