1//
2//  ANTLRRuleStack.m
3//  ANTLR
4//
5//  Created by Alan Condit on 6/9/10.
6// [The "BSD licence"]
7// Copyright (c) 2010 Alan Condit
8// All rights reserved.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions
12// are met:
13// 1. Redistributions of source code must retain the above copyright
14//    notice, this list of conditions and the following disclaimer.
15// 2. Redistributions in binary form must reproduce the above copyright
16//    notice, this list of conditions and the following disclaimer in the
17//    documentation and/or other materials provided with the distribution.
18// 3. The name of the author may not be used to endorse or promote products
19//    derived from this software without specific prior written permission.
20//
21// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32#define SUCCESS (0)
33#define FAILURE (-1)
34
35#import "ANTLRRuleStack.h"
36#import "ANTLRTree.h"
37
38/*
39 * Start of ANTLRRuleStack
40 */
41@implementation ANTLRRuleStack
42
43+ (ANTLRRuleStack *)newANTLRRuleStack
44{
45    return [[ANTLRRuleStack alloc] init];
46}
47
48+ (ANTLRRuleStack *)newANTLRRuleStack:(NSInteger)cnt
49{
50    return [[ANTLRRuleStack alloc] initWithLen:cnt];
51}
52
53- (id)init
54{
55	if ((self = [super init]) != nil) {
56	}
57    return( self );
58}
59
60- (id)initWithLen:(NSInteger)cnt
61{
62	if ((self = [super initWithLen:cnt]) != nil) {
63	}
64    return( self );
65}
66
67- (void)dealloc
68{
69	[super dealloc];
70}
71
72- (id) copyWithZone:(NSZone *)aZone
73{
74    return [super copyWithZone:aZone];
75}
76
77- (NSInteger)count
78{
79    ANTLRRuleMemo *anElement;
80    NSInteger aCnt = 0;
81    for( int i = 0; i < BuffSize; i++ ) {
82        if ((anElement = ptrBuffer[i]) != nil)
83            aCnt++;
84    }
85    return aCnt;
86}
87
88- (NSInteger)size
89{
90    ANTLRRuleMemo *anElement;
91    NSInteger aSize = 0;
92    for( int i = 0; i < BuffSize; i++ ) {
93        if ((anElement = ptrBuffer[i]) != nil) {
94            aSize++;
95        }
96    }
97    return aSize;
98}
99
100- (ANTLRHashRule *)pop
101{
102    return (ANTLRHashRule *)[super pop];
103}
104
105- (void) insertObject:(ANTLRHashRule *)aRule atIndex:(NSInteger)idx
106{
107    if ( idx >= BuffSize ) {
108        NSLog( @"In ANTLRRuleStack attempting to insert aRule at Index %d, but Buffer is only %d long\n", idx, BuffSize );
109        [self ensureCapacity:idx];
110    }
111    if ( aRule != ptrBuffer[idx] ) {
112        if (ptrBuffer[idx] != nil) [ptrBuffer[idx] release];
113        [aRule retain];
114    }
115    ptrBuffer[idx] = aRule;
116}
117
118- (ANTLRHashRule *)objectAtIndex:(NSInteger)idx
119{
120    if (idx < BuffSize) {
121        return ptrBuffer[idx];
122    }
123    return nil;
124}
125
126- (void)putHashRuleAtRuleIndex:(NSInteger)aRuleIndex StartIndex:(NSInteger)aStartIndex StopIndex:(NSInteger)aStopIndex
127{
128    ANTLRHashRule *aHashRule;
129    ANTLRRuleMemo *aRuleMemo;
130
131    if (aRuleIndex >= BuffSize) {
132        NSLog( @"putHashRuleAtRuleIndex attempting to insert aRule at Index %d, but Buffer is only %d long\n", aRuleIndex, BuffSize );
133        [self ensureCapacity:aRuleIndex];
134    }
135    if ((aHashRule = ptrBuffer[aRuleIndex]) == nil) {
136        aHashRule = [[ANTLRHashRule newANTLRHashRuleWithLen:17] retain];
137        ptrBuffer[aRuleIndex] = aHashRule;
138    }
139    if (( aRuleMemo = [aHashRule objectAtIndex:aStartIndex] ) == nil ) {
140        aRuleMemo = [[ANTLRRuleMemo newANTLRRuleMemo] retain];
141        [aHashRule insertObject:aRuleMemo atIndex:aStartIndex];
142    }
143    [aRuleMemo setStartIndex:[NSNumber numberWithInteger:aStartIndex]];
144    [aRuleMemo setStopIndex:[NSNumber numberWithInteger:aStopIndex]];
145}
146
147@end
148