xref: /aosp_15_r20/external/antlr/runtime/ObjC/Framework/BaseRecognizer.h (revision 16467b971bd3e2009fad32dd79016f2c7e421deb)
1*16467b97STreehugger Robot // [The "BSD licence"]
2*16467b97STreehugger Robot // Copyright (c) 2006-2007 Kay Roepke 2010 Alan Condit
3*16467b97STreehugger Robot // All rights reserved.
4*16467b97STreehugger Robot //
5*16467b97STreehugger Robot // Redistribution and use in source and binary forms, with or without
6*16467b97STreehugger Robot // modification, are permitted provided that the following conditions
7*16467b97STreehugger Robot // are met:
8*16467b97STreehugger Robot // 1. Redistributions of source code must retain the above copyright
9*16467b97STreehugger Robot //    notice, this list of conditions and the following disclaimer.
10*16467b97STreehugger Robot // 2. Redistributions in binary form must reproduce the above copyright
11*16467b97STreehugger Robot //    notice, this list of conditions and the following disclaimer in the
12*16467b97STreehugger Robot //    documentation and/or other materials provided with the distribution.
13*16467b97STreehugger Robot // 3. The name of the author may not be used to endorse or promote products
14*16467b97STreehugger Robot //    derived from this software without specific prior written permission.
15*16467b97STreehugger Robot //
16*16467b97STreehugger Robot // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*16467b97STreehugger Robot // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*16467b97STreehugger Robot // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*16467b97STreehugger Robot // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*16467b97STreehugger Robot // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21*16467b97STreehugger Robot // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*16467b97STreehugger Robot // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*16467b97STreehugger Robot // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*16467b97STreehugger Robot // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*16467b97STreehugger Robot // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*16467b97STreehugger Robot 
27*16467b97STreehugger Robot 
28*16467b97STreehugger Robot #import <Foundation/Foundation.h>
29*16467b97STreehugger Robot 
30*16467b97STreehugger Robot #import "IntStream.h"
31*16467b97STreehugger Robot #import "AMutableArray.h"
32*16467b97STreehugger Robot 
33*16467b97STreehugger Robot // This is an abstract superclass for lexers and parsers.
34*16467b97STreehugger Robot 
35*16467b97STreehugger Robot #define ANTLR_MEMO_RULE_FAILED -2
36*16467b97STreehugger Robot #define ANTLR_MEMO_RULE_UNKNOWN -1
37*16467b97STreehugger Robot #define ANTLR_INITIAL_FOLLOW_STACK_SIZE 100
38*16467b97STreehugger Robot 
39*16467b97STreehugger Robot #import "MapElement.h"
40*16467b97STreehugger Robot #import "ANTLRBitSet.h"
41*16467b97STreehugger Robot #import "Token.h"
42*16467b97STreehugger Robot #import "RecognizerSharedState.h"
43*16467b97STreehugger Robot #import "RecognitionException.h"
44*16467b97STreehugger Robot #import "MissingTokenException.h"
45*16467b97STreehugger Robot #import "MismatchedTokenException.h"
46*16467b97STreehugger Robot #import "MismatchedTreeNodeException.h"
47*16467b97STreehugger Robot #import "UnwantedTokenException.h"
48*16467b97STreehugger Robot #import "NoViableAltException.h"
49*16467b97STreehugger Robot #import "EarlyExitException.h"
50*16467b97STreehugger Robot #import "MismatchedSetException.h"
51*16467b97STreehugger Robot #import "MismatchedNotSetException.h"
52*16467b97STreehugger Robot #import "FailedPredicateException.h"
53*16467b97STreehugger Robot 
54*16467b97STreehugger Robot @interface BaseRecognizer : NSObject {
55*16467b97STreehugger Robot     __strong RecognizerSharedState *state;  // the state of this recognizer. Might be shared with other recognizers, e.g. in grammar import scenarios.
56*16467b97STreehugger Robot     __strong NSString *grammarFileName;          // where did the grammar come from. filled in by codegeneration
57*16467b97STreehugger Robot     __strong NSString *sourceName;
58*16467b97STreehugger Robot     __strong AMutableArray *tokenNames;
59*16467b97STreehugger Robot }
60*16467b97STreehugger Robot 
61*16467b97STreehugger Robot + (void) initialize;
62*16467b97STreehugger Robot 
63*16467b97STreehugger Robot + (BaseRecognizer *) newBaseRecognizer;
64*16467b97STreehugger Robot + (BaseRecognizer *) newBaseRecognizerWithRuleLen:(NSInteger)aLen;
65*16467b97STreehugger Robot + (BaseRecognizer *) newBaseRecognizer:(RecognizerSharedState *)aState;
66*16467b97STreehugger Robot 
67*16467b97STreehugger Robot + (AMutableArray *)getTokenNames;
68*16467b97STreehugger Robot + (void)setTokenNames:(NSArray *)aTokNamArray;
69*16467b97STreehugger Robot + (void)setGrammarFileName:(NSString *)aFileName;
70*16467b97STreehugger Robot 
71*16467b97STreehugger Robot - (id) init;
72*16467b97STreehugger Robot - (id) initWithLen:(NSInteger)aLen;
73*16467b97STreehugger Robot - (id) initWithState:(RecognizerSharedState *)aState;
74*16467b97STreehugger Robot 
75*16467b97STreehugger Robot - (void) dealloc;
76*16467b97STreehugger Robot 
77*16467b97STreehugger Robot // simple accessors
78*16467b97STreehugger Robot - (NSInteger) getBacktrackingLevel;
79*16467b97STreehugger Robot - (void) setBacktrackingLevel:(NSInteger) level;
80*16467b97STreehugger Robot 
81*16467b97STreehugger Robot - (BOOL) getFailed;
82*16467b97STreehugger Robot - (void) setFailed: (BOOL) flag;
83*16467b97STreehugger Robot 
84*16467b97STreehugger Robot - (RecognizerSharedState *) getState;
85*16467b97STreehugger Robot - (void) setState:(RecognizerSharedState *) theState;
86*16467b97STreehugger Robot 
87*16467b97STreehugger Robot // reset this recognizer - might be extended by codegeneration/grammar
88*16467b97STreehugger Robot - (void) reset;
89*16467b97STreehugger Robot 
90*16467b97STreehugger Robot /** Match needs to return the current input symbol, which gets put
91*16467b97STreehugger Robot  *  into the label for the associated token ref; e.g., x=ID.  Token
92*16467b97STreehugger Robot  *  and tree parsers need to return different objects. Rather than test
93*16467b97STreehugger Robot  *  for input stream type or change the IntStream interface, I use
94*16467b97STreehugger Robot  *  a simple method to ask the recognizer to tell me what the current
95*16467b97STreehugger Robot  *  input symbol is.
96*16467b97STreehugger Robot  *
97*16467b97STreehugger Robot  *  This is ignored for lexers.
98*16467b97STreehugger Robot  */
99*16467b97STreehugger Robot - (id) input;
100*16467b97STreehugger Robot 
101*16467b97STreehugger Robot - (void)skip;
102*16467b97STreehugger Robot 
103*16467b97STreehugger Robot // do actual matching of tokens/characters
104*16467b97STreehugger Robot - (id) match:(id<IntStream>)anInput TokenType:(NSInteger)ttype Follow:(ANTLRBitSet *)follow;
105*16467b97STreehugger Robot - (void) matchAny:(id<IntStream>)anInput;
106*16467b97STreehugger Robot - (BOOL) mismatchIsUnwantedToken:(id<IntStream>)anInput TokenType:(NSInteger) ttype;
107*16467b97STreehugger Robot - (BOOL) mismatchIsMissingToken:(id<IntStream>)anInput Follow:(ANTLRBitSet *)follow;
108*16467b97STreehugger Robot 
109*16467b97STreehugger Robot // error reporting and recovery
110*16467b97STreehugger Robot - (void) reportError:(RecognitionException *)e;
111*16467b97STreehugger Robot - (void) displayRecognitionError:(AMutableArray *)theTokNams Exception:(RecognitionException *)e;
112*16467b97STreehugger Robot - (NSString *)getErrorMessage:(RecognitionException *)e TokenNames:(AMutableArray *)theTokNams;
113*16467b97STreehugger Robot - (NSInteger) getNumberOfSyntaxErrors;
114*16467b97STreehugger Robot - (NSString *)getErrorHeader:(RecognitionException *)e;
115*16467b97STreehugger Robot - (NSString *)getTokenErrorDisplay:(id<Token>)t;
116*16467b97STreehugger Robot - (void) emitErrorMessage:(NSString *)msg;
117*16467b97STreehugger Robot - (void) recover:(id<IntStream>)anInput Exception:(RecognitionException *)e;
118*16467b97STreehugger Robot 
119*16467b97STreehugger Robot // begin hooks for debugger
120*16467b97STreehugger Robot - (void) beginResync;
121*16467b97STreehugger Robot - (void) endResync;
122*16467b97STreehugger Robot // end hooks for debugger
123*16467b97STreehugger Robot 
124*16467b97STreehugger Robot // compute the bitsets necessary to do matching and recovery
125*16467b97STreehugger Robot - (ANTLRBitSet *)computeErrorRecoverySet;
126*16467b97STreehugger Robot - (ANTLRBitSet *)computeContextSensitiveRuleFOLLOW;
127*16467b97STreehugger Robot - (ANTLRBitSet *)combineFollows:(BOOL) exact;
128*16467b97STreehugger Robot 
129*16467b97STreehugger Robot - (id<Token>) recoverFromMismatchedToken:(id<IntStream>)anInput
130*16467b97STreehugger Robot                                     TokenType:(NSInteger)ttype
131*16467b97STreehugger Robot                                        Follow:(ANTLRBitSet *)follow;
132*16467b97STreehugger Robot 
133*16467b97STreehugger Robot - (id<Token>)recoverFromMismatchedSet:(id<IntStream>)anInput
134*16467b97STreehugger Robot                                     Exception:(RecognitionException *)e
135*16467b97STreehugger Robot                                     Follow:(ANTLRBitSet *)follow;
136*16467b97STreehugger Robot 
137*16467b97STreehugger Robot - (id) getCurrentInputSymbol:(id<IntStream>)anInput;
138*16467b97STreehugger Robot - (id) getMissingSymbol:(id<IntStream>)anInput
139*16467b97STreehugger Robot               Exception:(RecognitionException *)e
140*16467b97STreehugger Robot               TokenType:(NSInteger) expectedTokenType
141*16467b97STreehugger Robot                 Follow:(ANTLRBitSet *)follow;
142*16467b97STreehugger Robot 
143*16467b97STreehugger Robot // helper methods for recovery. try to resync somewhere
144*16467b97STreehugger Robot - (void) consumeUntilTType:(id<IntStream>)anInput TokenType:(NSInteger)ttype;
145*16467b97STreehugger Robot - (void) consumeUntilFollow:(id<IntStream>)anInput Follow:(ANTLRBitSet *)bitSet;
146*16467b97STreehugger Robot - (void) pushFollow:(ANTLRBitSet *)fset;
147*16467b97STreehugger Robot - (ANTLRBitSet *)popFollow;
148*16467b97STreehugger Robot 
149*16467b97STreehugger Robot // to be used by the debugger to do reporting. maybe hook in incremental stuff here, too.
150*16467b97STreehugger Robot - (AMutableArray *) getRuleInvocationStack;
151*16467b97STreehugger Robot - (AMutableArray *) getRuleInvocationStack:(RecognitionException *)exception
152*16467b97STreehugger Robot                                  Recognizer:(NSString *)recognizerClassName;
153*16467b97STreehugger Robot 
154*16467b97STreehugger Robot - (AMutableArray *) getTokenNames;
155*16467b97STreehugger Robot - (NSString *)getGrammarFileName;
156*16467b97STreehugger Robot - (NSString *)getSourceName;
157*16467b97STreehugger Robot - (AMutableArray *) toStrings:(NSArray *)tokens;
158*16467b97STreehugger Robot // support for memoization
159*16467b97STreehugger Robot - (NSInteger) getRuleMemoization:(NSInteger)ruleIndex StartIndex:(NSInteger)ruleStartIndex;
160*16467b97STreehugger Robot - (BOOL) alreadyParsedRule:(id<IntStream>)anInput RuleIndex:(NSInteger)ruleIndex;
161*16467b97STreehugger Robot - (void) memoize:(id<IntStream>)anInput
162*16467b97STreehugger Robot          RuleIndex:(NSInteger)ruleIndex
163*16467b97STreehugger Robot         StartIndex:(NSInteger)ruleStartIndex;
164*16467b97STreehugger Robot - (NSInteger) getRuleMemoizationCacheSize;
165*16467b97STreehugger Robot - (void)traceIn:(NSString *)ruleName Index:(NSInteger)ruleIndex Object:(id)inputSymbol;
166*16467b97STreehugger Robot - (void)traceOut:(NSString *)ruleName Index:(NSInteger)ruleIndex Object:(id)inputSymbol;
167*16467b97STreehugger Robot 
168*16467b97STreehugger Robot 
169*16467b97STreehugger Robot // support for syntactic predicates. these are called indirectly to support funky stuff in grammars,
170*16467b97STreehugger Robot // like supplying selectors instead of writing code directly into the actions of the grammar.
171*16467b97STreehugger Robot - (BOOL) evaluateSyntacticPredicate:(SEL)synpredFragment;
172*16467b97STreehugger Robot // stream:(id<IntStream>)anInput;
173*16467b97STreehugger Robot 
174*16467b97STreehugger Robot @property (retain) RecognizerSharedState *state;
175*16467b97STreehugger Robot @property (retain) NSString *grammarFileName;
176*16467b97STreehugger Robot @property (retain) NSString *sourceName;
177*16467b97STreehugger Robot @property (retain) AMutableArray *tokenNames;
178*16467b97STreehugger Robot 
179*16467b97STreehugger Robot @end
180