Home | History | Annotate | Download | only in Framework
      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 extern NSInteger debug;
     36 
     37 #import "ANTLRRuleStack.h"
     38 #import "ANTLRTree.h"
     39 
     40 /*
     41  * Start of ANTLRRuleStack
     42  */
     43 @implementation ANTLRRuleStack
     44 
     45 + (ANTLRRuleStack *)newANTLRRuleStack
     46 {
     47     return [[ANTLRRuleStack alloc] init];
     48 }
     49 
     50 + (ANTLRRuleStack *)newANTLRRuleStack:(NSInteger)cnt
     51 {
     52     return [[ANTLRRuleStack alloc] initWithLen:cnt];
     53 }
     54 
     55 - (id)init
     56 {
     57 	if ((self = [super init]) != nil) {
     58 	}
     59     return( self );
     60 }
     61 
     62 - (id)initWithLen:(NSInteger)cnt
     63 {
     64 	if ((self = [super initWithLen:cnt]) != nil) {
     65 	}
     66     return( self );
     67 }
     68 
     69 - (void)dealloc
     70 {
     71 #ifdef DEBUG_DEALLOC
     72     NSLog( @"called dealloc in ANTLRRuleStack" );
     73 #endif
     74 	[super dealloc];
     75 }
     76 
     77 - (id) copyWithZone:(NSZone *)aZone
     78 {
     79     return [super copyWithZone:aZone];
     80 }
     81 
     82 - (NSInteger)count
     83 {
     84     ANTLRRuleMemo *anElement;
     85     NSInteger aCnt = 0;
     86     for( int i = 0; i < BuffSize; i++ ) {
     87         if ((anElement = ptrBuffer[i]) != nil)
     88             aCnt++;
     89     }
     90     return aCnt;
     91 }
     92 
     93 - (NSInteger)size
     94 {
     95     ANTLRRuleMemo *anElement;
     96     NSInteger aSize = 0;
     97     for( int i = 0; i < BuffSize; i++ ) {
     98         if ((anElement = ptrBuffer[i]) != nil) {
     99             aSize++;
    100         }
    101     }
    102     return aSize;
    103 }
    104 
    105 - (ANTLRHashRule *)pop
    106 {
    107     return (ANTLRHashRule *)[super pop];
    108 }
    109 
    110 - (void) insertObject:(ANTLRHashRule *)aRule atIndex:(NSInteger)idx
    111 {
    112     if ( idx >= BuffSize ) {
    113         if ( debug > 2 ) NSLog( @"In ANTLRRuleStack attempting to insert aRule at Index %d, but Buffer is only %d long\n", idx, BuffSize );
    114         [self ensureCapacity:idx];
    115     }
    116     if ( aRule != ptrBuffer[idx] ) {
    117         if ( ptrBuffer[idx] ) [ptrBuffer[idx] release];
    118         [aRule retain];
    119     }
    120     ptrBuffer[idx] = aRule;
    121 }
    122 
    123 - (ANTLRHashRule *)objectAtIndex:(NSInteger)idx
    124 {
    125     if (idx < BuffSize) {
    126         return ptrBuffer[idx];
    127     }
    128     return nil;
    129 }
    130 
    131 - (void)putHashRuleAtRuleIndex:(NSInteger)aRuleIndex StartIndex:(NSInteger)aStartIndex StopIndex:(NSInteger)aStopIndex
    132 {
    133     ANTLRHashRule *aHashRule;
    134     ANTLRRuleMemo *aRuleMemo;
    135 
    136     if (aRuleIndex >= BuffSize) {
    137         if ( debug) NSLog( @"putHashRuleAtRuleIndex attempting to insert aRule at Index %d, but Buffer is only %d long\n", aRuleIndex, BuffSize );
    138         [self ensureCapacity:aRuleIndex];
    139     }
    140     if ((aHashRule = ptrBuffer[aRuleIndex]) == nil) {
    141         aHashRule = [[ANTLRHashRule newANTLRHashRuleWithLen:17] retain];
    142         ptrBuffer[aRuleIndex] = aHashRule;
    143     }
    144     if (( aRuleMemo = [aHashRule objectAtIndex:aStartIndex] ) == nil ) {
    145         aRuleMemo = [[ANTLRRuleMemo newANTLRRuleMemo] retain];
    146         [aHashRule insertObject:aRuleMemo atIndex:aStartIndex];
    147     }
    148     [aRuleMemo setStartIndex:[NSNumber numberWithInteger:aStartIndex]];
    149     [aRuleMemo setStopIndex:[NSNumber numberWithInteger:aStopIndex]];
    150 }
    151 
    152 @end
    153