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 Antlr.Runtime.JavaExtensions;
     36 
     37     using Console = System.Console;
     38     using IOException = System.IO.IOException;
     39 
     40     public class DebugParser : Parser
     41     {
     42         /** <summary>Who to notify when events in the parser occur.</summary> */
     43         public IDebugEventListener dbg = null;
     44 
     45         /** <summary>
     46          *  Used to differentiate between fixed lookahead and cyclic DFA decisions
     47          *  while profiling.
     48          *  </summary>
     49          */
     50         public bool isCyclicDecision = false;
     51 
     52         /** <summary>
     53          *  Create a normal parser except wrap the token stream in a debug
     54          *  proxy that fires consume events.
     55          *  </summary>
     56          */
     57         public DebugParser( ITokenStream input, IDebugEventListener dbg, RecognizerSharedState state )
     58             : base( input is DebugTokenStream ? input : new DebugTokenStream( input, dbg ), state )
     59         {
     60             SetDebugListener(dbg);
     61         }
     62 
     63         public DebugParser( ITokenStream input, RecognizerSharedState state )
     64             : base( input is DebugTokenStream ? input : new DebugTokenStream( input, null ), state )
     65         {
     66         }
     67 
     68         public DebugParser( ITokenStream input, IDebugEventListener dbg )
     69             : this( input is DebugTokenStream ? input : new DebugTokenStream( input, dbg ), dbg, null )
     70         {
     71         }
     72 
     73         public override IDebugEventListener DebugListener
     74         {
     75             get
     76             {
     77                 return dbg;
     78             }
     79         }
     80 
     81         /** <summary>
     82          *  Provide a new debug event listener for this parser.  Notify the
     83          *  input stream too that it should send events to this listener.
     84          *  </summary>
     85          */
     86         public virtual void SetDebugListener(IDebugEventListener value)
     87         {
     88             DebugTokenStream debugTokenStream = input as DebugTokenStream;
     89             if (debugTokenStream != null)
     90                 debugTokenStream.DebugListener = value;
     91 
     92             dbg = value;
     93         }
     94 
     95         public virtual void ReportError( IOException e )
     96         {
     97             Console.Error.WriteLine( e );
     98             ExceptionExtensions.PrintStackTrace( e, Console.Error );
     99         }
    100 
    101         public override void BeginResync()
    102         {
    103             dbg.BeginResync();
    104             base.BeginResync();
    105         }
    106 
    107         public override void EndResync()
    108         {
    109             dbg.EndResync();
    110             base.EndResync();
    111         }
    112 
    113         public virtual void BeginBacktrack( int level )
    114         {
    115             dbg.BeginBacktrack( level );
    116         }
    117 
    118         public virtual void EndBacktrack( int level, bool successful )
    119         {
    120             dbg.EndBacktrack( level, successful );
    121         }
    122 
    123         public override void ReportError( RecognitionException e )
    124         {
    125             base.ReportError(e);
    126             dbg.RecognitionException( e );
    127         }
    128     }
    129 }
    130