aboutsummaryrefslogtreecommitdiff
path: root/antlr-3.4/runtime/ObjC/ANTLR.framework/Headers/ANTLRBaseRecognizer.h
blob: 1a922bda37ff68d9ec88a26c2ab85a9a7feff0b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// [The "BSD licence"]
// Copyright (c) 2006-2007 Kay Roepke 2010 Alan Condit
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
//    derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

#import "ANTLRIntStream.h"

// This is an abstract superclass for lexers and parsers.

#define ANTLR_MEMO_RULE_FAILED -2
#define ANTLR_MEMO_RULE_UNKNOWN -1
#define ANTLR_INITIAL_FOLLOW_STACK_SIZE 100

#import "ANTLRMapElement.h"
#import "ANTLRBitSet.h"
#import "ANTLRToken.h"
#import "ANTLRRecognizerSharedState.h"
#import "ANTLRRecognitionException.h"
#import "ANTLRMissingTokenException.h"
#import "ANTLRMismatchedTokenException.h"
#import "ANTLRMismatchedTreeNodeException.h"
#import "ANTLRUnwantedTokenException.h"
#import "ANTLRNoViableAltException.h"
#import "ANTLREarlyExitException.h"
#import "ANTLRMismatchedSetException.h"
#import "ANTLRMismatchedNotSetException.h"
#import "ANTLRFailedPredicateException.h"

@interface ANTLRBaseRecognizer : NSObject {
	ANTLRRecognizerSharedState *state;	// the state of this recognizer. Might be shared with other recognizers, e.g. in grammar import scenarios.
	NSString *grammarFileName;			// where did the grammar come from. filled in by codegeneration
//    BOOL failed;
    NSString *sourceName;
//    NSInteger numberOfSyntaxErrors;
    NSArray *tokenNames;
}

@property (retain, getter=getState, setter=setState) ANTLRRecognizerSharedState *state;
@property (retain, getter=getGrammarFileName, setter=setGrammarFileName) NSString *grammarFileName;
//@property (assign, getter=getFailed, setter=setFailed) BOOL failed;
@property (retain, getter=getTokenNames, setter=setTokenNames) NSArray *tokenNames;
@property (retain, getter=getSourceName, setter=setSourceName) NSString *sourceName;
//@property (assign, getter=getNumberOfSyntaxErrors, setter=setNumberOfSyntaxErrors) NSInteger numberOfSyntaxErrors;

+ (void) initialize;

+ (ANTLRBaseRecognizer *) newANTLRBaseRecognizer;
+ (ANTLRBaseRecognizer *) newANTLRBaseRecognizerWithRuleLen:(NSInteger)aLen;
+ (ANTLRBaseRecognizer *) newANTLRBaseRecognizer:(ANTLRRecognizerSharedState *)aState;

+ (NSArray *)getTokenNames;
+ (void)setTokenNames:(NSArray *)aTokNamArray;
+ (void)setGrammarFileName:(NSString *)aFileName;

- (id) init;
- (id) initWithLen:(NSInteger)aLen;
- (id) initWithState:(ANTLRRecognizerSharedState *)aState;

- (void) dealloc;

// simple accessors
- (NSInteger) getBacktrackingLevel;
- (void) setBacktrackingLevel:(NSInteger) level;

- (BOOL) getFailed;
- (void) setFailed: (BOOL) flag;

- (ANTLRRecognizerSharedState *) getState;
- (void) setState:(ANTLRRecognizerSharedState *) theState;

// reset this recognizer - might be extended by codegeneration/grammar
- (void) reset;

/** Match needs to return the current input symbol, which gets put
 *  into the label for the associated token ref; e.g., x=ID.  Token
 *  and tree parsers need to return different objects. Rather than test
 *  for input stream type or change the IntStream interface, I use
 *  a simple method to ask the recognizer to tell me what the current
 *  input symbol is.
 * 
 *  This is ignored for lexers.
 */
- (id) getInput;

- (void)skip;

// do actual matching of tokens/characters
- (id) match:(id<ANTLRIntStream>)anInput TokenType:(NSInteger)ttype Follow:(ANTLRBitSet *)follow;
- (void) matchAny:(id<ANTLRIntStream>)anInput;
- (BOOL) mismatchIsUnwantedToken:(id<ANTLRIntStream>)anInput TokenType:(NSInteger) ttype;
- (BOOL) mismatchIsMissingToken:(id<ANTLRIntStream>)anInput Follow:(ANTLRBitSet *)follow;

// error reporting and recovery
- (void) reportError:(ANTLRRecognitionException *)e;
- (void) displayRecognitionError:(NSArray *)theTokNams Exception:(ANTLRRecognitionException *)e;
- (NSString *)getErrorMessage:(ANTLRRecognitionException *)e TokenNames:(NSArray *)theTokNams;
- (NSInteger) getNumberOfSyntaxErrors;
- (NSString *)getErrorHeader:(ANTLRRecognitionException *)e;
- (NSString *)getTokenErrorDisplay:(id<ANTLRToken>)t;
- (void) emitErrorMessage:(NSString *)msg;
- (void) recover:(id<ANTLRIntStream>)anInput Exception:(ANTLRRecognitionException *)e;

// begin hooks for debugger
- (void) beginResync;
- (void) endResync;
// end hooks for debugger

// compute the bitsets necessary to do matching and recovery
- (ANTLRBitSet *)computeErrorRecoverySet;
- (ANTLRBitSet *)computeContextSensitiveRuleFOLLOW;
- (ANTLRBitSet *)combineFollows:(BOOL) exact;

- (id<ANTLRToken>) recoverFromMismatchedToken:(id<ANTLRIntStream>)anInput 
                                    TokenType:(NSInteger)ttype 
                                       Follow:(ANTLRBitSet *)follow;
                                    
- (id<ANTLRToken>)recoverFromMismatchedSet:(id<ANTLRIntStream>)anInput
                                    Exception:(ANTLRRecognitionException *)e
                                    Follow:(ANTLRBitSet *)follow;

- (id) getCurrentInputSymbol:(id<ANTLRIntStream>)anInput;
- (id) getMissingSymbol:(id<ANTLRIntStream>)anInput
              Exception:(ANTLRRecognitionException *)e
              TokenType:(NSInteger) expectedTokenType
                Follow:(ANTLRBitSet *)follow;

// helper methods for recovery. try to resync somewhere
- (void) consumeUntilTType:(id<ANTLRIntStream>)anInput TokenType:(NSInteger)ttype;
- (void) consumeUntilFollow:(id<ANTLRIntStream>)anInput Follow:(ANTLRBitSet *)bitSet;
- (void) pushFollow:(ANTLRBitSet *)fset;
- (ANTLRBitSet *)popFollow;

// to be used by the debugger to do reporting. maybe hook in incremental stuff here, too.
- (NSMutableArray *) getRuleInvocationStack;
- (NSMutableArray *) getRuleInvocationStack:(ANTLRRecognitionException *)exception
					             Recognizer:(NSString *)recognizerClassName;

- (NSArray *) getTokenNames;
- (NSString *)getGrammarFileName;
- (NSString *)getSourceName;
- (NSMutableArray *) toStrings:(NSArray *)tokens;
// support for memoization
- (NSInteger) getRuleMemoization:(NSInteger)ruleIndex StartIndex:(NSInteger)ruleStartIndex;
- (BOOL) alreadyParsedRule:(id<ANTLRIntStream>)anInput RuleIndex:(NSInteger)ruleIndex;
- (void) memoize:(id<ANTLRIntStream>)anInput
	     RuleIndex:(NSInteger)ruleIndex
	    StartIndex:(NSInteger)ruleStartIndex;
- (NSInteger) getRuleMemoizationCacheSize;
- (void)traceIn:(NSString *)ruleName Index:(NSInteger)ruleIndex Object:(id)inputSymbol;
- (void)traceOut:(NSString *)ruleName Index:(NSInteger)ruleIndex Object:(id)inputSymbol;


// support for syntactic predicates. these are called indirectly to support funky stuff in grammars,
// like supplying selectors instead of writing code directly into the actions of the grammar.
- (BOOL) evaluateSyntacticPredicate:(SEL)synpredFragment;
// stream:(id<ANTLRIntStream>)anInput;

@end