Browse files

Fixes submitted by Yspadadden to fix memory leak in parser.

  • Loading branch information...
1 parent 69a5b0b commit 759de3d6d6d46635a9064e4d99ec5dc5b324797b Alan Condit committed Apr 19, 2012
View
18 runtime/ObjC/Framework/Parser.m
@@ -41,18 +41,18 @@ + (Parser *)newParser:(id<TokenStream>)anInput State:(RecognizerSharedState *)aS
- (id) initWithTokenStream:(id<TokenStream>)theStream
{
- if ((self = [super init]) != nil) {
- input = theStream;
- }
- return self;
+ if ((self = [super init]) != nil) {
+ [self setInput:theStream];
+ }
+ return self;
}
- (id) initWithTokenStream:(id<TokenStream>)theStream State:(RecognizerSharedState *)aState
{
- if ((self = [super initWithState:aState]) != nil) {
- input = theStream;
- }
- return self;
+ if ((self = [super initWithState:aState]) != nil) {
+ [self setInput:theStream];
+ }
+ return self;
}
- (void) reset
@@ -68,7 +68,7 @@ - (void) dealloc
#ifdef DEBUG_DEALLOC
NSLog( @"called dealloc in Parser" );
#endif
- [self setInput:nil];
+ [input release];
[super dealloc];
}
View
56 runtime/ObjC/Framework/UnbufferedCommonTreeNodeStream.h
@@ -26,26 +26,26 @@
#import <Cocoa/Cocoa.h>
-#import "ANTLRTreeNodeStream.h"
-#import "ANTLRCommonTokenStream.h"
-#import "ANTLRCommonTree.h"
-#import "ANTLRCommonTreeAdaptor.h"
+#import "TreeNodeStream.h"
+#import "CommonTokenStream.h"
+#import "CommonTree.h"
+#import "CommonTreeAdaptor.h"
-@interface ANTLRUnbufferedCommonTreeNodeStream : NSObject < ANTLRTreeNodeStream > {
+@interface ANTLRUnbufferedCommonTreeNodeStream : NSObject < TreeNodeStream > {
BOOL shouldUseUniqueNavigationNodes;
- ANTLRCommonTree *root;
- ANTLRCommonTree *currentNode;
- ANTLRCommonTree *previousNode;
+ CommonTree *root;
+ CommonTree *currentNode;
+ CommonTree *previousNode;
- id<ANTLRTreeAdaptor> treeAdaptor;
+ id<TreeAdaptor> treeAdaptor;
- id<ANTLRTokenStream> tokenStream;
+ id<TokenStream> tokenStream;
NSMutableArray *nodeStack;
NSMutableArray *indexStack;
- ANTLRPtrBuffer *markers;
+ PtrBuffer *markers;
NSInteger lastMarker;
NSInteger currentChildIndex;
@@ -56,34 +56,34 @@
NSUInteger tail;
}
-@property (retain, getter=getRoot, setter=setRoot:) ANTLRCommonTree *root;
-@property (retain, getter=getCurrentNode, setter=setCurrentNode:) ANTLRCommonTree *currentNode;
-@property (retain, getter=getPreviousNode, setter=setPreviousNode:) ANTLRCommonTree *previousNode;
-@property (retain, getter=getTreeAdaptor, setter=setTreeAdaptor:) id<ANTLRTreeAdaptor> treeAdaptor;
-@property (retain, getter=getTokenStream, setter=setTokenStream:) id<ANTLRTokenStream> tokenStream;
+@property (retain, getter=getRoot, setter=setRoot:) CommonTree *root;
+@property (retain, getter=getCurrentNode, setter=setCurrentNode:) CommonTree *currentNode;
+@property (retain, getter=getPreviousNode, setter=setPreviousNode:) CommonTree *previousNode;
+@property (retain, getter=getTreeAdaptor, setter=setTreeAdaptor:) id<TreeAdaptor> treeAdaptor;
+@property (retain, getter=getTokenStream, setter=setTokenStream:) id<TokenStream> tokenStream;
@property (retain, getter=getNodeStack, setter=setNodeStack:) NSMutableArray *nodeStack;
@property (retain, getter=getIndexStack, setter=setIndexStackStack:) NSMutableArray *indexStack;
-@property (retain, getter=getMarkers, setter=setMarkers:) ANTLRPtrBuffer *markers;
+@property (retain, getter=getMarkers, setter=setMarkers:) PtrBuffer *markers;
@property (assign, getter=getLastMarker, setter=setLastMarker:) NSInteger lastMarker;
@property (assign, getter=getCurrentChildIndex, setter=setCurrentChildIndex:) NSInteger currentChildIndex;
@property (assign, getter=getAbsoluteNodeIndex, setter=setAbsoluteNodeIndex:) NSInteger absoluteNodeIndex;
@property (retain, getter=getLookahead, setter=setLookahead:) NSMutableArray *lookahead;
@property (assign, getter=getHead, setter=setHead:) NSUInteger head;
@property (assign, getter=getTail, setter=setTail:) NSUInteger tail;
-- (id) initWithTree:(ANTLRCommonTree *)theTree;
-- (id) initWithTree:(ANTLRCommonTree *)theTree treeAdaptor:(ANTLRCommonTreeAdaptor *)theAdaptor;
+- (id) initWithTree:(CommonTree *)theTree;
+- (id) initWithTree:(CommonTree *)theTree treeAdaptor:(CommonTreeAdaptor *)theAdaptor;
- (void) reset;
#pragma mark ANTLRTreeNodeStream conformance
- (id) LT:(NSInteger)k;
- (id) treeSource;
-- (id<ANTLRTreeAdaptor>) getTreeAdaptor;
-- (void)setTreeAdaptor:(id<ANTLRTreeAdaptor>)aTreeAdaptor;
-- (id<ANTLRTokenStream>) getTokenStream;
-- (void) setTokenStream:(id<ANTLRTokenStream>)aTokenStream; ///< Added by subclass, not in protocol
+- (id<TreeAdaptor>) getTreeAdaptor;
+- (void)setTreeAdaptor:(id<TreeAdaptor>)aTreeAdaptor;
+- (id<TokenStream>) getTokenStream;
+- (void) setTokenStream:(id<TokenStream>)aTokenStream; ///< Added by subclass, not in protocol
- (void) setUsesUniqueNavigationNodes:(BOOL)flag;
- (id) nodeAtIndex:(NSUInteger) idx;
@@ -104,19 +104,19 @@
- (NSUInteger) size;
#pragma mark Lookahead Handling
-- (void) addLookahead:(id<ANTLRBaseTree>)aNode;
+- (void) addLookahead:(id<BaseTree>)aNode;
- (NSUInteger) lookaheadSize;
- (void) fillBufferWithLookahead:(NSInteger)k;
- (id) nextObject;
#pragma mark Node visiting
-- (ANTLRCommonTree *) handleRootNode;
-- (ANTLRCommonTree *) visitChild:(NSInteger)childNumber;
+- (CommonTree *) handleRootNode;
+- (CommonTree *) visitChild:(NSInteger)childNumber;
- (void) walkBackToMostRecentNodeWithUnvisitedChildren;
- (void) addNavigationNodeWithType:(NSInteger)tokenType;
#pragma mark Accessors
-- (ANTLRCommonTree *) root;
-- (void) setRoot: (ANTLRCommonTree *) aRoot;
+- (CommonTree *) root;
+- (void) setRoot: (CommonTree *) aRoot;
@end
View
60 runtime/ObjC/Framework/UnbufferedCommonTreeNodeStream.m
@@ -25,10 +25,10 @@
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#import "ANTLRUnbufferedCommonTreeNodeStream.h"
-#import "ANTLRUnbufferedCommonTreeNodeStreamState.h"
-#import "ANTLRBaseTree.h"
-#import "ANTLRToken.h"
+#import "UnbufferedCommonTreeNodeStream.h"
+#import "UnbufferedCommonTreeNodeStreamState.h"
+#import "BaseTree.h"
+#import "Token.h"
#define INITIAL_LOOKAHEAD_BUFFER_SIZE 5
@implementation ANTLRUnbufferedCommonTreeNodeStream
@@ -48,22 +48,22 @@ @implementation ANTLRUnbufferedCommonTreeNodeStream
@synthesize head;
@synthesize tail;
-- (id) initWithTree:(ANTLRCommonTree *)theTree
+- (id) initWithTree:(CommonTree *)theTree
{
return [self initWithTree:theTree treeAdaptor:nil];
}
-- (id) initWithTree:(ANTLRCommonTree *)theTree treeAdaptor:(ANTLRCommonTreeAdaptor *)theAdaptor
+- (id) initWithTree:(CommonTree *)theTree treeAdaptor:(CommonTreeAdaptor *)theAdaptor
{
if ((self = [super init]) != nil) {
[self setRoot:theTree];
if ( theAdaptor == nil )
- [self setTreeAdaptor:[ANTLRCommonTreeAdaptor newTreeAdaptor]];
+ [self setTreeAdaptor:[CommonTreeAdaptor newTreeAdaptor]];
else
[self setTreeAdaptor:theAdaptor];
nodeStack = [[NSMutableArray arrayWithCapacity:5] retain];
indexStack = [[NSMutableArray arrayWithCapacity:5] retain];
- markers = [[ANTLRPtrBuffer newANTLRPtrBufferWithLen:100] retain];
+ markers = [[PtrBuffer newPtrBufferWithLen:100] retain];
// [markers insertObject:[NSNull null] atIndex:0]; // markers is one based - maybe fix this later
lookahead = [NSMutableArray arrayWithCapacity:INITIAL_LOOKAHEAD_BUFFER_SIZE]; // lookahead is filled with [NSNull null] in -reset
[lookahead retain];
@@ -113,7 +113,7 @@ - (id) LT:(NSInteger)k
if (k < 0)
@throw [NSException exceptionWithName:@"ANTLRTreeException" reason:@"-LT: looking back more than one node unsupported for unbuffered streams" userInfo:nil];
if (k == 0)
- return ANTLRBaseTree.INVALID_NODE;
+ return BaseTree.INVALID_NODE;
[self fillBufferWithLookahead:k];
return [lookahead objectAtIndex:(head+k-1) % [lookahead count]];
}
@@ -123,12 +123,12 @@ - (id) treeSource
return [self root];
}
-- (id<ANTLRTreeAdaptor>) getTreeAdaptor;
+- (id<TreeAdaptor>) getTreeAdaptor;
{
return treeAdaptor;
}
-- (void)setTreeAdaptor:(id<ANTLRTreeAdaptor>)aTreeAdaptor
+- (void)setTreeAdaptor:(id<TreeAdaptor>)aTreeAdaptor
{
if (treeAdaptor != aTreeAdaptor) {
[aTreeAdaptor retain];
@@ -137,12 +137,12 @@ - (void)setTreeAdaptor:(id<ANTLRTreeAdaptor>)aTreeAdaptor
}
}
-- (id<ANTLRTokenStream>) getTokenStream
+- (id<TokenStream>) getTokenStream
{
return tokenStream;
}
-- (void) setTokenStream:(id<ANTLRTokenStream>)aTokenStream
+- (void) setTokenStream:(id<TokenStream>)aTokenStream
{
if (tokenStream != aTokenStream) {
[tokenStream release];
@@ -188,9 +188,9 @@ - (void) consume
- (NSInteger) LA:(NSUInteger) i
{
- ANTLRCommonTree *node = [self LT:i];
+ CommonTree *node = [self LT:i];
if (!node)
- return ANTLRTokenTypeInvalid;
+ return TokenTypeInvalid;
int ttype = [node getType];
return ttype;
}
@@ -270,7 +270,7 @@ - (NSUInteger) size;
#pragma mark Lookahead Handling
-- (void) addLookahead:(id<ANTLRBaseTree>)aNode
+- (void) addLookahead:(id<BaseTree>)aNode
{
[lookahead replaceObjectAtIndex:tail withObject:aNode];
tail = (tail+1) % [lookahead count];
@@ -319,7 +319,7 @@ - (id) nextObject
// NOTE: this could/should go into an NSEnumerator subclass for treenode streams.
if (currentNode == nil) {
if ( navigationNodeEOF == nil ) {
- navigationNodeEOF = [[ANTLRTreeNavigationNodeEOF alloc] init];
+ navigationNodeEOF = [[TreeNavigationNodeEOF alloc] init];
}
[self addLookahead:navigationNodeEOF];
return nil;
@@ -339,9 +339,9 @@ - (id) nextObject
}
#pragma mark Node visiting
-- (ANTLRCommonTree *) handleRootNode
+- (CommonTree *) handleRootNode
{
- ANTLRCommonTree *node = currentNode;
+ CommonTree *node = currentNode;
currentChildIndex = 0;
if ([node isNil]) {
node = [self visitChild:currentChildIndex];
@@ -354,14 +354,14 @@ - (ANTLRCommonTree *) handleRootNode
return node;
}
-- (ANTLRCommonTree *) visitChild:(NSInteger)childNumber
+- (CommonTree *) visitChild:(NSInteger)childNumber
{
- ANTLRCommonTree *node = nil;
+ CommonTree *node = nil;
[nodeStack addObject:currentNode];
[indexStack addObject:[NSNumber numberWithInt:childNumber]];
if (childNumber == 0 && ![currentNode isNil])
- [self addNavigationNodeWithType:ANTLRTokenTypeDOWN];
+ [self addNavigationNodeWithType:TokenTypeDOWN];
currentNode = [currentNode getChild:childNumber];
currentChildIndex = 0;
@@ -375,14 +375,14 @@ - (void) walkBackToMostRecentNodeWithUnvisitedChildren
{
while (currentNode != nil && currentChildIndex >= (NSInteger)[currentNode getChildCount])
{
- currentNode = (ANTLRCommonTree *)[nodeStack lastObject];
+ currentNode = (CommonTree *)[nodeStack lastObject];
[nodeStack removeLastObject];
currentChildIndex = [(NSNumber *)[indexStack lastObject] intValue];
[indexStack removeLastObject];
currentChildIndex++; // move to next child
if (currentChildIndex >= (NSInteger)[currentNode getChildCount]) {
if (![currentNode isNil]) {
- [self addNavigationNodeWithType:ANTLRTokenTypeUP];
+ [self addNavigationNodeWithType:TokenTypeUP];
}
if (currentNode == root) { // we done yet?
currentNode = nil;
@@ -396,16 +396,16 @@ - (void) addNavigationNodeWithType:(NSInteger)tokenType
{
// TODO: this currently ignores shouldUseUniqueNavigationNodes.
switch (tokenType) {
- case ANTLRTokenTypeDOWN: {
+ case TokenTypeDOWN: {
if (navigationNodeDown == nil) {
- navigationNodeDown = [[ANTLRTreeNavigationNodeDown alloc] init];
+ navigationNodeDown = [[TreeNavigationNodeDown alloc] init];
}
[self addLookahead:navigationNodeDown];
break;
}
- case ANTLRTokenTypeUP: {
+ case TokenTypeUP: {
if (navigationNodeUp == nil) {
- navigationNodeUp = [[ANTLRTreeNavigationNodeUp alloc] init];
+ navigationNodeUp = [[TreeNavigationNodeUp alloc] init];
}
[self addLookahead:navigationNodeUp];
break;
@@ -414,12 +414,12 @@ - (void) addNavigationNodeWithType:(NSInteger)tokenType
}
#pragma mark Accessors
-- (ANTLRCommonTree *) root
+- (CommonTree *) root
{
return root;
}
-- (void) setRoot: (ANTLRCommonTree *) aRoot
+- (void) setRoot: (CommonTree *) aRoot
{
if (root != aRoot) {
[aRoot retain];
View
14 runtime/ObjC/Framework/UnbufferedCommonTreeNodeStreamState.h
@@ -25,11 +25,11 @@
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#import <Cocoa/Cocoa.h>
-#import "ANTLRCommonTree.h"
+#import "CommonTree.h"
@interface ANTLRUnbufferedCommonTreeNodeStreamState : NSObject {
- ANTLRCommonTree *currentNode;
- ANTLRCommonTree *previousNode;
+ CommonTree *currentNode;
+ CommonTree *previousNode;
int currentChildIndex;
int absoluteNodeIndex;
@@ -39,11 +39,11 @@
NSMutableArray *lookahead;
}
-- (ANTLRCommonTree *) currentNode;
-- (void) setCurrentNode: (ANTLRCommonTree *) aCurrentNode;
+- (CommonTree *) currentNode;
+- (void) setCurrentNode: (CommonTree *) aCurrentNode;
-- (ANTLRCommonTree *) previousNode;
-- (void) setPreviousNode: (ANTLRCommonTree *) aPreviousNode;
+- (CommonTree *) previousNode;
+- (void) setPreviousNode: (CommonTree *) aPreviousNode;
- (NSInteger) currentChildIndex;
- (void) setCurrentChildIndex: (NSInteger) aCurrentChildIndex;
View
10 runtime/ObjC/Framework/UnbufferedCommonTreeNodeStreamState.m
@@ -24,7 +24,7 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#import "ANTLRUnbufferedCommonTreeNodeStreamState.h"
+#import "UnbufferedCommonTreeNodeStreamState.h"
@implementation ANTLRUnbufferedCommonTreeNodeStreamState
@@ -45,12 +45,12 @@ - (void) dealloc
[super dealloc];
}
-- (ANTLRCommonTree *) currentNode
+- (CommonTree *) currentNode
{
return currentNode;
}
-- (void) setCurrentNode: (ANTLRCommonTree *) aCurrentNode
+- (void) setCurrentNode: (CommonTree *) aCurrentNode
{
if (currentNode != aCurrentNode) {
[aCurrentNode retain];
@@ -59,12 +59,12 @@ - (void) setCurrentNode: (ANTLRCommonTree *) aCurrentNode
}
}
-- (ANTLRCommonTree *) previousNode
+- (CommonTree *) previousNode
{
return previousNode;
}
-- (void) setPreviousNode: (ANTLRCommonTree *) aPreviousNode
+- (void) setPreviousNode: (CommonTree *) aPreviousNode
{
if (previousNode != aPreviousNode) {
[aPreviousNode retain];

0 comments on commit 759de3d

Please sign in to comment.