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