package org.antlr.works.debugger.tivo;

import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.debug.DebugEventListener;
import org.antlr.runtime.debug.RemoteDebugEventSocketListener;
import org.antlr.runtime.debug.RemoteDebugEventSocketListener.ProxyTree;
import org.antlr.works.debugger.events.DBEvent;
import org.antlr.works.debugger.events.DBEventFactory;
import org.antlr.works.debugger.tree.DBTreeToken;
/*

[The "BSD licence"]
Copyright (c) 2005-2006 Jean Bovet
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

public class DBRecorderEventListener implements DebugEventListener {

    public DBRecorder recorder;

	public DBRecorderEventListener(DBRecorder recorder) {
        this.recorder = recorder;
    }

    public void event(DBEvent event) {
        recorder.listenerEvent(event);
    }

    public void commence() {
        event(DBEventFactory.createCommence());
    }

    public void terminate() {
        event(DBEventFactory.createTerminate());
    }

    public void enterRule(String grammarFileName, String ruleName) {
        event(DBEventFactory.createEnterRule(grammarFileName, ruleName));
    }

    public void exitRule(String grammarFileName, String ruleName) {
        event(DBEventFactory.createExitRule(grammarFileName, ruleName));
    }

    public void enterSubRule(int decisionNumber) {
        event(DBEventFactory.createEnterSubRule(decisionNumber));
    }

    public void exitSubRule(int decisionNumber) {
        event(DBEventFactory.createExitSubRule(decisionNumber));
    }

    public void enterDecision(int decisionNumber) {
        event(DBEventFactory.createEnterDecision(decisionNumber));
    }

    public void exitDecision(int decisionNumber) {
        event(DBEventFactory.createExitDecision(decisionNumber));
    }

    public void enterAlt(int alt) {
        event(DBEventFactory.createEnterAlt(alt));
    }

    public void location(int line, int pos) {
        event(DBEventFactory.createLocation(line, pos));
    }

    public void consumeToken(Token token) {
        event(DBEventFactory.createConsumeToken(token));
    }

    public void consumeHiddenToken(Token token) {
        event(DBEventFactory.createConsumeHiddenToken(token));
    }

    public void LT(int i, Token token) {
        event(DBEventFactory.createLT(i, token));
    }

    public void mark(int i) {
        event(DBEventFactory.createMark(i));
    }

    public void rewind(int i) {
        event(DBEventFactory.createRewind(i));
    }

    public void rewind() {
        event(DBEventFactory.createRewind());
    }

    public void beginBacktrack(int level) {
        event(DBEventFactory.createBeginBacktrack(level));
    }

    public void endBacktrack(int level, boolean successful) {
        event(DBEventFactory.createEndBacktrack(level, successful));
    }

    public void recognitionException(RecognitionException e) {
        event(DBEventFactory.createRecognitionException(e));
    }

    public void beginResync() {
        event(DBEventFactory.createBeginResync());
    }

    public void endResync() {
        event(DBEventFactory.createEndResync());
    }

    public void semanticPredicate(boolean result, String predicate) {
        /** Currently ignored */
    }

    /** AST events */

	public void nilNode(Object t) {
		RemoteDebugEventSocketListener.ProxyTree p = (RemoteDebugEventSocketListener.ProxyTree)t;
		event(DBEventFactory.createNilNode(p.ID));
	}

	public void errorNode(Object t) {
		RemoteDebugEventSocketListener.ProxyTree p = (RemoteDebugEventSocketListener.ProxyTree)t;
		event(DBEventFactory.createErrorNode(p.ID, p.text, p.type));
	}

    public void createNode(Object t) {
		RemoteDebugEventSocketListener.ProxyTree p = (RemoteDebugEventSocketListener.ProxyTree)t;
		event(DBEventFactory.createCreateNode(p.ID, p.text, p.type));
    }

    public void createNode(Object node, Token token) {
		RemoteDebugEventSocketListener.ProxyTree p = (RemoteDebugEventSocketListener.ProxyTree)node;
        event(DBEventFactory.createCreateNode(p.ID, token.getTokenIndex()));
    }

    public void becomeRoot(Object newRoot, Object oldRoot) {
		RemoteDebugEventSocketListener.ProxyTree n = (RemoteDebugEventSocketListener.ProxyTree)newRoot;
		RemoteDebugEventSocketListener.ProxyTree o = (RemoteDebugEventSocketListener.ProxyTree)oldRoot;
        event(DBEventFactory.createBecomeRoot(n.ID, o.ID));
    }

    public void addChild(Object root, Object child) {
		RemoteDebugEventSocketListener.ProxyTree r = (RemoteDebugEventSocketListener.ProxyTree)root;
		RemoteDebugEventSocketListener.ProxyTree c = (RemoteDebugEventSocketListener.ProxyTree)child;
        event(DBEventFactory.createAddChild(r.ID, c.ID));
    }

    public void setTokenBoundaries(Object t, int tokenStartIndex, int tokenStopIndex) {
		RemoteDebugEventSocketListener.ProxyTree p = (RemoteDebugEventSocketListener.ProxyTree)t;
        event(DBEventFactory.createSetTokenBoundaries(p.ID, tokenStartIndex, tokenStopIndex));
    }

    /** Tree parsing */

    public void consumeNode(Object t) {
		ProxyTree p = (ProxyTree)t;
        /** Create a special kind of token holding information about the tree node. This allow
         * us to use the same method for token parser and tree parser.
         */
		event(DBEventFactory.createConsumeToken(new DBTreeToken(p)));
    }

    public void LT(int i, Object t) {
		ProxyTree p = (ProxyTree)t;
        /** See consumeNode() comment */
        event(DBEventFactory.createLT(i, new DBTreeToken(p)));
    }

}
