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