Home | History | Annotate | Download | only in debug
      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  */
     28 package org.antlr.runtime.debug;
     29 
     30 import org.antlr.runtime.*;
     31 
     32 import java.io.IOException;
     33 
     34 public class DebugParser extends Parser {
     35 	/** Who to notify when events in the parser occur. */
     36 	protected DebugEventListener dbg = null;
     37 
     38 	/** Used to differentiate between fixed lookahead and cyclic DFA decisions
     39 	 *  while profiling.
     40  	 */
     41 	public boolean isCyclicDecision = false;
     42 
     43 	/** Create a normal parser except wrap the token stream in a debug
     44 	 *  proxy that fires consume events.
     45 	 */
     46 	public DebugParser(TokenStream input, DebugEventListener dbg, RecognizerSharedState state) {
     47 		super(input instanceof DebugTokenStream?input:new DebugTokenStream(input,dbg), state);
     48 		setDebugListener(dbg);
     49 	}
     50 
     51 	public DebugParser(TokenStream input, RecognizerSharedState state) {
     52 		super(input instanceof DebugTokenStream?input:new DebugTokenStream(input,null), state);
     53 	}
     54 
     55 	public DebugParser(TokenStream input, DebugEventListener dbg) {
     56 		this(input instanceof DebugTokenStream?input:new DebugTokenStream(input,dbg), dbg, null);
     57 	}
     58 
     59 	/** Provide a new debug event listener for this parser.  Notify the
     60 	 *  input stream too that it should send events to this listener.
     61 	 */
     62 	public void setDebugListener(DebugEventListener dbg) {
     63 		if ( input instanceof DebugTokenStream ) {
     64 			((DebugTokenStream)input).setDebugListener(dbg);
     65 		}
     66 		this.dbg = dbg;
     67 	}
     68 
     69 	public DebugEventListener getDebugListener() {
     70 		return dbg;
     71 	}
     72 
     73 	public void reportError(IOException e) {
     74 		System.err.println(e);
     75 		e.printStackTrace(System.err);
     76 	}
     77 
     78 	public void beginResync() {
     79 		dbg.beginResync();
     80 	}
     81 
     82 	public void endResync() {
     83 		dbg.endResync();
     84 	}
     85 
     86 	public void beginBacktrack(int level) {
     87 		dbg.beginBacktrack(level);
     88 	}
     89 
     90 	public void endBacktrack(int level, boolean successful) {
     91 		dbg.endBacktrack(level,successful);
     92 	}
     93 
     94 	public void reportError(RecognitionException e) {
     95 		super.reportError(e);
     96 		dbg.recognitionException(e);
     97 	}
     98 }
     99