ANTLRBaseRecognizer.h revision 324c4644fee44b9898524c09511bd33c3f12e2df
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
28#import <Cocoa/Cocoa.h>
29#import <Foundation/Foundation.h>
30
31#import "ANTLRIntStream.h"
32
33// This is an abstract superclass for lexers and parsers.
34
35#define ANTLR_MEMO_RULE_FAILED -2
36#define ANTLR_MEMO_RULE_UNKNOWN -1
37#define ANTLR_INITIAL_FOLLOW_STACK_SIZE 100
38
39#import "ANTLRMapElement.h"
40#import "ANTLRBitSet.h"
41#import "ANTLRToken.h"
42#import "ANTLRRecognizerSharedState.h"
43#import "ANTLRRecognitionException.h"
44#import "ANTLRMissingTokenException.h"
45#import "ANTLRMismatchedTokenException.h"
46#import "ANTLRMismatchedTreeNodeException.h"
47#import "ANTLRUnwantedTokenException.h"
48#import "ANTLRNoViableAltException.h"
49#import "ANTLREarlyExitException.h"
50#import "ANTLRMismatchedSetException.h"
51#import "ANTLRMismatchedNotSetException.h"
52#import "ANTLRFailedPredicateException.h"
53
54@interface ANTLRBaseRecognizer : NSObject {
55	ANTLRRecognizerSharedState *state;	// the state of this recognizer. Might be shared with other recognizers, e.g. in grammar import scenarios.
56	NSString *grammarFileName;			// where did the grammar come from. filled in by codegeneration
57//    BOOL failed;
58    NSString *sourceName;
59//    NSInteger numberOfSyntaxErrors;
60    NSArray *tokenNames;
61}
62
63@property (retain, getter=getState, setter=setState) ANTLRRecognizerSharedState *state;
64@property (retain, getter=getGrammarFileName, setter=setGrammarFileName) NSString *grammarFileName;
65//@property (assign, getter=getFailed, setter=setFailed) BOOL failed;
66@property (retain, getter=getTokenNames, setter=setTokenNames) NSArray *tokenNames;
67@property (retain, getter=getSourceName, setter=setSourceName) NSString *sourceName;
68//@property (assign, getter=getNumberOfSyntaxErrors, setter=setNumberOfSyntaxErrors) NSInteger numberOfSyntaxErrors;
69
70+ (void) initialize;
71
72+ (ANTLRBaseRecognizer *) newANTLRBaseRecognizer;
73+ (ANTLRBaseRecognizer *) newANTLRBaseRecognizerWithRuleLen:(NSInteger)aLen;
74+ (ANTLRBaseRecognizer *) newANTLRBaseRecognizer:(ANTLRRecognizerSharedState *)aState;
75
76+ (NSArray *)getTokenNames;
77+ (void)setTokenNames:(NSArray *)aTokNamArray;
78+ (void)setGrammarFileName:(NSString *)aFileName;
79
80- (id) init;
81- (id) initWithLen:(NSInteger)aLen;
82- (id) initWithState:(ANTLRRecognizerSharedState *)aState;
83
84- (void) dealloc;
85
86// simple accessors
87- (NSInteger) getBacktrackingLevel;
88- (void) setBacktrackingLevel:(NSInteger) level;
89
90- (BOOL) getFailed;
91- (void) setFailed: (BOOL) flag;
92
93- (ANTLRRecognizerSharedState *) getState;
94- (void) setState:(ANTLRRecognizerSharedState *) theState;
95
96// reset this recognizer - might be extended by codegeneration/grammar
97- (void) reset;
98
99/** Match needs to return the current input symbol, which gets put
100 *  into the label for the associated token ref; e.g., x=ID.  Token
101 *  and tree parsers need to return different objects. Rather than test
102 *  for input stream type or change the IntStream interface, I use
103 *  a simple method to ask the recognizer to tell me what the current
104 *  input symbol is.
105 *
106 *  This is ignored for lexers.
107 */
108- (id) getInput;
109
110- (void)skip;
111
112// do actual matching of tokens/characters
113- (id) match:(id<ANTLRIntStream>)anInput TokenType:(NSInteger)ttype Follow:(ANTLRBitSet *)follow;
114- (void) matchAny:(id<ANTLRIntStream>)anInput;
115- (BOOL) mismatchIsUnwantedToken:(id<ANTLRIntStream>)anInput TokenType:(NSInteger) ttype;
116- (BOOL) mismatchIsMissingToken:(id<ANTLRIntStream>)anInput Follow:(ANTLRBitSet *)follow;
117
118// error reporting and recovery
119- (void) reportError:(ANTLRRecognitionException *)e;
120- (void) displayRecognitionError:(NSArray *)theTokNams Exception:(ANTLRRecognitionException *)e;
121- (NSString *)getErrorMessage:(ANTLRRecognitionException *)e TokenNames:(NSArray *)theTokNams;
122- (NSInteger) getNumberOfSyntaxErrors;
123- (NSString *)getErrorHeader:(ANTLRRecognitionException *)e;
124- (NSString *)getTokenErrorDisplay:(id<ANTLRToken>)t;
125- (void) emitErrorMessage:(NSString *)msg;
126- (void) recover:(id<ANTLRIntStream>)anInput Exception:(ANTLRRecognitionException *)e;
127
128// begin hooks for debugger
129- (void) beginResync;
130- (void) endResync;
131// end hooks for debugger
132
133// compute the bitsets necessary to do matching and recovery
134- (ANTLRBitSet *)computeErrorRecoverySet;
135- (ANTLRBitSet *)computeContextSensitiveRuleFOLLOW;
136- (ANTLRBitSet *)combineFollows:(BOOL) exact;
137
138- (id<ANTLRToken>) recoverFromMismatchedToken:(id<ANTLRIntStream>)anInput
139                                    TokenType:(NSInteger)ttype
140                                       Follow:(ANTLRBitSet *)follow;
141
142- (id<ANTLRToken>)recoverFromMismatchedSet:(id<ANTLRIntStream>)anInput
143                                    Exception:(ANTLRRecognitionException *)e
144                                    Follow:(ANTLRBitSet *)follow;
145
146- (id) getCurrentInputSymbol:(id<ANTLRIntStream>)anInput;
147- (id) getMissingSymbol:(id<ANTLRIntStream>)anInput
148              Exception:(ANTLRRecognitionException *)e
149              TokenType:(NSInteger) expectedTokenType
150                Follow:(ANTLRBitSet *)follow;
151
152// helper methods for recovery. try to resync somewhere
153- (void) consumeUntilTType:(id<ANTLRIntStream>)anInput TokenType:(NSInteger)ttype;
154- (void) consumeUntilFollow:(id<ANTLRIntStream>)anInput Follow:(ANTLRBitSet *)bitSet;
155- (void) pushFollow:(ANTLRBitSet *)fset;
156- (ANTLRBitSet *)popFollow;
157
158// to be used by the debugger to do reporting. maybe hook in incremental stuff here, too.
159- (NSMutableArray *) getRuleInvocationStack;
160- (NSMutableArray *) getRuleInvocationStack:(ANTLRRecognitionException *)exception
161					             Recognizer:(NSString *)recognizerClassName;
162
163- (NSArray *) getTokenNames;
164- (NSString *)getGrammarFileName;
165- (NSString *)getSourceName;
166- (NSMutableArray *) toStrings:(NSArray *)tokens;
167// support for memoization
168- (NSInteger) getRuleMemoization:(NSInteger)ruleIndex StartIndex:(NSInteger)ruleStartIndex;
169- (BOOL) alreadyParsedRule:(id<ANTLRIntStream>)anInput RuleIndex:(NSInteger)ruleIndex;
170- (void) memoize:(id<ANTLRIntStream>)anInput
171	     RuleIndex:(NSInteger)ruleIndex
172	    StartIndex:(NSInteger)ruleStartIndex;
173- (NSInteger) getRuleMemoizationCacheSize;
174- (void)traceIn:(NSString *)ruleName Index:(NSInteger)ruleIndex Object:(id)inputSymbol;
175- (void)traceOut:(NSString *)ruleName Index:(NSInteger)ruleIndex Object:(id)inputSymbol;
176
177
178// support for syntactic predicates. these are called indirectly to support funky stuff in grammars,
179// like supplying selectors instead of writing code directly into the actions of the grammar.
180- (BOOL) evaluateSyntacticPredicate:(SEL)synpredFragment;
181// stream:(id<ANTLRIntStream>)anInput;
182
183@end
184