@@ -5,22 +5,25 @@ module SyntaxTree
55 # from Visitor. The module overrides a few visit methods to automatically keep
66 # track of local variables and arguments defined in the current environment.
77 # Example usage:
8- # class MyVisitor < Visitor
9- # include WithEnvironment
108 #
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)
9+ # class MyVisitor < Visitor
10+ # include WithEnvironment
1511 #
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
12+ # def visit_ident(node)
13+ # # Check if we're visiting an identifier for an argument, a local
14+ # # variable or something else
15+ # local = current_environment.find_local(node)
16+ #
17+ # if local.type == :argument
18+ # # handle identifiers for arguments
19+ # elsif local.type == :variable
20+ # # handle identifiers for variables
21+ # else
22+ # # handle other identifiers, such as method names
23+ # end
2224 # end
23- # end
25+ # end
26+ #
2427 module WithEnvironment
2528 # The environment class is used to keep track of local variables and
2629 # arguments inside a particular scope
@@ -37,19 +40,16 @@ class Local
3740 # [Array[Location]] The locations of all usages of this local
3841 attr_reader :usages
3942
40- # initialize: (Symbol type) -> void
4143 def initialize ( type )
4244 @type = type
4345 @definitions = [ ]
4446 @usages = [ ]
4547 end
4648
47- # add_definition: (Location location) -> void
4849 def add_definition ( location )
4950 @definitions << location
5051 end
5152
52- # add_usage: (Location location) -> void
5353 def add_usage ( location )
5454 @usages << location
5555 end
@@ -62,17 +62,15 @@ def add_usage(location)
6262 # [Environment | nil] The parent environment
6363 attr_reader :parent
6464
65- # initialize: (Environment | nil parent) -> void
6665 def initialize ( parent = nil )
6766 @locals = { }
6867 @parent = parent
6968 end
7069
7170 # Adding a local definition will either insert a new entry in the locals
72- # hash or append a new definition location to an existing local. Notice that
73- # it's not possible to change the type of a local after it has been
74- # registered
75- # add_local_definition: (Ident | Label identifier, Symbol type) -> void
71+ # hash or append a new definition location to an existing local. Notice
72+ # that it's not possible to change the type of a local after it has been
73+ # registered.
7674 def add_local_definition ( identifier , type )
7775 name = identifier . value . delete_suffix ( ":" )
7876
@@ -83,8 +81,7 @@ def add_local_definition(identifier, type)
8381 # Adding a local usage will either insert a new entry in the locals
8482 # hash or append a new usage location to an existing local. Notice that
8583 # it's not possible to change the type of a local after it has been
86- # registered
87- # add_local_usage: (Ident | Label identifier, Symbol type) -> void
84+ # registered.
8885 def add_local_usage ( identifier , type )
8986 name = identifier . value . delete_suffix ( ":" )
9087
@@ -93,8 +90,7 @@ def add_local_usage(identifier, type)
9390 end
9491
9592 # Try to find the local given its name in this environment or any of its
96- # parents
97- # find_local: (String name) -> Local | nil
93+ # parents.
9894 def find_local ( name )
9995 local = @locals [ name ]
10096 return local unless local . nil?
@@ -116,7 +112,7 @@ def with_new_environment
116112 end
117113
118114 # Visits for nodes that create new environments, such as classes, modules
119- # and method definitions
115+ # and method definitions.
120116 def visit_class ( node )
121117 with_new_environment { super }
122118 end
@@ -127,7 +123,7 @@ def visit_module(node)
127123
128124 # When we find a method invocation with a block, only the code that happens
129125 # inside of the block needs a fresh environment. The method invocation
130- # itself happens in the same environment
126+ # itself happens in the same environment.
131127 def visit_method_add_block ( node )
132128 visit ( node . call )
133129 with_new_environment { visit ( node . block ) }
@@ -138,7 +134,7 @@ def visit_def(node)
138134 end
139135
140136 # Visit for keeping track of local arguments, such as method and block
141- # arguments
137+ # arguments.
142138 def visit_params ( node )
143139 add_argument_definitions ( node . requireds )
144140
0 commit comments