13447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/*
23447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein [The "BSD license"]
33447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Copyright (c) 2005-2009 Terence Parr
43447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein All rights reserved.
53447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
63447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Redistribution and use in source and binary forms, with or without
73447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein modification, are permitted provided that the following conditions
83447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein are met:
93447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1. Redistributions of source code must retain the above copyright
103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     notice, this list of conditions and the following disclaimer.
113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2. Redistributions in binary form must reproduce the above copyright
123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     notice, this list of conditions and the following disclaimer in the
133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     documentation and/or other materials provided with the distribution.
143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 3. The name of the author may not be used to endorse or promote products
153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     derived from this software without specific prior written permission.
163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */
283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpackage org.antlr.runtime.debug;
293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.*;
313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.tree.TreeNodeStream;
323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.tree.TreeParser;
333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport java.io.IOException;
353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic class DebugTreeParser extends TreeParser {
373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Who to notify when events in the parser occur. */
383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	protected DebugEventListener dbg = null;
393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Used to differentiate between fixed lookahead and cyclic DFA decisions
413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  while profiling.
423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 	 */
433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public boolean isCyclicDecision = false;
443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Create a normal parser except wrap the token stream in a debug
463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  proxy that fires consume events.
473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public DebugTreeParser(TreeNodeStream input, DebugEventListener dbg, RecognizerSharedState state) {
493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		super(input instanceof DebugTreeNodeStream?input:new DebugTreeNodeStream(input,dbg), state);
503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		setDebugListener(dbg);
513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public DebugTreeParser(TreeNodeStream input, RecognizerSharedState state) {
543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		super(input instanceof DebugTreeNodeStream?input:new DebugTreeNodeStream(input,null), state);
553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public DebugTreeParser(TreeNodeStream input, DebugEventListener dbg) {
583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this(input instanceof DebugTreeNodeStream?input:new DebugTreeNodeStream(input,dbg), dbg, null);
593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Provide a new debug event listener for this parser.  Notify the
623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  input stream too that it should send events to this listener.
633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setDebugListener(DebugEventListener dbg) {
653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( input instanceof DebugTreeNodeStream ) {
663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein			((DebugTreeNodeStream)input).setDebugListener(dbg);
673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		}
683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.dbg = dbg;
693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public DebugEventListener getDebugListener() {
723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return dbg;
733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void reportError(IOException e) {
763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		System.err.println(e);
773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		e.printStackTrace(System.err);
783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void reportError(RecognitionException e) {
813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		dbg.recognitionException(e);
823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	protected Object getMissingSymbol(IntStream input,
853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein									  RecognitionException e,
863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein									  int expectedTokenType,
873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein									  BitSet follow)
883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	{
893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		Object o = super.getMissingSymbol(input, e, expectedTokenType, follow);
903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		dbg.consumeNode(o);
913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return o;
923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void beginResync() {
953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		dbg.beginResync();
963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void endResync() {
993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		dbg.endResync();
1003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void beginBacktrack(int level) {
1033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		dbg.beginBacktrack(level);
1043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void endBacktrack(int level, boolean successful) {
1073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		dbg.endBacktrack(level,successful);
1083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein}
110