+
+ trace_variable(variable_name, breaker)
+
+ print "Successfully tracing {parts_of_command[1]}"
+ end
+
+ #######################################################################
+ ## Trace Management functions ##
+ #######################################################################
+
+ # Effectively untraces the variable called *variable_name*
+ #
+ # Returns true if the variable exists, false otherwise
+ private fun untrace_variable(variable_name: String): Bool
+ do
+ var to_remove: nullable TraceObject = null
+ for i in self.traces do
+ if i.is_variable_traced_in_frame(variable_name, frame) then
+ to_remove = i
+ end
+ end
+
+ if to_remove != null then
+ self.traces.remove(to_remove)
+ return true
+ else
+ return false
+ end
+ end
+
+ # Effectively traces the variable *variable_name* either in print or break mode depending on the value of breaker (break if true, print if false)
+ #
+ private fun trace_variable(variable_name: String, breaker: Bool)
+ do
+ for i in self.traces do
+ if i.is_variable_traced_in_frame(variable_name, frame) then
+ print "This variable is already traced"
+ return
+ end
+ end
+
+ var trace_object: TraceObject
+
+ if breaker then
+ trace_object = new TraceObject(true)
+ else
+ trace_object = new TraceObject(false)
+ end
+
+ # We trace the current variable found for the current frame
+ trace_object.add_frame_variable(self.frame, variable_name)
+
+ var position_of_variable_in_arguments = get_position_of_variable_in_arguments(frame, variable_name)
+
+ # Start parsing the frames starting with the parent of the current one, until the highest
+ # When the variable traced is declared locally, the loop stops
+ for i in [1 .. frames.length-1] do
+
+ # If the variable was reported to be an argument of the previous frame
+ if position_of_variable_in_arguments != -1 then
+
+ var local_name = get_identifiers_in_current_instruction(get_function_arguments(frames[i].current_node.location.text))[position_of_variable_in_arguments]
+
+ position_of_variable_in_arguments = get_position_of_variable_in_arguments(frames[i], local_name)
+
+ trace_object.add_frame_variable(frames[i], local_name)
+ else
+ break
+ end
+ end
+
+ self.traces.add(trace_object)
+ end
+
+ # If the variable *variable_name* is an argument of the function being executed in the frame *frame*
+ # The function returns its position in the arguments
+ # Else, it returns -1
+ private fun get_position_of_variable_in_arguments(frame: Frame, variable_name: String): Int
+ do
+ var identifiers = get_identifiers_in_current_instruction(get_function_arguments(frame.mpropdef.location.text))
+ for i in [0 .. identifiers.length-1] do
+ # If the current traced variable is an argument of the current function, we trace its parent (at least)
+ if identifiers[i] == variable_name then return i
+ end
+ return -1
+ end
+
+ # Gets all the identifiers of an instruction (uses the rules of Nit as of Mar 05 2013)
+ #
+ fun get_identifiers_in_current_instruction(instruction: Text): Array[String]
+ do
+ var result_array = new Array[String]
+ var instruction_buffer = new FlatBuffer
+
+ var trigger_char_escape = false
+ var trigger_string_escape = false
+ var trigger_concat_in_string = false
+
+ for i in instruction.chars do
+ if trigger_char_escape then
+ if i == '\'' then trigger_char_escape = false
+ else if trigger_string_escape then
+ if i == '{' then
+ trigger_concat_in_string = true
+ trigger_string_escape = false
+ else if i == '\"' then trigger_string_escape = false
+ else
+ if i.is_alphanumeric or i == '_' then
+ instruction_buffer.add(i)
+ else if i == '.' then
+ if instruction_buffer.is_numeric or (instruction_buffer.chars[0] >= 'A' and instruction_buffer.chars[0] <= 'Z') then
+ instruction_buffer.clear
+ else
+ result_array.push(instruction_buffer.to_s)
+ instruction_buffer.add(i)
+ end
+ else if i == '\'' then
+ trigger_char_escape = true
+ else if i == '\"' then
+ trigger_string_escape = true
+ else if i == '}' then
+ trigger_concat_in_string = false
+ trigger_string_escape = true
+ else
+ if instruction_buffer.length > 0 and not instruction_buffer.is_numeric and not (instruction_buffer.chars[0] >= 'A' and instruction_buffer.chars[0] <= 'Z') then result_array.push(instruction_buffer.to_s)
+ instruction_buffer.clear
+ end
+ end
+ end
+
+ if instruction_buffer.length > 0 and not instruction_buffer.is_numeric and not (instruction_buffer.chars[0] >= 'A' and instruction_buffer.chars[0] <= 'Z') then result_array.push(instruction_buffer.to_s)
+
+ return result_array
+ end
+
+ # Takes a function call or declaration and strips all but the arguments
+ #
+ fun get_function_arguments(function: Text): String
+ do
+ var buf = new FlatBuffer
+ var trigger_copy = false
+
+ for i in function.chars do
+ if i == ')' then break
+ if trigger_copy then buf.add(i)
+ if i == '(' then trigger_copy = true
+ end
+
+ return buf.to_s
+ end
+
+ #######################################################################
+ ## Alias management functions ##
+ #######################################################################
+
+ # Adds a new alias to the tables
+ fun add_alias(var_represented: String, alias: String)
+ do
+ self.aliases[alias] = var_represented
+ end
+
+ # Gets the real name of a variable hidden by an alias
+ fun get_variable_name_by_alias(alias: String): nullable String
+ do
+ if self.aliases.keys.has(alias) then
+ return self.aliases[alias]
+ end
+
+ return null
+ end
+
+ # Gets the variable named by name, whether it is an alias or not
+ fun get_real_variable_name(name: String): String
+ do
+ var explode_string = name.split_with(".")
+ var final_string = new FlatBuffer
+ for i in explode_string do
+ var alias_resolved = get_variable_name_by_alias(i)
+ if alias_resolved != null then
+ final_string.append(get_real_variable_name(alias_resolved))
+ else
+ final_string.append(i)
+ end
+ final_string.append(".")
+ end
+
+ return final_string.substring(0,final_string.length-1).to_s