1 /*
2  * Note to JL: Refactored extension methods
3  *
4  * [The "BSD licence"]
5  * Copyright (c) 2005-2008 Terence Parr
6  * All rights reserved.
7  *
8  * Conversion to C#:
9  * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 namespace Antlr.Runtime.Debug {
36     using Antlr.Runtime.JavaExtensions;
37 
38     using Console = System.Console;
39     using IOException = System.IO.IOException;
40     using ITreeNodeStream = Antlr.Runtime.Tree.ITreeNodeStream;
41     using TreeParser = Antlr.Runtime.Tree.TreeParser;
42 
43     public class DebugTreeParser : TreeParser {
44         /** <summary>Who to notify when events in the parser occur.</summary> */
45         public IDebugEventListener dbg = null;
46 
47         /** <summary>
48          *  Used to differentiate between fixed lookahead and cyclic DFA decisions
49          *  while profiling.
50          *  </summary>
51          */
52         public bool isCyclicDecision = false;
53 
54         /** <summary>
55          *  Create a normal parser except wrap the token stream in a debug
56          *  proxy that fires consume events.
57          *  </summary>
58          */
DebugTreeParser(ITreeNodeStream input, IDebugEventListener dbg, RecognizerSharedState state)59         public DebugTreeParser(ITreeNodeStream input, IDebugEventListener dbg, RecognizerSharedState state)
60             : base(input is DebugTreeNodeStream ? input : new DebugTreeNodeStream(input, dbg), state) {
61             SetDebugListener(dbg);
62         }
63 
DebugTreeParser(ITreeNodeStream input, RecognizerSharedState state)64         public DebugTreeParser(ITreeNodeStream input, RecognizerSharedState state)
65             : base(input is DebugTreeNodeStream ? input : new DebugTreeNodeStream(input, null), state) {
66         }
67 
DebugTreeParser(ITreeNodeStream input, IDebugEventListener dbg)68         public DebugTreeParser(ITreeNodeStream input, IDebugEventListener dbg)
69             : this(input is DebugTreeNodeStream ? input : new DebugTreeNodeStream(input, dbg), dbg, null) {
70         }
71 
72         public override IDebugEventListener DebugListener {
73             get {
74                 return dbg;
75             }
76         }
77 
78         /** <summary>
79          *  Provide a new debug event listener for this parser.  Notify the
80          *  input stream too that it should send events to this listener.
81          *  </summary>
82          */
SetDebugListener(IDebugEventListener value)83         public virtual void SetDebugListener(IDebugEventListener value) {
84             if (input is DebugTreeNodeStream)
85                 ((DebugTreeNodeStream)input).DebugListener = value;
86 
87             this.dbg = value;
88         }
89 
ReportError(IOException e)90         public virtual void ReportError(IOException e) {
91             Console.Error.WriteLine(e);
92             ExceptionExtensions.PrintStackTrace(e, Console.Error);
93         }
94 
ReportError(RecognitionException e)95         public override void ReportError(RecognitionException e) {
96             dbg.RecognitionException(e);
97         }
98 
GetMissingSymbol(IIntStream input, RecognitionException e, int expectedTokenType, BitSet follow)99         protected override object GetMissingSymbol(IIntStream input,
100                                           RecognitionException e,
101                                           int expectedTokenType,
102                                           BitSet follow) {
103             object o = base.GetMissingSymbol(input, e, expectedTokenType, follow);
104             dbg.ConsumeNode(o);
105             return o;
106         }
107 
BeginResync()108         public override void BeginResync() {
109             dbg.BeginResync();
110         }
111 
EndResync()112         public override void EndResync() {
113             dbg.EndResync();
114         }
115 
BeginBacktrack(int level)116         public virtual void BeginBacktrack(int level) {
117             dbg.BeginBacktrack(level);
118         }
119 
EndBacktrack(int level, bool successful)120         public virtual void EndBacktrack(int level, bool successful) {
121             dbg.EndBacktrack(level, successful);
122         }
123     }
124 }
125