/* [The "BSD licence"] Copyright (c) 2005-2006 Terence Parr 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. */ /* in sync with Java/Java.stg revision 107 */ group Python implements ANTLRCore; /** 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 sys from antlr3 import * from antlr3.tree import *<\n> from antlr3.compat import set, frozenset <@end> !> # for convenience in actions HIDDEN = BaseRecognizer.HIDDEN # token types =}; separator="\n"> def main(argv, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr): from antlr3.main import LexerMain main = LexerMain()<\n> from antlr3.main import ParserMain main = ParserMain("Lexer", )<\n> from antlr3.main import WalkerMain main = WalkerMain()<\n> main.stdin = stdin main.stdout = stdout main.stderr = stderr main.execute(argv)<\n> if __name__ == '__main__': main(sys.argv) >> lexer(grammar, name, tokens, scopes, rules, numRules, labelType="Token", filterMode, superClass="Lexer") ::= << import }; separator="\n"> class (<@superClassName><@end>): }> grammarFileName = "" antlr_version = version_str_to_tuple("") antlr_version_str = "" def __init__(self}>, input=None, state=None): if state is None: state = RecognizerSharedState() <@superClassName><@end>.__init__(self, input, state) self._state.ruleMemo = {} = (, }>self, input, state)}; separator="\n"> = }; separator="\n"> }; separator="\n"> }; separator="\n"> }> >> /** 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. * Make rule memoization happen only at levels above 1 as we start mTokens * at backtracking==1. */ filteringNextToken() ::= << def nextToken(self): while True: if self.input.LA(1) == EOF: return EOF_TOKEN self._state.token = None self._state.channel = DEFAULT_CHANNEL self._state.tokenStartCharIndex = self.input.index() self._state.tokenStartCharPositionInLine = self.input.charPositionInLine self._state.tokenStartLine = self.input.line self._state._text = None try: m = self.input.mark() try: # means we won't throw slow exception self._state.backtracking = 1 try: self.mTokens() finally: self._state.backtracking = 0 except BacktrackingFailed: # mTokens backtracks with synpred at backtracking==2 # and we set the synpredgate to allow actions at level 1. self.input.rewind(m) self.input.consume() # advance one char and try again else: self.emit() return self._state.token except RecognitionException, re: # shouldn't happen in backtracking mode, but... self.reportError(re) self.recover(re) def memoize(self, input, ruleIndex, ruleStartIndex, success): if self._state.backtracking > 1: # is Lexer always superclass? <@superClassName><@end>.memoize(self, input, ruleIndex, ruleStartIndex, success) def alreadyParsedRule(self, input, ruleIndex): if self._state.backtracking > 1: return <@superClassName><@end>.alreadyParsedRule(self, input, ruleIndex) return False >> filteringActionGate() ::= "self._state.backtracking == 1" /** How to generate a parser */ genericParser(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets, inputStreamType, superClass, ASTLabelType="Object", labelType, members, rewriteElementType, init) ::= << # token names tokenNames = [ "\", "\", "\", "\", ]<\n> from import tokenNames<\n> }> import }; separator="\n"> }> class (): grammarFileName = "" antlr_version = version_str_to_tuple("") antlr_version_str = "" tokenNames = tokenNames def __init__(self}>, input, state=None): if state is None: state = RecognizerSharedState() .__init__(self, input, state) self._state.ruleMemo = {} = }; separator="\n"> }; separator="\n"> }; separator="\n"> }> }> = (, }>self, input, state)}; separator="\n"> = (self}>, input, state)}; separator="\n"!> <@init> <@end> <@members> <@end> # Delegated rules }; separator="\n"> }> _in_ = frozenset([};separator=", ">])<\n>}> >> delegateRule(ruleDescriptor) ::= << def (self, ): <\ > return self..(}; separator=", ">) >> parser(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets, ASTLabelType="object", superClass="Parser", labelType="Token", members={}) ::= << }, ...)> >> /** How to generate a tree parser; same as parser except the input * stream is a different type. */ treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules, numRules, bitsets, labelType={}, ASTLabelType="Object", superClass="TreeParser", members={}) ::= << }, ...)> >> /** 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) ::= << # $ANTLR start "" def _fragment(self, ): self.traceIn("_fragment", ) try: finally: self.traceOut("_fragment", ) # $ANTLR end "" >> synpred(name) ::= << def (self): self._state.backtracking += 1 <@start()> start = self.input.mark() try: self._fragment() except BacktrackingFailed: success = False else: success = True self.input.rewind(start) <@stop()> self._state.backtracking -= 1 return success >> lexerSynpred(name) ::= << >> ruleMemoization(name) ::= << if self._state.backtracking > 0 and self.alreadyParsedRule(self.input, ): # for cached failed rules, alreadyParsedRule will raise an exception success = True return >> /** This rule has failed, exit indicating failure during backtrack */ ruleBacktrackFailure() ::= << if self._state.backtracking > 0: raise BacktrackingFailed >> /** How to generate code for a rule. This includes any return type * data aggregates required for multiple return values. */ rule(ruleName,ruleDescriptor,block,emptyRule,description,exceptions,finally,memoize) ::= << # $ANTLR start "" # : def (self, ): self.traceIn("", )<\n> <@preamble()> success = False<\n> try: try: <(ruleDescriptor.actions.after):execAction()> success = True<\n> <\n>}> except RecognitionException, re: self.reportError(re) self.recover(self.input, re) <@setErrorReturnValue()> finally: pass finally: self.traceOut("", )<\n> pass <@postamble()> return # $ANTLR end "" >> catch(decl,action) ::= << except : >> ruleDeclarations() ::= << retval = self._return() retval.start = self.input.LT(1)<\n> = None }> _StartIndex = self.input.index() >> ruleScopeSetUp() ::= << _stack.append(_scope())}; separator="\n"> _stack.append(_scope())}; separator="\n"> >> ruleScopeCleanUp() ::= << _stack.pop()}; separator="\n"> _stack.pop()}; separator="\n"> >> ruleLabelDefs() ::= << <[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels] :{ = None}; separator="\n" > <[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels] :{list_ = None}; separator="\n" > <[ruleDescriptor.ruleLabels,ruleDescriptor.ruleListLabels] :ruleLabelDef(label=it); separator="\n" > = None}; separator="\n"> >> lexerRuleLabelDefs() ::= << <[ruleDescriptor.tokenLabels, ruleDescriptor.tokenListLabels, ruleDescriptor.ruleLabels] :{ = None}; separator="\n" > = None}; separator="\n"> <[ruleDescriptor.tokenListLabels, ruleDescriptor.ruleListLabels, ruleDescriptor.ruleListLabels] :{list_ = None}; separator="\n" > >> ruleReturnValue() ::= << retval >> ruleCleanUp() ::= << retval.stop = self.input.LT(-1)<\n> >> memoize() ::= << if self._state.backtracking > 0: self.memoize(self.input, , _StartIndex, success) >> /** How to generate a rule in the lexer; naked blocks are used for * fragment rules. */ lexerRule(ruleName,nakedBlock,ruleDescriptor,block,memoize) ::= << # $ANTLR start "" def m(self, ): self.traceIn("", )<\n> success = False<\n> try: <\n> _type = _channel = DEFAULT_CHANNEL self._state.type = _type self._state.channel = _channel <(ruleDescriptor.actions.after):execAction()> success = True<\n> finally: self.traceOut("", )<\n> pass # $ANTLR end "" >> /** How to generate code for the implicitly-defined lexer grammar rule * that chooses between lexer rules. */ tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::= << def mTokens(self): <\n> >> // S U B R U L E S /** A (...) subrule with multiple alternatives */ block(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << # : alt = <@predecision()> <@postdecision()> <@prebranch()> <@postbranch()> >> /** A rule block with multiple alternatives */ ruleBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << # : alt = <@predecision()> <@postdecision()> >> ruleBlockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= << # : <@prealt()> <@postalt()> >> /** A special case of a (...) subrule with a single alternative */ blockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= << # : <@prealt()> <@postalt()> >> /** A (..)+ block with 1 or more alternatives */ positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << # : cnt = 0 <@preloop()> while True: #loop alt = <@predecision()> <@postdecision()> else: if cnt >= 1: break #loop eee = EarlyExitException(, self.input) <@earlyExitException()> raise eee cnt += 1 <@postloop()> >> positiveClosureBlockSingleAlt ::= positiveClosureBlock /** A (..)* block with 1 or more alternatives */ closureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= << # : <@preloop()> while True: #loop alt = <@predecision()> <@postdecision()> else: break #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() ::= << if alt == : <@prealt()> >> /** An alternative is just a list of elements; at outermost level */ alt(elements,altNum,description,autoAST,outerAlt, treeLevel,rew) ::= << # : pass <@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) ::= <<