/** \file
 *  This OBJC source file was generated by $ANTLR version 3.4
 *
 *     -  From the grammar source file : /Users/acondit/source/antlr/code/antlr3/runtime/ObjC/Framework/examples/treerewrite/TreeRewrite.g
 *     -                            On : 2012-02-16 17:42:35
 *     -                for the parser : TreeRewriteParserParser
 *
 * Editing it, at least manually, is not wise.
 *
 * ObjC language generator and runtime by Alan Condit, acondit|hereisanat|ipns|dotgoeshere|com.
 *
 *
*/
// $ANTLR 3.4 /Users/acondit/source/antlr/code/antlr3/runtime/ObjC/Framework/examples/treerewrite/TreeRewrite.g 2012-02-16 17:42:35


/* -----------------------------------------
 * Include the ANTLR3 generated header file.
 */
#import "TreeRewriteParser.h"
/* ----------------------------------------- */


/* ============================================================================= */
/* =============================================================================
 * Start of recognizer
 */

#pragma mark Bitsets
static ANTLRBitSet *FOLLOW_INT_in_rule26;
static const unsigned long long FOLLOW_INT_in_rule26_data[] = { 0x0000000000000010LL};
static ANTLRBitSet *FOLLOW_subrule_in_rule28;
static const unsigned long long FOLLOW_subrule_in_rule28_data[] = { 0x0000000000000002LL};
static ANTLRBitSet *FOLLOW_INT_in_subrule53;
static const unsigned long long FOLLOW_INT_in_subrule53_data[] = { 0x0000000000000002LL};


#pragma mark Dynamic Global globalAttributeScopeImplementation

#pragma mark Dynamic Rule Scopes ruleAttributeScopeImplementation

#pragma mark Rule Return Scopes returnScopeImplementation
@implementation TreeRewriteParser_rule_return /* returnScopeImplementation */
/* AST returnScope.synthesize */
@synthesize tree; /* start of synthesize -- OBJC-Line 1837 */
+ (TreeRewriteParser_rule_return *)newTreeRewriteParser_rule_return
{
return [[[TreeRewriteParser_rule_return alloc] init] retain];
}

- (id) init
{
self = [super init];
return self;
}

/* AST returnScope.methods */
- (CommonTree *)getTree
{
    return tree;
}

- (void) setTree:(CommonTree *)aTree
{
    if (tree != aTree) {
        if (tree != nil) [tree release];
        if (aTree != nil) [aTree retain];
        tree = aTree;
    }
}

- (void) dealloc
{
    self.tree = nil;
    [super dealloc];
}


@end /* end of returnScope implementation */

@implementation TreeRewriteParser_subrule_return /* returnScopeImplementation */
/* AST returnScope.synthesize */
@synthesize tree; /* start of synthesize -- OBJC-Line 1837 */
+ (TreeRewriteParser_subrule_return *)newTreeRewriteParser_subrule_return
{
return [[[TreeRewriteParser_subrule_return alloc] init] retain];
}

- (id) init
{
self = [super init];
return self;
}

/* AST returnScope.methods */
- (CommonTree *)getTree
{
    return tree;
}

- (void) setTree:(CommonTree *)aTree
{
    if (tree != aTree) {
        if (tree != nil) [tree release];
        if (aTree != nil) [aTree retain];
        tree = aTree;
    }
}

- (void) dealloc
{
    self.tree = nil;
    [super dealloc];
}


@end /* end of returnScope implementation */



@implementation TreeRewriteParser  // line 637

/* ObjC start of ruleAttributeScope */
#pragma mark Dynamic Rule Scopes ruleAttributeScope
/* ObjC end of ruleAttributeScope */
#pragma mark global Attribute Scopes globalAttributeScope
/* ObjC start globalAttributeScope */
/* ObjC end globalAttributeScope */
/* ObjC start actions.(actionScope).synthesize */
/* ObjC start synthesize() */
/* AST genericParser.synthesize */
/* AST parserProperties */
@synthesize treeAdaptor;

+ (void) initialize
{
    #pragma mark Bitsets
    FOLLOW_INT_in_rule26 = [[ANTLRBitSet newBitSetWithBits:(const unsigned long long *)FOLLOW_INT_in_rule26_data Count:(NSUInteger)1] retain];
    FOLLOW_subrule_in_rule28 = [[ANTLRBitSet newBitSetWithBits:(const unsigned long long *)FOLLOW_subrule_in_rule28_data Count:(NSUInteger)1] retain];
    FOLLOW_INT_in_subrule53 = [[ANTLRBitSet newBitSetWithBits:(const unsigned long long *)FOLLOW_INT_in_subrule53_data Count:(NSUInteger)1] retain];

    [BaseRecognizer setTokenNames:[[AMutableArray arrayWithObjects:@"<invalid>", @"<EOR>", @"<DOWN>", @"<UP>", 
 @"INT", @"WS", nil] retain]];
    [BaseRecognizer setGrammarFileName:@"/Users/acondit/source/antlr/code/antlr3/runtime/ObjC/Framework/examples/treerewrite/TreeRewrite.g"];
}

+ (TreeRewriteParser *)newTreeRewriteParser:(id<TokenStream>)aStream
{
    return [[TreeRewriteParser alloc] initWithTokenStream:aStream];
}

- (id) initWithTokenStream:(id<TokenStream>)aStream
{
    self = [super initWithTokenStream:aStream State:[[RecognizerSharedState newRecognizerSharedStateWithRuleLen:2+1] retain]];
    if ( self != nil ) {
        /* start of actions-actionScope-init */
        /* start of init */
        /* AST genericParser.init */
        [self setTreeAdaptor:[[CommonTreeAdaptor newTreeAdaptor] retain]];
    }
    return self;
}

- (void) dealloc
{
    /* AST genericParser.dealloc */
    [self setTreeAdaptor:nil];

    [super dealloc];
}

/* ObjC start actions.(actionScope).methods */
/* ObjC end actions.(actionScope).methods */
/* ObjC start methods() */
/* AST genericParser.methods */
/* AST parserMethods */
- (id<TreeAdaptor>) getTreeAdaptor
{
	return treeAdaptor;
}

- (void) setTreeAdaptor:(id<TreeAdaptor>)aTreeAdaptor
{
	if (aTreeAdaptor != treeAdaptor) {
		treeAdaptor = aTreeAdaptor;
	}
}
/* ObjC end methods() */
/* ObjC start rules */
/*
 * $ANTLR start rule
 * /Users/acondit/source/antlr/code/antlr3/runtime/ObjC/Framework/examples/treerewrite/TreeRewrite.g:8:1: rule : INT subrule -> ^( subrule INT ) ;
 */
- (TreeRewriteParser_rule_return *) rule
{
    /* ruleScopeSetUp */

    /* AST ruleDeclarations */
    /* ruleDeclarations */
    TreeRewriteParser_rule_return * retval = [TreeRewriteParser_rule_return newTreeRewriteParser_rule_return];
    [retval setStart:[input LT:1]];


    CommonTree *root_0 = nil;

    @try {
        /* AST ruleLabelDefs */
        /* ruleLabelDefs entry */
        CommonToken *INT1 = nil;TreeRewriteParser_subrule_return * subrule2 = nil ;


        CommonTree *INT1_tree=nil;
        RewriteRuleTokenStream *stream_INT =
            [[RewriteRuleTokenStream newRewriteRuleTokenStream:treeAdaptor
                                                             description:@"token INT"] retain];
        RewriteRuleSubtreeStream *stream_subrule =
            [[RewriteRuleSubtreeStream newRewriteRuleSubtreeStream:treeAdaptor
                                                                description:@"rule subrule"] retain];
        // /Users/acondit/source/antlr/code/antlr3/runtime/ObjC/Framework/examples/treerewrite/TreeRewrite.g:8:5: ( INT subrule -> ^( subrule INT ) ) // ruleBlockSingleAlt
        // /Users/acondit/source/antlr/code/antlr3/runtime/ObjC/Framework/examples/treerewrite/TreeRewrite.g:8:7: INT subrule // alt
        {

        INT1=(CommonToken *)[self match:input TokenType:INT Follow:FOLLOW_INT_in_rule26];  
            [stream_INT addElement:INT1];

         
        /* ruleRef */
        [self pushFollow:FOLLOW_subrule_in_rule28];
        subrule2 = [self subrule];

        [self popFollow];


        [stream_subrule addElement:[subrule2 getTree]];
         
        // AST REWRITE
        // elements: subrule, INT
        // token labels: 
        // rule labels: retval
        // token list labels: 
        // rule list labels: 
        // wildcard labels: 
        retval.tree = root_0;

        RewriteRuleSubtreeStream *stream_retval =
            [[RewriteRuleSubtreeStream newRewriteRuleSubtreeStream:treeAdaptor
                description:@"token retval" element:retval!=nil?[retval getTree]:nil] retain];

        root_0 = (CommonTree *)[[[treeAdaptor class] newEmptyTree] retain];

        // 8:19: -> ^( subrule INT )
        {
            // /Users/acondit/source/antlr/code/antlr3/runtime/ObjC/Framework/examples/treerewrite/TreeRewrite.g:8:22: ^( subrule INT )
            {
                CommonTree *root_1 = (CommonTree *)[[[treeAdaptor class] newEmptyTree] retain];
                root_1 = (CommonTree *)[treeAdaptor becomeRoot:(id<Tree>)[stream_subrule nextNode] old:root_1];

                 // TODO: args: 
                [treeAdaptor addChild:
                            [stream_INT nextNode]
                 toTree:root_1];

                [treeAdaptor addChild:root_1 toTree:root_0];
            }

        }


        retval.tree = root_0;


        }

        /* ASTParser ruleCleanUp */
        /* AST ruleCleanUp */
        /* token+rule list labels */
        [retval setStop:[input LT:-1]];


        [stream_INT release];
        [stream_subrule release];

            retval.tree = (CommonTree *)[treeAdaptor rulePostProcessing:root_0];
            [treeAdaptor setTokenBoundaries:retval.tree From:retval.start To:retval.stopToken];

    }
    @catch (RecognitionException *re) {
        [self reportError:re];
        [self recover:input Exception:re];
        /* ASTParser rule.setErrorReturnValue */
        retval.tree = (CommonTree *)[treeAdaptor errorNode:input From:retval.start To:[input LT:-1] Exception:re];

    }

    @finally {
        /* ruleScopeCleanUp */

    }
    return retval;
}
/* $ANTLR end rule */

/*
 * $ANTLR start subrule
 * /Users/acondit/source/antlr/code/antlr3/runtime/ObjC/Framework/examples/treerewrite/TreeRewrite.g:11:1: subrule : INT ;
 */
- (TreeRewriteParser_subrule_return *) subrule
{
    /* ruleScopeSetUp */

    /* AST ruleDeclarations */
    /* ruleDeclarations */
    TreeRewriteParser_subrule_return * retval = [TreeRewriteParser_subrule_return newTreeRewriteParser_subrule_return];
    [retval setStart:[input LT:1]];


    CommonTree *root_0 = nil;

    @try {
        /* AST ruleLabelDefs */
        /* ruleLabelDefs entry */
        CommonToken *INT3 = nil;

        CommonTree *INT3_tree=nil;

        // /Users/acondit/source/antlr/code/antlr3/runtime/ObjC/Framework/examples/treerewrite/TreeRewrite.g:12:5: ( INT ) // ruleBlockSingleAlt
        // /Users/acondit/source/antlr/code/antlr3/runtime/ObjC/Framework/examples/treerewrite/TreeRewrite.g:12:9: INT // alt
        {
        root_0 = (CommonTree *)[[[treeAdaptor class] newEmptyTree] retain];



        /* ASTParser tokenRef */
        INT3=(CommonToken *)[self match:input TokenType:INT Follow:FOLLOW_INT_in_subrule53]; 
        INT3_tree = /* ASTParser createNodeFromToken */
        (CommonTree *)[[treeAdaptor create:INT3] retain]
        ;
        [treeAdaptor addChild:INT3_tree  toTree:root_0];

         
        }

        /* ASTParser ruleCleanUp */
        /* AST ruleCleanUp */
        /* token+rule list labels */
        [retval setStop:[input LT:-1]];



            retval.tree = (CommonTree *)[treeAdaptor rulePostProcessing:root_0];
            [treeAdaptor setTokenBoundaries:retval.tree From:retval.start To:retval.stopToken];

    }
    @catch (RecognitionException *re) {
        [self reportError:re];
        [self recover:input Exception:re];
        /* ASTParser rule.setErrorReturnValue */
        retval.tree = (CommonTree *)[treeAdaptor errorNode:input From:retval.start To:[input LT:-1] Exception:re];

    }

    @finally {
        /* ruleScopeCleanUp */

    }
    return retval;
}
/* $ANTLR end subrule */
/* ObjC end rules */

@end /* end of TreeRewriteParser implementation line 692 */
