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.Token;
31import org.antlr.runtime.tree.TreeAdaptor;
32
33/** Print out (most of) the events... Useful for debugging, testing... */
34public class TraceDebugEventListener extends BlankDebugEventListener {
35	TreeAdaptor adaptor;
36
37	public TraceDebugEventListener(TreeAdaptor adaptor) {
38		this.adaptor = adaptor;
39	}
40
41	public void enterRule(String ruleName) { System.out.println("enterRule "+ruleName); }
42	public void exitRule(String ruleName) { System.out.println("exitRule "+ruleName); }
43	public void enterSubRule(int decisionNumber) { System.out.println("enterSubRule"); }
44	public void exitSubRule(int decisionNumber) { System.out.println("exitSubRule"); }
45	public void location(int line, int pos) {System.out.println("location "+line+":"+pos);}
46
47	// Tree parsing stuff
48
49	public void consumeNode(Object t) {
50		int ID = adaptor.getUniqueID(t);
51		String text = adaptor.getText(t);
52		int type = adaptor.getType(t);
53		System.out.println("consumeNode "+ID+" "+text+" "+type);
54	}
55
56	public void LT(int i, Object t) {
57		int ID = adaptor.getUniqueID(t);
58		String text = adaptor.getText(t);
59		int type = adaptor.getType(t);
60		System.out.println("LT "+i+" "+ID+" "+text+" "+type);
61	}
62
63
64	// AST stuff
65	public void nilNode(Object t) {System.out.println("nilNode "+adaptor.getUniqueID(t));}
66
67	public void createNode(Object t) {
68		int ID = adaptor.getUniqueID(t);
69		String text = adaptor.getText(t);
70		int type = adaptor.getType(t);
71		System.out.println("create "+ID+": "+text+", "+type);
72	}
73
74	public void createNode(Object node, Token token) {
75		int ID = adaptor.getUniqueID(node);
76		String text = adaptor.getText(node);
77		int tokenIndex = token.getTokenIndex();
78		System.out.println("create "+ID+": "+tokenIndex);
79	}
80
81	public void becomeRoot(Object newRoot, Object oldRoot) {
82		System.out.println("becomeRoot "+adaptor.getUniqueID(newRoot)+", "+
83						   adaptor.getUniqueID(oldRoot));
84	}
85
86	public void addChild(Object root, Object child) {
87		System.out.println("addChild "+adaptor.getUniqueID(root)+", "+
88						   adaptor.getUniqueID(child));
89	}
90
91	public void setTokenBoundaries(Object t, int tokenStartIndex, int tokenStopIndex) {
92		System.out.println("setTokenBoundaries "+adaptor.getUniqueID(t)+", "+
93						   tokenStartIndex+", "+tokenStopIndex);
94	}
95}
96
97