From f3898c6934085e59a90ab55ac1b5284b2b7d31ef Mon Sep 17 00:00:00 2001
From: pompolic <pompolic@special-circumstanc.es>
Date: Wed, 16 Nov 2022 21:10:15 +0100
Subject: [PATCH] Fix hammer-parse-apply not stopping after parser application

---
 gdb-port/commands.py           | 10 ++++++++--
 gdb-port/hammer-breakpoints.py |  3 ++-
 gdb-port/top-level-parse.py    |  1 +
 3 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/gdb-port/commands.py b/gdb-port/commands.py
index 7474e1c..bcfa0c7 100644
--- a/gdb-port/commands.py
+++ b/gdb-port/commands.py
@@ -225,13 +225,16 @@ class HammerParseApply(FlowControlWithPrint):
 		#profiler = cProfile.Profile() # DEBUG
 		#profiler.enable() # DEBUG
 		with cProfile.Profile() as profiler: # DEBUG
-			#top_level_parse.setup_ast_stack_index(0) #TODO: is it a problem if this command overwrites it? would it better to use a convenience variable, as with parse-step?
+			top_level_parse.setup_ast_stack_index(0) #TODO: is it a problem if this command overwrites it? would it better to use a convenience variable, as with parse-step?
+			showmap_old = top_level_parse.show_input_map_after_apply
+			top_level_parse.show_input_map_after_apply = False
 
 			# HDoParseRetBreakpoint sets hammer_step_counter to 1 after capturing the AST subtree, so this will stop at the first h_do_parse invocation afterwards
 			if gdb.selected_inferior().pid > 0:
 				gdb.execute("continue")
 
-			#top_level_parse.clear_ast_stack_index()
+			top_level_parse.show_input_map_after_apply = showmap_old
+			top_level_parse.clear_ast_stack_index()
 
 			self.conditionally_print_backtrace()
 		profiler.print_stats(sort='tottime') # DEBUG
@@ -254,11 +257,14 @@ class HammerParseApplyAndShowAST(FlowControlWithPrint):
 		#profiler.enable() # DEBUG
 		with cProfile.Profile() as profiler: # DEBUG
 			top_level_parse.setup_ast_stack_index(0) #TODO: is it a problem if this command overwrites it? would it better to use a convenience variable, as with parse-step?
+			showmap_old = top_level_parse.show_input_map_after_apply
+			top_level_parse.show_input_map_after_apply = True
 
 			# HDoParseRetBreakpoint sets hammer_step_counter to 1 after capturing the AST subtree, so this will stop at the first h_do_parse invocation afterwards
 			if gdb.selected_inferior().pid > 0:
 				gdb.execute("continue")
 
+			top_level_parse.show_input_map_after_apply = showmap_old
 			top_level_parse.clear_ast_stack_index()
 
 			self.conditionally_print_backtrace()
diff --git a/gdb-port/hammer-breakpoints.py b/gdb-port/hammer-breakpoints.py
index d2ea306..32b923e 100644
--- a/gdb-port/hammer-breakpoints.py
+++ b/gdb-port/hammer-breakpoints.py
@@ -101,7 +101,8 @@ class HDoParseRetBreakpoint(gdb.Breakpoint):
 			ast_manager.print_ast()
 			#print(top_level_parse.peek_parserstack().p_stack) #TODO:sometimes the hammer-parser-backtrace gets messed up
 			top_level_parse.input_ptr = parse_state['input_stream']['input']
-			top_level_parse.print_input_map(ast_manager.top_node.ast)
+			if top_level_parse.show_input_map_after_apply:
+				top_level_parse.print_input_map(ast_manager.top_node.ast)
 			#print(top_level_parse.peek_parserstack().p_stack)
 			# Do not stop at this breakpoint, but stop at the next HDoParseBreakpoint
 			gdb.set_convenience_variable("hammer_step_counter", 1)
diff --git a/gdb-port/top-level-parse.py b/gdb-port/top-level-parse.py
index eaeff82..ff26265 100644
--- a/gdb-port/top-level-parse.py
+++ b/gdb-port/top-level-parse.py
@@ -8,6 +8,7 @@ class TopLevelParse:
 		self.current_parse_depth = 0 # Used by the AST handling code.
 		self.starting_input_index = []
 		self.input_token_map = {}
+		self.show_input_map_after_apply = False
 		self.input_ptr = None
 		# Technically, 1 virtual parse function + 1 h_do_parse + 1 perform_lowlevel_parse (if packrat)
 		# TODO: len(self.parser_stacks[-1]) and self.current_parse_depth serve the same purpose. avoid multiple sources of truth
-- 
GitLab