1// [The "BSD licence"]
2// Copyright (c) 2006-2007 Kay Roepke
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9//    notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11//    notice, this list of conditions and the following disclaimer in the
12//    documentation and/or other materials provided with the distribution.
13// 3. The name of the author may not be used to endorse or promote products
14//    derived from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#import "ANTLRDebugTreeParser.h"
28
29
30@implementation ANTLRDebugTreeParser
31
32- (id) initWithTreeNodeStream:(id<ANTLRTreeNodeStream>)theStream
33{
34	return [self initWithTreeNodeStream:theStream debugListener:nil debuggerPort:-1];
35}
36
37- (id) initWithTreeNodeStream:(id<ANTLRTreeNodeStream>)theStream
38				 debuggerPort:(NSInteger)portNumber
39{
40	return [self initWithTreeNodeStream:theStream debugListener:nil debuggerPort:portNumber];
41}
42
43- (id) initWithTreeNodeStream:(id<ANTLRTreeNodeStream>)theStream
44				debugListener:(id<ANTLRDebugEventListener>)theDebugListener
45				 debuggerPort:(NSInteger)portNumber
46{
47	id<ANTLRDebugEventListener,NSObject> debugger = nil;
48	id<ANTLRTreeNodeStream> treeNodeStream = nil;
49	if (theDebugListener) {
50		debugger = (id<ANTLRDebugEventListener>)theDebugListener;
51	} else {
52		debugger = [[ANTLRDebugEventProxy alloc] initWithGrammarName:[self grammarFileName] debuggerPort:portNumber];
53	}
54	if (theStream && ![theStream isKindOfClass:[ANTLRDebugTreeNodeStream class]]) {
55		treeNodeStream = [[ANTLRDebugTreeNodeStream alloc] initWithTreeNodeStream:theStream debugListener:debugger];
56	} else {
57		treeNodeStream = theStream;
58	}
59	self = [super initWithStream:treeNodeStream];
60	if ( self ) {
61		[self setDebugListener:debugger];
62		//[debugger release];
63		//[treeNodeStream release];
64		[debugListener waitForDebuggerConnection];
65	}
66	return self;
67}
68
69- (void) dealloc
70{
71    [self setDebugListener: nil];
72    [super dealloc];
73}
74
75- (id<ANTLRDebugEventListener>) debugListener
76{
77    return debugListener; 
78}
79
80- (void) setDebugListener: (id<ANTLRDebugEventListener>) aDebugListener
81{
82    if (debugListener != aDebugListener) {
83        [(id<ANTLRDebugEventListener,NSObject>)aDebugListener retain];
84        [(id<ANTLRDebugEventListener,NSObject>)debugListener release];
85        debugListener = aDebugListener;
86    }
87}
88
89#pragma mark -
90#pragma mark Overrides
91
92- (void) beginResync
93{
94	[debugListener beginResync];
95}
96
97- (void) endResync
98{
99	[debugListener endResync];
100}
101- (void)beginBacktracking:(NSInteger)level
102{
103	[debugListener beginBacktrack:level];
104}
105
106- (void)endBacktracking:(NSInteger)level wasSuccessful:(BOOL)successful
107{
108	[debugListener endBacktrack:level wasSuccessful:successful];
109}
110
111- (void) recoverFromMismatchedToken:(id<ANTLRIntStream>)inputStream 
112						  exception:(NSException *)e 
113						  tokenType:(ANTLRTokenType)ttype 
114							 follow:(ANTLRBitSet *)follow
115{
116#warning TODO: recoverFromMismatchedToken in debugger
117	[self recoverFromMismatchedToken:inputStream exception:e follow:follow];
118}
119
120- (void) recoverFromMismatchedSet:(id<ANTLRIntStream>)inputStream
121						exception:(NSException *)e
122						   follow:(ANTLRBitSet *)follow
123{
124#warning TODO: recoverFromMismatchedSet in debugger
125	[super recoverFromMismatchedSet:inputStream];
126}
127
128@end
129