1//
2//  ANTLRRewriteRuleNodeStream.m
3//  ANTLR
4//
5//  Created by Kay Röpke on 7/16/07.
6//  Copyright 2007 classDump. All rights reserved.
7//
8
9#import "ANTLRRewriteRuleNodeStream.h"
10#import "ANTLRRuntimeException.h"
11
12@implementation ANTLRRewriteRuleNodeStream
13
14+ (ANTLRRewriteRuleNodeStream*) newANTLRRewriteRuleNodeStream:(id<ANTLRTreeAdaptor>)aTreeAdaptor description:(NSString *)anElementDescription;
15{
16    return [[ANTLRRewriteRuleNodeStream alloc] initWithTreeAdaptor:aTreeAdaptor description:anElementDescription];
17}
18
19+ (ANTLRRewriteRuleNodeStream*) newANTLRRewriteRuleNodeStream:(id<ANTLRTreeAdaptor>)aTreeAdaptor description:(NSString *)anElementDescription element:(id)anElement;
20{
21    return [[ANTLRRewriteRuleNodeStream alloc] initWithTreeAdaptor:aTreeAdaptor description:anElementDescription element:anElement];
22}
23
24+ (ANTLRRewriteRuleNodeStream*) newANTLRRewriteRuleNode:(id<ANTLRTreeAdaptor>)aTreeAdaptor description:(NSString *)anElementDescription elements:(NSArray *)theElements;
25{
26    return [[ANTLRRewriteRuleNodeStream alloc] initWithTreeAdaptor:aTreeAdaptor description:anElementDescription elements:theElements];
27}
28
29- (id) initWithTreeAdaptor:(id<ANTLRTreeAdaptor>)aTreeAdaptor description:(NSString *)anElementDescription
30{
31    if ((self = [super initWithTreeAdaptor:aTreeAdaptor description:anElementDescription]) != nil) {
32        dirty = NO;
33        isSingleElement = YES;
34    }
35    return self;
36}
37
38- (id) initWithTreeAdaptor:(id<ANTLRTreeAdaptor>)aTreeAdaptor description:(NSString *)anElementDescription element:(id)anElement
39{
40    if ((self = [super initWithTreeAdaptor:aTreeAdaptor description:anElementDescription element:anElement]) != nil) {
41        dirty = NO;
42    }
43    return self;
44}
45
46- (id) initWithTreeAdaptor:(id<ANTLRTreeAdaptor>)aTreeAdaptor description:(NSString *)anElementDescription elements:(NSArray *)theElements
47{
48    if ((self = [super init]) != nil) {
49        dirty = NO;
50    }
51    return self;
52}
53
54
55- (id) nextNode
56{
57    if (dirty || (cursor >= [self size] && [self size] == 1))
58        return [treeAdaptor dupNode:[self _next]];
59    else
60        return [self _next];
61}
62
63- (id<ANTLRBaseTree>) toTree:(id<ANTLRBaseTree>)element
64{
65    return [treeAdaptor dupNode:element];
66}
67
68- (id) dup:(id)element
69{
70    return [treeAdaptor dupTree:element];
71    @throw [ANTLRRuntimeException newException:@"ANTLRUnsupportedOperationException" reason:@"dup can't be called for a node stream."];
72}
73
74@end
75