<s> """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> import os <EOL> import os . path <EOL> import re <EOL> import string <EOL> java_parsing = <NUM_LIT:1> <EOL> default_java_version = '<STR_LIT>' <EOL> if java_parsing : <EOL> _reToken = re . compile ( r'<STR_LIT>' + <EOL> r'<STR_LIT>' + <EOL> r'<STR_LIT>' ) <EOL> class OuterState : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , version = default_java_version ) : <EOL> if not version in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> msg = "<STR_LIT>" % version <EOL> raise NotImplementedError , msg <EOL> self . version = version <EOL> self . listClasses = [ ] <EOL> self . listOutputs = [ ] <EOL> self . stackBrackets = [ ] <EOL> self . brackets = <NUM_LIT:0> <EOL> self . nextAnon = <NUM_LIT:1> <EOL> self . localClasses = [ ] <EOL> self . stackAnonClassBrackets = [ ] <EOL> self . anonStacksStack = [ [ <NUM_LIT:0> ] ] <EOL> self . package = None <EOL> def trace ( self ) : <EOL> pass <EOL> def __getClassState ( self ) : <EOL> try : <EOL> return self . classState <EOL> except AttributeError : <EOL> ret = ClassState ( self ) <EOL> self . classState = ret <EOL> return ret <EOL> def __getPackageState ( self ) : <EOL> try : <EOL> return self . packageState <EOL> except AttributeError : <EOL> ret = PackageState ( self ) <EOL> self . packageState = ret <EOL> return ret <EOL> def __getAnonClassState ( self ) : <EOL> try : <EOL> return self . anonState <EOL> except AttributeError : <EOL> self . outer_state = self <EOL> ret = SkipState ( <NUM_LIT:1> , AnonClassState ( self ) ) <EOL> self . anonState = ret <EOL> return ret <EOL> def __getSkipState ( self ) : <EOL> try : <EOL> return self . skipState <EOL> except AttributeError : <EOL> ret = SkipState ( <NUM_LIT:1> , self ) <EOL> self . skipState = ret <EOL> return ret <EOL> def __getAnonStack ( self ) : <EOL> return self . anonStacksStack [ - <NUM_LIT:1> ] <EOL> def openBracket ( self ) : <EOL> self . brackets = self . brackets + <NUM_LIT:1> <EOL> def closeBracket ( self ) : <EOL> self . brackets = self . brackets - <NUM_LIT:1> <EOL> if len ( self . stackBrackets ) and self . brackets == self . stackBrackets [ - <NUM_LIT:1> ] : <EOL> self . listOutputs . append ( string . join ( self . listClasses , '<STR_LIT:$>' ) ) <EOL> self . localClasses . pop ( ) <EOL> self . listClasses . pop ( ) <EOL> self . anonStacksStack . pop ( ) <EOL> self . stackBrackets . pop ( ) <EOL> if len ( self . stackAnonClassBrackets ) and self . brackets == self . stackAnonClassBrackets [ - <NUM_LIT:1> ] : <EOL> self . __getAnonStack ( ) . pop ( ) <EOL> self . stackAnonClassBrackets . pop ( ) <EOL> def parseToken ( self , token ) : <EOL> if token [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> return IgnoreState ( '<STR_LIT:\n>' , self ) <EOL> elif token == '<STR_LIT>' : <EOL> return IgnoreState ( '<STR_LIT>' , self ) <EOL> elif token == '<STR_LIT:{>' : <EOL> self . openBracket ( ) <EOL> elif token == '<STR_LIT:}>' : <EOL> self . closeBracket ( ) <EOL> elif token in [ '<STR_LIT:">' , "<STR_LIT:'>" ] : <EOL> return IgnoreState ( token , self ) <EOL> elif token == "<STR_LIT>" : <EOL> if len ( self . listClasses ) > <NUM_LIT:0> : <EOL> return self . __getAnonClassState ( ) <EOL> return self . __getSkipState ( ) <EOL> elif token in [ '<STR_LIT:class>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if len ( self . listClasses ) == <NUM_LIT:0> : <EOL> self . nextAnon = <NUM_LIT:1> <EOL> self . stackBrackets . append ( self . brackets ) <EOL> return self . __getClassState ( ) <EOL> elif token == '<STR_LIT>' : <EOL> return self . __getPackageState ( ) <EOL> elif token == '<STR_LIT:.>' : <EOL> return self . __getSkipState ( ) <EOL> return self <EOL> def addAnonClass ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . version in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> clazz = self . listClasses [ <NUM_LIT:0> ] <EOL> self . listOutputs . append ( '<STR_LIT>' % ( clazz , self . nextAnon ) ) <EOL> elif self . version in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . stackAnonClassBrackets . append ( self . brackets ) <EOL> className = [ ] <EOL> className . extend ( self . listClasses ) <EOL> self . __getAnonStack ( ) [ - <NUM_LIT:1> ] = self . __getAnonStack ( ) [ - <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> for anon in self . __getAnonStack ( ) : <EOL> className . append ( str ( anon ) ) <EOL> self . listOutputs . append ( string . join ( className , '<STR_LIT:$>' ) ) <EOL> self . nextAnon = self . nextAnon + <NUM_LIT:1> <EOL> self . __getAnonStack ( ) . append ( <NUM_LIT:0> ) <EOL> def setPackage ( self , package ) : <EOL> self . package = package <EOL> class AnonClassState : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , old_state ) : <EOL> self . outer_state = old_state . outer_state <EOL> self . old_state = old_state <EOL> self . brace_level = <NUM_LIT:0> <EOL> def parseToken ( self , token ) : <EOL> if token [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> return IgnoreState ( '<STR_LIT:\n>' , self ) <EOL> elif token == '<STR_LIT>' : <EOL> return IgnoreState ( '<STR_LIT>' , self ) <EOL> elif token == '<STR_LIT:\n>' : <EOL> return self <EOL> elif token [ <NUM_LIT:0> ] == '<STR_LIT:<>' and token [ - <NUM_LIT:1> ] == '<STR_LIT:>>' : <EOL> return self <EOL> elif token == '<STR_LIT:(>' : <EOL> self . brace_level = self . brace_level + <NUM_LIT:1> <EOL> return self <EOL> if self . brace_level > <NUM_LIT:0> : <EOL> if token == '<STR_LIT>' : <EOL> return SkipState ( <NUM_LIT:1> , AnonClassState ( self ) ) <EOL> elif token in [ '<STR_LIT:">' , "<STR_LIT:'>" ] : <EOL> return IgnoreState ( token , self ) <EOL> elif token == '<STR_LIT:)>' : <EOL> self . brace_level = self . brace_level - <NUM_LIT:1> <EOL> return self <EOL> if token == '<STR_LIT:{>' : <EOL> self . outer_state . addAnonClass ( ) <EOL> return self . old_state . parseToken ( token ) <EOL> class SkipState : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tokens_to_skip , old_state ) : <EOL> self . tokens_to_skip = tokens_to_skip <EOL> self . old_state = old_state <EOL> def parseToken ( self , token ) : <EOL> self . tokens_to_skip = self . tokens_to_skip - <NUM_LIT:1> <EOL> if self . tokens_to_skip < <NUM_LIT:1> : <EOL> return self . old_state <EOL> return self <EOL> class ClassState : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , outer_state ) : <EOL> self . outer_state = outer_state <EOL> def parseToken ( self , token ) : <EOL> if token == '<STR_LIT:\n>' : <EOL> return self <EOL> if self . outer_state . localClasses and self . outer_state . stackBrackets [ - <NUM_LIT:1> ] > self . outer_state . stackBrackets [ - <NUM_LIT:2> ] + <NUM_LIT:1> : <EOL> locals = self . outer_state . localClasses [ - <NUM_LIT:1> ] <EOL> try : <EOL> idx = locals [ token ] <EOL> locals [ token ] = locals [ token ] + <NUM_LIT:1> <EOL> except KeyError : <EOL> locals [ token ] = <NUM_LIT:1> <EOL> token = str ( locals [ token ] ) + token <EOL> self . outer_state . localClasses . append ( { } ) <EOL> self . outer_state . listClasses . append ( token ) <EOL> self . outer_state . anonStacksStack . append ( [ <NUM_LIT:0> ] ) <EOL> return self . outer_state <EOL> class IgnoreState : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ignore_until , old_state ) : <EOL> self . ignore_until = ignore_until <EOL> self . old_state = old_state <EOL> def parseToken ( self , token ) : <EOL> if self . ignore_until == token : <EOL> return self . old_state <EOL> return self <EOL> class PackageState : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , outer_state ) : <EOL> self . outer_state = outer_state <EOL> def parseToken ( self , token ) : <EOL> self . outer_state . setPackage ( token ) <EOL> return self . outer_state <EOL> def parse_java_file ( fn , version = default_java_version ) : <EOL> return parse_java ( open ( fn , '<STR_LIT:r>' ) . read ( ) , version ) <EOL> def parse_java ( contents , version = default_java_version , trace = None ) : <EOL> """<STR_LIT>""" <EOL> package = None <EOL> initial = OuterState ( version ) <EOL> currstate = initial <EOL> for token in _reToken . findall ( contents ) : <EOL> currstate = currstate . parseToken ( token ) <EOL> if trace : trace ( token , currstate ) <EOL> if initial . package : <EOL> package = string . replace ( initial . package , '<STR_LIT:.>' , os . sep ) <EOL> return ( package , initial . listOutputs ) <EOL> else : <EOL> def parse_java_file ( fn ) : <EOL> """<STR_LIT>""" <EOL> return os . path . split ( file ) </s>
