1// [The "BSD licence"]
2// Copyright (c) 2006-2007 Kay Roepke 2010 Alan Condit
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9//    notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11//    notice, this list of conditions and the following disclaimer in the
12//    documentation and/or other materials provided with the distribution.
13// 3. The name of the author may not be used to endorse or promote products
14//    derived from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#import "Parser.h"
28
29
30@implementation Parser
31
32+ (Parser *)newParser:(id<TokenStream>)anInput
33{
34    return [[Parser alloc] initWithTokenStream:anInput];
35}
36
37+ (Parser *)newParser:(id<TokenStream>)anInput State:(RecognizerSharedState *)aState
38{
39    return [[Parser alloc] initWithTokenStream:anInput State:aState];
40}
41
42- (id) initWithTokenStream:(id<TokenStream>)theStream
43{
44    if ((self = [super init]) != nil) {
45        [self setInput:theStream];
46    }
47    return self;
48}
49
50- (id) initWithTokenStream:(id<TokenStream>)theStream State:(RecognizerSharedState *)aState
51{
52    if ((self = [super initWithState:aState]) != nil) {
53        [self setInput:theStream];
54    }
55    return self;
56}
57
58- (void) reset
59{
60    [super reset]; // reset all recognizer state variables
61    if ( input!=nil ) {
62        [input seek:0]; // rewind the input
63    }
64}
65
66- (void) dealloc
67{
68#ifdef DEBUG_DEALLOC
69    NSLog( @"called dealloc in Parser" );
70#endif
71    [input release];
72	[super dealloc];
73}
74
75//----------------------------------------------------------
76//  input
77//----------------------------------------------------------
78- (id<TokenStream>) input
79{
80    return input;
81}
82
83- (void) setInput: (id<TokenStream>) anInput
84{
85    if (input != anInput) {
86        if ( input ) [input release];
87        [anInput retain];
88    }
89    input = anInput;
90}
91
92- (id) getCurrentInputSymbol:(id<TokenStream>)anInput
93{
94    state.token = [input LT:1];
95    return state.token;
96}
97
98- (CommonToken *)getMissingSymbol:(id<TokenStream>)anInput
99                             Exception:(RecognitionException *)e
100                                 TType:(NSInteger)expectedTokenType
101                                BitSet:(ANTLRBitSet *)follow
102{
103    NSString *tokenText = nil;
104    if ( expectedTokenType == TokenTypeEOF )
105        tokenText = @"<missing EOF>";
106    else
107        tokenText = [NSString stringWithFormat:@"<missing %@>\n",[[BaseRecognizer getTokenNames] objectAtIndex:expectedTokenType]];
108    CommonToken *t = [[CommonToken newToken:expectedTokenType Text:tokenText] retain];
109    CommonToken *current = [anInput LT:1];
110    if ( current.type == TokenTypeEOF ) {
111        current = [anInput LT:-1];
112    }
113    t.line = current.line;
114    t.charPositionInLine = current.charPositionInLine;
115    t.channel = TokenChannelDefault;
116    t.input = current.input;
117    return t;
118}
119
120/** Set the token stream and reset the parser */
121- (void) setTokenStream:(id<TokenStream>)anInput
122{
123    input = nil;
124    [self reset];
125    input = anInput;
126}
127
128- (id<TokenStream>)getTokenStream
129{
130    return input;
131}
132
133- (NSString *)getSourceName
134{
135    return [input getSourceName];
136}
137
138- (void) traceIn:(NSString *)ruleName Index:(int)ruleIndex
139{
140    [super traceIn:ruleName Index:ruleIndex Object:[input LT:1]];
141}
142
143- (void) traceOut:(NSString *)ruleName Index:(NSInteger) ruleIndex
144{
145    [super traceOut:ruleName Index:ruleIndex Object:[input LT:1]];
146}
147
148@end
149