<s> '''<STR_LIT>''' <EOL> '''<STR_LIT>''' <EOL> import os <EOL> import sys <EOL> import re <EOL> import json <EOL> import subprocess <EOL> import tempfile <EOL> import sublime <EOL> import re <EOL> import string <EOL> _constants = { } <EOL> startupinfo = None <EOL> if os . name == '<STR_LIT>' : <EOL> startupinfo = subprocess . STARTUPINFO ( ) <EOL> startupinfo . dwFlags |= subprocess . STARTF_USESHOWWINDOW <EOL> def get_all_token_names ( ) : <EOL> '''<STR_LIT>''' <EOL> global _constants <EOL> _constants [ <NUM_LIT:0> ] = None <EOL> php = '<STR_LIT>' <EOL> for i in range ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> php += "<STR_LIT>" . format ( code = i ) <EOL> result = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , php ] , bufsize = <NUM_LIT:1> , stdout = subprocess . PIPE , shell = False , startupinfo = startupinfo ) . communicate ( ) [ <NUM_LIT:0> ] <EOL> for constant in result . split ( '<STR_LIT:|>' ) : <EOL> if constant . find ( '<STR_LIT:U+002C>' ) >= <NUM_LIT:0> : <EOL> code , name = constant . split ( '<STR_LIT:U+002C>' ) <EOL> if name != '<STR_LIT>' : <EOL> _constants [ str ( code ) ] = name <EOL> def get_token_name ( code ) : <EOL> '''<STR_LIT>''' <EOL> name = None <EOL> code = str ( code ) <EOL> if code in _constants : <EOL> name = _constants [ code ] <EOL> return name <EOL> def token ( token ) : <EOL> '''<STR_LIT>''' <EOL> if type ( token ) . __name__ == '<STR_LIT:list>' : <EOL> kind = get_token_name ( token [ <NUM_LIT:0> ] ) <EOL> stmt = token [ <NUM_LIT:1> ] <EOL> line = token [ <NUM_LIT:2> ] <EOL> else : <EOL> kind = get_token_name ( <NUM_LIT:0> ) <EOL> stmt = token <EOL> line = <NUM_LIT:0> <EOL> return kind , stmt , line <EOL> def get_all_tokens ( source = None , filename = None ) : <EOL> '''<STR_LIT>''' <EOL> tmp = None <EOL> if source : <EOL> '''<STR_LIT>''' <EOL> tmp , filename = tempfile . mkstemp ( ) <EOL> with os . fdopen ( tmp , "<STR_LIT:wb>" ) as f : <EOL> f . write ( source . encode ( '<STR_LIT:utf-8>' ) ) <EOL> php = "<STR_LIT>" . format ( source = filename ) <EOL> tokens = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , php ] , bufsize = <NUM_LIT:1> , stdout = subprocess . PIPE , shell = False , startupinfo = startupinfo ) . communicate ( ) [ <NUM_LIT:0> ] <EOL> tokens = json . loads ( tokens ) <EOL> for i in range ( <NUM_LIT:0> , len ( tokens ) ) : <EOL> tokens [ i ] = token ( tokens [ i ] ) <EOL> if source and tmp : <EOL> os . unlink ( filename ) <EOL> return tokens <EOL> def apply_patterns ( source , kind ) : <EOL> s = sublime . load_settings ( "<STR_LIT>" ) <EOL> patterns = s . get ( kind ) <EOL> for p in patterns : <EOL> if p : <EOL> try : <EOL> m = re . search ( p . get ( '<STR_LIT>' ) , source , re . UNICODE ) <EOL> if m : <EOL> c = p . get ( '<STR_LIT:class>' ) <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> if m . lastindex >= i and c . find ( '<STR_LIT:%>' + str ( i ) ) >= <NUM_LIT:0> : <EOL> value = m . group ( i ) <EOL> if p . get ( '<STR_LIT>' ) : <EOL> value = value [ <NUM_LIT:0> : <NUM_LIT:1> ] . capitalize ( ) + value [ <NUM_LIT:1> : ] <EOL> c = c . replace ( '<STR_LIT:%>' + str ( i ) , value ) <EOL> source = source . replace ( m . group ( <NUM_LIT:0> ) , c ) <EOL> except Exception as e : <EOL> print e <EOL> print '<STR_LIT>' + p . get ( '<STR_LIT>' ) <EOL> return source <EOL> def get_context ( source , point ) : <EOL> '''<STR_LIT>''' <EOL> fullsource = source <EOL> source = source [ : point ] <EOL> source = apply_patterns ( source , '<STR_LIT>' ) <EOL> source = apply_patterns ( source , '<STR_LIT>' ) <EOL> visibility = None <EOL> context = [ ] <EOL> operator = None <EOL> tokens = get_all_tokens ( source ) <EOL> tokens . reverse ( ) <EOL> nest = <NUM_LIT:0> <EOL> end = <NUM_LIT:0> <EOL> for t in tokens : <EOL> kind , stmt , line = t <EOL> if kind == '<STR_LIT>' and visibility == None : <EOL> visibility = '<STR_LIT>' <EOL> if kind == '<STR_LIT>' and operator == None : <EOL> operator = '<STR_LIT>' <EOL> if kind == '<STR_LIT>' and operator == None : <EOL> operator = '<STR_LIT>' <EOL> if len ( context ) == <NUM_LIT:0> and ( kind == '<STR_LIT>' or kind == '<STR_LIT>' ) : <EOL> context . append ( '<STR_LIT>' ) <EOL> if kind == None and stmt == '<STR_LIT:(>' : <EOL> nest += <NUM_LIT:1> <EOL> if kind == None and stmt == '<STR_LIT:)>' : <EOL> nest -= <NUM_LIT:1> <EOL> if kind == None and stmt == '<STR_LIT:=>' : <EOL> break <EOL> if kind == '<STR_LIT>' : <EOL> break <EOL> if kind == '<STR_LIT>' : <EOL> break <EOL> if kind == None and stmt == '<STR_LIT:;>' : <EOL> break <EOL> if kind == None and stmt == '<STR_LIT:.>' : <EOL> break <EOL> if kind == None and stmt == '<STR_LIT:U+002C>' : <EOL> break <EOL> if kind == None and ( stmt == '<STR_LIT:{>' or stmt == '<STR_LIT:}>' ) : <EOL> break <EOL> if kind == '<STR_LIT>' : <EOL> break <EOL> if kind == '<STR_LIT>' : <EOL> break <EOL> if nest == <NUM_LIT:0> and kind == '<STR_LIT>' : <EOL> context . append ( stmt ) <EOL> if stmt == '<STR_LIT>' and visibility == None : <EOL> visibility = '<STR_LIT:all>' <EOL> if visibility == None : <EOL> visibility = '<STR_LIT>' <EOL> if nest == <NUM_LIT:0> and kind == '<STR_LIT>' : <EOL> context . append ( stmt ) <EOL> if visibility == None : <EOL> visibility = '<STR_LIT>' <EOL> if nest > <NUM_LIT:0> : <EOL> break <EOL> end += <NUM_LIT:1> <EOL> context . reverse ( ) <EOL> if context and context [ <NUM_LIT:0> ] . startswith ( '<STR_LIT:$>' ) : <EOL> class_name = None <EOL> if context [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) : <EOL> tokens = convert_raw_tokens ( get_all_tokens ( fullsource ) ) <EOL> class_name = tokens [ <NUM_LIT:0> ] [ '<STR_LIT:class>' ] if tokens else None <EOL> if class_name == None : <EOL> searchtext = context [ <NUM_LIT:0> ] . replace ( '<STR_LIT:$>' , '<STR_LIT>' ) <EOL> searchtext = searchtext . replace ( '<STR_LIT:(>' , '<STR_LIT>' ) <EOL> searchtext = searchtext . replace ( '<STR_LIT:)>' , '<STR_LIT>' ) <EOL> searchtext += '<STR_LIT>' <EOL> t = re . findall ( '<STR_LIT>' + searchtext , source ) <EOL> if t : <EOL> class_name = t [ <NUM_LIT:0> ] <EOL> if class_name : <EOL> context [ <NUM_LIT:0> ] = class_name <EOL> return context , visibility , operator <EOL> def convert_raw_tokens ( raw_tokens ) : <EOL> '''<STR_LIT>''' <EOL> nest = <NUM_LIT:0> <EOL> in_class = False <EOL> class_name = None <EOL> kind = None <EOL> visibility = None <EOL> static = False <EOL> name = None <EOL> extends = None <EOL> implements = None <EOL> args = [ ] <EOL> in_args = False <EOL> returns = None <EOL> doc = None <EOL> declarations = [ ] <EOL> def save ( ) : <EOL> if class_name or name : <EOL> fields = { <EOL> '<STR_LIT:class>' : unicode ( class_name ) if class_name else '<STR_LIT>' , <EOL> '<STR_LIT>' : unicode ( extends ) if extends else '<STR_LIT>' , <EOL> '<STR_LIT>' : unicode ( implements ) if implements else '<STR_LIT>' , <EOL> '<STR_LIT>' : unicode ( visibility ) if visibility else '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:1>' if static else '<STR_LIT:0>' , <EOL> '<STR_LIT>' : unicode ( kind ) if kind else '<STR_LIT>' , <EOL> '<STR_LIT:name>' : unicode ( name ) if name else '<STR_LIT>' , <EOL> '<STR_LIT:args>' : args , <EOL> '<STR_LIT>' : unicode ( returns ) if returns else '<STR_LIT>' , <EOL> '<STR_LIT>' : unicode ( doc ) if doc else '<STR_LIT>' , <EOL> } <EOL> declarations . append ( fields ) <EOL> def search_ahead ( n , search ) : <EOL> for i in range ( n , len ( raw_tokens ) ) : <EOL> t , stmt , line = raw_tokens [ i ] <EOL> if t == search : <EOL> return stmt <EOL> n = <NUM_LIT:0> <EOL> for t , stmt , line in raw_tokens : <EOL> if t == '<STR_LIT>' : <EOL> pass <EOL> elif stmt == '<STR_LIT:{>' : <EOL> nest += <NUM_LIT:1> <EOL> elif stmt == '<STR_LIT:}>' : <EOL> nest -= <NUM_LIT:1> <EOL> if nest == <NUM_LIT:0> : <EOL> save ( ) <EOL> visibility = None <EOL> kind = None <EOL> in_class = False <EOL> class_name = None <EOL> extends = None <EOL> implements = None <EOL> name = None <EOL> args = [ ] <EOL> in_args = False <EOL> static = False <EOL> doc = None <EOL> returns = None <EOL> if in_class and nest == <NUM_LIT:1> : <EOL> save ( ) <EOL> visibility = None <EOL> kind = None <EOL> name = None <EOL> args = [ ] <EOL> in_args = False <EOL> static = False <EOL> doc = None <EOL> returns = None <EOL> elif t == None and stmt == '<STR_LIT:;>' and in_class and nest == <NUM_LIT:1> : <EOL> save ( ) <EOL> visibility = None <EOL> kind = None <EOL> name = None <EOL> static = False <EOL> returns = None <EOL> doc = None <EOL> elif t == '<STR_LIT>' : <EOL> visibility = '<STR_LIT>' <EOL> elif t == '<STR_LIT>' : <EOL> visibility = '<STR_LIT>' <EOL> elif t == '<STR_LIT>' : <EOL> visibility = '<STR_LIT>' <EOL> elif t == '<STR_LIT>' : <EOL> static = True <EOL> elif t == '<STR_LIT>' : <EOL> doc = stmt <EOL> elif t == '<STR_LIT>' and kind == None and in_class and nest == <NUM_LIT:1> and name == None : <EOL> kind = '<STR_LIT>' <EOL> name = stmt <EOL> args = [ ] <EOL> if doc : <EOL> data = re . findall ( '<STR_LIT>' , doc ) <EOL> if data : <EOL> returns = data [ <NUM_LIT:0> ] <EOL> elif t == '<STR_LIT>' : <EOL> kind = '<STR_LIT>' <EOL> name = search_ahead ( n , '<STR_LIT>' ) <EOL> args = [ ] <EOL> static = True <EOL> if doc : <EOL> data = re . findall ( '<STR_LIT>' , doc ) <EOL> if data : <EOL> returns = data [ <NUM_LIT:0> ] <EOL> elif t == '<STR_LIT>' : <EOL> kind = '<STR_LIT>' <EOL> name = search_ahead ( n , '<STR_LIT>' ) <EOL> if name == '<STR_LIT>' and class_name : <EOL> returns = class_name <EOL> if doc : <EOL> data = re . findall ( '<STR_LIT>' , doc ) <EOL> if data : <EOL> returns = data [ <NUM_LIT:0> ] <EOL> elif kind == '<STR_LIT>' and stmt == '<STR_LIT:(>' and not args and ( nest == <NUM_LIT:0> or in_class and nest == <NUM_LIT:1> ) : <EOL> in_args = True <EOL> elif kind == '<STR_LIT>' and stmt == '<STR_LIT:)>' and in_args : <EOL> in_args = False <EOL> elif kind == '<STR_LIT>' and t == '<STR_LIT>' and in_args : <EOL> vtype = '<STR_LIT>' <EOL> if doc : <EOL> v = stmt . replace ( '<STR_LIT:$>' , '<STR_LIT>' ) <EOL> data = re . findall ( '<STR_LIT>' + v + '<STR_LIT>' , doc ) <EOL> if data : <EOL> vtype = data [ <NUM_LIT:0> ] <EOL> args . append ( [ stmt , vtype ] ) <EOL> elif t == '<STR_LIT>' : <EOL> in_class = True <EOL> class_name = search_ahead ( n , '<STR_LIT>' ) <EOL> elif t == '<STR_LIT>' : <EOL> class_name = search_ahead ( n , '<STR_LIT>' ) <EOL> elif t == '<STR_LIT>' : <EOL> extends = search_ahead ( n , '<STR_LIT>' ) <EOL> elif t == '<STR_LIT>' : <EOL> implements = search_ahead ( n , '<STR_LIT>' ) <EOL> n += <NUM_LIT:1> <EOL> return declarations <EOL> def scan_file ( filename , extension = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> declarations = [ ] <EOL> name , ext = os . path . splitext ( filename ) <EOL> if ext == extension : <EOL> raw_tokens = get_all_tokens ( filename = filename ) <EOL> declarations = convert_raw_tokens ( raw_tokens ) <EOL> for i in range ( <NUM_LIT:0> , len ( declarations ) ) : <EOL> declarations [ i ] [ '<STR_LIT:path>' ] = filename <EOL> return declarations <EOL> def scan_all_files ( base_folder , extension = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> declarations = [ ] <EOL> for root , dirs , files in os . walk ( base_folder ) : <EOL> for name in files : <EOL> filename , ext = os . path . splitext ( name ) <EOL> if ext == extension : <EOL> path = os . path . join ( root , name ) <EOL> sys . stderr . write ( path + '<STR_LIT:\n>' ) <EOL> declarations . extend ( scan_file ( path ) ) <EOL> return declarations <EOL> '''<STR_LIT>''' <EOL> get_all_token_names ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> '''<STR_LIT>''' <EOL> filename = sys . argv [ <NUM_LIT:1> ] <EOL> if os . path . isdir ( filename ) : <EOL> declarations = scan_all_files ( filename ) <EOL> for d in declarations : <EOL> sys . stdout . write ( json . dumps ( d ) ) <EOL> else : <EOL> declarations = scan_file ( filename ) <EOL> print json . dumps ( declarations ) </s>
