/* [The "BSD licence"] Copyright (c) 2006,2007 Kay Roepke All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* Template group file for the Objective C code generator. Heavily based on Java.stg Written by Kay Roepke This file is part of ANTLR and subject to the same license as ANTLR itself. */ group ObjC implements ANTLRCore; objcTypeInitMap ::= [ "int":"0", "long":"0", "float":"0.0", "double":"0.0", "boolean":"false", "byte":"0", "short":"0", "char":"0", "id":"nil", default:"nil" // anything other than an atomic type ] className() ::= "LexerTreeParser" /** The overall file structure of a recognizer; stores methods for rules * and cyclic DFAs plus support code. */ outputFile(LEXER,PARSER,TREE_PARSER, actionScope, actions, docComment, recognizer, name, tokens, tokenNames, rules, cyclicDFAs, bitsets, buildTemplate, buildAST, rewriteMode, profile, backtracking, synpreds, memoize, numRules, fileName, ANTLRVersion, generatedTimestamp, trace, scopes, superClass,literals) ::= << // $ANTLR <@imports> #import "LexerTreeParser.h" <@end> >> headerFile(LEXER,PARSER,TREE_PARSER, actionScope, actions, docComment, recognizer, name, tokens, tokenNames, rules, cyclicDFAs, bitsets, buildTemplate, buildAST, rewriteMode, profile, backtracking, synpreds, memoize, numRules, fileName, ANTLRVersion, generatedTimestamp, trace, scopes, superClass,literals) ::= << // $ANTLR <@imports> #import \ #import \ <@end> >> lexerHeaderFile() ::= << #pragma mark Rule return scopes start }> }> #pragma mark Rule return scopes end #pragma mark Tokens _ }; separator="\n"> @interface : ANTLRLexer { DFA *dfa;}; separator="\n"> SyntacticPredicate;}; separator="\n"> } }>) m; }> @end >> lexer(grammar, name, tokens, scopes, rules, numRules, labelType="id ", filterMode) ::= << /** As per Terence: No returns for lexer rules! #pragma mark Rule return scopes start }> }> #pragma mark Rule return scopes end */ @implementation static NSArray *tokenNames; + (void) initialize { // todo: get tokenNames into lexer - requires changes to CodeGenerator.java and ANTLRCore.sti tokenNames = [[NSArray alloc] init]; } - (id) initWithCharStream:(id\)anInput { if (nil!=(self = [super initWithCharStream:anInput])) { // initialize the memoization cache - the indices are 1-based in the runtime code! [ruleMemo addObject:[NSNull null]]; // dummy entry to ensure 1-basedness. for (int i = 0; i \< ; i++) { [[state ruleMemo] addObject:[NSMutableDictionary dictionary]]; } };separator="\n"> = [[DFA alloc] initWithRecognizer:self];}; separator="\n"> } return self; } - (void) dealloc { release];}; separator="\n"> [super dealloc]; } + (NSString *) tokenNameForType:(int)aTokenType { return nil; } + (NSArray *) tokenNames { return tokenNames; } - (NSString *) grammarFileName { return @""; } - (void) reset { [super reset]; } @end >> /** A override of Lexer.nextToken() that backtracks over mTokens() looking * for matches. No error can be generated upon error; just rewind, consume * a token and then try again. backtracking needs to be set as well. */ filteringNextToken() ::= << - (id\) nextToken { while (YES) { if ( [input LA:1] == ANTLRCharStreamEOF ) { return nil; // should really be a +eofToken call here -> go figure } [self setToken:nil]; [_state setChannel:ANTLRTokenChannelDefault]; [_state setTokenStartLine:[input line]]; [_state setTokenCharPositionInLine:[input charPositionInLine]]; [_state setTokenStartCharIndex:[self charIndex]]; @try { int m = [input mark]; [_state setBacktracking:1]; [_state setIsFailed:NO]; [self mTokens]; [_state setBacktracking:0]; if ( [_state isFailed] ) { [input rewind:m]; [input consume]; } else { [self emit]; return token; } } @catch (ANTLRRecognitionException *re) { // shouldn't happen in backtracking mode, but... [self reportError:re]; [self recover:re]; } } } >> filteringActionGate() ::= "[_state backtracking] == 1" treeParserHeaderFile(LEXER, PARSER, TREE_PARSER, actionScope, actions, docComment, recognizer, name, tokens, tokenNames, rules, cyclicDFAs, bitsets, buildTemplate, profile, backtracking, synpreds, memoize, numRules, fileName, ANTLRVersion, generatedTimestamp, trace, scopes, superClass="ANTLRTreeParser") ::= << >> parserHeaderFile(LEXER, PARSER, TREE_PARSER, actionScope, actions, docComment, recognizer, name, tokens, tokenNames, rules, cyclicDFAs, bitsets, buildTemplate, profile, backtracking, synpreds, memoize, numRules, fileName, ANTLRVersion, generatedTimestamp, trace, scopes, superClass="ANTLRParser") ::= << #pragma mark Tokens _ }; separator="\n"> #pragma mark Dynamic Global Scopes }> #pragma mark Dynamic Rule Scopes }> }> #pragma mark Rule Return Scopes }> }> @interface : <@superClassName><@end> { DFA *dfa;}; separator="\n"> SyntacticPredicate;}; separator="\n"> }> }> <@ivars()> } }>) ; }> <@methodsdecl()> @end >> /** How to generate a parser */ genericParser(name, scopes, tokens, tokenNames, rules, numRules, cyclicDFAs, bitsets, inputStreamType, superClass, ASTLabelType="id", labelType, members) ::= << #pragma mark Bitsets _in_}, words64=it.bits)> #pragma mark Dynamic Global Scopes }> #pragma mark Dynamic Rule Scopes }> #pragma mark Rule return scopes start }> }> @implementation static NSArray *tokenNames; + (void) initialize { _in_}, words64=it.bits)> tokenNames = [[NSArray alloc] initWithObjects:@"\", @"\", @"\", @"\", }; separator=", ", wrap="\n ">, nil]; } - (id) initWithTokenStream:()aStream { if ((self = [super initWithTokenStream:aStream])) { - (id) initWithTreeNodeStream:()aStream { if ((self = [super initWithTreeNodeStream:aStream])) { <\n> ruleMemo = [[NSMutableDictionary alloc] initWithCapacity:+1]; = [[DFA alloc] initWithRecognizer:self];}; separator="\n"> }> }> <@init()> } return self; } - (void) dealloc { [ruleMemo release]; release];}; separator="\n"> }> <@dealloc()> [super dealloc]; } - (NSString *) grammarFileName { return @""; } }> <@methods()> @end >> parser(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets, ASTLabelType, superClass="ANTLRParser", labelType="id ", members={}) ::= << ", ...)> >> treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules, numRules, bitsets, labelType={}, ASTLabelType="id", superClass="ANTLRTreeParser", members={}) ::= << ", ...)> >> /** Maintain a local variable for the shared state object to avoid calling the accessor all the time. */ sharedStateLocalVarDefinition() ::= << ANTLRLexerStateANTLRBaseRecognizerState *_state = [self state]; >> /** A simpler version of a rule template that is specific to the imaginary * rules created for syntactic predicates. As they never have return values * nor parameters etc..., just give simplest possible method. Don't do * any of the normal memoization stuff in here either; it's a waste. * As predicates cannot be inlined into the invoking rule, they need to * be in a rule by themselves. */ synpredRule(ruleName, ruleDescriptor, block, description, nakedBlock) ::= << - (void) { NSLog(@"enter %d failed=%@ backtracking=%d", [input LA:1], [_state isFailed] ? @"YES" : @"NO", [_state backtracking]); @try { } @finally { NSLog(@"exit %d failed=%@ backtracking=%d", [input LA:1], [_state isFailed] ? @"YES" : @"NO", [_state backtracking]); } } >> /** How to test for failure and return from rule */ checkRuleBacktrackFailure() ::= << if ([_state isFailed]) return ; >> /** This rule has failed, exit indicating failure during backtrack */ ruleBacktrackFailure() ::= << if ([_state isBacktracking]) {[_state setIsFailed:YES]; return ;} >> synpred(name) ::= << SyntacticPredicate = @selector(); !> >> lexerSynpred(name) ::= << >> ruleMemoization(name) ::= << if ([_state isBacktracking] && [self alreadyParsedRule:input ruleIndex:] ) { return ; } >> /** How to generate code for a rule. * The return type aggregates are declared in the header file (headerFile template) */ rule(ruleName,ruleDescriptor,block,emptyRule,description,exceptions,finally,memoize) ::= << // $ANTLR start // : - () { NSLog(@"enter %@ failed=%@ backtracking=%d", [input LT:1], [_state isFailed] ? @"YES" : @"NO", [_state backtracking]); <@preamble()> @try { } <\n>}> @catch (ANTLRRecognitionException *re) { [self reportError:re]; [self recover:input exception:re]; }<\n> @finally { NSLog(@"exit %@ failed=%@ backtracking=%d", [input LT:1], [_state isFailed] ? @"YES" : @"NO", [_state backtracking]); <(ruleDescriptor.actions.finally):execAction()> } <@postamble()> return ; } // $ANTLR end >> catch(decl,action) ::= << catch () { } >> ruleDeclarations() ::= << _retval = [[[ alloc] init] autorelease]; [_retval setStart:[input LT:1]];<\n> _ = ; int _StartIndex = [input index]; >> ruleScopeSetUp() ::= << __stack addObject:[[[Scope alloc] init] autorelease]];}; separator="\n"> __stack addObject:[[[Scope alloc] init] autorelease]];}; separator="\n"> >> ruleScopeCleanUp() ::= << __stack removeLastObject];}; separator="\n"> __stack removeLastObject];}; separator="\n"> >> ruleLabelDefs() ::= << <[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels] :{ _ = nil;}; separator="\n" > <[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels] :{NSMutableArray *_list_ = nil;}; separator="\n" > = nil;}; separator="\n"> >> ruleReturnValue() ::= << _ _retval >> ruleCleanUp() ::= << // token+rule list labels <[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels]:{[_list_ release];}; separator="\n"> [_retval setStop:[input LT:-1]];<\n> if ( [_state isBacktracking] ) { [self memoize:input ruleIndex: startIndex:_StartIndex]; } >> /** How to generate a rule in the lexer; naked blocks are used for * fragment rules. */ lexerRule(ruleName,nakedBlock,ruleDescriptor,block, memoize) ::= << - (void) m { NSLog(@"enter %C line=%d:%d failed=%@ backtracking=%d", [input LA:1], [self line], [self charPositionInLine], [_state isFailed] ? @"YES" : @"NO", [_state backtracking]); @try { <\n> int _type = _; [_state setTokenType:_type];<\n> } @finally { NSLog(@"exit %C line=%d:%d failed=%@ backtracking=%d", [input LA:1], [self line], [self charPositionInLine], [_state isFailed] ? @"YES" : @"NO", [_state backtracking]); // rule cleanup <(ruleDescriptor.actions.finally):execAction()> } return; } // $ANTLR end >> /** How to generate code for the implicitly-defined lexer grammar rule * that chooses between lexer rules. */ tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::= << - (void) mTokens { <\n> } >> // S U B R U L E S /** A (...) subrule with multiple alternatives */ block(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << // : // block int alt=; <@predecision()> <@postdecision()> <@prebranch()> switch (alt) { } <@postbranch()> >> /** A rule block with multiple alternatives */ ruleBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << // : //ruleblock int alt=; <@predecision()> <@postdecision()> switch (alt) { } >> ruleBlockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= << // : // ruleBlockSingleAlt <@prealt()> <@postalt()> >> /** A special case of a (...) subrule with a single alternative */ blockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= << // : // blockSingleAlt <@prealt()> <@postalt()> >> /** A (..)+ block with 0 or more alternatives */ positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << // : // positiveClosureBlock int cnt=0; <@preloop()> do { int alt=; <@predecision()> <@postdecision()> switch (alt) { default : if ( cnt >= 1 ) goto loop; ANTLREarlyExitException *eee = [ANTLREarlyExitException exceptionWithStream:input decisionNumber:]; <@earlyExitException()> @throw eee; } cnt++; } while (YES); loop: ; <@postloop()> >> positiveClosureBlockSingleAlt ::= positiveClosureBlock /** A (..)* block with 0 or more alternatives */ closureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << <@preloop()> do { int alt=; <@predecision()> <@postdecision()> switch (alt) { default : goto loop; } } while (YES); loop: ; <@postloop()> >> closureBlockSingleAlt ::= closureBlock /** Optional blocks (x)? are translated to (x|) by before code generation * so we can just use the normal block template */ optionalBlock ::= block optionalBlockSingleAlt ::= block /** A case in a switch that jumps to an alternative given the alternative * number. A DFA predicts the alternative and then a simple switch * does the jump to the code that actually matches that alternative. */ altSwitchCase() ::= << case : <@prealt()> break;<\n> >> /** An alternative is just a list of elements; at outermost level */ alt(elements,altNum,description,autoAST,outerAlt,treeLevel) ::= << // : // alt { <@declarations()> <@cleanup()> } >> /** What to emit when there is no rewrite. For auto build * mode, does nothing. */ noRewrite(rewriteBlockLevel, treeLevel) ::= "" // E L E M E N T S /** Dump the elements one per line */ element() ::= << <@prematch()> <\n> >> /** match a token optionally with a label in front */ tokenRef(token,label,elementIndex,hetero) ::= << _