<s> import re <EOL> import os <EOL> import types <EOL> class DocStract ( ) : <EOL> class Type ( ) : <EOL> def __init__ ( self , val , orig ) : <EOL> val . pop ( '<STR_LIT>' ) <EOL> if ( val . has_key ( '<STR_LIT:name>' ) and len ( val ) == <NUM_LIT:1> ) : <EOL> self . value = val [ '<STR_LIT:name>' ] <EOL> else : <EOL> self . value = val <EOL> self . orig = orig <EOL> def __repr__ ( self ) : <EOL> return self . orig <EOL> def __init__ ( self ) : <EOL> self . docBlockFindPat = re . compile ( '<STR_LIT>' , re . S | re . X ) <EOL> self . docBlockProcessPat = re . compile ( '<STR_LIT>' , re . S | re . X ) <EOL> self . blockFilterPat = re . compile ( '<STR_LIT>' , re . M ) <EOL> self . unescapeTagPat = re . compile ( '<STR_LIT>' , re . M ) <EOL> self . tokenizePat = re . compile ( r'''<STR_LIT>''' , re . M | re . X ) ; <EOL> self . markerPat = re . compile ( r'''<STR_LIT>''' , re . M | re . X ) <EOL> self . blockTypes = { <EOL> '<STR_LIT>' : ConstructorBlockHandler ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : FunctionBlockHandler ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ModuleBlockHandler ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : PropertyBlockHandler ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ClassBlockHandler ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : EndClassBlockHandler ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : TypedefBlockHandler ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : EndTypedefBlockHandler ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : EventBlockHandler ( '<STR_LIT>' ) <EOL> } <EOL> self . aliases = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . tags = { <EOL> '<STR_LIT>' : ParamTagHandler ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : DescTagHandler ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ReturnTagHandler ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : SeeTagHandler ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ThrowsTagHandler ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : TypeTagHandler ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : PayloadTagHandler ( '<STR_LIT>' ) <EOL> } <EOL> self . typeGuessers = [ <EOL> hasGetSetterIsPropertyTypeGuesser , <EOL> isFunctionIfKeywordInCodeTypeGuesser , <EOL> firstBlockIsModuleTypeGuesser , <EOL> assignmentIsProbablyPropertyTypeGuesser , <EOL> typeWithoutReturnsIsProbablyPropertyTypeGuesser <EOL> ] <EOL> self . nameGuessers = [ <EOL> standardFunctionNameGuesser , <EOL> getSetterNameGuesser , <EOL> objectPropertyNameGuesser , <EOL> commonJSNameGuesser , <EOL> assignToPropertyNameGuesser <EOL> ] <EOL> def _isMarker ( self , tok ) : <EOL> return type ( tok ) == types . StringType and self . markerPat . match ( tok ) <EOL> def _popNonMarker ( self , toks ) : <EOL> nxt = None <EOL> if ( len ( toks ) == <NUM_LIT:0> ) : <EOL> return None <EOL> if not self . _isMarker ( self . _peekTok ( toks ) ) : <EOL> nxt = toks . pop ( <NUM_LIT:0> ) <EOL> return nxt <EOL> def _peekTok ( self , toks ) : <EOL> if ( len ( toks ) ) : <EOL> return toks [ <NUM_LIT:0> ] <EOL> return None <EOL> def _consumeToks ( self , tokens , currentObj ) : <EOL> cur = tokens . pop ( <NUM_LIT:0> ) <EOL> handler = None <EOL> if cur in self . blockTypes : <EOL> handler = self . blockTypes [ cur ] <EOL> if currentObj [ '<STR_LIT>' ] : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( currentObj [ '<STR_LIT>' ] . tagName , <EOL> handler . tagName ) + <EOL> "<STR_LIT>" ) <EOL> currentObj [ '<STR_LIT>' ] = handler <EOL> elif cur in self . tags : <EOL> handler = self . tags [ cur ] <EOL> args = [ ] <EOL> while len ( tokens ) > <NUM_LIT:0> and not self . _isMarker ( self . _peekTok ( tokens ) ) : <EOL> t = tokens . pop ( <NUM_LIT:0> ) <EOL> args . append ( t ) <EOL> if not handler == None : <EOL> arg = None <EOL> if handler . takesArg : <EOL> if len ( args ) == <NUM_LIT:0> and not handler . argOptional : <EOL> raise RuntimeError ( "<STR_LIT>" % cur ) <EOL> elif not len ( args ) == <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" % cur ) <EOL> ctx = handler . parse ( args ) <EOL> if handler . mayRecur : <EOL> if cur not in currentObj [ "<STR_LIT>" ] : <EOL> currentObj [ "<STR_LIT>" ] [ cur ] = [ ] <EOL> currentObj [ "<STR_LIT>" ] [ cur ] . append ( ctx ) <EOL> else : <EOL> if cur in currentObj [ "<STR_LIT>" ] : <EOL> raise RuntimeError ( "<STR_LIT>" % cur ) <EOL> currentObj [ "<STR_LIT>" ] [ cur ] = ctx <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" % cur ) <EOL> def _guessBlockName ( self , codeChunk , blockType ) : <EOL> for func in self . nameGuessers : <EOL> t = func ( codeChunk , blockType ) <EOL> if t != None : <EOL> return t <EOL> return None <EOL> def _guessBlockType ( self , firstBlock , codeChunk , context , tags ) : <EOL> tagSet = set ( tags ) <EOL> possibilities = [ ] <EOL> for bt in self . blockTypes : <EOL> bt = self . blockTypes [ bt ] <EOL> if context not in bt . allowedContexts : <EOL> continue <EOL> if not tagSet . issubset ( bt . allowedTags ) : <EOL> continue <EOL> possibilities . append ( bt . tagName ) <EOL> if len ( possibilities ) == <NUM_LIT:1> : <EOL> return possibilities [ <NUM_LIT:0> ] <EOL> for func in self . typeGuessers : <EOL> t = func ( firstBlock , codeChunk , context , tags , possibilities ) <EOL> if t != None : <EOL> return t <EOL> raise RuntimeError ( "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( possibilities ) ) <EOL> def _whatContext ( self , stack ) : <EOL> return stack [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> def _analyzeBlock ( self , block , codeChunk , firstBlock , stack , lineStart , lineEnd ) : <EOL> tokens = self . tokenizePat . split ( block ) <EOL> tokens = [ n for n in tokens if not n == None ] <EOL> tokens = [ n . lstrip ( "<STR_LIT>" ) . lstrip ( '<STR_LIT:\r\n>' ) . rstrip ( ) for n in tokens if n . strip ( ) ] <EOL> tokens = [ self . unescapeTagPat . sub ( "<STR_LIT:@>" , t ) for t in tokens ] <EOL> if not self . _isMarker ( tokens [ <NUM_LIT:0> ] ) : <EOL> tokens . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> tokens = [ self . aliases [ n ] if self . aliases . has_key ( n ) else n for n in tokens ] <EOL> tokenGroups = [ ] <EOL> while len ( tokens ) : <EOL> i = <NUM_LIT:1> <EOL> while i < len ( tokens ) : <EOL> if ( tokens [ i ] in self . blockTypes ) : <EOL> break <EOL> i += <NUM_LIT:1> <EOL> tokenGroups . append ( tokens [ : i ] ) <EOL> tokens = tokens [ i : ] <EOL> for tokens in tokenGroups : <EOL> parseData = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> newtoks = [ ] <EOL> for t in tokens : <EOL> if len ( t ) >= <NUM_LIT:2> and t [ <NUM_LIT:0> : <NUM_LIT:1> ] == '<STR_LIT:{>' and t [ - <NUM_LIT:1> : ] == '<STR_LIT:}>' : <EOL> stack . append ( ( '<STR_LIT>' , { } ) ) <EOL> tokObj = { } <EOL> t2 = "<STR_LIT>" + t [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> self . _analyzeBlock ( t2 , codeChunk , False , stack , lineStart , lineEnd ) <EOL> self . _analyzeBlock ( "<STR_LIT>" , codeChunk , False , stack , lineStart , lineEnd ) <EOL> newtoks . append ( DocStract . Type ( stack [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] , t ) ) <EOL> stack . pop ( ) <EOL> else : <EOL> newtoks . append ( t ) <EOL> tokens = newtoks <EOL> while len ( tokens ) : <EOL> self . _consumeToks ( tokens , parseData ) <EOL> thisContext = self . _whatContext ( stack ) <EOL> if parseData [ '<STR_LIT>' ] == None : <EOL> guessedType = self . _guessBlockType ( firstBlock , codeChunk , thisContext , parseData [ '<STR_LIT>' ] . keys ( ) ) <EOL> if guessedType not in self . blockTypes : <EOL> raise RuntimeError ( "<STR_LIT>" % guessedType ) <EOL> parseData [ '<STR_LIT>' ] = self . blockTypes [ guessedType ] <EOL> guessedName = self . _guessBlockName ( codeChunk , parseData [ '<STR_LIT>' ] . tagName ) <EOL> if thisContext not in parseData [ '<STR_LIT>' ] . allowedContexts : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( parseData [ '<STR_LIT>' ] . tagName , <EOL> thisContext ) ) <EOL> for tag in parseData [ '<STR_LIT>' ] : <EOL> if not tag == parseData [ '<STR_LIT>' ] . tagName and tag not in parseData [ '<STR_LIT>' ] . allowedTags : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( tag , parseData [ '<STR_LIT>' ] . tagName ) ) <EOL> doc = { } <EOL> for tag in parseData [ '<STR_LIT>' ] : <EOL> val = parseData [ '<STR_LIT>' ] [ tag ] <EOL> if not type ( val ) == types . ListType : <EOL> val = [ val ] <EOL> for v in val : <EOL> handler = self . tags [ tag ] if tag in self . tags else self . blockTypes [ tag ] <EOL> handler . attach ( v , doc , parseData [ '<STR_LIT>' ] . tagName ) <EOL> parseData [ '<STR_LIT>' ] . setLineNumber ( lineStart , lineEnd , doc ) <EOL> if parseData [ '<STR_LIT>' ] . tagName in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> doc = stack . pop ( ) [ <NUM_LIT:1> ] <EOL> parseData [ '<STR_LIT>' ] . merge ( doc , stack [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] , guessedName , self . _whatContext ( stack ) ) <EOL> if parseData [ '<STR_LIT>' ] . tagName == '<STR_LIT>' : <EOL> stack . append ( ( '<STR_LIT:class>' , doc ) ) <EOL> elif parseData [ '<STR_LIT>' ] . tagName == '<STR_LIT>' : <EOL> stack . append ( ( '<STR_LIT:type>' , doc ) ) <EOL> def extractFromFile ( self , filename ) : <EOL> contents = "<STR_LIT>" <EOL> with open ( filename , "<STR_LIT:r>" ) as f : <EOL> contents = f . read ( ) <EOL> data = self . extract ( contents ) <EOL> mod = os . path . basename ( filename ) <EOL> dotLoc = mod . rfind ( "<STR_LIT:.>" ) <EOL> if ( dotLoc > <NUM_LIT:0> ) : <EOL> mod = mod [ : dotLoc ] <EOL> if not "<STR_LIT>" in data : <EOL> data [ "<STR_LIT>" ] = mod <EOL> if not "<STR_LIT:filename>" in data : <EOL> data [ "<STR_LIT:filename>" ] = filename <EOL> return data <EOL> def extract ( self , contents ) : <EOL> stack = [ ( '<STR_LIT>' , { } ) ] <EOL> firstBlock = True <EOL> line = <NUM_LIT:0> <EOL> for text in self . docBlockFindPat . split ( contents ) : <EOL> lineStart = line + <NUM_LIT:1> <EOL> line += text . count ( '<STR_LIT:\n>' ) <EOL> m = self . docBlockProcessPat . match ( text ) <EOL> if m : <EOL> block = self . blockFilterPat . sub ( "<STR_LIT>" , m . group ( <NUM_LIT:2> ) ) . strip ( ) <EOL> context = m . group ( <NUM_LIT:4> ) . strip ( ) <EOL> try : <EOL> self . _analyzeBlock ( block , context , firstBlock , stack , lineStart , line ) <EOL> except RuntimeError , exc : <EOL> args = exc . args <EOL> if not args : <EOL> arg0 = '<STR_LIT>' <EOL> else : <EOL> arg0 = args [ <NUM_LIT:0> ] <EOL> arg0 += '<STR_LIT>' % lineStart <EOL> exc . args = ( arg0 , ) + args [ <NUM_LIT:1> : ] <EOL> raise <EOL> firstBlock = False <EOL> return stack [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> class TagHandler ( object ) : <EOL> takesArg = False <EOL> argOptional = False <EOL> mayRecur = False <EOL> def __init__ ( self , tagname ) : <EOL> self . tagName = tagname <EOL> def parse ( self , args ) : <EOL> return "<STR_LIT:U+0020>" . join ( [ str ( a ) for a in args ] ) if len ( args ) > <NUM_LIT:0> else None <EOL> def attach ( self , obj , parent , blockType ) : <EOL> parent [ self . tagName [ <NUM_LIT:1> : ] ] = obj <EOL> def _isType ( self , arg ) : <EOL> return isinstance ( arg , DocStract . Type ) <EOL> def _argPrint ( self , args ) : <EOL> return ( "<STR_LIT:(>" + str ( len ( args ) ) + "<STR_LIT>" ) + "<STR_LIT>" . join ( [ str ( x ) [ : <NUM_LIT:10> ] for x in args ] ) [ : <NUM_LIT> ] + "<STR_LIT>" <EOL> class ParamTagHandler ( TagHandler ) : <EOL> mayRecur = True <EOL> takesArg = True <EOL> _nameAndDescPat = re . compile ( '<STR_LIT>' , re . S ) ; <EOL> _optionalPat = re . compile ( '<STR_LIT>' ) <EOL> def parse ( self , args ) : <EOL> p = { } <EOL> if ( len ( args ) == <NUM_LIT:2> and self . _isType ( args [ <NUM_LIT:0> ] ) ) : <EOL> p [ '<STR_LIT:type>' ] = args [ <NUM_LIT:0> ] . value <EOL> args = args [ <NUM_LIT:1> : ] <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> m = self . _nameAndDescPat . match ( args [ <NUM_LIT:0> ] ) <EOL> if not m or self . _isType ( args [ <NUM_LIT:0> ] ) : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( self . tagName , self . _argPrint ( args ) ) ) <EOL> if m . group ( <NUM_LIT:1> ) : <EOL> p [ '<STR_LIT:name>' ] = m . group ( <NUM_LIT:1> ) <EOL> if m . group ( <NUM_LIT:2> ) : <EOL> p [ '<STR_LIT>' ] = m . group ( <NUM_LIT:2> ) <EOL> elif len ( args ) == <NUM_LIT:2> : <EOL> if self . _isType ( args [ <NUM_LIT:0> ] ) or not self . _isType ( args [ <NUM_LIT:1> ] ) : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( self . tagName , self . _argPrint ( args ) ) ) <EOL> p [ '<STR_LIT:name>' ] = args [ <NUM_LIT:0> ] <EOL> p [ '<STR_LIT:type>' ] = args [ <NUM_LIT:1> ] . value <EOL> elif len ( args ) == <NUM_LIT:3> : <EOL> if self . _isType ( args [ <NUM_LIT:0> ] ) or not self . _isType ( args [ <NUM_LIT:1> ] ) or self . _isType ( args [ <NUM_LIT:2> ] ) : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( self . tagName , self . _argPrint ( args ) ) ) <EOL> p [ '<STR_LIT:name>' ] = args [ <NUM_LIT:0> ] <EOL> p [ '<STR_LIT:type>' ] = args [ <NUM_LIT:1> ] . value <EOL> p [ '<STR_LIT>' ] = args [ <NUM_LIT:2> ] <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( self . tagName , self . _argPrint ( args ) ) ) <EOL> return p <EOL> def _handleOptionalSyntax ( self , obj ) : <EOL> if ( '<STR_LIT:name>' in obj ) : <EOL> m = self . _optionalPat . match ( obj [ '<STR_LIT:name>' ] ) <EOL> if m : <EOL> obj [ '<STR_LIT:name>' ] = m . group ( <NUM_LIT:1> ) <EOL> obj [ '<STR_LIT>' ] = True <EOL> def attach ( self , obj , current , blockType ) : <EOL> self . _handleOptionalSyntax ( obj ) <EOL> if not '<STR_LIT>' in current : <EOL> current [ '<STR_LIT>' ] = [ ] <EOL> current [ '<STR_LIT>' ] . append ( obj ) <EOL> class SeeTagHandler ( TagHandler ) : <EOL> takesArg = True <EOL> mayRecur = True <EOL> def attach ( self , obj , current , blockType ) : <EOL> if not '<STR_LIT>' in current : <EOL> current [ '<STR_LIT>' ] = [ ] <EOL> current [ '<STR_LIT>' ] . append ( obj ) <EOL> class DescTagHandler ( TagHandler ) : <EOL> takesArg = True <EOL> mayRecur = True <EOL> def attach ( self , obj , current , blockType ) : <EOL> if '<STR_LIT>' in current : <EOL> current [ '<STR_LIT>' ] = current [ '<STR_LIT>' ] + "<STR_LIT>" + obj <EOL> else : <EOL> current [ '<STR_LIT>' ] = obj <EOL> class ReturnTagHandler ( TagHandler ) : <EOL> takesArg = True <EOL> _pat = re . compile ( '<STR_LIT>' , re . S ) ; <EOL> def parse ( self , args ) : <EOL> rv = { } <EOL> for a in args : <EOL> if self . _isType ( a ) : <EOL> if '<STR_LIT:type>' in rv : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> rv [ '<STR_LIT:type>' ] = a . value <EOL> else : <EOL> if '<STR_LIT>' in rv : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( self . tagName , self . _argPrint ( args ) ) ) <EOL> rv [ '<STR_LIT>' ] = a <EOL> return rv <EOL> def attach ( self , obj , current , blockType ) : <EOL> if '<STR_LIT>' in current : <EOL> for k in current [ '<STR_LIT>' ] : <EOL> if k in obj : <EOL> raise RuntimeError ( "<STR_LIT>" % k + <EOL> "<STR_LIT>" ) <EOL> else : <EOL> current [ '<STR_LIT>' ] = { } <EOL> for k in obj : <EOL> current [ '<STR_LIT>' ] [ k ] = obj [ k ] <EOL> class TypeTagHandler ( TagHandler ) : <EOL> takesArg = True <EOL> _isWordPat = re . compile ( '<STR_LIT>' , re . S ) ; <EOL> def parse ( self , args ) : <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( self . tagName , self . _argPrint ( args ) ) ) <EOL> if self . _isType ( args [ <NUM_LIT:0> ] ) : <EOL> args [ <NUM_LIT:0> ] = args [ <NUM_LIT:0> ] . value . strip ( ) <EOL> else : <EOL> m = self . _isWordPat . match ( args [ <NUM_LIT:0> ] ) <EOL> if not m : <EOL> raise RuntimeError ( "<STR_LIT>" % ( self . tagName , args [ <NUM_LIT:0> ] ) ) <EOL> return args [ <NUM_LIT:0> ] <EOL> def attach ( self , obj , current , blockType ) : <EOL> if ( blockType == '<STR_LIT>' ) : <EOL> current [ '<STR_LIT:type>' ] = obj <EOL> else : <EOL> if '<STR_LIT>' not in current : <EOL> current [ '<STR_LIT>' ] = { } <EOL> if '<STR_LIT:type>' in current [ '<STR_LIT>' ] : <EOL> raise RuntimeError ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> current [ '<STR_LIT>' ] [ '<STR_LIT:type>' ] = obj <EOL> class ThrowsTagHandler ( ReturnTagHandler ) : <EOL> mayRecur = True <EOL> def attach ( self , obj , current , blockType ) : <EOL> if '<STR_LIT>' not in current : <EOL> current [ '<STR_LIT>' ] = [ ] <EOL> current [ '<STR_LIT>' ] . append ( obj ) <EOL> class PayloadTagHandler ( ReturnTagHandler ) : <EOL> mayRecur = False <EOL> def attach ( self , obj , current , blockType ) : <EOL> if '<STR_LIT>' in current : <EOL> raise RuntimeError ( "<STR_LIT>" ) ; <EOL> current [ '<STR_LIT>' ] = obj <EOL> class BlockHandler ( TagHandler ) : <EOL> allowedTags = [ ] <EOL> allowedContexts = [ '<STR_LIT>' , '<STR_LIT:class>' ] <EOL> def merge ( self , doc , parent , guessedName , context ) : <EOL> for k in doc : <EOL> parent [ k ] = doc [ k ] <EOL> def setLineNumber ( self , lineStart , lineEnd , doc ) : <EOL> doc [ '<STR_LIT>' ] = [ lineStart , lineEnd ] <EOL> class ModuleBlockHandler ( BlockHandler ) : <EOL> allowedTags = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> allowedContexts = [ '<STR_LIT>' ] <EOL> takesArg = True <EOL> _pat = re . compile ( '<STR_LIT>' , re . S ) ; <EOL> def parse ( self , args ) : <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> self . tagName ) <EOL> text = args [ <NUM_LIT:0> ] <EOL> m = self . _pat . match ( text ) <EOL> if not m : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( self . tagName , ( text [ : <NUM_LIT:20> ] + "<STR_LIT>" ) ) ) <EOL> a = { } <EOL> if m . group ( <NUM_LIT:1> ) : <EOL> a [ "<STR_LIT:name>" ] = m . group ( <NUM_LIT:1> ) <EOL> else : <EOL> if m . group ( <NUM_LIT:2> ) : <EOL> a [ "<STR_LIT:name>" ] = m . group ( <NUM_LIT:2> ) <EOL> if m . group ( <NUM_LIT:3> ) : <EOL> a [ "<STR_LIT>" ] = m . group ( <NUM_LIT:3> ) <EOL> return a <EOL> def attach ( self , obj , current , blockType ) : <EOL> if "<STR_LIT:name>" in obj : <EOL> current [ '<STR_LIT>' ] = obj [ "<STR_LIT:name>" ] <EOL> if "<STR_LIT>" in obj : <EOL> if "<STR_LIT>" in current : <EOL> obj [ '<STR_LIT>' ] = current [ '<STR_LIT>' ] + "<STR_LIT>" + obj [ '<STR_LIT>' ] <EOL> current [ '<STR_LIT>' ] = obj [ '<STR_LIT>' ] <EOL> def merge ( self , doc , parent , guessedName , context ) : <EOL> for f in doc : <EOL> if f == '<STR_LIT>' : <EOL> parent [ '<STR_LIT>' ] = parent [ '<STR_LIT>' ] + "<STR_LIT>" + doc [ '<STR_LIT>' ] if '<STR_LIT>' in parent else doc [ '<STR_LIT>' ] <EOL> elif f == "<STR_LIT>" : <EOL> parent [ '<STR_LIT>' ] = doc [ '<STR_LIT>' ] <EOL> elif ( f in doc and f not in parent ) : <EOL> parent [ f ] = doc [ f ] <EOL> class FunctionBlockHandler ( ModuleBlockHandler ) : <EOL> allowedTags = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> allowedContexts = [ '<STR_LIT>' , '<STR_LIT:class>' ] <EOL> def attach ( self , obj , current , blockType ) : <EOL> if "<STR_LIT:name>" in obj : <EOL> current [ '<STR_LIT:name>' ] = obj [ "<STR_LIT:name>" ] <EOL> if "<STR_LIT>" in obj : <EOL> if "<STR_LIT>" in current : <EOL> obj [ '<STR_LIT>' ] = current [ '<STR_LIT>' ] + "<STR_LIT>" + obj [ '<STR_LIT>' ] <EOL> current [ '<STR_LIT>' ] = obj [ '<STR_LIT>' ] <EOL> def merge ( self , doc , parent , guessedName , context ) : <EOL> if "<STR_LIT:name>" not in doc : <EOL> doc [ '<STR_LIT:name>' ] = guessedName <EOL> if doc [ '<STR_LIT:name>' ] == None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if not "<STR_LIT>" in parent : <EOL> parent [ "<STR_LIT>" ] = [ ] <EOL> for f in parent [ "<STR_LIT>" ] : <EOL> if doc [ "<STR_LIT:name>" ] == f [ '<STR_LIT:name>' ] : <EOL> raise RuntimeError ( "<STR_LIT>" % doc [ "<STR_LIT:name>" ] ) <EOL> parent [ "<STR_LIT>" ] . append ( doc ) <EOL> class EventBlockHandler ( ModuleBlockHandler ) : <EOL> allowedTags = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> allowedContexts = [ '<STR_LIT>' , '<STR_LIT:class>' ] <EOL> def attach ( self , obj , current , blockType ) : <EOL> if "<STR_LIT:name>" in obj : <EOL> current [ '<STR_LIT:name>' ] = obj [ "<STR_LIT:name>" ] <EOL> if "<STR_LIT>" in obj : <EOL> if "<STR_LIT>" in current : <EOL> obj [ '<STR_LIT>' ] = current [ '<STR_LIT>' ] + "<STR_LIT>" + obj [ '<STR_LIT>' ] <EOL> current [ '<STR_LIT>' ] = obj [ '<STR_LIT>' ] <EOL> def merge ( self , doc , parent , guessedName , context ) : <EOL> if "<STR_LIT:name>" not in doc : <EOL> doc [ '<STR_LIT:name>' ] = guessedName <EOL> if doc [ '<STR_LIT:name>' ] == None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if not "<STR_LIT>" in parent : <EOL> parent [ "<STR_LIT>" ] = [ ] <EOL> for e in parent [ "<STR_LIT>" ] : <EOL> if doc [ "<STR_LIT:name>" ] == e [ '<STR_LIT:name>' ] : <EOL> raise RuntimeError ( "<STR_LIT>" % doc [ "<STR_LIT:name>" ] ) <EOL> parent [ "<STR_LIT>" ] . append ( doc ) <EOL> class ConstructorBlockHandler ( BlockHandler ) : <EOL> allowedTags = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> takesArg = True <EOL> argOptional = True <EOL> allowedContexts = [ '<STR_LIT:class>' ] <EOL> def attach ( self , obj , current , blockType ) : <EOL> if obj : <EOL> if "<STR_LIT>" in current : <EOL> obj = current [ '<STR_LIT>' ] + "<STR_LIT>" + obj <EOL> current [ '<STR_LIT>' ] = obj <EOL> def merge ( self , doc , parent , guessedName , context ) : <EOL> if not "<STR_LIT>" in parent : <EOL> parent [ "<STR_LIT>" ] = [ ] <EOL> parent [ "<STR_LIT>" ] . append ( doc ) <EOL> class ClassBlockHandler ( FunctionBlockHandler ) : <EOL> allowedTags = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def merge ( self , doc , parent , guessedName , context ) : <EOL> if "<STR_LIT:name>" not in doc : <EOL> doc [ '<STR_LIT:name>' ] = guessedName <EOL> return doc <EOL> class EndClassBlockHandler ( BlockHandler ) : <EOL> allowedContexts = [ '<STR_LIT:class>' ] <EOL> def attach ( self , obj , current , blockType ) : <EOL> pass <EOL> def merge ( self , doc , parent , guessedName , context ) : <EOL> if not "<STR_LIT>" in parent : <EOL> parent [ "<STR_LIT>" ] = [ ] <EOL> for c in parent [ "<STR_LIT>" ] : <EOL> if doc [ "<STR_LIT:name>" ] == c [ '<STR_LIT:name>' ] : <EOL> raise RuntimeError ( "<STR_LIT>" % doc [ "<STR_LIT:name>" ] ) <EOL> parent [ "<STR_LIT>" ] . append ( doc ) <EOL> class TypedefBlockHandler ( FunctionBlockHandler ) : <EOL> allowedTags = [ ] <EOL> allowedContexts = [ '<STR_LIT>' ] <EOL> takesArg = True <EOL> _pat = re . compile ( '<STR_LIT>' , re . S ) ; <EOL> def parse ( self , args ) : <EOL> if len ( args ) != <NUM_LIT:1> or self . _isType ( args [ <NUM_LIT:0> ] ) : <EOL> raise RuntimeError ( "<STR_LIT>" % self . tagName ) <EOL> return args [ <NUM_LIT:0> ] <EOL> def attach ( self , obj , current , blockType ) : <EOL> current [ '<STR_LIT:name>' ] = obj <EOL> def merge ( self , doc , parent , guessedName , context ) : <EOL> parent [ '<STR_LIT>' ] = doc <EOL> class EndTypedefBlockHandler ( BlockHandler ) : <EOL> allowedContexts = [ '<STR_LIT:type>' ] <EOL> def attach ( self , obj , current , blockType ) : <EOL> pass <EOL> def merge ( self , doc , parent , guessedName , context ) : <EOL> pass <EOL> class PropertyBlockHandler ( ParamTagHandler , BlockHandler ) : <EOL> allowedTags = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> allowedContexts = [ '<STR_LIT:type>' , '<STR_LIT:class>' , '<STR_LIT>' ] <EOL> def attach ( self , obj , current , blockType ) : <EOL> for x in obj : <EOL> current [ x ] = obj [ x ] <EOL> def merge ( self , doc , parent , guessedName , context ) : <EOL> if "<STR_LIT:name>" not in doc : <EOL> doc [ '<STR_LIT:name>' ] = guessedName <EOL> if doc [ "<STR_LIT:name>" ] == None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if not "<STR_LIT>" in parent : <EOL> parent [ "<STR_LIT>" ] = [ ] <EOL> for p in parent [ "<STR_LIT>" ] : <EOL> if doc [ "<STR_LIT:name>" ] == p [ '<STR_LIT:name>' ] : <EOL> raise RuntimeError ( "<STR_LIT>" % doc [ "<STR_LIT:name>" ] ) <EOL> if context == "<STR_LIT:type>" : <EOL> self . _handleOptionalSyntax ( doc ) <EOL> parent [ "<STR_LIT>" ] . append ( doc ) <EOL> def firstBlockIsModuleTypeGuesser ( firstBlock , codeChunk , context , tags , possibilities ) : <EOL> if '<STR_LIT>' in possibilities and firstBlock : <EOL> return '<STR_LIT>' <EOL> return None <EOL> _functionKeywordPat = re . compile ( '<STR_LIT>' ) ; <EOL> def isFunctionIfKeywordInCodeTypeGuesser ( firstBlock , codeChunk , context , tags , possibilities ) : <EOL> if '<STR_LIT>' in possibilities and _functionKeywordPat . search ( codeChunk ) : <EOL> return '<STR_LIT>' <EOL> return None <EOL> def typeWithoutReturnsIsProbablyPropertyTypeGuesser ( firstBlock , codeChunk , context , tags , possibilities ) : <EOL> if '<STR_LIT>' in tags and '<STR_LIT>' not in tags and '<STR_LIT>' in possibilities : <EOL> return '<STR_LIT>' <EOL> return None <EOL> _assignmentPat = re . compile ( '<STR_LIT>' , re . M ) ; <EOL> def assignmentIsProbablyPropertyTypeGuesser ( firstBlock , codeChunk , context , tags , possibilities ) : <EOL> if '<STR_LIT>' in possibilities and _assignmentPat . match ( codeChunk ) : <EOL> return '<STR_LIT>' <EOL> return None <EOL> _hasGetSetterPat = re . compile ( '<STR_LIT>' ) ; <EOL> def hasGetSetterIsPropertyTypeGuesser ( firstBlock , codeChunk , context , tags , possibilities ) : <EOL> if '<STR_LIT>' in possibilities and _hasGetSetterPat . search ( codeChunk ) : <EOL> return '<STR_LIT>' <EOL> return None <EOL> _findExportsPat = re . compile ( '<STR_LIT>' , re . M ) ; <EOL> def commonJSNameGuesser ( codeChunk , blockType ) : <EOL> m = _findExportsPat . search ( codeChunk ) <EOL> if m : <EOL> return m . group ( <NUM_LIT:1> ) <EOL> return None <EOL> _findPropPat = re . compile ( '<STR_LIT>' , re . M ) ; <EOL> def assignToPropertyNameGuesser ( codeChunk , blockType ) : <EOL> m = _findPropPat . search ( codeChunk ) <EOL> if m : <EOL> return m . group ( <NUM_LIT:1> ) <EOL> return None <EOL> _standardFunctionPat = re . compile ( '<STR_LIT>' ) ; <EOL> def standardFunctionNameGuesser ( codeChunk , blockType ) : <EOL> m = _standardFunctionPat . match ( codeChunk ) <EOL> if m : <EOL> return m . group ( <NUM_LIT:1> ) <EOL> return None <EOL> _objectPropertyPat = re . compile ( '<STR_LIT>' ) ; <EOL> def objectPropertyNameGuesser ( codeChunk , blockType ) : <EOL> m = _objectPropertyPat . match ( codeChunk ) <EOL> if m : <EOL> return m . group ( <NUM_LIT:1> ) <EOL> return None <EOL> _getSetterNameGuesserPat = re . compile ( r'''<STR_LIT>''' , re . X ) ; <EOL> def getSetterNameGuesser ( codeChunk , blockType ) : <EOL> m = _getSetterNameGuesserPat . search ( codeChunk ) <EOL> if m : <EOL> return m . group ( <NUM_LIT:1> ) if m . group ( <NUM_LIT:1> ) else m . group ( <NUM_LIT:2> ) <EOL> return None <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> import json <EOL> ds = DocStract ( ) <EOL> docs = None <EOL> if len ( sys . argv ) == <NUM_LIT:2> : <EOL> docs = ds . extractFromFile ( sys . argv [ <NUM_LIT:1> ] ) <EOL> elif len ( sys . argv ) == <NUM_LIT:1> : <EOL> docs = ds . extract ( sys . stdin . read ( ) ) <EOL> else : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> print json . dumps ( docs , indent = <NUM_LIT:2> , sort_keys = True ) + "<STR_LIT:\n>" </s>
