1/*
2 [The "BSD license"]
3 Copyright (c) 2005-2009 Terence Parr
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12     notice, this list of conditions and the following disclaimer in the
13     documentation and/or other materials provided with the distribution.
14 3. The name of the author may not be used to endorse or promote products
15     derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28package org.antlr.runtime.debug;
29
30import org.antlr.runtime.*;
31import org.antlr.runtime.tree.TreeNodeStream;
32import org.antlr.runtime.tree.TreeParser;
33
34import java.io.IOException;
35
36public class DebugTreeParser extends TreeParser {
37	/** Who to notify when events in the parser occur. */
38	protected DebugEventListener dbg = null;
39
40	/** Used to differentiate between fixed lookahead and cyclic DFA decisions
41	 *  while profiling.
42 	 */
43	public boolean isCyclicDecision = false;
44
45	/** Create a normal parser except wrap the token stream in a debug
46	 *  proxy that fires consume events.
47	 */
48	public DebugTreeParser(TreeNodeStream input, DebugEventListener dbg, RecognizerSharedState state) {
49		super(input instanceof DebugTreeNodeStream?input:new DebugTreeNodeStream(input,dbg), state);
50		setDebugListener(dbg);
51	}
52
53	public DebugTreeParser(TreeNodeStream input, RecognizerSharedState state) {
54		super(input instanceof DebugTreeNodeStream?input:new DebugTreeNodeStream(input,null), state);
55	}
56
57	public DebugTreeParser(TreeNodeStream input, DebugEventListener dbg) {
58		this(input instanceof DebugTreeNodeStream?input:new DebugTreeNodeStream(input,dbg), dbg, null);
59	}
60
61	/** Provide a new debug event listener for this parser.  Notify the
62	 *  input stream too that it should send events to this listener.
63	 */
64	public void setDebugListener(DebugEventListener dbg) {
65		if ( input instanceof DebugTreeNodeStream ) {
66			((DebugTreeNodeStream)input).setDebugListener(dbg);
67		}
68		this.dbg = dbg;
69	}
70
71	public DebugEventListener getDebugListener() {
72		return dbg;
73	}
74
75	public void reportError(IOException e) {
76		System.err.println(e);
77		e.printStackTrace(System.err);
78	}
79
80	public void reportError(RecognitionException e) {
81		dbg.recognitionException(e);
82	}
83
84	protected Object getMissingSymbol(IntStream input,
85									  RecognitionException e,
86									  int expectedTokenType,
87									  BitSet follow)
88	{
89		Object o = super.getMissingSymbol(input, e, expectedTokenType, follow);
90		dbg.consumeNode(o);
91		return o;
92	}
93
94	public void beginResync() {
95		dbg.beginResync();
96	}
97
98	public void endResync() {
99		dbg.endResync();
100	}
101
102	public void beginBacktrack(int level) {
103		dbg.beginBacktrack(level);
104	}
105
106	public void endBacktrack(int level, boolean successful) {
107		dbg.endBacktrack(level,successful);
108	}
109}
110