|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module SyntaxTree |
| 4 | + # WithEnvironment is a module intended to be included in classes inheriting |
| 5 | + # from Visitor. The module overrides a few visit methods to automatically keep |
| 6 | + # track of local variables and arguments defined in the current environment. |
| 7 | + # Example usage: |
| 8 | + # class MyVisitor < Visitor |
| 9 | + # include WithEnvironment |
| 10 | + # |
| 11 | + # def visit_ident(node) |
| 12 | + # # Check if we're visiting an identifier for an argument, a local |
| 13 | + # variable or something else |
| 14 | + # local = current_environment.find_local(node) |
| 15 | + # |
| 16 | + # if local.type == :argument |
| 17 | + # # handle identifiers for arguments |
| 18 | + # elsif local.type == :variable |
| 19 | + # # handle identifiers for variables |
| 20 | + # else |
| 21 | + # # handle other identifiers, such as method names |
| 22 | + # end |
| 23 | + # end |
| 24 | + module WithEnvironment |
| 25 | + def current_environment |
| 26 | + @current_environment ||= Environment.new |
| 27 | + end |
| 28 | + |
| 29 | + def with_new_environment |
| 30 | + previous_environment = @current_environment |
| 31 | + @current_environment = Environment.new(previous_environment) |
| 32 | + yield |
| 33 | + @current_environment = previous_environment |
| 34 | + end |
| 35 | + |
| 36 | + # Visits for nodes that create new environments, such as classes, modules |
| 37 | + # and method definitions |
| 38 | + def visit_class(node) |
| 39 | + with_new_environment { super } |
| 40 | + end |
| 41 | + |
| 42 | + def visit_module(node) |
| 43 | + with_new_environment { super } |
| 44 | + end |
| 45 | + |
| 46 | + def visit_method_add_block(node) |
| 47 | + with_new_environment { super } |
| 48 | + end |
| 49 | + |
| 50 | + def visit_def(node) |
| 51 | + with_new_environment { super } |
| 52 | + end |
| 53 | + |
| 54 | + def visit_defs(node) |
| 55 | + with_new_environment { super } |
| 56 | + end |
| 57 | + |
| 58 | + def visit_def_endless(node) |
| 59 | + with_new_environment { super } |
| 60 | + end |
| 61 | + |
| 62 | + # Visit for keeping track of local arguments, such as method and block |
| 63 | + # arguments |
| 64 | + def visit_params(node) |
| 65 | + node.requireds.each do |param| |
| 66 | + @current_environment.register_local(param, :argument) |
| 67 | + end |
| 68 | + |
| 69 | + node.posts.each do |param| |
| 70 | + @current_environment.register_local(param, :argument) |
| 71 | + end |
| 72 | + |
| 73 | + node.keywords.each do |param| |
| 74 | + @current_environment.register_local(param.first, :argument) |
| 75 | + end |
| 76 | + |
| 77 | + node.optionals.each do |param| |
| 78 | + @current_environment.register_local(param.first, :argument) |
| 79 | + end |
| 80 | + |
| 81 | + super |
| 82 | + end |
| 83 | + |
| 84 | + def visit_rest_param(node) |
| 85 | + name = node.name |
| 86 | + @current_environment.register_local(name, :argument) if name |
| 87 | + |
| 88 | + super |
| 89 | + end |
| 90 | + |
| 91 | + def visit_kwrest_param(node) |
| 92 | + name = node.name |
| 93 | + @current_environment.register_local(name, :argument) if name |
| 94 | + |
| 95 | + super |
| 96 | + end |
| 97 | + |
| 98 | + def visit_blockarg(node) |
| 99 | + name = node.name |
| 100 | + @current_environment.register_local(name, :argument) if name |
| 101 | + |
| 102 | + super |
| 103 | + end |
| 104 | + |
| 105 | + # Visits for keeping track of local variables |
| 106 | + def visit_aref_field(node) |
| 107 | + name = node.collection.value |
| 108 | + @current_environment.register_local(name, :variable) if name |
| 109 | + |
| 110 | + super |
| 111 | + end |
| 112 | + |
| 113 | + def visit_var_field(node) |
| 114 | + value = node.value |
| 115 | + |
| 116 | + if value.is_a?(SyntaxTree::Ident) |
| 117 | + @current_environment.register_local(value, :variable) |
| 118 | + end |
| 119 | + |
| 120 | + super |
| 121 | + end |
| 122 | + |
| 123 | + alias visit_var_ref visit_var_field |
| 124 | + end |
| 125 | +end |
0 commit comments