[COMMITTED] a68: use text_art::tree_widget in a68_dump_parse_tree
Jose E. Marchesi
jemarch@gnu.org
Fri Oct 24 22:54:34 GMT 2025
This commit makes the AST dumper to use the GCC tree_widget machinery.
Example of output:
Parse Tree
`- prelude:0:1 [0] particular program
`- prelude:0:1 [1] enclosed clause
`- prelude:0:1 [2] closed clause
+- prelude:0:1 [3] begin symbol
+- prelude:0:6 [4] serial clause
| +- prelude:0:6 [5] serial clause
| | `- prelude:0:6 [6] unit
| | `- prelude:0:6 [7] tertiary
| | `- prelude:0:6 [8] secondary
| | `- prelude:0:6 [9] primary
| | `- prelude:0:6 [10] enclosed clause
| | `- prelude:0:6 [11] closed clause
| | +- prelude:0:6 [12] begin symbol
| | +- foo.a68:1:1 [13] serial clause
| | | `- foo.a68:1:1 [14] unit
| | | `- foo.a68:1:1 [15] tertiary
| | | `- foo.a68:1:1 [16] secondary
| | | `- foo.a68:1:1 [17] primary
| | | `- foo.a68:1:1 [18] enclosed clause
| | | `- foo.a68:1:1 [19] closed clause
| | | +- foo.a68:1:1 [20] begin symbol
| | | +- foo.a68:1:7 [21] serial clause
| | | | `- foo.a68:1:7 [22] unit
| | | | `- foo.a68:1:7 [23] tertiary
| | | | `- foo.a68:1:7 [24] secondary
| | | | `- foo.a68:1:7 [25] primary
| | | | `- foo.a68:1:7 [26] enclosed clause
| | | | `- foo.a68:1:7 [27] loop clause
| | | | +- foo.a68:1:7 [28] for-part
| | | | | +- foo.a68:1:7 [29] for-symbol
| | | | | `- foo.a68:1:11 [30] defining identifier i
| | | | +- foo.a68:1:13 [31] to-part
| | | | | +- foo.a68:1:13 [32] to-symbol
| | | | | `- foo.a68:1:16 [33] unit
| | | | | `- foo.a68:1:16 [34] tertiary
| | | | | `- foo.a68:1:16 [35] secondary
| | | | | `- foo.a68:1:16 [36] primary
| | | | | `- foo.a68:1:16 [37] denotation
| | | | | `- foo.a68:1:16 [38] integral denotation
| | | | `- foo.a68:1:19 [39] alt do part
| | | | +- foo.a68:1:19 [40] alt do symbol
| | | | +- foo.a68:1:22 [41] serial clause
| | | | | `- foo.a68:1:22 [42] unit
| | | | | `- foo.a68:1:22 [43] skip unit
| | | | | `- foo.a68:1:22 [44] skip-symbol
| | | | `- foo.a68:1:27 [45] od-symbol
| | | `- foo.a68:2:1 [46] end-symbol
| | `- postlude:0:7 [47] end-symbol
| +- postlude:0:10 [48] semi-symbol
| `- postlude:0:7 [49] labeled unit
| +- postlude:0:7 [50] label
| | +- postlude:0:7 [51] defining identifier stop
| | `- postlude:0:11 [52] colon-symbol
| `- postlude:0:13 [53] unit
| `- postlude:0:13 [54] skip unit
| `- postlude:0:13 [55] skip-symbol
`- postlude:0:1 [56] end-symbol
gcc/ChangeLog
* algol68/a68-parser-debug.cc (a68_dump_parse_tree_1): Use tree_widget.
(a68_dump_parse_tree): Likewise.
---
gcc/algol68/a68-parser-debug.cc | 72 ++++++++++++++++++++++-----------
1 file changed, 49 insertions(+), 23 deletions(-)
diff --git a/gcc/algol68/a68-parser-debug.cc b/gcc/algol68/a68-parser-debug.cc
index fb862407d2d..e54ce4eb969 100644
--- a/gcc/algol68/a68-parser-debug.cc
+++ b/gcc/algol68/a68-parser-debug.cc
@@ -17,9 +17,16 @@
#define INCLUDE_MEMORY
#include "config.h"
+#define INCLUDE_VECTOR
#include "system.h"
#include "coretypes.h"
#include "diagnostic.h"
+#include "text-art/types.h"
+#include "text-art/dump.h"
+#include "text-art/dump-widget-info.h"
+#include "text-art/canvas.h"
+#include "text-art/theme.h"
+#include "text-art/tree-widget.h"
#include "a68.h"
@@ -27,55 +34,74 @@
standard output. */
static void
-a68_dump_parse_tree_1 (NODE_T *p, int level)
+a68_dump_parse_tree_1 (NODE_T *p, const text_art::dump_widget_info &dwi,
+ text_art::tree_widget &widget)
{
for (; p != NO_NODE; FORWARD (p))
{
- int i;
- location_t loc = a68_get_node_location (p);
-
- for (i = 0; i < level; ++i)
- printf (" ");
- printf ("NODE %d::%s",
- NUMBER (p),
- a68_attribute_name (ATTRIBUTE (p)));
-
+ char *symbol;
if (ATTRIBUTE (p) == IDENTIFIER
|| ATTRIBUTE (p) == DEFINING_IDENTIFIER
|| ATTRIBUTE (p) == DEFINING_OPERATOR
|| ATTRIBUTE (p) == BOLD_TAG)
- printf (" %s", NSYMBOL (p));
+ symbol = xasprintf (" %s", NSYMBOL (p));
+ else
+ symbol = xstrdup ("");
+ char mode[BUFFER_SIZE];
if (MOID (p) != NO_MOID)
{
MOID_T *moid = MOID (p);
- char b[BUFFER_SIZE];
- b[0] = '\0';
+ mode[0] = '\0';
+ a68_bufcat (mode, " (", 2);
if (IS (moid, SERIES_MODE))
{
if (PACK (moid) != NO_PACK && NEXT (PACK (moid)) == NO_PACK)
- a68_bufcat (b, a68_moid_to_string (MOID (PACK (moid)), MOID_ERROR_WIDTH, p),
+ a68_bufcat (mode, a68_moid_to_string (MOID (PACK (moid)), MOID_ERROR_WIDTH, p),
BUFFER_SIZE);
else
- a68_bufcat (b, a68_moid_to_string (moid, MOID_ERROR_WIDTH, p), BUFFER_SIZE);
+ a68_bufcat (mode, a68_moid_to_string (moid, MOID_ERROR_WIDTH, p), BUFFER_SIZE);
}
else
- a68_bufcat (b, a68_moid_to_string (moid, MOID_ERROR_WIDTH, p), BUFFER_SIZE);
-
- printf (" (%s)", b);
+ a68_bufcat (mode, a68_moid_to_string (moid, MOID_ERROR_WIDTH, p), BUFFER_SIZE);
+ a68_bufcat (mode, ")", 2);
}
- printf (" %s:%d:%d",
- LOCATION_FILE (loc), LOCATION_LINE (loc), LOCATION_COLUMN (loc));
- printf ("\n");
- a68_dump_parse_tree_1 (SUB (p), level + 1);
+
+ location_t loc = a68_get_node_location (p);
+ std::unique_ptr<text_art::tree_widget> cwidget
+ = text_art::tree_widget::from_fmt (dwi, nullptr,
+ "%s:%d:%d [%d] %s%s%s",
+ LOCATION_FILE (loc),
+ LOCATION_LINE (loc),
+ LOCATION_COLUMN (loc),
+ NUMBER (p),
+ a68_attribute_name (ATTRIBUTE (p)),
+ symbol,
+ mode);
+ free (symbol);
+
+ a68_dump_parse_tree_1 (SUB (p), dwi, *cwidget);
+ widget.add_child (std::move (cwidget));
}
}
void
a68_dump_parse_tree (NODE_T *p)
{
- a68_dump_parse_tree_1 (p, 0);
+ text_art::style_manager sm;
+ text_art::style::id_t default_style_id (sm.get_or_create_id (text_art::style ()));
+ text_art::ascii_theme theme;
+ text_art::dump_widget_info dwi (sm, theme, default_style_id);
+ std::unique_ptr<text_art::tree_widget> widget
+ = text_art::tree_widget::from_fmt (dwi, nullptr, "Parse Tree");
+
+ a68_dump_parse_tree_1 (p, dwi, *widget);
+
+ text_art::canvas c (widget->to_canvas (sm));
+ pretty_printer *const pp = global_dc->get_reference_printer ();
+ c.print_to_pp (pp);
+ printf ("%s", pp_formatted_text (pp));
}
void
--
2.30.2
More information about the Algol68
mailing list