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 #import "TreeParser.h" 28 29 @implementation TreeParser 30 31 @synthesize input; 32 33 + (id) newTreeParser:(id<TreeNodeStream>)anInput 34 { 35 return [[TreeParser alloc] initWithStream:anInput]; 36 } 37 38 + (id) newTreeParser:(id<TreeNodeStream>)anInput State:(RecognizerSharedState *)theState 39 { 40 return [[TreeParser alloc] initWithStream:anInput State:theState]; 41 } 42 43 - (id) initWithStream:(id<TreeNodeStream>)theInput 44 { 45 if ((self = [super init]) != nil) { 46 [self setInput:theInput]; 47 } 48 return self; 49 } 50 51 - (id) initWithStream:(id<TreeNodeStream>)theInput State:(RecognizerSharedState *)theState 52 { 53 if ((self = [super init]) != nil) { 54 [self setInput:theInput]; 55 state = theState; 56 } 57 return self; 58 } 59 60 - (void) dealloc 61 { 62 #ifdef DEBUG_DEALLOC 63 NSLog( @"called dealloc in TreeParser" ); 64 #endif 65 if ( input ) [input release]; 66 [super dealloc]; 67 } 68 69 - (void) reset 70 { 71 [super reset]; // reset all recognizer state variables 72 if ( input != nil ) { 73 [input seek:0]; // rewind the input 74 } 75 } 76 77 - (void) mismatch:(id<IntStream>)aStream tokenType:(TokenType)aTType follow:(ANTLRBitSet *)aBitset 78 { 79 MismatchedTreeNodeException *mte = [MismatchedTreeNodeException newException:aTType Stream:aStream]; 80 [mte setNode:[((id<TreeNodeStream>)aStream) LT:1]]; 81 [self recoverFromMismatchedToken:aStream Type:aTType Follow:aBitset]; 82 } 83 84 - (void) setTreeNodeStream:(id<TreeNodeStream>) anInput 85 { 86 input = anInput; 87 } 88 89 - (id<TreeNodeStream>) getTreeNodeStream 90 { 91 return input; 92 } 93 94 - (NSString *)getSourceName 95 { 96 return [input getSourceName]; 97 } 98 99 - (id) getCurrentInputSymbol:(id<IntStream>) anInput 100 { 101 return [(id<TreeNodeStream>)anInput LT:1]; 102 } 103 104 - (id) getMissingSymbol:(id<IntStream>)anInput 105 Exception:(RecognitionException *)e 106 ExpectedToken:(NSInteger)expectedTokenType 107 BitSet:(ANTLRBitSet *)follow 108 { 109 NSString *tokenText =[NSString stringWithFormat:@"<missing %@ %d>", [self getTokenNames], expectedTokenType]; 110 //id<TreeAdaptor> anAdaptor = (id<TreeAdaptor>)[((id<TreeNodeStream>)e.input) getTreeAdaptor]; 111 //return [anAdaptor createToken:expectedTokenType Text:tokenText]; 112 return [CommonToken newToken:expectedTokenType Text:tokenText]; 113 } 114 115 /** Match '.' in tree parser has special meaning. Skip node or 116 * entire tree if node has children. If children, scan until 117 * corresponding UP node. 118 */ 119 - (void) matchAny:(id<IntStream>)ignore 120 { // ignore stream, copy of input 121 state.errorRecovery = NO; 122 state.failed = NO; 123 id look = [input LT:1]; 124 if ( [((CommonTreeAdaptor *)[input getTreeAdaptor]) getChildCount:look] == 0) { 125 [input consume]; // not subtree, consume 1 node and return 126 return; 127 } 128 // current node is a subtree, skip to corresponding UP. 129 // must count nesting level to get right UP 130 int level=0; 131 int tokenType = [((id<TreeAdaptor>)[input getTreeAdaptor]) getType:look]; 132 while ( tokenType != TokenTypeEOF && !( tokenType == TokenTypeUP && level == 0) ) { 133 [input consume]; 134 look = [input LT:1]; 135 tokenType = [((id<TreeAdaptor>)[input getTreeAdaptor]) getType:look]; 136 if ( tokenType == TokenTypeDOWN ) { 137 level++; 138 } 139 else if ( tokenType == TokenTypeUP ) { 140 level--; 141 } 142 } 143 [input consume]; // consume UP 144 } 145 146 /** We have DOWN/UP nodes in the stream that have no line info; override. 147 * plus we want to alter the exception type. Don't try to recover 148 * from tree parser errors inline... 149 */ 150 - (id) recoverFromMismatchedToken:(id<IntStream>)anInput Type:(NSInteger)ttype Follow:(ANTLRBitSet *)follow 151 { 152 @throw [MismatchedTreeNodeException newException:ttype Stream:anInput]; 153 } 154 155 /** Prefix error message with the grammar name because message is 156 * always intended for the programmer because the parser built 157 * the input tree not the user. 158 */ 159 - (NSString *)getErrorHeader:(RecognitionException *)e 160 { 161 return [NSString stringWithFormat:@"%@: node after line %@:%@", 162 [self getGrammarFileName], e.line, e.charPositionInLine]; 163 } 164 165 /** Tree parsers parse nodes they usually have a token object as 166 * payload. Set the exception token and do the default behavior. 167 */ 168 - (NSString *)getErrorMessage:(RecognitionException *)e TokenNames:(AMutableArray *) theTokNams 169 { 170 if ( [self isKindOfClass:[TreeParser class]] ) { 171 CommonTreeAdaptor *adaptor = (CommonTreeAdaptor *)[((id<TreeNodeStream>)e.input) getTreeAdaptor]; 172 e.token = [adaptor getToken:((CommonTree *)e.node)]; 173 if ( e.token == nil ) { // could be an UP/DOWN node 174 e.token = [CommonToken newToken:[adaptor getType:(CommonTree *)e.node] 175 Text:[adaptor getText:(CommonTree *)e.node]]; 176 } 177 } 178 return [super getErrorMessage:e TokenNames:theTokNams]; 179 } 180 181 - (void) traceIn:(NSString *)ruleName Index:(NSInteger)ruleIndex 182 { 183 [super traceIn:ruleName Index:ruleIndex Object:[input LT:1]]; 184 } 185 186 - (void) traceOut:(NSString *)ruleName Index:(NSInteger)ruleIndex 187 { 188 [super traceOut:ruleName Index:ruleIndex Object:[input LT:1]]; 189 } 190 191 192 @end 193