<s> from vint . ast . plugin . scope_plugin . scope_detector import ( <EOL> detect_scope_visibility , <EOL> normalize_variable_name , <EOL> is_builtin_variable , <EOL> ) <EOL> from vint . ast . plugin . scope_plugin . scope_linker import ScopeLinker <EOL> from vint . ast . plugin . scope_plugin . identifier_classifier import ( <EOL> IdentifierClassifier , <EOL> is_function_identifier , <EOL> ) <EOL> REACHABILITY_FLAG = '<STR_LIT>' <EOL> REFERECED_FLAG = '<STR_LIT>' <EOL> class ReferenceReachabilityTester ( object ) : <EOL> """<STR_LIT>""" <EOL> class TwoWayScopeReferenceAttacher ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def attach ( cls , root_scope_tree ) : <EOL> root_scope_tree [ '<STR_LIT>' ] = None <EOL> return cls . _attach_recursively ( root_scope_tree ) <EOL> @ classmethod <EOL> def _attach_recursively ( cls , scope_tree ) : <EOL> for child_scope in scope_tree [ '<STR_LIT>' ] : <EOL> child_scope [ '<STR_LIT>' ] = scope_tree <EOL> cls . _attach_recursively ( child_scope ) <EOL> return scope_tree <EOL> def process ( self , ast ) : <EOL> scope_linker = ScopeLinker ( ) <EOL> scope_linker . process ( ast ) <EOL> id_collector = IdentifierClassifier . IdentifierCollector ( ) <EOL> classified_id_group = id_collector . collect_identifiers ( ast ) <EOL> dec_id_nodes = classified_id_group [ '<STR_LIT>' ] <EOL> ref_id_nodes = classified_id_group [ '<STR_LIT>' ] <EOL> self . _scope_tree = scope_linker . scope_tree <EOL> self . _link_registry = scope_linker . link_registry <EOL> ReferenceReachabilityTester . TwoWayScopeReferenceAttacher . attach ( self . _scope_tree ) <EOL> for dec_id_node in dec_id_nodes : <EOL> dec_id_node [ REFERECED_FLAG ] = False <EOL> for ref_id_node in ref_id_nodes : <EOL> is_reachable = self . check_reachability ( ref_id_node ) <EOL> ref_id_node [ REACHABILITY_FLAG ] = is_reachable <EOL> def get_objective_scope_visibility ( self , node ) : <EOL> """<STR_LIT>""" <EOL> context_scope = self . _link_registry . get_scope_by_referencing_identifier ( node ) <EOL> return detect_scope_visibility ( node , context_scope ) [ '<STR_LIT>' ] <EOL> def _reset_referenced_flag ( self , scope_tree ) : <EOL> for child_scope in scope_tree [ '<STR_LIT>' ] : <EOL> for functions in child_scope [ '<STR_LIT>' ] . values ( ) : <EOL> for function in functions : <EOL> function [ REFERECED_FLAG ] = False <EOL> for variables in child_scope [ '<STR_LIT>' ] . values ( ) : <EOL> for variable in variables : <EOL> variable [ REFERECED_FLAG ] = False <EOL> self . _reset_referenced_flag ( child_scope ) <EOL> def check_reachability ( self , ref_id_node ) : <EOL> scope = self . _link_registry . get_context_scope_by_identifier ( ref_id_node ) <EOL> var_name = normalize_variable_name ( ref_id_node , scope ) <EOL> is_func_id = is_function_identifier ( ref_id_node ) <EOL> while scope is not None : <EOL> if is_func_id : <EOL> functions_list = scope [ '<STR_LIT>' ] <EOL> if var_name in functions_list : <EOL> for variable in functions_list [ var_name ] : <EOL> declaring_id_node = self . _link_registry . get_declarative_identifier_by_variable ( variable ) <EOL> declaring_id_node [ REFERECED_FLAG ] = True <EOL> return True <EOL> else : <EOL> pass <EOL> variables_list = scope [ '<STR_LIT>' ] <EOL> if var_name in variables_list : <EOL> for variable in variables_list [ var_name ] : <EOL> declaring_id_node = self . _link_registry . get_declarative_identifier_by_variable ( variable ) <EOL> declaring_id_node [ REFERECED_FLAG ] = True <EOL> return True <EOL> scope = scope [ '<STR_LIT>' ] <EOL> return is_builtin_variable ( ref_id_node ) <EOL> def is_reference_identifier ( node ) : <EOL> return REACHABILITY_FLAG in node <EOL> def is_reachable_reference_identifier ( node ) : <EOL> return node . get ( REACHABILITY_FLAG , False ) <EOL> def is_declarative_identifier ( node ) : <EOL> return REFERECED_FLAG in node <EOL> def is_referenced_declarative_identifier ( node ) : <EOL> return node . get ( REFERECED_FLAG , False ) </s>
